jFreeChart是Java開發(fā)中常用的統(tǒng)計(jì)類組件,主要包括柱狀圖,餅狀圖等。下面我們介紹一下jFreeChart最簡單的用法。
首先需要導(dǎo)入jFreeChart的jar包,放在項(xiàng)目web\WEB-INF\lib文件夾下。然后我們以最簡潔的代碼實(shí)現(xiàn)一個統(tǒng)計(jì)功能。
1.柱狀圖
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.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;

 public class BarChartTest {
 public static void main(String[] args) {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(20, "企業(yè)備案數(shù)", "北京局");
dataset.addValue(18, "企業(yè)備案數(shù)", "上海局");
dataset.addValue(16, "企業(yè)備案數(shù)", "天津局");
dataset.addValue(15, "企業(yè)備案數(shù)", "重慶局");
dataset.addValue(45, "企業(yè)備案數(shù)", "山東局");
JFreeChart chart = ChartFactory.createBarChart("企業(yè)備案圖", "直屬局", "企業(yè)備案數(shù)",dataset, PlotOrientation.HORIZONTAL, true, false, false);
 /** *//***************A start*********/
//設(shè)置標(biāo)題字體樣式
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("黑體", Font.PLAIN, 20));
//設(shè)置柱狀體顏色
CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
categoryplot.getRenderer().setSeriesPaint(0, new Color(228,109,10));
NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
CategoryAxis domainAxis = categoryplot.getDomainAxis();
//設(shè)置X軸坐標(biāo)上的字體樣式
domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
//設(shè)置X軸的標(biāo)題字體樣式
domainAxis.setLabelFont(new Font("宋體", Font.PLAIN, 12));
//設(shè)置Y軸坐標(biāo)上的字體樣式
numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
//設(shè)置Y軸的標(biāo)題字體樣式
numberaxis.setLabelFont(new Font("黑體", Font.PLAIN, 12));
//設(shè)置圖片最底部字體樣式
 if (chart.getLegend() != null) {
chart.getLegend().setItemFont(new Font("宋體", Font.PLAIN, 12));
}
 /** *//***************A end*********/
 try {
ChartUtilities.writeChartAsPNG(new FileOutputStream("D:\\barChart.jpg"), chart, 400, 200);
 } catch (FileNotFoundException e) {
e.printStackTrace();
 } catch (IOException e) {
e.printStackTrace();
}
}
}

生成的文件顯示效果如下:

2.餅狀圖
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.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;

 public class PieChartTest {
 public static void main(String[] args) {
DefaultPieDataset pieDataset = new DefaultPieDataset();
pieDataset.setValue("北京局",20);
pieDataset.setValue("上海局",18);
pieDataset.setValue("天津局",16);
pieDataset.setValue("重慶局",15);
pieDataset.setValue("山東局",45);
JFreeChart chart = ChartFactory.createPieChart3D("企業(yè)備案圖",pieDataset, true, false, false);
 /** *//***************A start*********/
//設(shè)置標(biāo)題字體樣式
chart.getTitle().setFont(new Font("黑體",Font.BOLD,20));
//設(shè)置餅狀圖里描述字體樣式
PiePlot piePlot= (PiePlot) chart.getPlot();
piePlot.setLabelFont(new Font("黑體",Font.BOLD,10));
//設(shè)置顯示百分比樣式
piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator(
("{0}({2})"), NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
//設(shè)置統(tǒng)計(jì)圖背景
piePlot.setBackgroundPaint(Color.white);
//設(shè)置圖片最底部字體樣式
chart.getLegend().setItemFont(new Font("黑體",Font.BOLD,10));
 /** *//***************A end*********/
 try {
ChartUtilities.writeChartAsPNG(new FileOutputStream("D:\\pieChart.jpg"), chart, 400, 300);
 } catch (FileNotFoundException e) {
e.printStackTrace();
 } catch (IOException e) {
e.printStackTrace();
}
}

}

生成的文件顯示效果如下:

其中以上兩段代碼中的“A”代碼塊中的內(nèi)容是為了解決圖片生成過程中遇到的亂碼問題,在實(shí)際開發(fā)中可以寫到一個公共類中,此時應(yīng)注意服務(wù)器的操作系統(tǒng)上是否缺少上述代碼中所用到的字體。關(guān)于jFreeChart詳細(xì)參數(shù)的使用請參考官方文檔。
|