1.我用的jar包如下:
jfreechart-1.0.13.jar
jcommon.jar
gnujaxp.jar
2. 程序实现过程:
1)新建一个类:MyChartFactory
2)添加下面这个用于生成DataSet的方法。
private static TableXYDataset createDataset(){
TimeTableXYDataset localTimeTableXYDataset = new TimeTableXYDataset();
Year year=new Year(2010);
localTimeTableXYDataset.add(new Month(1,year), 0.0D, "ELECTRIC MILES");
localTimeTableXYDataset.add(new Month(1,year), 200.0D, "ELECTRIC MILES");
localTimeTableXYDataset.add(new Month(2,year), 150.0D, "ELECTRIC MILES");
localTimeTableXYDataset.add(new Month(3,year), 150.0D, "ELECTRIC MILES");
localTimeTableXYDataset.add(new Month(4,year), 150.0D, "ELECTRIC MILES");
localTimeTableXYDataset.add(new Month(5,year), 150.0D, "ELECTRIC MILES");
localTimeTableXYDataset.add(new Month(6,year), 150.0D, "ELECTRIC MILES");
localTimeTableXYDataset.add(new Month(7,year), 150.0D, "ELECTRIC MILES");
localTimeTableXYDataset.add(new Month(8,year), 150.0D, "ELECTRIC MILES");
localTimeTableXYDataset.add(new Month(9,year), 150.0D, "ELECTRIC MILES");
localTimeTableXYDataset.add(new Month(10,year), 150.0D, "ELECTRIC MILES");
localTimeTableXYDataset.add(new Month(11,year), 150.0D, "ELECTRIC MILES");
localTimeTableXYDataset.add(new Month(12,year), 150.0D, "ELECTRIC MILES");
localTimeTableXYDataset.add(new Month(1,year), 800.0D, "GAS");
localTimeTableXYDataset.add(new Month(2,year), 700.0D, "GAS");
localTimeTableXYDataset.add(new Month(3,year), 830.0D, "GAS");
localTimeTableXYDataset.add(new Month(4,year), 810.0D, "GAS");
localTimeTableXYDataset.add(new Month(5,year), 790.0D, "GAS");
localTimeTableXYDataset.add(new Month(6,year), 940.0D, "GAS");
localTimeTableXYDataset.add(new Month(7,year), 1500.0D, "GAS");
localTimeTableXYDataset.add(new Month(8,year), 700.0D, "GAS");
localTimeTableXYDataset.add(new Month(9,year), 480.0D, "GAS");
localTimeTableXYDataset.add(new Month(10,year), 860.0D, "GAS");
localTimeTableXYDataset.add(new Month(11,year), 710.0D, "GAS");
localTimeTableXYDataset.add(new Month(12,year), 770.0D, "GAS");
return localTimeTableXYDataset;
}
3)核心方法:
private static JFreeChart createChart(TableXYDataset paramTableXYDataset)
{
DateAxis localDateAxis = new DateAxis("");
localDateAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
localDateAxis.setLowerMargin(0.01D);
localDateAxis.setUpperMargin(0.01D);
SimpleDateFormat sdf=new SimpleDateFormat("MMM",Locale.ENGLISH); //格式化时间轴显示
localDateAxis.setTickUnit(new DateTickUnit(DateTickUnit.MONTH,1,sdf));
NumberAxis localNumberAxis = new NumberAxis("MILES");
localNumberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
localNumberAxis.setUpperMargin(0.1D);
localNumberAxis.setTickUnit(new NumberTickUnit(500));
StackedXYBarRenderer localStackedXYBarRenderer = new StackedXYBarRenderer(0.05D);
localStackedXYBarRenderer.setGradientPaintTransformer(new StandardGradientPaintTransformer(GradientPaintTransformType.VERTICAL));
localStackedXYBarRenderer.setDrawBarOutline(false);
//柱状上不显示数据
localStackedXYBarRenderer.setBaseItemLabelsVisible(false);
localStackedXYBarRenderer.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
localStackedXYBarRenderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER));
localStackedXYBarRenderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator("{0} : {1} = {2}", new SimpleDateFormat("yyyy"), new DecimalFormat("0")));
GradientPaint localGradientPaint1 = new GradientPaint(0.0F, 0.0F, new Color(123,192,12), 0.0F, 0.0F, new Color(0, 0, 64));
GradientPaint localGradientPaint2 = new GradientPaint(0.0F, 0.0F, new Color(113,160,255), 0.0F, 0.0F, new Color(0, 64, 0));
localStackedXYBarRenderer.setSeriesPaint(0, localGradientPaint1);
localStackedXYBarRenderer.setSeriesPaint(1, localGradientPaint2);
//设置每个平行柱的之间距离
localStackedXYBarRenderer.setSeriesOutlinePaint(0, Color.BLACK);
localStackedXYBarRenderer.setMargin(0.65);
XYPlot localXYPlot = new XYPlot(paramTableXYDataset, localDateAxis, localNumberAxis, localStackedXYBarRenderer);
JFreeChart localJFreeChart = new JFreeChart("Result", localXYPlot);
localJFreeChart.removeLegend();
LegendTitle localLegendTitle = new LegendTitle(localXYPlot);
localLegendTitle.setBackgroundPaint(new Color(132,201,248));
localLegendTitle.setPosition(RectangleEdge.BOTTOM);
localJFreeChart.addSubtitle(localLegendTitle);
localJFreeChart.setBackgroundPaint(new Color(132,201,248));
return localJFreeChart;
}
4)将生成的Chart以图片形式保存:
public static void saveAsFile(JFreeChart chart,String outputPath,int width,int height){
FileOutputStream out=null;
try {
File outFile=new File(outputPath);
if(!outFile.getParentFile().exists()){
outFile.getParentFile().mkdirs();
}
out=new FileOutputStream(outputPath);
//保存为PNG文件
ChartUtilities.writeChartAsPNG(out, chart, width, height);
} catch (Exception e) {
e.printStackTrace();
}
}
5)main函数中:
JFreeChart localJFreeChart = createChart(createDataset());
saveAsFile(localJFreeChart, "D:/a.png", 800, 350);