Jfree Chart 示例詳解

import java.awt.Color;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.PieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.RectangleEdge;

/**
 * 餅圖模板
 *
 * @author hongming du
 * @date 2010-03-16
 * @version 1.0
 *
 */
/**
 * 整個餅圖包括了許多的東西
 * 一張餅圖的本生圖片叫做PiePlot3D plot,
 * 而整個餅圖又是由許都的餅子組成,每一個組成的成員餅子叫做 PieSectionLabelGenerator lable
 *  在經常看到餅圖的旁邊,比如下方或者其他,會顯示許多不同顏色的但具有相同形狀的小圖像,這個小圖像叫Legend
 *  圖片顯示出不同的分佈,這個分佈是由數據形成的,某一個餅子在整個餅圖中所在的比例是由DefaultPieDataset dataset決定的
 *  當然,每個餅圖具有不同的title
 *  最後這個所有形成的餅圖叫做 JFreeChart chart
 */
public class PieChartTemplate {

    private static NumberFormat numberFormat;

    private static DecimalFormat decimalFormat;
    /**
     *  整個餅圖是由許多的餅圖平湊而成,PieSectionLabelGenerator表示的就是其中的一個餅子
     *  在他的參數中,第一個參數中:表示的是餅子的整個構造以及形成,參數第一個{0}表示的是餅子所代表的名字{2}表示的是自定義, 而{1}表示的是所代表的餅子的數值
     *  第二個參數:代表的是對第一個參數的格式化,或者叫對第一個參數的指認
     *  第三個參數:代表的是這個這個餅子在整個餅圖中所佔有比例的格式-->forExample:10.1%
     */
//一個餅塊生成一個標籤,標籤的內容可以自定義
    private static PieSectionLabelGenerator labelGenerator;

    static {
        numberFormat = NumberFormat.getNumberInstance();
        decimalFormat = new DecimalFormat("0.0 %");

        // 圖片中顯示百分比:自定義方式,{0} 表示選項, {1} 表示數值, {2} 表示所佔比例 ,小數點後兩位
        // 設置樣式,0表示KEY,1表示VALUE,2表示百分之幾,DecimalFormat用來顯示百分比的格式
        labelGenerator = new StandardPieSectionLabelGenerator(
                "{0}( {2} )", numberFormat, decimalFormat);
    }

    public static JFreeChart getPieChart3D(String title,
//            構造一個新的數據集,最初是空的
            DefaultPieDataset dataset) {
//        創建一個具有三維使用指定的數據集效果餅圖
        PiePlot3D plot = new PiePlot3D(dataset);
        // 沒有數據時顯示的消息
        plot.setNoDataMessage("沒有需要的數據");
        // 沒有數據時顯示的消息顏色
        plot.setNoDataMessagePaint(Color.RED);
        // 餅圖的初始角度
        plot.setStartAngle(120);
        // 數據區的前景透明度(0.0~1.0)
        plot.setForegroundAlpha(0.8f);
        // 指定顯示的餅圖上圓形(false)還橢圓形(true)
        plot.setCircular(false);
        // 3D餅圖的Z軸高度(0.0~1.0)
        plot.setDepthFactor(0.2f);
        // 分類標籤的格式,設置成null則整個標籤包括連接線都不顯示
        plot.setLabelGenerator(labelGenerator);
        JFreeChart chart = new JFreeChart(title, plot);
        // 背景色
        chart.setBackgroundPaint(Color.WHITE);
        // 邊界線條顏色
        chart.setBorderPaint(Color.WHITE);
        // 返回圖表示圖 ,設置標題的位置,RectangleEdge用來表示一個矩形的邊緣

        chart.getLegend().setPosition(RectangleEdge.RIGHT);

        return chart;
    }
}

 

在網上找了不少的資料,但沒有個是詳細的。最近公司要畫圖,逼着學習了JfreeChart ,在這裏做了點小小的總結,希望

對學習者有所用。最後還要感謝我公司的同事“Michael Gu“.

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