关于图表部分的实现JasperReport底层是用的Jfreechart实现的,这里也是用的自定义实现图表部分,自定义类继承JRAbstractChartCustomizer重写customize方法,然后将该类导成jar包放进classpaht即可. 注意: 1.关于XY系列的图表在内部已经有了相关的实现. 2.关于CategoryPlot部分的图,这里是以坐标显示(0~23)为例子.
PS: 关于XY系列的图表的显示参数是可以通过在jrxml中配置然后调用JRAbstractChartCustomizer中的get系列方法获取的.
public class YvesCustomizer extends JRAbstractChartCustomizer { @Override public void customize(JFreeChart chart, JRChart jasperChart) { Double minValue = 0.0;//x轴的最小值 Double maxValue = 24.0;//x轴的最大值 Double tickUnit = 1.0;//间隔单元 if (chart.getPlot() instanceof XYPlot) {/* 针对XY的图 */ XYPlot plot = (XYPlot) chart.getPlot(); ValueAxis valueAxis = plot.getDomainAxis(); if (valueAxis != null) { if (minValue != null || maxValue != null) { valueAxis.setRange(minValue == null ? valueAxis.getRange().getLowerBound() : minValue, maxValue == null ? valueAxis.getRange().getUpperBound() : maxValue); } if (valueAxis instanceof NumberAxis) { NumberAxis numAxis = (NumberAxis) valueAxis; numAxis.setTickUnit(new NumberTickUnit(tickUnit)); } } } else if (chart.getPlot() instanceof CategoryPlot) {/* * 针对普通的柱状图,线图,面积图... */ CategoryPlot plot = (CategoryPlot) chart.getPlot(); // 参数解释: value(Y轴), series,category(x轴) // dataset.setValue(value, rowKey, columnKey); /* 设置X轴 */ CategoryAxis axis = plot.getDomainAxis(); DefaultCategoryDataset dataset = (DefaultCategoryDataset) plot.getDataset(); List columnKeys = dataset.getColumnKeys(); List rowKeys = dataset.getRowKeys(); DefaultCategoryDataset result = new DefaultCategoryDataset(); for (int i = 0; i < rowKeys.size(); i++) { Comparable rowKey = (Comparable) rowKeys.get(i); setDataSet(result, rowKey); } // 更新数据 for (int i = 0; i < columnKeys.size(); i++) { for (int j = 0; j < rowKeys.size(); j++) { result.setValue(dataset.getValue((Comparable) rowKeys.get(j), (Comparable) columnKeys.get(i)), (Comparable) rowKeys.get(j), (Comparable) columnKeys.get(i)); } } plot.setDataset(result); } else if (chart.getPlot() instanceof PiePlot) { } } private static void setDataSet(DefaultCategoryDataset dataset, Comparable rowKey) { dataset.setValue(null, rowKey, new Integer(0)); dataset.setValue(null, rowKey, new Integer(1)); dataset.setValue(null, rowKey, new Integer(2)); dataset.setValue(null, rowKey, new Integer(3)); dataset.setValue(null, rowKey, new Integer(4)); dataset.setValue(null, rowKey, new Integer(5)); dataset.setValue(null, rowKey, new Integer(6)); dataset.setValue(null, rowKey, new Integer(7)); dataset.setValue(null, rowKey, new Integer(8)); dataset.setValue(null, rowKey, new Integer(9)); dataset.setValue(null, rowKey, new Integer(10)); dataset.setValue(null, rowKey, new Integer(11)); dataset.setValue(null, rowKey, new Integer(12)); dataset.setValue(null, rowKey, new Integer(13)); dataset.setValue(null, rowKey, new Integer(14)); dataset.setValue(null, rowKey, new Integer(15)); dataset.setValue(null, rowKey, new Integer(16)); dataset.setValue(null, rowKey, new Integer(17)); dataset.setValue(null, rowKey, new Integer(18)); dataset.setValue(null, rowKey, new Integer(19)); dataset.setValue(null, rowKey, new Integer(20)); dataset.setValue(null, rowKey, new Integer(21)); dataset.setValue(null, rowKey, new Integer(22)); dataset.setValue(null, rowKey, new Integer(23)); } }