JFreeChart 生成图表

来源:互联网 发布:天猫魔盒 vpn软件 编辑:程序博客网 时间:2024/04/27 01:59

http://www.jfree.org/jfreechart/samples.html  查看示例

 

 

一、饼图

代码

 
view plaincopy to clipboardprint?
01./** 
02. *  ClassName: PieChartTest.java 
03. *  created on 2008-12-21 
04. *  Copyrights 2008 qjyong All rights reserved. 
05. *  EMail: qjyong@gmail.com 
06. */ 
07.package test;  
08. 
09.import java.awt.BasicStroke;  
10.import java.awt.Color;  
11.import java.awt.Font;  
12.import java.io.FileNotFoundException;  
13.import java.io.FileOutputStream;  
14.import java.io.IOException;  
15.import java.text.DecimalFormat;  
16.import java.text.NumberFormat;  
17. 
18.import org.jfree.chart.ChartFactory;  
19.import org.jfree.chart.ChartFrame;  
20.import org.jfree.chart.ChartUtilities;  
21.import org.jfree.chart.JFreeChart;  
22.import org.jfree.chart.labels.StandardPieSectionLabelGenerator;  
23.import org.jfree.chart.labels.StandardPieToolTipGenerator;  
24.import org.jfree.chart.plot.PiePlot;  
25.import org.jfree.chart.title.TextTitle;  
26.import org.jfree.chart.urls.StandardPieURLGenerator;  
27.import org.jfree.data.general.DefaultPieDataset;  
28.import org.jfree.data.general.PieDataset;  
29.import org.jfree.util.Rotation;  
30. 
31./** 
32. * 使用JFreeChart绘制饼图 
33. * @author qiujy 
34. */ 
35.public class PieChartTest {  
36. 
37.    /** 
38.     * step1:创建数据集对象 
39.     * @return 
40.     */ 
41.    public static PieDataset createDataSet() {  
42.        DefaultPieDataset dataset = new DefaultPieDataset();  
43.        dataset.setValue("java程序设计语言", 10000);  
44.        dataset.setValue("JSP基础与案例开发详解", 20000);  
45.        dataset.setValue("struts基础与案例开发详解", 30000);  
46.        dataset.setValue("精通JSF", 40000);  
47.      
48.        return dataset;  
49.    }  
50. 
51.    /** 
52.     * step2:创建图表 
53.     * @param dataset 
54.     * @return 
55.     */ 
56.    public static JFreeChart createChart(PieDataset dataset) {  
57.         JFreeChart chart = ChartFactory.createPieChart3D(  
58.        //JFreeChart chart = ChartFactory.createPieChart(  
59.                "原创图书销量统计", // 图表标题  
60.                dataset, // 数据集  
61.                true, // 是否显示图例  
62.                true, // 是否显示工具提示  
63.                true // 是否生成URL  
64.                );  
65.      
66.        //设置标题字体==为了防止中文乱码:必须设置字体  
67.        chart.setTitle(new TextTitle("原创图书销量统计", new Font("黑体", Font.ITALIC, 22)));  
68.        //设置图例的字体==为了防止中文乱码:必须设置字体  
69.        chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 12));   
70.        // 获取饼图的Plot对象(实际图表)  
71.        PiePlot plot = (PiePlot) chart.getPlot();   
72.        //图形边框颜色     
73.        plot.setBaseSectionOutlinePaint(Color.GRAY);     
74.        //图形边框粗细     
75.        plot.setBaseSectionOutlineStroke(new BasicStroke(0.0f));   
76.        //设置饼状图的绘制方向,可以按顺时针方向绘制,也可以按逆时针方向绘制     
77.        plot.setDirection(Rotation.ANTICLOCKWISE);     
78.        //设置绘制角度(图形旋转角度)     
79.        plot.setStartAngle(70);     
80.        //设置突出显示的数据块     
81.        plot.setExplodePercent("One", 0.1D);    
82.        //设置背景色透明度  
83.        plot.setBackgroundAlpha(0.7F);   
84.        // 设置前景色透明度  
85.        plot.setForegroundAlpha(0.65F);   
86.        //设置区块标签的字体==为了防止中文乱码:必须设置字体  
87.        plot.setLabelFont(new Font("隶书", Font.PLAIN, 12));   
88.        // 扇区分离显示,对3D图不起效  
89.        plot.setExplodePercent(dataset.getKey(3), 0.1D);  
90.        // 图例显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位  
91.        plot.setLabelGenerator(new StandardPieSectionLabelGenerator(  
92.                "{0}:{1}/r/n({2})", NumberFormat.getNumberInstance(),  
93.                new DecimalFormat("0.00%")));  
94.        // 图例显示百分比  
95.        // plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));  
96.        // 指定显示的饼图为:圆形(true) 还是椭圆形(false)  
97.        plot.setCircular(false);  
98.        // 没有数据的时候显示的内容  
99.        plot.setNoDataMessage("找不到可用数据...");      
100.          
101.        //设置鼠标悬停提示  
102.        plot.setToolTipGenerator(new StandardPieToolTipGenerator());  
103.        //设置热点链接  
104.        plot.setURLGenerator(new StandardPieURLGenerator("detail.jsp"));  
105.          
106.        return chart;  
107.    }  
108.      
109.    /** 
110.     * step3: 输出图表到Swing Frame 
111.     * @param chart 
112.     */ 
113.    public static void drawToFrame(JFreeChart chart){  
114.        //输出图表到Swing Frame  
115.        ChartFrame frame = new ChartFrame("原创图书销量统计", chart);  
116.        frame.pack();  
117.        frame.setVisible(true);  
118.    }  
119.      
120.    /** 
121.     * step3: 输出图表到指定的磁盘 
122.     * @param destPath 
123.     * @param chart 
124.     */ 
125.    public static void drawToOutputStream(String destPath, JFreeChart chart) {  
126.        FileOutputStream fos = null;  
127.        try {  
128.            fos = new FileOutputStream(destPath);  
129.            // ChartUtilities.writeChartAsJPEG(  
130.            ChartUtilities.writeChartAsPNG(fos, // 指定目标输出流  
131.                    chart, // 图表对象  
132.                    600, // 宽  
133.                    400, // 高  
134.                    null); // ChartRenderingInfo信息  
135.        } catch (IOException e) {  
136.            e.printStackTrace();  
137.        } finally {  
138.            try {   fos.close();      
139.            } catch (IOException e) {  
140.                e.printStackTrace();  
141.            }  
142.        }  
143.    }  
144.      
145.    public static void main(String[] args) throws FileNotFoundException {  
146.        // step1:创建数据集对象  
147.        PieDataset dataset = createDataSet();  
148.      
149.        // step2:创建图表  
150.        JFreeChart chart = createChart(dataset);  
151.      
152.        // step3: 输出图表到Swing窗口  
153.        //drawToFrame(chart);  
154.      
155.        // step3: 输出图表到磁盘  
156.        drawToOutputStream("D://mybook-pie.png", chart);  
157.    }  
158.} 
/**
 *  ClassName: PieChartTest.java
 *  created on 2008-12-21
 *  Copyrights 2008 qjyong All rights reserved.
 *  EMail: qjyong@gmail.com
 */
