昨天晚上接触JFreeChart觉得这东西很好用,
所以下载资源包试着去编译。其中也遇到点困难。
进不了sourceforge这是极度郁闷的事情,还好csdn上有下,
第一次接触,出问题是很正常的事。尤其是中文字体不显示的问题,
今天早上一早就来处理这个问题,以下是我的代码及注释
1
package com.test.jfree;
2
3
import java.awt.Font;
4
import java.io.FileNotFoundException;
5
import java.io.FileOutputStream;
6
import java.io.IOException;
7
8
import org.jfree.chart.ChartFactory;
9
import org.jfree.chart.ChartFrame;
10
import org.jfree.chart.ChartUtilities;
11
import org.jfree.chart.JFreeChart;
12
import org.jfree.chart.plot.PiePlot;
13
import org.jfree.chart.title.LegendTitle;
14
import org.jfree.chart.title.TextTitle;
15
import org.jfree.data.general.DefaultPieDataset;
16
17
public class JFreeChartTest
{
18
public static void main(String[] args)
{
19
DefaultPieDataset dpd = new DefaultPieDataset();
20
dpd.setValue("人事部", 15);
21
dpd.setValue("市场部", 20);
22
dpd.setValue("物流部", 21);
23
dpd.setValue("开发部", 16);
24
25
JFreeChart chart = ChartFactory.createPieChart("公司人员信息", dpd, true,
26
true, false);
27
// 设置标题文字,并将其字体设置 此处为图片正上方文字
28
chart.setTitle(new TextTitle("公司人员信息",new Font("黑体",Font.BOLD,16)));
29
// 取得统一表的第一个图列
30
LegendTitle legend = chart.getLegend(0);
31
// 修改图例字体
32
legend.setItemFont(new Font("宋体",Font.BOLD,14));
33
// 取得饼图plot对象
34
PiePlot plot = (PiePlot)chart.getPlot();
35
// 设置饼图各部分标签字体
36
plot.setLabelFont(new Font("隶书",Font.ITALIC,20));
37
// 设置标题文字
38
// 以下是显示
39
ChartFrame chartFrame = new ChartFrame("公司人员信息", chart);
40
chartFrame.pack();
41
chartFrame.setVisible(true);
42
43
// 以下是作为jpg文件写入到c盘根目录下
44
try
{
45
FileOutputStream fos = new FileOutputStream("c:\\test.jpg");
46
ChartUtilities.writeChartAsJPEG(fos,chart,300,400);
47
fos.close();
48
} catch (FileNotFoundException e)
{
49
e.printStackTrace();
50
} catch (IOException e)
{
51
e.printStackTrace();
52
}
53
}
54
}
需要增加的两个外部jar包,jcommon和jfreechart。