views:

862

answers:

2

How to dynamically remove the legend from a chart using the Jasper report API?

I have a chart to display dynamically based on some configurations, which includes the legend as a option, so if legend is unchecked I need to hide or remove the legend form the chart. Is there any function available in the API which can do that?

A: 

Here there is a method setShowLegend(Boolean)

You can obtain the chart this way (I haven't tested it, so there might be some issues):

InputStream is = this.getClaa().getResourceAsStream(jasperFilePath);
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(is);
JRChart chart = (JRChart) report.getDetail().getElementByKey("chart");
chart.setShowLegend(Boolean.FALSE);
Bozho
Thank youbut how do i instantiate JRChart's constructor?this is my code to fill the jasper object:-JasperCompileManager.compileReportToFile(jrxmlPath, jasperpath); jasperPrint = JasperFillManager.fillReport(jasperpath, param, connection); JasperExportManager.exportReportToHtmlFile(jasperPrint, htmlFile);how do i make an instance out of this
stanley
I edited my response with the details. I'm note sure if it would work, but give it a try.
Bozho
i tried it, but i guess its deprecated:-JasperDesign design = JRXmlLoader.load (jrxmlPath); JasperReport jasperReport = JasperCompileManager.compileReport(design);JRChart chart = (JRChart) jasperReport.getSummary().getElementByKey("element-1");chart.setShowLegend(Boolean.FALSE);
stanley
any exceptions, or it just doesn't work?
Bozho
there is no exceptions, even if i try to print 'chart.getShowLegend()' it returns null, but if try to print width and height of the chart 'chart.getHeight()' it works.
stanley
What version of JasperReport are you using?P.S. edit your question - it points to a wrong Jasper :)
Bozho
+1  A: 

I think that the JasperReport description syntax (the XML tags composing the jrxml file) do not let you do such a thing.

Here is a pie chart description with legend (default) :

<pieChart>
    <chart isShowLegend="true">
     <reportElement x="19" y="18" width="518" height="196"/>
     <chartTitle/>
     <chartSubtitle/>
     <chartLegend/>
    </chart>
    <pieDataset>
     <keyExpression><![CDATA[$F{name}]]></keyExpression>
     <valueExpression><![CDATA[$F{value}]]></valueExpression>
     <labelExpression><![CDATA["<"+$F{name}+">"]]></labelExpression>
    </pieDataset>
    <piePlot>
     <plot/>
     <itemLabel color="#000000" backgroundColor="#FFFFFF"/>
    </piePlot>
</pieChart>

and here the same chart without legend :

<pieChart>
    <chart isShowLegend="false">
     <reportElement x="19" y="18" width="518" height="196"/>
     <chartTitle/>
     <chartSubtitle/>
     <chartLegend/>
    </chart>
    <pieDataset>
     <keyExpression><![CDATA[$F{name}]]></keyExpression>
     <valueExpression><![CDATA[$F{value}]]></valueExpression>
     <labelExpression><![CDATA["<"+$F{name}+">"]]></labelExpression>
    </pieDataset>
    <piePlot>
     <plot/>
     <itemLabel color="#000000" backgroundColor="#FFFFFF"/>
    </piePlot>
</pieChart>

The only difference is the isShowLegend attribute in the chart tag. (You can access it in the chart properties panel in iReport).

But this attribute is a value, and you can't use a expression such as :

$P{DISPLAY_LEGEND}.booleanValue();

Where DISPLAY_LEGEND would be a parameter of the report (type is java.lang.Boolean)


If you really want to realise a such thing, following trick should work:

Define you graph to time. One with legend and one without. Graph should be overlapped. (you can do so by coping the concerned tag directly in the XML text)

After that edit the print when expression property of each graph, in oder that just one of the 2 graph is printed, depending of a condition.

Here is the result example with my DISPLAY_LEGEND parameter. (but it could be an other condition, also calculated. Important is that the two conditions are symmetric)

<pieChart>
    <chart isShowLegend="true">
     <reportElement x="19" y="18" width="518" height="196">
      <printWhenExpression><![CDATA[$P{DISPLAY_LEGEND}]]></printWhenExpression>
     </reportElement>
     <!-- end of the chart definition-->
    </chart>
    <!-- pieDataset and piePlot-->
</pieChart>
<pieChart>
    <chart isShowLegend="false">
     <reportElement x="19" y="18" width="518" height="196">
      <printWhenExpression><![CDATA[new Boolean(!$P{DISPLAY_LEGEND}.booleanValue())]]></printWhenExpression>
     </reportElement>
     <!-- end of the chart definition-->
    </chart>
    <!-- pieDataset and piePlot-->
</pieChart>


I also wanted to mention that you can access the the JFreeChart Object during the report generation. Here some forum posts, that helped me doing so :

Jmini
Thank you very much... it works
stanley