com.ouc.cpss.view.SupTradeChartBuilder.java Source code

Java tutorial

Introduction

Here is the source code for com.ouc.cpss.view.SupTradeChartBuilder.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ouc.cpss.view;

import com.ouc.cpss.vo.ViewEmpsel;
import com.ouc.cpss.vo.ViewSupTrade;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;

/**
 *
 * @author LIUYIYU
 */

@SuppressWarnings("serial")
public class SupTradeChartBuilder extends JFrame {

    String title;
    static List<ViewSupTrade> list;//??

    public SupTradeChartBuilder(String title, List list) {
        super(title);
        this.title = title;
        this.list = list;
        //???
        this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        this.setContentPane(createPanel()); //?????

    }

    private static PieDataset createDataSet() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        for (ViewSupTrade vst : list) {
            //?????
            dataset.setValue(vst.getSupname(), vst.getPurtotal());
        }
        return dataset;
    }

    private static JFreeChart createJFreeChart(PieDataset dataset) {
        /**
         * JFreeChart
         */
        //?     
        StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
        //     
        standardChartTheme.setExtraLargeFont(new Font("", Font.BOLD, 20));
        //    
        standardChartTheme.setRegularFont(new Font("", Font.PLAIN, 15));
        //?     
        standardChartTheme.setLargeFont(new Font("", Font.PLAIN, 15));
        //?   
        ChartFactory.setChartTheme(standardChartTheme);
        //??  
        //createPieChart 2D; createPieChart3D  3D
        JFreeChart chart = ChartFactory.createPieChart("", dataset, true, true, false);

        //,?
        chart.setTitle(new TextTitle("", new Font("", Font.ITALIC, 22)));

        //?
        LegendTitle legend = chart.getLegend(0);
        //,ture,?
        legend.setItemFont(new Font("", Font.BOLD, 20));

        //?(??)
        PiePlot plot = (PiePlot) chart.getPlot();
        //?==?
        plot.setLabelFont(new Font("", Font.BOLD, 22));
        //
        plot.setBaseSectionOutlinePaint(Color.BLUE);
        //
        plot.setBaseSectionOutlineStroke(new BasicStroke(0.5f));
        //?,??,??
        plot.setDirection(Rotation.CLOCKWISE);//,Rotation.CLOCKWISE
        //()
        plot.setStartAngle(70);
        //???
        //plot.setExplodePercent(1, 0.5D);
        //plot.setExplodePercent("One", 0.5D);
        //,3D?
        plot.setExplodePercent(dataset.getKey(0), 0.1d);
        //
        plot.setLabelLinkPaint(Color.BLUE);
        //
        plot.setLabelOutlinePaint(Color.black);
        //
        plot.setLabelShadowPaint(Color.RED);
        //
        plot.setSectionPaint(1, Color.BLACK);
        // :,{0},{1},{2}?,???
        plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}:{1}\r\n{2}",
                NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));

        //
        plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={2}"));
        //:(true),(false)
        plot.setCircular(true);
        //?
        plot.setNoDataMessage("??...");

        //???
        plot.setToolTipGenerator(new StandardPieToolTipGenerator());
        //
        //plot.setURLGenerator(new StandardPieURLGenerator("detail.jsp"));

        return chart;
    }

    public static JPanel createPanel() {
        // ?JFreeChart(????)
        JFreeChart chart = createJFreeChart(createDataSet());
        // JFreeChar?JPanel
        return new ChartPanel(chart);
    }

    public static void main(String[] args) {

    }

}