JFreeChart的使用 java …

聲明 :本篇博客爲我在網上看到的博客中複製過來的,因爲我的是新浪博客,沒法轉載,所以直接複製過來了
作者莫怪,寫的很詳細,有需要的朋友可以參考一下

個人官方網站 :點擊進入



JFreeChart的使用

前提:導入需要的2個jar文件,jcommon-版本號.jar,jfreechart-版本號.jar。可以去官網下載:http://sourceforge.net/projects/jfreechart/files/

注意:下載的Jfreechart版本不要太高,新版本對中文的顯示會出問題,我自己後來下的是1.0.10的版本。

 

實例一:比較簡單的application版本的餅圖

 

複製代碼

package com.test.jfreechart;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;

public class JFreeChartTest
{
public static void main(String[] args)
{
DefaultPieDataset dpd=new DefaultPieDataset(); //建立一個默認的餅圖
dpd.setValue("管理人員", 25); //輸入數據
dpd.setValue("市場人員", 25);
dpd.setValue("開發人員", 45);
dpd.setValue("其他人員", 10);

JFreeChart chart=ChartFactory.createPieChart("某公司人員組織數據圖",dpd,true,true,false);
//可以查具體的API文檔,第一個參數是標題,第二個參數是一個數據集,第三個參數表示是否顯示Legend,第四個參數表示是否顯示提示,第五個參數表示圖中是否存在URL

ChartFrame chartFrame=new ChartFrame("某公司人員組織數據圖",chart);
//chart要放在Java容器組件中,ChartFrame繼承自java的Jframe類。該第一個參數的數據是放在窗口左上角的,不是正中間的標題。
chartFrame.pack(); //以合適的大小展現圖形
chartFrame.setVisible(true);//圖形是否可見

}
}
複製代碼

 

運行結果如下:

 

注:一個圖表由以下3個部分組成:


實例二:一個結構更加明晰的application版本的柱狀圖,將邏輯分裝到各個函數中去。

 

複製代碼

package com.test.jfreechart;

import java.awt.Font;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;

public class JFreeChartTest2 extends ApplicationFrame
{
public JFreeChartTest2(String title)
{
super(title);
this.setContentPane(createPanel()); //構造函數中自動創建Java的panel面板
}

public static CategoryDataset createDataset() //創建柱狀圖數據集
{
DefaultCategoryDataset dataset=new DefaultCategoryDataset();
dataset.setValue(10,"a","管理人員");
dataset.setValue(20,"b","市場人員");
dataset.setValue(40,"c","開發人員");
dataset.setValue(15,"d","其他人員");
return dataset;
}

public static JFreeChart createChart(CategoryDataset dataset) //用數據集創建一個圖表
{
JFreeChart chart=ChartFactory.createBarChart("hi", "人員分佈",
"人員數量", dataset, PlotOrientation.VERTICAL, true, true, false); //創建一個JFreeChart
chart.setTitle(new TextTitle("某公司組織結構圖",new Font("宋體",Font.BOLD+Font.ITALIC,20)));//可以重新設置標題,替換“hi”標題
CategoryPlot plot=(CategoryPlot)chart.getPlot();//獲得圖標中間部分,即plot
CategoryAxis categoryAxis=plot.getDomainAxis();//獲得橫座標
categoryAxis.setLabelFont(new Font("微軟雅黑",Font.BOLD,12));//設置橫座標字體
return chart;
}

public static JPanel createPanel()
{
JFreeChart chart =createChart(createDataset());
return new ChartPanel(chart); //將chart對象放入Panel面板中去,ChartPanel類已繼承Jpanel
}

public static void main(String[] args)
{
JFreeChartTest2 chart=new JFreeChartTest2("某公司組織結構圖");
chart.pack();//以合適的大小顯示
chart.setVisible(true);

}
}
複製代碼

運行結果如下:


實例三:將chart圖表轉換成JPEG格式的圖片的application

複製代碼

package com.test.jfreechart;

import java.awt.Font;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;

public class JFreeChartTest3
{
public static void main(String[] args) throws Exception
{
JFreeChart chart=ChartFactory.createPieChart("某公司人員組織數據圖",getDataset(),true,true,false);
chart.setTitle(new TextTitle("某公司組織結構圖",new Font("宋體",Font.BOLD+Font.ITALIC,20)));

LegendTitle legend=chart.getLegend(0);//設置Legend
legend.setItemFont(new Font("宋體",Font.BOLD,14));
PiePlot plot=(PiePlot) chart.getPlot();//設置Plot
plot.setLabelFont(new Font("隸書",Font.BOLD,16));

OutputStream os = new FileOutputStream("company.jpeg");//圖片是文件格式的,故要用到FileOutputStream用來輸出。
ChartUtilities.writeChartAsJPEG(os, chart, 1000, 800);
//使用一個面向application的工具類,將chart轉換成JPEG格式的圖片。第3個參數是寬度,第4個參數是高度。

os.close();//關閉輸出流
}

private static DefaultPieDataset getDataset()
{
DefaultPieDataset dpd=new DefaultPieDataset(); //建立一個默認的餅圖
dpd.setValue("管理人員", 25); //輸入數據
dpd.setValue("市場人員", 25);
dpd.setValue("開發人員", 45);
dpd.setValue("其他人員", 10);
return dpd;
}
}
複製代碼

 

運行結果如下,在該項目的根目錄下生成了JPEG格式的圖片,注意不是在webroot目錄下。

 

實例四:將類似實例三生成的圖片嵌入到JSP頁面中去。

1.web.xml中加入以下配置信息.

 

複製代碼

    <</span>servlet>
<</span>servlet-name>DisplayChart</</span>servlet-name>
<</span>servlet-class>
org.jfree.chart.servlet.DisplayChart
</</span>servlet-class>
</</span>servlet>

<</span>servlet-mapping>
<</span>servlet-name>DisplayChart</</span>servlet-name>
<</span>url-pattern>/DisplayChart</</span>url-pattern>
</</span>servlet-mapping>
複製代碼

 

2.jfreeChart.jsp

複製代碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章