JFreeChart

创建图表前先设置数据源
数据源:
DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
//在数据源中为每个字段添加信息,从左到右一次是数据、图例、轴向名称
//addValue中三个参数(数据,图例,轴向名称)
dataSet.addValue(100, "App", "Apple");
dataSet.addValue(200, "Goo", "Google");
dataSet.addValue(70, "MS", "Microsoft");

在输出图片之前先设置中文编码
StandardChartTheme theme = new StandardChartTheme("mytheme");
//设置标题字体
theme.setExtraLargeFont(new Font("黑体",Font.BOLD,32));
//设置图例字体
theme.setRegularFont(new Font("黑体",Font.BOLD,14));
//设置轴向字体
theme.setLargeFont(new Font("黑体",Font.BOLD,14));
ChartFactory.setChartTheme(theme);

生成图表:
JFreeChart chart = ChartFactory.createBarChart(
"手机占有率图", //标题
"公司名称", //目录轴名称
"销量", //数据周名称
dataSet, //数据源
PlotOrientation.VERTICAL, //图标方向:水平,垂直
true, //是否显示图例
false, //是否生成工具
false //是否生成URL连接
);
FileOutputStream fos = new FileOutputStream(C:\\barchart.jpg");
ChartUtilities.writeChartAsJPEG(fos, chart, 400, 300);
fos.flush();
fos.close();

3D柱状图
JFreeChart chart = ChartFactory.createBarChart3D(...)

3D饼状图
数据源
DefaultPieDataset dataSet = new DefaultPieDataset();
dataSet.setValue("Java", 300);
dataSet.setValue("PHP", 240);
dataSet.setValue(".NET", 140);
• 生成图表
JFreeChart pieChart = ChartFactory.createPieChart3D("技术统计",
dataSet, true,false,false);
FileOutputStream fos = new FileOutputStream("C:\\piechart.jpg");
ChartUtilities.writeChartAsJPEG(fos, pieChart, 400, 300);
fos.close();

折线图
数据源
DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
dataSet.setValue(130, "A", "1月");
dataSet.setValue(230, "A", "2月");
dataSet.setValue(430, "A", "3月");
dataSet.setValue(330, "A", "4月");
dataSet.setValue(230, "A", "5月");
dataSet.setValue(630, "A", "6月");
dataSet.setValue(140, "B", "1月");
dataSet.setValue(250, "B", "2月");
dataSet.setValue(330, "B", "3月");
dataSet.setValue(560, "B", "4月");
dataSet.setValue(210, "B", "5月");
dataSet.setValue(990, "B", "6月");

生成图表
JFreeChart chart = ChartFactory.createLineChart(“销售统计”, “月份”,
"销量", dataSet, PlotOrientation.VERTICAL, true, false, false);
FileOutputStream fos = new FileOutputStream("C:\\linechart.jpg");
ChartUtilities.writeChartAsJPEG(fos, chart, 400, 300);
fos.close();

更多图表
http://www.ibm.com/developerworks/cn/java/l-jfreechart/
http://www.jfree.org/jfreechart/samples.html
FlashChart
http://teethgrinder.co.uk/open-flash-chart/
http://www.fusioncharts.com/
JavaScriptChart
http://www.flotcharts.org/
http://www.highcharts.com/

 

posted on 2015-10-30 15:39  shininguang  阅读(255)  评论(0编辑  收藏  举报