利用JFreeChart生成简单柱状图(Java)
生活随笔
收集整理的這篇文章主要介紹了
利用JFreeChart生成简单柱状图(Java)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1 package barchartdemo1;
2
3 import <a href="http://lib.csdn.net/base/javaee" class='replace_word' title="Java EE知識庫" target='_blank' style='color:#df3434; font-weight:bold;'>Java</a>.awt.Font;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import org.jfree.chart.ChartFactory;
7 import org.jfree.chart.ChartUtilities;
8 import org.jfree.chart.JFreeChart;
9 import org.jfree.chart.axis.CategoryAxis;
10 import org.jfree.chart.axis.NumberAxis;
11 import org.jfree.chart.plot.CategoryPlot;
12 import org.jfree.chart.plot.PlotOrientation;
13 import org.jfree.data.category.CategoryDataset;
14 import org.jfree.data.category.DefaultCategoryDataset;
15
16 /**
17 *
18 * @author Administrator
19 */
20 public class Main {
21
22 /**
23 * @param args the command line arguments
24 */
25 public static void main(String[] args) throws IOException {
26 CategoryDataset ds = getDataSet();
27 JFreeChart chart = ChartFactory.createBarChart3D(
28 "水果產(chǎn)量圖", //圖表標(biāo)題
29 "水果", //目錄軸的顯示標(biāo)簽
30 "產(chǎn)量", //數(shù)值軸的顯示標(biāo)簽
31 ds, //數(shù)據(jù)集
32 PlotOrientation.VERTICAL, //圖表方向
33 true, //是否顯示圖例,對于簡單的柱狀圖必須為false
34 false, //是否生成提示工具
35 false); //是否生成url鏈接
36
37 CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
38
39 NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
40
41 CategoryAxis domainAxis = categoryplot.getDomainAxis();
42
43 /*------設(shè)置X軸坐標(biāo)上的文字-----------*/
44 domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
45
46 /*------設(shè)置X軸的標(biāo)題文字------------*/
47 domainAxis.setLabelFont(new Font("宋體", Font.PLAIN, 12));
48
49 /*------設(shè)置Y軸坐標(biāo)上的文字-----------*/
50 numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
51
52 /*------設(shè)置Y軸的標(biāo)題文字------------*/
53 numberaxis.setLabelFont(new Font("黑體", Font.PLAIN, 12));
54
55 /*------這句代碼解決了底部漢字亂碼的問題-----------*/
56 chart.getLegend().setItemFont(new Font("宋體", Font.PLAIN, 12));
57
58 /*******這句代碼解決了標(biāo)題漢字亂碼的問題********/
59 chart.getTitle().setFont(new Font("宋體", Font.PLAIN, 12));
60
61 FileOutputStream out = null;
62 try {
63 out = new FileOutputStream("E://Items//BarChartDemo1//image//1.jpg");
64 ChartUtilities.writeChartAsJPEG(out, 0.5f, chart, 400, 300, null);
65 } finally {
66 try {
67 out.close();
68 } catch (Exception ex) {
69 ex.printStackTrace();
70 }
71 }
72 }
73
74 private static CategoryDataset getDataSet() {
75 DefaultCategoryDataset ds = new DefaultCategoryDataset();
76 ds.addValue(100, "北京", "蘋果");
77 ds.addValue(100, "上海", "蘋果");
78 ds.addValue(100, "廣州", "蘋果");
79 ds.addValue(200, "北京", "梨子");
80 ds.addValue(200, "上海", "梨子");
81 ds.addValue(200, "廣州", "梨子");
82 ds.addValue(300, "北京", "葡萄");
83 ds.addValue(300, "上海", "葡萄");
84 ds.addValue(300, "廣州", "葡萄");
85 ds.addValue(400, "北京", "橘子");
86 ds.addValue(400, "上海", "橘子");
87 ds.addValue(400, "廣州", "橘子");
88 ds.addValue(500, "北京", "香蕉");
89 ds.addValue(500, "上海", "香蕉");
90 ds.addValue(500, "廣州", "香蕉");
91 return ds;
92 }
93 } 1 package barchartdemo1;
2
3 import <a href="http://lib.csdn.net/base/javaee" class='replace_word' title="Java EE知識庫" target='_blank' style='color:#df3434; font-weight:bold;'>Java</a>.awt.Font;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import org.jfree.chart.ChartFactory;
7 import org.jfree.chart.ChartUtilities;
8 import org.jfree.chart.JFreeChart;
9 import org.jfree.chart.axis.CategoryAxis;
10 import org.jfree.chart.axis.NumberAxis;
11 import org.jfree.chart.plot.CategoryPlot;
12 import org.jfree.chart.plot.PlotOrientation;
13 import org.jfree.data.category.CategoryDataset;
14 import org.jfree.data.category.DefaultCategoryDataset;
15
16 /**
17 *
18 * @author Administrator
19 */
20 public class Main {
21
22 /**
23 * @param args the command line arguments
24 */
25 public static void main(String[] args) throws IOException {
26 CategoryDataset ds = getDataSet();
27 JFreeChart chart = ChartFactory.createBarChart3D(
28 "水果產(chǎn)量圖", //圖表標(biāo)題
29 "水果", //目錄軸的顯示標(biāo)簽
30 "產(chǎn)量", //數(shù)值軸的顯示標(biāo)簽
31 ds, //數(shù)據(jù)集
32 PlotOrientation.VERTICAL, //圖表方向
33 true, //是否顯示圖例,對于簡單的柱狀圖必須為false
34 false, //是否生成提示工具
35 false); //是否生成url鏈接
36
37 CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
38
39 NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
40
41 CategoryAxis domainAxis = categoryplot.getDomainAxis();
42
43 /*------設(shè)置X軸坐標(biāo)上的文字-----------*/
44 domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
45
46 /*------設(shè)置X軸的標(biāo)題文字------------*/
47 domainAxis.setLabelFont(new Font("宋體", Font.PLAIN, 12));
48
49 /*------設(shè)置Y軸坐標(biāo)上的文字-----------*/
50 numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
51
52 /*------設(shè)置Y軸的標(biāo)題文字------------*/
53 numberaxis.setLabelFont(new Font("黑體", Font.PLAIN, 12));
54
55 /*------這句代碼解決了底部漢字亂碼的問題-----------*/
56 chart.getLegend().setItemFont(new Font("宋體", Font.PLAIN, 12));
57
58 /*******這句代碼解決了標(biāo)題漢字亂碼的問題********/
59 chart.getTitle().setFont(new Font("宋體", Font.PLAIN, 12));
60
61 FileOutputStream out = null;
62 try {
63 out = new FileOutputStream("E://Items//BarChartDemo1//image//1.jpg");
64 ChartUtilities.writeChartAsJPEG(out, 0.5f, chart, 400, 300, null);
65 } finally {
66 try {
67 out.close();
68 } catch (Exception ex) {
69 ex.printStackTrace();
70 }
71 }
72 }
73
74 private static CategoryDataset getDataSet() {
75 DefaultCategoryDataset ds = new DefaultCategoryDataset();
76 ds.addValue(100, "北京", "蘋果");
77 ds.addValue(100, "上海", "蘋果");
78 ds.addValue(100, "廣州", "蘋果");
79 ds.addValue(200, "北京", "梨子");
80 ds.addValue(200, "上海", "梨子");
81 ds.addValue(200, "廣州", "梨子");
82 ds.addValue(300, "北京", "葡萄");
83 ds.addValue(300, "上海", "葡萄");
84 ds.addValue(300, "廣州", "葡萄");
85 ds.addValue(400, "北京", "橘子");
86 ds.addValue(400, "上海", "橘子");
87 ds.addValue(400, "廣州", "橘子");
88 ds.addValue(500, "北京", "香蕉");
89 ds.addValue(500, "上海", "香蕉");
90 ds.addValue(500, "廣州", "香蕉");
91 return ds;
92 }
93 }
?
?
圖片效果:
總結(jié)
以上是生活随笔為你收集整理的利用JFreeChart生成简单柱状图(Java)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 鼠标拖动div移动js代码
- 下一篇: colpick-jQuery颜色选择器使