一些jfreechart的api:http://topic.csdn.net/t/20050925/03/4292228.html
要建立类似下面的报表(折线图和柱状图都存在,图片不好贴,直接给个flicckr地址):http://www.flickr.com/photos/44025540@N03/4241013620/
先贴段代码,用到的地方尽量加载注释里:
package com.wasw100.chart; import java.awt.Color; import java.awt.Font; import java.io.FileOutputStream; import java.io.IOException; import java.util.LinkedHashMap; import java.util.Map; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.renderer.category.BarRenderer; import org.jfree.chart.renderer.category.LineAndShapeRenderer; import org.jfree.chart.title.TextTitle; import org.jfree.data.category.DefaultCategoryDataset; /** * * @author wasw100 * */ public class ChartTest { /** * @param stuScoreMap * @return */ private static DefaultCategoryDataset getDataSet( Map<String, Number> scoreMap, String category) { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); // 遍历Map for (String key : scoreMap.keySet()) { double value = scoreMap.get(key).doubleValue(); dataset.setValue(value, category, key); } return dataset; } /** * * @param stuScoreMap * 学生成绩 * @param avgScoreMap * 班级平均成绩 * @return */ private static JFreeChart getChart(Map<String, Number> stuScoreMap, Map<String, Number> avgScoreMap) { CategoryPlot categoryPlot = new CategoryPlot(); // 横轴、纵轴显示的标题 categoryPlot.setDomainAxis(new CategoryAxis("")); categoryPlot.setRangeAxis(new NumberAxis("分数")); // 显示的方向,横向还是纵向? // categoryPlot.setOrientation(PlotOrientation.VERTICAL); // 设置是否显示水平网格线 categoryPlot.setRangeGridlinesVisible(true); // 设置是否显示垂直网格线 // categoryPlot.setDomainGridlinesVisible(true); DefaultCategoryDataset avgData = getDataSet(avgScoreMap, "班级平均成绩");// 得到数据集 LineAndShapeRenderer lineAndShapeRenderer = new LineAndShapeRenderer(); // 不显示线条 lineAndShapeRenderer.setBaseLinesVisible(false); // 将线条添加进图表 categoryPlot.setRenderer(lineAndShapeRenderer); categoryPlot.setDataset(avgData); // lineAndShapeRenderer的数值显示出来 StandardCategoryItemLabelGenerator standardcategoryitemlabelgenerator = new StandardCategoryItemLabelGenerator(); lineAndShapeRenderer .setBaseItemLabelGenerator(standardcategoryitemlabelgenerator); lineAndShapeRenderer.setBaseItemLabelsVisible(true); // 个人成绩的添加 DefaultCategoryDataset stuData = getDataSet(stuScoreMap, "成绩");// 得到数据集 BarRenderer barRenderer = new BarRenderer();// 柱状图渲染器 categoryPlot.setDataset(1, stuData); categoryPlot.setRenderer(1, barRenderer); // 颜色显示为黄色 barRenderer.setSeriesPaint(0, Color.YELLOW); barRenderer.setShadowVisible(false); // barRenderer.setSeriesPaint(0, new Color(0, 0, 255)); // 显示边框 barRenderer.setDrawBarOutline(true); barRenderer.setBaseOutlinePaint(Color.GRAY); // 设置纵轴的最小值和最大值 ValueAxis valueAxis = categoryPlot.getRangeAxis(); valueAxis.setLowerBound(40); valueAxis.setUpperBound(110); JFreeChart chart = new JFreeChart(categoryPlot); // 设置title Font titleFont = new Font("黑体", Font.BOLD, 10); // 图片标题 chart .setTitle(new TextTitle("高2011级6班在高一下期末的平均成绩与班级平均成绩的比较", titleFont)); chart.setBackgroundPaint(Color.WHITE); return chart; } public static void main(String[] args) { Map<String, Number> avgScoreMap = new LinkedHashMap<String, Number>(); avgScoreMap.put("总分", 0); avgScoreMap.put("语文", 97.02); avgScoreMap.put("数学", 73.71); avgScoreMap.put("英语", 77.9); avgScoreMap.put("理综", 0); avgScoreMap.put("物理", 52.73); avgScoreMap.put("化学", 66.98); Map<String, Number> stuScoreMap = new LinkedHashMap<String, Number>(); stuScoreMap.put("总分", 0); stuScoreMap.put("语文", 98); stuScoreMap.put("数学", 70); stuScoreMap.put("英语", 50); stuScoreMap.put("理综", 0); stuScoreMap.put("物理", 50); stuScoreMap.put("化学", 70); JFreeChart chart = getChart(avgScoreMap, avgScoreMap); FileOutputStream fos_jpg = null; try { fos_jpg = new FileOutputStream("D:\\score.jpg"); ChartUtilities.writeChartAsJPEG(fos_jpg, 1, chart, 400, 300, null); } catch (Exception e) { e.printStackTrace(); } finally { try { fos_jpg.close(); } catch (IOException e) { } } } }
还有就是图例如果显示在右面,可以这样设置
// 设置图例位置,字体 chart.getLegend().setPosition(RectangleEdge.RIGHT); Font kfont = new Font("宋体", Font.PLAIN, 10); chart.getLegend().setItemFont(kfont);
还没有解决的问题:
1.右侧刻度间距没有手动控制
2.柱状图中间的白色竖线去不掉,欢迎指点
补充:刻度手动控制解决,只要把上面99-102行的代码改为下面即可
// 数据轴精度 NumberAxis numberAxis = (NumberAxis) categoryPlot.getRangeAxis(); numberAxis.setTickUnit(new NumberTickUnit(10)); numberAxis.setLowerBound(0); numberAxis.setUpperBound(110);
NumberAxis继承ValueAxis,所以ValueAxis有得属性方法, NumberAxis都可以使用。