package test;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;

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.StandardPieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieToolTipGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.urls.StandardPieURLGenerator;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;

/**
 * 使用JFreeChart绘制饼图
 * @author qiujy
 */
public class PieChartTest {

 /**
  * step1:创建数据集对象
  * @return
  */
 public static PieDataset createDataSet() {
  DefaultPieDataset dataset = new DefaultPieDataset();
  dataset.setValue("java程序设计语言", 10000);
  dataset.setValue("JSP基础与案例开发详解", 20000);
  dataset.setValue("struts基础与案例开发详解", 30000);
  dataset.setValue("精通JSF", 40000);
 
  return dataset;
 }

 /**
  * step2:创建图表
  * @param dataset
  * @return
  */
 public static JFreeChart createChart(PieDataset dataset) {
   JFreeChart chart = ChartFactory.createPieChart3D(
  //JFreeChart chart = ChartFactory.createPieChart(
    "原创图书销量统计", // 图表标题
    dataset, // 数据集
    true, // 是否显示图例
    true, // 是否显示工具提示
    true // 是否生成URL
    );
 
  //设置标题字体==为了防止中文乱码:必须设置字体
  chart.setTitle(new TextTitle("原创图书销量统计", new Font("黑体", Font.ITALIC, 22)));
  //设置图例的字体==为了防止中文乱码:必须设置字体
  chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 12));
  // 获取饼图的Plot对象(实际图表)
  PiePlot plot = (PiePlot) chart.getPlot();
  //图形边框颜色  
  plot.setBaseSectionOutlinePaint(Color.GRAY);  
  //图形边框粗细  
  plot.setBaseSectionOutlineStroke(new BasicStroke(0.0f));
  //设置饼状图的绘制方向,可以按顺时针方向绘制,也可以按逆时针方向绘制  
  plot.setDirection(Rotation.ANTICLOCKWISE);  
  //设置绘制角度(图形旋转角度)  
  plot.setStartAngle(70);  
  //设置突出显示的数据块  
  plot.setExplodePercent("One", 0.1D); 
  //设置背景色透明度
  plot.setBackgroundAlpha(0.7F);
  // 设置前景色透明度
  plot.setForegroundAlpha(0.65F);
  //设置区块标签的字体==为了防止中文乱码:必须设置字体
  plot.setLabelFont(new Font("隶书", Font.PLAIN, 12));
  // 扇区分离显示,对3D图不起效
  plot.setExplodePercent(dataset.getKey(3), 0.1D);
  // 图例显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位
  plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
    "{0}:{1}/r/n({2})", NumberFormat.getNumberInstance(),
    new DecimalFormat("0.00%")));
  // 图例显示百分比
  // plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));
  // 指定显示的饼图为:圆形(true) 还是椭圆形(false)
  plot.setCircular(false);
  // 没有数据的时候显示的内容
  plot.setNoDataMessage("找不到可用数据...");   
  
  //设置鼠标悬停提示
  plot.setToolTipGenerator(new StandardPieToolTipGenerator());
  //设置热点链接
  plot.setURLGenerator(new StandardPieURLGenerator("detail.jsp"));
  
  return chart;
 }
 
 /**
  * step3: 输出图表到Swing Frame
  * @param chart
  */
 public static void drawToFrame(JFreeChart chart){
  //输出图表到Swing Frame
  ChartFrame frame = new ChartFrame("原创图书销量统计", chart);
  frame.pack();
  frame.setVisible(true);
 }
 
 /**
  * step3: 输出图表到指定的磁盘
  * @param destPath
  * @param chart
  */
 public static void drawToOutputStream(String destPath, JFreeChart chart) {
  FileOutputStream fos = null;
  try {
   fos = new FileOutputStream(destPath);
   // ChartUtilities.writeChartAsJPEG(
   ChartUtilities.writeChartAsPNG(fos, // 指定目标输出流
     chart, // 图表对象
     600, // 宽
     400, // 高
     null); // ChartRenderingInfo信息
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   try { fos.close(); 
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
 
 public static void main(String[] args) throws FileNotFoundException {
  // step1:创建数据集对象
  PieDataset dataset = createDataSet();
 
  // step2:创建图表
  JFreeChart chart = createChart(dataset);
 
  // step3: 输出图表到Swing窗口
  //drawToFrame(chart);
 
  // step3: 输出图表到磁盘
  drawToOutputStream("D://mybook-pie.png", chart);
 }
}
 

产生的图

 

二、饼图

代码

view plaincopy to clipboardprint?
01./** 
02. *  ClassName: BarChartTest.java 
03. *  created on 2008-12-21 
04. *  Copyrights 2008 qjyong All rights reserved. 
05. *  EMail: qjyong@gmail.com 
06. */ 
07.package test;  
08. 
09.import java.awt.Color;  
10.import java.awt.Font;  
11.import java.io.FileNotFoundException;  
12.import java.io.FileOutputStream;  
13.import java.io.IOException;  
14. 
15.import org.jfree.chart.ChartFactory;  
16.import org.jfree.chart.ChartFrame;  
17.import org.jfree.chart.ChartUtilities;  
18.import org.jfree.chart.JFreeChart;  
19.import org.jfree.chart.axis.CategoryAxis;  
20.import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;  
21.import org.jfree.chart.plot.CategoryPlot;  
22.import org.jfree.chart.plot.PlotOrientation;  
23.import org.jfree.chart.renderer.category.BarRenderer;  
24.import org.jfree.chart.title.LegendTitle;  
25.import org.jfree.chart.title.TextTitle;  
26.import org.jfree.data.category.CategoryDataset;  
27.import org.jfree.data.category.DefaultCategoryDataset;  
28. 
29. 
30./** 
31. * 柱状图和折线图 
32. * @author qiujy 
33. */ 
34.public class BarChartTest {  
35.    /** 
36.     * step1:创建 简单数据集对象 
37.     * @return 
38.     */ 
39.    public static CategoryDataset createDataSet() {  
40.        DefaultCategoryDataset dataset = new DefaultCategoryDataset();  
41.        dataset.setValue(10000, "","Corejava");  
42.        dataset.setValue(20000, "","JavaWeb");  
43.        dataset.setValue(30000, "","易用struts");  
44.        dataset.setValue(40000, "","精通JSF");  
45.      
46.        return dataset;  
47.    }  
48.      
49.    /** 
50.     * 组合数据集对象 
51.     * @return 
52.     */ 
53.public static CategoryDataset createDataSet2() {  
54.    DefaultCategoryDataset dataset = new DefaultCategoryDataset();  
55.    dataset.setValue(5000, "北京","Corejava");  
56.    dataset.setValue(3000, "上海","Corejava");  
57.    dataset.setValue(2000, "广州","Corejava");  
58.      
59.    dataset.setValue(10000, "北京","JavaWeb");  
60.    dataset.setValue(6000, "上海","JavaWeb");  
61.    dataset.setValue(4000, "广州","JavaWeb");  
62.      
63.    dataset.setValue(15000, "北京","易用struts");  
64.    dataset.setValue(5000, "上海","易用struts");  
65.    dataset.setValue(10000, "广州","易用struts");  
66.      
67.    dataset.setValue(20000, "北京","精通JSF");  
68.    dataset.setValue(10000, "上海","精通JSF");  
69.    dataset.setValue(10000, "广州","精通JSF");  
70. 
71.    return dataset;  
72.}  
73.      
74.    /** 
75.     * step2:创建图表 
76.     * @param dataset 
77.     * @return 
78.     */ 
79.    public static JFreeChart createChart(CategoryDataset dataset) {  
80.        JFreeChart chart = ChartFactory.createBarChart3D(   //3D柱状图  
81.        //JFreeChart chart = ChartFactory.createLineChart3D(  //3D折线图  
82.                "原创图书销量统计", //图表的标题  
83.                "图书名",  //目录轴的显示标签   
84.                "销量",   //数值轴的显示标签  
85.                dataset, //数据集  
86.                PlotOrientation.VERTICAL,  //图表方式:V垂直;H水平   
87.                true, // 是否显示图例  
88.                false, // 是否显示工具提示  
89.                false // 是否生成URL  
90.                );  
91.          
92.        //===============为了防止中文乱码:必须设置字体  
93.        chart.setTitle(new TextTitle("原创图书销量统计", new Font("黑体", Font.ITALIC, 22)));  
94.      
95.        LegendTitle legend = chart.getLegend(); // 获取图例  
96.        legend.setItemFont(new Font("宋体", Font.BOLD, 12)); //设置图例的字体,防止中文乱码  
97.      
98.        CategoryPlot plot = (CategoryPlot) chart.getPlot(); // 获取柱图的Plot对象(实际图表)  
99.        // 设置柱图背景色(注意,系统取色的时候要使用16位的模式来查看颜色编码,这样比较准确)  
100.        plot.setBackgroundPaint(new Color(255, 255, 204));  
101.        plot.setForegroundAlpha(0.65F); //设置前景色透明度  
102.          
103.        // 设置横虚线可见  
104.        plot.setRangeGridlinesVisible(true);  
105.        // 虚线色彩  
106.        plot.setRangeGridlinePaint(Color.gray);  
107.          
108.        CategoryAxis h = plot.getDomainAxis(); //获取x轴  
109.        h.setMaximumCategoryLabelWidthRatio(1.0f);// 横轴上的 Lable 是否完整显示  
110.        h.setLabelFont(new Font("宋体", Font.BOLD, 12));//设置字体,防止中文乱码  
111.        h.setTickLabelFont(new Font("宋体", Font.BOLD, 12));// 轴数值   
112.        //h.setCategoryLabelPositions(CategoryLabelPositions.UP_45);//45度倾斜  
113.          
114.        plot.getRangeAxis().setLabelFont(new Font("宋体", Font.BOLD, 12)); //Y轴设置字体,防止中文乱码  
115.          
116.        //柱图的呈现器  
117.        BarRenderer renderer = new BarRenderer();   
118.        // 设置柱子宽度   
119.        //renderer.setMaximumBarWidth(0.05);   
120.        // 设置柱子高度   
121.        //renderer.setMinimumBarLength(0.2);   
122.        // 设置柱子边框颜色   
123.        renderer.setBaseOutlinePaint(Color.BLACK);   
124.        // 设置柱子边框可见   
125.        renderer.setDrawBarOutline(true);   
126.        //设置每个柱的颜色   
127.        renderer.setSeriesPaint(0, Color.BLUE);   
128.        renderer.setSeriesPaint(1, Color.GREEN);   
129.        renderer.setSeriesPaint(2, Color.RED);   
130.        //设置每个地区所包含的平行柱的之间距离   
131.        renderer.setItemMargin(0.05);   
132.        // 显示每个柱的数值,并修改该数值的字体属性   
133.        renderer.setIncludeBaseInRange(true);   
134.        renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());   
135.        renderer.setBaseItemLabelsVisible(true);   
136.        // 设置柱的透明度   
137.        plot.setForegroundAlpha(1.0f);   
138.        //给柱图添加呈现器  
139.        plot.setRenderer(renderer);   
140.          
141.        // 没有数据的时候显示的内容  
142.        plot.setNoDataMessage("找不到可用数据...");  
143.          
144.        return chart;  
145.    }  
146.      
147.    /** 
148.     * step3: 输出图表到Swing Frame 
149.     * @param chart 
150.     */ 
151.    public static void drawToFrame(JFreeChart chart){  
152.        //输出图表到Swing Frame  
153.        ChartFrame frame = new ChartFrame("原创图书销量统计", chart);  
154.        frame.pack();  
155.        frame.setVisible(true);  
156.    }  
157.      
158.    /** 
159.     * step3: 输出图表到指定的磁盘 
160.     * @param destPath 
161.     * @param chart 
162.     */ 
163.    public static void drawToOutputStream(String destPath, JFreeChart chart) {  
164.        FileOutputStream fos = null;  
165.        try {  
166.            fos = new FileOutputStream(destPath);  
167.            // ChartUtilities.writeChartAsJPEG(  
168.            ChartUtilities.writeChartAsPNG(fos, // 指定目标输出流  
169.                    chart, // 图表对象  
170.                    600, // 宽  
171.                    500, // 高  
172.                    null); // ChartRenderingInfo信息  
173.        } catch (IOException e) {  
174.            e.printStackTrace();  
175.        } finally {  
176.            try {  
177.                fos.close();  
178.            } catch (IOException e) {  
179.                e.printStackTrace();  
180.            }  
181.        }  
182.    }  
183. 
184.    public static void main(String[] args) throws FileNotFoundException {  
185.        // step1:创建数据集对象  
186.        CategoryDataset dataset = createDataSet2();  
187.      
188.        // step2:创建饼图  
189.        JFreeChart chart = createChart(dataset);  
190.      
191.        // step3: 输出图表到Swing窗口  
192.        //drawToFrame(chart);  
193.      
194.        // step3: 输出图表到磁盘  
195.        drawToOutputStream("D://mybook-bar.png", chart);  
196.    }  
197. 
198.} 
/**
 *  ClassName: BarChartTest.java
 *  created on 2008-12-21
 *  Copyrights 2008 qjyong All rights reserved.
 *  EMail: qjyong@gmail.com
 */
package test;

import java.awt.Color;
import java.awt.Font;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;


/**
 * 柱状图和折线图
 * @author qiujy
 */
public class BarChartTest {
 /**
  * step1:创建 简单数据集对象
  * @return
  */
 public static CategoryDataset createDataSet() {
  DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  dataset.setValue(10000, "","Corejava");
  dataset.setValue(20000, "","JavaWeb");
  dataset.setValue(30000, "","易用struts");
  dataset.setValue(40000, "","精通JSF");
 
  return dataset;
 }
 
 /**
  * 组合数据集对象
  * @return
  */
public static CategoryDataset createDataSet2() {
 DefaultCategoryDataset dataset = new DefaultCategoryDataset();
 dataset.setValue(5000, "北京","Corejava");
 dataset.setValue(3000, "上海","Corejava");
 dataset.setValue(2000, "广州","Corejava");
 
 dataset.setValue(10000, "北京","JavaWeb");
 dataset.setValue(6000, "上海","JavaWeb");
 dataset.setValue(4000, "广州","JavaWeb");
 
 dataset.setValue(15000, "北京","易用struts");
 dataset.setValue(5000, "上海","易用struts");
 dataset.setValue(10000, "广州","易用struts");
 
 dataset.setValue(20000, "北京","精通JSF");
 dataset.setValue(10000, "上海","精通JSF");
 dataset.setValue(10000, "广州","精通JSF");

 return dataset;
}
 
 /**
  * step2:创建图表
  * @param dataset
  * @return
  */
 public static JFreeChart createChart(CategoryDataset dataset) {
  JFreeChart chart = ChartFactory.createBarChart3D( //3D柱状图
  //JFreeChart chart = ChartFactory.createLineChart3D(  //3D折线图
    "原创图书销量统计", //图表的标题
    "图书名",  //目录轴的显示标签
    "销量",   //数值轴的显示标签
    dataset, //数据集
    PlotOrientation.VERTICAL,  //图表方式:V垂直;H水平
    true, // 是否显示图例
    false, // 是否显示工具提示
    false // 是否生成URL
    );
  
  //===============为了防止中文乱码:必须设置字体
  chart.setTitle(new TextTitle("原创图书销量统计", new Font("黑体", Font.ITALIC, 22)));
 
  LegendTitle legend = chart.getLegend(); // 获取图例
  legend.setItemFont(new Font("宋体", Font.BOLD, 12)); //设置图例的字体,防止中文乱码
 
  CategoryPlot plot = (CategoryPlot) chart.getPlot(); // 获取柱图的Plot对象(实际图表)
  // 设置柱图背景色(注意,系统取色的时候要使用16位的模式来查看颜色编码,这样比较准确)
  plot.setBackgroundPaint(new Color(255, 255, 204));
  plot.setForegroundAlpha(0.65F); //设置前景色透明度
  
  // 设置横虚线可见
  plot.setRangeGridlinesVisible(true);
  // 虚线色彩
  plot.setRangeGridlinePaint(Color.gray);
  
  CategoryAxis h = plot.getDomainAxis(); //获取x轴
  h.setMaximumCategoryLabelWidthRatio(1.0f);// 横轴上的 Lable 是否完整显示
  h.setLabelFont(new Font("宋体", Font.BOLD, 12));//设置字体,防止中文乱码
  h.setTickLabelFont(new Font("宋体", Font.BOLD, 12));// 轴数值
  //h.setCategoryLabelPositions(CategoryLabelPositions.UP_45);//45度倾斜
  
  plot.getRangeAxis().setLabelFont(new Font("宋体", Font.BOLD, 12)); //Y轴设置字体,防止中文乱码
  
  //柱图的呈现器
  BarRenderer renderer = new BarRenderer();
  // 设置柱子宽度
  //renderer.setMaximumBarWidth(0.05);
  // 设置柱子高度
  //renderer.setMinimumBarLength(0.2);
  // 设置柱子边框颜色
  renderer.setBaseOutlinePaint(Color.BLACK);
  // 设置柱子边框可见
  renderer.setDrawBarOutline(true);
  //设置每个柱的颜色
  renderer.setSeriesPaint(0, Color.BLUE);
  renderer.setSeriesPaint(1, Color.GREEN);
  renderer.setSeriesPaint(2, Color.RED);
  //设置每个地区所包含的平行柱的之间距离
  renderer.setItemMargin(0.05);
  // 显示每个柱的数值,并修改该数值的字体属性
  renderer.setIncludeBaseInRange(true);
  renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
  renderer.setBaseItemLabelsVisible(true);
  // 设置柱的透明度
  plot.setForegroundAlpha(1.0f);
  //给柱图添加呈现器
  plot.setRenderer(renderer);
  
  // 没有数据的时候显示的内容
  plot.setNoDataMessage("找不到可用数据...");
  
  return chart;
 }
 
 /**
  * step3: 输出图表到Swing Frame
  * @param chart
  */
 public static void drawToFrame(JFreeChart chart){
  //输出图表到Swing Frame
  ChartFrame frame = new ChartFrame("原创图书销量统计", chart);
  frame.pack();
  frame.setVisible(true);
 }
 
 /**
  * step3: 输出图表到指定的磁盘
  * @param destPath
  * @param chart
  */
 public static void drawToOutputStream(String destPath, JFreeChart chart) {
  FileOutputStream fos = null;
  try {
   fos = new FileOutputStream(destPath);
   // ChartUtilities.writeChartAsJPEG(
   ChartUtilities.writeChartAsPNG(fos, // 指定目标输出流
     chart, // 图表对象
     600, // 宽
     500, // 高
     null); // ChartRenderingInfo信息
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   try {
    fos.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }

 public static void main(String[] args) throws FileNotFoundException {
  // step1:创建数据集对象
  CategoryDataset dataset = createDataSet2();
 
  // step2:创建饼图
  JFreeChart chart = createChart(dataset);
 
  // step3: 输出图表到Swing窗口
  //drawToFrame(chart);
 
  // step3: 输出图表到磁盘
  drawToOutputStream("D://mybook-bar.png", chart);
 }

}
   

产生的图

 

三、一些没有保密要求的图表,可以直接使用Google图表API(http://code.google.com/intl/zh-CN/apis/chart/)来生成,更简单更直接:

如:请求链接为http://chart.apis.google.com/chart?cht=p3&chco=0000ff&chd=t:40,30,20,10&chs=500x250&chl=java|jsp|ssh|ejb

产生的图片为

 

其它图表可参看Google提供的API

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/qjyong/archive/2009/12/08/4967705.aspx

原创粉丝点击