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 :