一、使用的框架:JFreeChart
二、条形图
1.使用的数据集DefaultCategoryDataset.
DefaultCategoryDataset dateset= new DefaultCategoryDataset();
dateset.setValue(100,"1","1");
dateset.setValue(90,"2","2");
dateset.setValue(90,"3","3");
dateset.setValue(90,"4","4");
dateset.setValue(90,"5","5");
dateset.setValue(90,"6","6");
dateset.setValue(90,"7","7");
dateset.setValue(90,"8","8");
dateset.setValue(90,"9","9");
dateset.setValue(90,"10","10");
dateset.setValue(90,"11","11");
dateset.setValue(90,"12","12");
setValue(纵轴的值,横轴的分类,横轴的值);
2.创建JFreeChart对象
JFreeChart panel=ChartFactory.createBarChart3D(
"title", //1.图表标题
"month", //2.横轴标题
"money", //3.纵轴标题
dateset, //4.数据源
PlotOrientation.VERTICAL,//5.图表方向,使用PlotOrientation对象中的常量PlotOrientation.HORIZONTAL
false, //6.是否显示图表标识
false, //7.是否显示tooltips
false //7.是否显示tooltips
);
3.使用输出流输出图表
OutputStream os=new FileOutputStream("f:\\test.jpg");
panel.setTitle(new TextTitle("月销售统计", new Font("宋体",Font.BOLD , 22)));
ChartUtilities.writeChartAsJPEG(os, panel, 1000, 500);
os.close();
输出结果:
由于在数据集中分成了12类,所以每一根柱子的颜色都不同。