1 28 29 30 import java.awt.Color ; 31 import java.awt.Paint ; 32 import java.awt.image.BufferedImage ; 33 34 import net.sf.jasperreports.engine.JRDefaultScriptlet; 35 import net.sf.jasperreports.engine.JRScriptletException; 36 37 import org.jCharts.axisChart.AxisChart; 38 import org.jCharts.chartData.AxisChartDataSet; 39 import org.jCharts.chartData.ChartDataException; 40 import org.jCharts.chartData.DataSeries; 41 import org.jCharts.properties.AreaChartProperties; 42 import org.jCharts.properties.AxisProperties; 43 import org.jCharts.properties.ChartProperties; 44 import org.jCharts.properties.LegendProperties; 45 import org.jCharts.types.ChartType; 46 47 48 52 public class JChartsScriptlet extends JRDefaultScriptlet 53 { 54 55 56 59 public void afterReportInit() throws JRScriptletException 60 { 61 try 62 { 63 AreaChartProperties areaChartProperties = new AreaChartProperties(); 64 65 double[][] data = {{10, 15, 30, 53}, {6, 30, 10, 21}, {20, 25, 20, 8}}; 66 Paint [] paints = {new Color ( 0, 255, 0, 100 ), new Color ( 255, 0, 0, 100 ), new Color ( 0, 0, 255, 100 )}; 67 String [] legendLabels = {"Games", "Events", "Players" }; 68 AxisChartDataSet axisChartDataSet = new AxisChartDataSet(data, legendLabels, paints, ChartType.AREA, areaChartProperties); 69 70 String [] axisLabels = {"January", "March", "May", "June"}; 71 DataSeries dataSeries = new DataSeries(axisLabels, "Months", "People", "Popular Events"); 72 dataSeries.addIAxisPlotDataSet(axisChartDataSet); 73 74 ChartProperties chartProperties = new ChartProperties(); 75 AxisProperties axisProperties = new AxisProperties(); 76 axisProperties.setYAxisRoundValuesToNearest(0); 77 LegendProperties legendProperties = new LegendProperties(); 78 79 AxisChart axisChart = new AxisChart(dataSeries, chartProperties, axisProperties, legendProperties, 500, 350); 80 81 BufferedImage bufferedImage = new BufferedImage (500, 350, BufferedImage.TYPE_INT_RGB); 82 83 axisChart.setGraphics2D(bufferedImage.createGraphics()); 84 axisChart.render(); 85 86 super.setVariableValue("ChartImage", bufferedImage); 87 } 88 catch(ChartDataException chartDataException) 89 { 90 throw new JRScriptletException(chartDataException); 91 } 92 } 93 94 95 } 96 | Popular Tags |