Question:
I created a report with XYLine JFreeChart. It works fine but I want to display the values on each points on the lines. How to achieve my requirements?
Answer:
A chart customizer should be created to achieve the requirements:
http://jasperreports.sourceforge.net/sample.reference/charts/index.html#chartcustomizers
Please find below sample Java code to enable XYLine chart labels:
//import java.awt.Color; import net.sf.jasperreports.engine.JRChart; import net.sf.jasperreports.engine.fill.JRFillChart; import net.sf.jasperreports.engine.fill.JRBaseFiller; import net.sf.jasperreports.engine.fill.JRFillChartDataset; import net.sf.jasperreports.engine.JRAbstractChartCustomizer; import org.jfree.chart.renderer.xy.XYItemRenderer; import org.jfree.chart.labels.XYItemLabelGenerator; import org.jfree.chart.JFreeChart; public class XYChartCustomizer extends JRAbstractChartCustomizer { private JRFillChart fillChart; @Override public void init(JRBaseFiller chartFiller, JRFillChart chart) { super.init(chartFiller, chart); this.fillChart = chart; } @Override public void customize(JFreeChart chart, JRChart jasperChart) { XYItemRenderer renderer = chart.getXYPlot().getRenderer(); renderer.setBaseItemLabelsVisible(true); renderer.setBaseItemLabelGenerator((XYItemLabelGenerator) ((JRFillChartDataset) fillChart.getDataset()).getLabelGenerator()); } }
In order to use this XYLine chart customizer, compile the code to a class, deploy it in the application class path, then reference it in the chart:
<xyLineChart> <chart evaluationTime="Report" customizerClass="XYChartCustomizer"> <reportElement x="0" y="0" width="800" height="550" uuid="6db96f94-44ed-4847-a505-6dda967fe299"/> <chartTitle/> ... </xyLineChart>
Printscreen of applying the customizer:
Ref. Case #00063995
Recommended Comments
There are no comments to display.