天天看點

JFreeChart—— 餅狀圖

1. 依賴包

JFreeChart—— 餅狀圖

2. XML 檔案配置

<servlet>
        <servlet-name>DisplayChart</servlet-name>
        <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>DisplayChart</servlet-name>
        <url-pattern>/DisplayChart</url-pattern>
    </servlet-mapping>
           

 3. 示例

3.1  平面圖

package com.zth;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.text.NumberFormat;

/**
 * @author zth
 * @Date 2019-05-24 23:13
 */
public class PieChart01 {
    public static String getPieChart(HttpSession session) throws Exception{
        DefaultPieDataset dataset = new DefaultPieDataset();

        dataset.setValue("java",0.15876);
        dataset.setValue("C",0.12424);
        dataset.setValue("python",0.07574);

        dataset.setValue("其他",0.64126);

        JFreeChart chart = ChartFactory.createPieChart("程式設計語言排行",dataset,true,true,true);

        // 副标題
        chart.addSubtitle(new TextTitle("2019年2月"));
        PiePlot piePlot = (PiePlot)chart.getPlot();
        piePlot.setLabelFont(new Font("宋體",0,11));
        // 設定餅圖是圓的(true),還是橢圓的(false);預設為true
        piePlot.setCircular(true);
        // 沒有資料的時候顯示的内容
        piePlot.setNoDataMessage("無資料顯示");

        // {0} 表示選項, {1} 表示數值, {2} 表示所占比例
        // {0}:({1}.{2})
        // {{0}={1}({2})
        StandardPieSectionLabelGenerator standarPieIG = new StandardPieSectionLabelGenerator(
                "{0}({2})", NumberFormat.getNumberInstance(), NumberFormat.getPercentInstance());
        piePlot.setLabelGenerator(standarPieIG);

        // 突出顯示
        piePlot.setExplodePercent("java",0.15876);

        String fileName = ServletUtilities.saveChartAsPNG(chart,400,300,null,session);

        return fileName;
    }
}
           

jsp 引用:

<center>
    <%
        String fileName= PieChart01.getPieChart(session);
    %>
    <img src="DisplayChart?filename=<%=fileName %>"  />
</center>
           

效果:

JFreeChart—— 餅狀圖

3.2 3D圖

package com.zth;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.util.Rotation;

import javax.servlet.http.HttpSession;
import java.awt.*;
import java.text.NumberFormat;

/**
 * @author zth
 * @Date 2019-05-24 23:13
 */
public class PieChart02 {
    public static String getPieChart(HttpSession session) throws Exception{
        DefaultPieDataset dataset = new DefaultPieDataset();

        dataset.setValue("java",0.15876);
        dataset.setValue("C",0.12424);
        dataset.setValue("python",0.07574);

        dataset.setValue("其他",0.64126);

        JFreeChart chart = ChartFactory.createPieChart3D("程式設計語言排行",dataset,true,true,true);

        // 副标題
        chart.addSubtitle(new TextTitle("2019年2月"));
        PiePlot piePlot = (PiePlot)chart.getPlot();
        piePlot.setLabelFont(new Font("宋體",0,11));
        // 設定餅圖是圓的(true),還是橢圓的(false);預設為true
        piePlot.setCircular(false);
        // 沒有資料的時候顯示的内容
        piePlot.setNoDataMessage("無資料顯示");

        // {0} 表示選項, {1} 表示數值, {2} 表示所占比例
        // {0}:({1}.{2})
        // {{0}={1}({2})
        StandardPieSectionLabelGenerator standarPieIG = new StandardPieSectionLabelGenerator(
                "{0}({2})", NumberFormat.getNumberInstance(), NumberFormat.getPercentInstance());
        piePlot.setLabelGenerator(standarPieIG);

        PiePlot3D piePlot3D = (PiePlot3D) chart.getPlot();

        //設定開始角度
        piePlot3D.setStartAngle(120D);
        //設定方向為”順時針方向“
        piePlot3D.setDirection(Rotation.CLOCKWISE);
        //設定透明度,0.5F為半透明,1為不透明,0為全透明
        piePlot3D.setForegroundAlpha(0.8F);

        String fileName = ServletUtilities.saveChartAsPNG(chart,400,300,null,session);

        return fileName;
    }
}
           

效果:

JFreeChart—— 餅狀圖