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

Java tutorial

Introduction

Here is the source code for com.ouc.cpss.view.ProfitChartBuilder.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.ViewProfit;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartPanel;
import java.awt.Font;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;

/**
 * ??
 * @author su
 */
public class ProfitChartBuilder extends JFrame {
    String title = null;
    static List<ViewProfit> list;//??

    public ProfitChartBuilder(String title, List list) {
        super(title);
        this.title = title;
        ProfitChartBuilder.list = list;
        this.setContentPane(createPanel());
    }

    private static CategoryDataset createDataSet() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        int num = 0;
        for (int i = 1; i <= 12 && num < list.size(); i++) {
            //?????
            if (i != list.get(num).getThemonth()) {
                dataset.addValue(0, "", i + "");
            } else {
                dataset.addValue(list.get(num).getTotalprofit(), "", list.get(num).getThemonth() + "");
                num++;
                if (num == list.size())
                    num -= 1;
            }

        }
        return dataset;
    }

    public static JFreeChart createJFreeChart(CategoryDataset dataset) {

        JFreeChart chart = ChartFactory.createLineChart("?", "", "?",
                dataset, PlotOrientation.VERTICAL, true, true, false);
        // ?
        LegendTitle legendTitle = chart.getLegend(0);
        // 
        legendTitle.setItemFont(new Font("", Font.BOLD, 18));
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        // ?
        org.jfree.chart.axis.CategoryAxis categoryAxis = plot.getDomainAxis();
        // 
        categoryAxis.setLabelFont(new Font("", Font.BOLD, 18));

        // 
        categoryAxis.setTickLabelFont(new Font("", Font.BOLD, 12));
        // ?
        NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();
        // 
        numberAxis.setLabelFont(new Font("", Font.BOLD, 14));

        numberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());//

        CategoryItemRenderer xylineandshaperenderer = plot.getRenderer();
        xylineandshaperenderer.setBaseItemLabelsVisible(true);
        xylineandshaperenderer.setBasePositiveItemLabelPosition(
                new ItemLabelPosition(ItemLabelAnchor.OUTSIDE2, TextAnchor.BASELINE_RIGHT));
        xylineandshaperenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        return chart;
    }

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