一些項目可能會使用圖形報表方式讓產品銷售,人員分佈,以及其他各種需要統計的東西更加的清晰直觀的表現出來,這裏介紹如何使用jfreechart生成圖形報表,
首先,你需要下載jfreeChart的包,導入其中的jar包,主要導入以下的jar包
這樣就可以使用JfreeChart,首先從柱狀圖開始
一,柱狀圖
顯示的圖形如下所示
現在貼上代碼如下
public static void main(String[] args) {
//構建數據集合
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(9, "中國","北京");
dataset.addValue(11, "中國","上海");
dataset.addValue(6, "中國","深圳");
//創建主張圖表
JFreeChart chart = ChartFactory.createBarChart3D(
"用戶統計報表",//圖形的主標題
"所屬單位的名稱",//種類軸標籤
"數量",//值軸的標籤
dataset, //圖形的數據集合
PlotOrientation.VERTICAL,//圖形的顯示樣式(水平和垂直)
true,//是否顯示子標題
true,//是否生產數據提示
true);//是否生產url鏈接
//處理主標題亂碼
chart.getTitle().setFont(new Font("宋體",Font.BOLD,15));
//處理子標題亂碼
chart.getLegend().setItemFont(new Font("宋體",Font.BOLD,15));
//獲取圖表區域對象
/**
* 三種獲取圖標對象的方法
* 1.debug
* 2.輸出(system.out.println())
* 3.api文檔
*/
CategoryPlot categoryPlot = chart.getCategoryPlot();
//取得x軸的對象
CategoryAxis categoryAxis = categoryPlot.getDomainAxis();
//取得y軸的對象
ValueAxis valueAxis = categoryPlot.getRangeAxis();
//解決x軸上的亂碼問題
categoryAxis.setTickLabelFont(new Font("宋體",Font.BOLD,15));
//解決x軸外的亂碼問題
categoryAxis.setLabelFont(new Font("宋體",Font.BOLD,15));
//解決y軸上的亂碼問題
valueAxis.setTickLabelFont(new Font("宋體",Font.BOLD,15));
//解決y軸外的亂碼問題
valueAxis.setLabelFont(new Font("宋體",Font.BOLD,15));
//設置y軸上的刻度
NumberAxis3D numberAxis3D = (NumberAxis3D) valueAxis;
numberAxis3D.setAutoTickUnitSelection(false);//設置手動設置刻度
//設置刻度從1開始
NumberTickUnit unit = new NumberTickUnit(1);
//設置新刻度
numberAxis3D.setTickUnit(unit);
//獲取繪圖區域對象
BarRenderer3D barRenderer3D = (BarRenderer3D) categoryPlot.getRenderer();
//讓圖形苗條些
barRenderer3D.setMaximumBarWidth(0.08);
//在圖形上產生數字
barRenderer3D.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
barRenderer3D.setBaseItemLabelsVisible(true);
barRenderer3D.setBaseItemLabelFont(new Font("宋體",Font.BOLD,15));
/**
* 在d:\下生產圖片
*
*/
File file = new File("D:/chart.png");
try {
ChartUtilities.saveChartAsJPEG(file, chart, 600, 500);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//使用Rrame加載圖形
ChartFrame chartFrame = new ChartFrame("xyz",chart);
chartFrame.setVisible(true);
//輸出圖形
chartFrame.pack();
}
解釋下上面的代碼,首先創建數據集合dataset,給數據集合放入值,比如我放入了,9, "中國","北京"等其他值,9表示數值,顯示在值軸的,中國,表示種類中的名字,而北京表示改種類下的一個具體種類,你可嘗試加入不同種類看效果,然後使用ChartFactory來生成柱狀圖的圖標對象,這裏也說明,jfreeChart是使用工廠模式
來生成各種圖形對象的,現在就可以輸出圖形了,使用ChartFrame來加載圖形即可,然後用pack()方法輸出圖形,但是如果你是使用中文寫的標題,或者,x,y軸的名稱,那麼
你需要處理中文的亂碼問題,處理方式,看代碼就好了
二,折線圖
折線圖和柱狀圖差不多,不過是將ChartFactory生成的柱狀圖換成了折線圖,然後我們看到設置不同屬性類圖形所表現的區別,代碼如下
public static void main(String[] args) {
//構建數據集合
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(12, "中國","北京");
dataset.addValue(10, "中國","上海");
dataset.addValue(6, "中國","深圳");
dataset.addValue(12, "美國","西雅圖");
dataset.addValue(10, "美國","紐約");
dataset.addValue(6, "美國","華盛頓");
dataset.addValue(7, "中國","天津");
dataset.addValue(9, "中國","南京");
dataset.addValue(11, "中國","江蘇");
//創建主張圖表
JFreeChart chart = ChartFactory.createLineChart(
"用戶統計報表",//圖形的主標題
"所屬單位的名稱",//種類軸標籤
"數量",//值軸的標籤
dataset, //圖形的數據集合
PlotOrientation.VERTICAL,//圖形的顯示樣式(水平和垂直)
true,//是否顯示子標題
true,//是否生產數據提示
true);//是否生產url鏈接
//處理主標題亂碼
chart.getTitle().setFont(new Font("宋體",Font.BOLD,15));
//處理子標題亂碼
chart.getLegend().setItemFont(new Font("宋體",Font.BOLD,15));
//獲取圖表區域對象
/**
* 三種獲取圖標對象的方法
* 1.debug
* 2.輸出(system.out.println())
* 3.api文檔
*/
CategoryPlot categoryPlot = chart.getCategoryPlot();
//取得x軸的對象
CategoryAxis categoryAxis = categoryPlot.getDomainAxis();
//取得y軸的對象
ValueAxis valueAxis = categoryPlot.getRangeAxis();
//解決x軸上的亂碼問題
categoryAxis.setTickLabelFont(new Font("宋體",Font.BOLD,15));
//解決x軸外的亂碼問題
categoryAxis.setLabelFont(new Font("宋體",Font.BOLD,15));
//解決y軸上的亂碼問題
valueAxis.setTickLabelFont(new Font("宋體",Font.BOLD,15));
//解決y軸外的亂碼問題
valueAxis.setLabelFont(new Font("宋體",Font.BOLD,15));
//設置y軸上的刻度
NumberAxis numberAxis = (NumberAxis) valueAxis;
numberAxis.setAutoTickUnitSelection(false);//設置手動設置刻度
//設置刻度從1開始
NumberTickUnit unit = new NumberTickUnit(1);
//設置新刻度
numberAxis.setTickUnit(unit);
//獲取繪圖區域對象
LineAndShapeRenderer lineAndShapeRenderer = (LineAndShapeRenderer) categoryPlot.getRenderer();
//在圖形上產生數字
lineAndShapeRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
lineAndShapeRenderer.setBaseItemLabelsVisible(true);
lineAndShapeRenderer.setBaseItemLabelFont(new Font("宋體",Font.BOLD,15));
//設置轉折點
Shape shape = new Rectangle(10,10);
lineAndShapeRenderer.setSeriesShape(0, shape);//第一個參數從第幾條線開始,默認從第0條
lineAndShapeRenderer.setSeriesShapesVisible(0, true);
lineAndShapeRenderer.setSeriesShape(1, shape);//第一個參數從第幾條線開始,默認從第0條
lineAndShapeRenderer.setSeriesShapesVisible(1, true);
lineAndShapeRenderer.setSeriesShape(2, shape);//第一個參數從第幾條線開始,默認從第0條
lineAndShapeRenderer.setSeriesShapesVisible(2, true);
/*
* 在d:\下生產圖片
*
*/
/* File file = new File("D:/chart.png");
try {
ChartUtilities.saveChartAsJPEG(file, chart, 600, 500);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
//使用Rrame加載圖形
ChartFrame chartFrame = new ChartFrame("xyz",chart);
chartFrame.setVisible(true);
//輸出圖形
chartFrame.pack();
}
代碼基本差不多,注意的是如何在圖形上生成一個方形來表示轉折,這裏,其實是使用了java中awt包的Rectangle(方形)類來製作的方形,所以可以查出,jfreeChart
還是很依賴java中awt這個類的,或者說他依賴於jdk的
三,扇形圖(餅圖)
餅圖比起上面兩個圖還要簡單,因爲餅圖沒有x,y軸,下面是代碼
public static void main(String[] args) {
//構建數據集合
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("北京",25);
dataset.setValue("上海",45);
dataset.setValue("深圳",30);
//創建主張圖表
JFreeChart chart = ChartFactory.createPieChart3D(
"用戶統計報表",//圖形的主標題
dataset, //圖形的數據集合
true,//是否顯示子標題
true,//是否生產數據提示
true);//是否生產url鏈接
//處理主標題亂碼
chart.getTitle().setFont(new Font("宋體",Font.BOLD,15));
//處理子標題亂碼
chart.getLegend().setItemFont(new Font("宋體",Font.BOLD,15));
//獲取圖表區域對象
/**
* 三種獲取圖標對象的方法
* 1.debug
* 2.輸出(system.out.println())
* 3.api文檔
*/
PiePlot3D piePlot3D = (PiePlot3D) chart.getPlot();
//處理圖形上顯示的亂碼問題
piePlot3D.setLabelFont(new Font("宋體",Font.BOLD,15));
//在圖形上顯示數值(格式:北京 12 (60%))
String labelFormat = "{0},{1},({2})";
piePlot3D.setLabelGenerator(new StandardPieSectionLabelGenerator(labelFormat));
//使用Rrame加載圖形
ChartFrame chartFrame = new ChartFrame("xyz",chart);
chartFrame.setVisible(true);
//輸出圖形
chartFrame.pack();
}
注意是如何在圖形上顯示如:北京,12 (60%)這種格式數據的,以上就是jfreeChart,三種基本圖形生成的方式