JFreeChart数据图表示范
JFreeChart数据图表示例import?java.awt.Font??import?java.io.File????import?org.jfree.chart.ChartFa
JFreeChart数据图表示例
- import?java.awt.Font;??
- import?java.io.File;??
- ??
- import?org.jfree.chart.ChartFactory;??
- import?org.jfree.chart.ChartFrame;??
- import?org.jfree.chart.ChartUtilities;??
- import?org.jfree.chart.JFreeChart;??
- import?org.jfree.chart.labels.StandardCategoryItemLabelGenerator;??
- import?org.jfree.chart.plot.CategoryPlot;??
- import?org.jfree.chart.plot.PiePlot;??
- import?org.jfree.chart.plot.PlotOrientation;??
- import?org.jfree.chart.renderer.category.LineAndShapeRenderer;??
- import?org.jfree.data.category.DefaultCategoryDataset;??
- import?org.jfree.data.general.DefaultPieDataset;??
- ??
- public?class?TestJFreeChart?{??
- ??
- ????public?static?void?main(String[]?args)?throws?Exception{??
- ??????????
- ????????System.out.println("start...");??
- ??????????
- ????????//测试饼图??
- ????????testPieChart();??
- ??????????
- ????????//测试折线图??
- ????????testLineChart();??
- ??????????
- ????????System.out.println("complete...");??
- ????}??
- ??
- ????/**?
- ?????*??
- ?????*?测试饼图?
- ?????*??
- ?????*?cjnetwork?2010-11-17下午05:21:15?
- ?????*/??
- ????private?static?void?testPieChart()?throws?Exception{??
- ????????DefaultPieDataset?dataset?=?new?DefaultPieDataset();??
- ??????????
- ????????dataset.setValue("初中",?25);??
- ????????dataset.setValue("高中",?55);??
- ????????dataset.setValue("大学",?100);??
- ??????????
- ????????JFreeChart?chart?=?ChartFactory.createPieChart3D("学历分布图",?dataset,?true,?true,?true);??
- ??????????
- ????????Font?font?=?new?Font("黑体",?Font.BOLD,?15);??
- ????????chart.getTitle().setFont(font);??
- ????????chart.getLegend().setItemFont(font);??
- ????????((PiePlot)chart.getPlot()).setLabelFont(font);??
- ??????????
- ????????ChartFrame?frame?=?new?ChartFrame("学历分布图",?chart);??
- ????????frame.pack();??
- ????????frame.setVisible(true);??
- ????????ChartUtilities.saveChartAsJPEG(new?File("d:/testPieChart.jpg"),?chart,?1024,?768);??
- ????}??
- ??
- ????/**?
- ?????*??
- ?????*?测试折线图?
- ?????*??
- ?????*?cjnetwork?2010-11-17下午05:21:07?
- ?????*/??
- ????private?static?void?testLineChart()?throws?Exception{??
- ??????????
- ????????DefaultCategoryDataset?dataset?=?new?DefaultCategoryDataset();??
- ??????????
- ????????dataset.addValue(100,?"苹果",?"2010-10-01");??
- ????????dataset.addValue(150,?"香蕉",?"2010-10-01");??
- ????????dataset.addValue(130,?"葡萄",?"2010-10-01");??
- ????????dataset.addValue(120,?"梨",?"2010-10-01");??
- ??????????
- ????????dataset.addValue(110,?"苹果",?"2010-10-02");??
- ????????dataset.addValue(160,?"香蕉",?"2010-10-02");??
- ????????dataset.addValue(70,?"葡萄",?"2010-10-02");??
- ????????dataset.addValue(145,?"梨",?"2010-10-02");??
- ??????????
- ????????dataset.addValue(150,?"苹果",?"2010-10-03");??
- ????????dataset.addValue(168,?"香蕉",?"2010-10-03");??
- ????????dataset.addValue(60,?"葡萄",?"2010-10-03");??
- ????????dataset.addValue(148,?"梨",?"2010-10-03");??
- ??????????
- ????????dataset.addValue(140,?"苹果",?"2010-10-04");??
- ????????dataset.addValue(190,?"香蕉",?"2010-10-04");??
- ????????dataset.addValue(60,?"葡萄",?"2010-10-04");??
- ????????dataset.addValue(163,?"梨",?"2010-10-04");??
- ??????????
- ????????JFreeChart?chart?=?ChartFactory.createLineChart("水果产量图",?"时间",?"产量",?dataset,?PlotOrientation.VERTICAL,?true,?true,?true);??
- ????????CategoryPlot?plot?=?chart.getCategoryPlot();??
- ????????plot.getRangeAxis().setUpperMargin(0.15);??
- ??????????
- ????????Font?font?=?new?Font("黑体",?Font.BOLD,?15);??
- ????????chart.getLegend().setItemFont(font);??
- ????????chart.getTitle().setFont(font);??
- ????????plot.getDomainAxis().setTickLabelFont(font);??
- ????????plot.getDomainAxis().setLabelFont(font);??
- ????????plot.getRangeAxis().setLabelFont(font);??
- ??????????
- ????????LineAndShapeRenderer?lineAndShapeRenderer?=?(LineAndShapeRenderer)?plot.getRenderer();??
- ????????lineAndShapeRenderer.setBaseLinesVisible(true);??
- ????????lineAndShapeRenderer.setBaseShapesVisible(true);??
- ????????lineAndShapeRenderer.setBaseItemLabelGenerator(new?StandardCategoryItemLabelGenerator());??
- ????????lineAndShapeRenderer.setBaseItemLabelsVisible(true);??
- ??????????
- ????????ChartFrame?frame?=?new?ChartFrame("水果产量图",?chart);??
- ????????frame.pack();??
- ????????frame.setVisible(true);???
- ??????????
- ????????ChartUtilities.saveChartAsJPEG(new?File("d:/testLineChart.jpg"),?chart,?1024,?768);??
- ????}??
- ??????
- ??????
- }