JFreeChart数据图表示例
- 摘要:importjava.awt.Font;importjava.io.File;importorg.jfree.chart.ChartFactory;importorg.jfree.chart.ChartFrame;importorg.jfree.chart.ChartUtilities;importorg.jfree.chart.JFreeChart;importorg.jfree.chart.labels.StandardCategoryItemLabelGenerator
- 标签:数据
class="dp-j" style="font-size: 12px; margin-bottom: 1px; padding-top: 2px; padding-bottom: 2px; border: 1px solid #d1d7dc; color: #2b91af; font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace;">
- 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...");??
- ????}??
- ??
- ?????
- ?
- ?
- ?
- ?
- ??
- ????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);??
- ????}??
- ??
- ?????
- ?
- ?
- ?
- ?
- ??
- ????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);??
- ????}??
- ??????
- ??????
- }