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

Java tutorial

Introduction

Here is the source code for com.ouc.cpss.view.ChartProBuilder.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.ViewPro;
import java.awt.Font;
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.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;

/**
 *
 * @author Administrator
 */
public class ChartProBuilder extends JFrame {
    String title;
    static int choice = -1;
    static List<ViewPro> list;//??

    public ChartProBuilder(String title, List list, int choice1) {
        super(title);
        choice = choice1;
        this.title = title;
        this.list = list;
        //???
        this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        // ???Frame?
        this.setContentPane(createPanel());
    }

    private static CategoryDataset createDataSet() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        // 
        int i = 0;
        if (choice == 1) {
            for (ViewPro v : list) {
                // ?????
                //  ??dateset 
                dataset.addValue(v.getTotalselcount(), v.getProname(), "" + (i + 1) + "??");
                i++;
            }
        } else if (choice == 2) {
            for (ViewPro v : list) {
                dataset.addValue(v.getTotalselmoney(), v.getProname(), "" + (i + 1) + "??");
                i++;
            }
        } else {
            return null;
        }
        return dataset;
    }

    private static JFreeChart createJFreeChart(CategoryDataset 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);
        //?
        // ?
        JFreeChart jfreeChart = null;
        if (choice == 1) {
            jfreeChart = ChartFactory.createBarChart3D("? -- ?TOP10", "",
                    "?", dataset, PlotOrientation.VERTICAL, true, false, false);
            /**
              * JFreeChart
              */
            jfreeChart.setTitle(new TextTitle("? -- ?TOP10",
                    new Font("", Font.BOLD + Font.ITALIC, 20)));
            CategoryPlot plot = (CategoryPlot) jfreeChart.getPlot();
            CategoryAxis categoryAxis = plot.getDomainAxis();
            categoryAxis.setLabelFont(new Font("", Font.ROMAN_BASELINE, 12));
        } else {
            jfreeChart = ChartFactory.createBarChart3D("? -- ?TOP10", "",
                    "?", dataset, PlotOrientation.VERTICAL, true, false, false);
            jfreeChart.setTitle(new TextTitle("? -- ?TOP10",
                    new Font("", Font.BOLD + Font.ITALIC, 20)));
            CategoryPlot plot = (CategoryPlot) jfreeChart.getPlot();
            CategoryAxis categoryAxis = plot.getDomainAxis();
            categoryAxis.setLabelFont(new Font("", Font.ROMAN_BASELINE, 12));
        }
        return jfreeChart;
    }

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

}