前言
JFreeChart是JAVA平台上的一个开放的图表绘制类库。它完全使用JAVA语言编写,是为applications, applets, servlets 以及JSP等使用所设计。JFreeChart可生成饼图、柱状图、散点图、时序图、甘特图等等多种图表,并且可以产生PNG和JPEG格式的输出,还可以与PDF和EXCEL关联
下来废话不多说 我们分别做个简单的折线图,柱状图,以及饼状图,想要构造出现这些图都并不是很难,当然这里只是最简单的一些用法。
首先我们需要导入相应的JFreeChart的jar包,jcommon-1.0.16.jar以及jfreechart-1[2].0.8a.jar。JFreeChart提供了一个工厂类ChartFactory专门来生成各种图。
柱状图
public class Menu extends JFrame {
private static final long serialVersionUID = 1L;
public JPanel frame = new JPanel();
public Menu(){
double[][] data = new double[][] {
{ 1230, 1110, 1120, 1210, 720, 750, 860, 800, 1230, 1110, 1120,
1210, 720, 750, 860, 800, 1230, 1110, 1120, 1210, 720,
750, 860, 800 },
{ 720, 750, 860, 800, 1230, 1110, 1120, 1210, 720, 750, 860,
800, 720, 750, 860, 800, 1230, 1110, 1120, 1210, 720,
750, 860, 800 },
{ 720, 750, 860, 800, 1230, 1110, 1120, 1210, 720, 750, 860,
800, 720, 750, 860, 800, 1230, 1110, 1120, 1210, 720,
750, 860, 800 }};
int[][] a = new int[][]{{2,3,4},{2,3,4},{2,3,45}};
String[] rowKeys = { "EasiNote", "EasiMeeting","SeewoClass" };
String[] columnKeys = { "0", "1", "2", "3", "4", "5", "6", "7", "8",
"9", "10", "11", "12", "13", "14", "15", "16", "17", "18",
"19", "20", "21", "22", "23" };
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
JFreeChart jfreechart = ChartFactory.createBarChart(
"bug数",//图表的标题
"月份",//目录轴的显示标签
"个数",//数值轴的显示标签
dataset,//数据集
PlotOrientation.VERTICAL,//图表的方向,垂直
true,// 是否显示图例
true, // 是否生成工具
false// 是否生成URL链接
);
ChartPanel chartPanel = new ChartPanel(jfreechart);
chartPanel.setBounds(0, 40, 600, 200);
frame.add(chartPanel);
setTitle("saii_Vesion 1.0");
setBounds(660, 150, 1249, 622);
add(frame);
setVisible(true);
}
public static void main(String []args) throws Exception{
Menu m=new Menu();
m.setVisible(true);
}
}
PS:这里还有一个需要注意的地方,项目新建以后的编码格式是GBK,记得要转成UTF-8的编码,如果使用GBK的话,中文的标题就会出现乱码,
饼状图
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("EasiNote",new Double(10));
dataset.setValue("SeewoClass",new Double(75));
dataset.setValue("EasiMeeting",new Double(15));
JFreeChart jfreechart = ChartFactory.createPieChart("Bug数",dataset,true,true,false);
折线图
DefaultCategoryDataset defaultCategoryDataset = new DefaultCategoryDataset();
// 各曲线名称
String series1 = "EasiNote";
String series2 = "EasiMeeting";
String series3 = "SeewoClass";
// 横轴名称(列名称)
String type1 = "1月";
String type2 = "2月";
String type3 = "3月";
defaultCategoryDataset.addValue(111.0, series1, type1);
defaultCategoryDataset.addValue(121.0, series1, type2);
defaultCategoryDataset.addValue(103.9, series1, type3);
defaultCategoryDataset.addValue(91.0, series2, type1);
defaultCategoryDataset.addValue(85.2, series2, type2);
defaultCategoryDataset.addValue(87.9, series2, type3);
defaultCategoryDataset.addValue(72.0, series3, type1);
defaultCategoryDataset.addValue(99.2, series3, type2);
defaultCategoryDataset.addValue(108.9, series3, type3);
JFreeChart jfreechart = ChartFactory.createLineChart("bug数","月份","个数",defaultCategoryDataset,PlotOrientation.VERTICAL,true,true,false);