include.picture.MyBarChart.java Source code

Java tutorial

Introduction

Here is the source code for include.picture.MyBarChart.java

Source

/*
 *this file is part of nseer erp
 *Copyright (C)2006-2010 Nseer(Beijing) Technology co.LTD/http://www.nseer.com 
 *
 *This program is free software; you can redistribute it and/or
 *modify it under the terms of the GNU General Public License
 *as published by the Free Software Foundation; either
 *version 2 of the License, or (at your option) any later version.
 */
package include.picture;

import java.io.*;

import org.jfree.data.*;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.BarRenderer3D;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import java.awt.Color;
import java.awt.Font;

/**
*?
*/
public class MyBarChart {

    private String title = null;//
    private String axisXLabel = null;//
    private String axisYLabel = null;//
    private int width = 400;//
    private int height = 300;//
    private String fileName = null;//???
    private double[][] data = null;//??
    private String[] category = null;//?
    private String[] series = null;//??

    public MyBarChart() {

    }

    /**
    *?
    */
    public void paint() {
        try {
            check();
            CategoryDataset dataset = getDataSet(series, category, data);
            JFreeChart chart = ChartFactory.createBarChart3D(title, axisXLabel, axisYLabel, dataset,
                    PlotOrientation.VERTICAL, true, false, false);
            chart.setBackgroundPaint(Color.WHITE);
            BarRenderer3D renderer = new BarRenderer3D();
            CategoryPlot plot = chart.getCategoryPlot();
            renderer.setMaxBarWidth(0.01);
            renderer.setBaseOutlinePaint(Color.BLACK);
            renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
            renderer.setItemLabelFont(new Font("", Font.PLAIN, 15));
            renderer.setItemLabelsVisible(true);
            renderer.setItemMargin(0.1);
            plot.setRenderer(renderer);
            FileOutputStream fos_jpg = null;
            try {
                fos_jpg = new FileOutputStream(fileName);
                ChartUtilities.writeChartAsJPEG(fos_jpg, 1000, chart, width, height, null);
            } finally {
                try {
                    fos_jpg.close();
                } catch (Exception e) {
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } // end of try-catch

    }

    /**
    *????
    */
    private void check() throws Exception {
        if (title == null || axisXLabel == null || axisYLabel == null || fileName == null || data == null
                || category == null || series == null) {
            throw new Exception("Some message should be assigned.");
        } // end of if ()

    }

    public String getTitle() {
        return this.title;
    }

    public void setTitle(String argTitle) {
        this.title = argTitle;
    }

    public String getAxisXLabel() {
        return this.axisXLabel;
    }

    public void setAxisXLabel(String argAxisXLabel) {
        this.axisXLabel = argAxisXLabel;
    }

    public String getAxisYLabel() {
        return this.axisYLabel;
    }

    public void setAxisYLabel(String argAxisYLabel) {
        this.axisYLabel = argAxisYLabel;
    }

    public int getWidth() {
        return this.width;
    }

    public void setWidth(int argWidth) {
        this.width = argWidth;
    }

    public int getHeight() {
        return this.height;
    }

    public void setHeight(int argHeight) {
        this.height = argHeight;
    }

    public double[][] getData() {
        return this.data;
    }

    public void setData(double[][] argData) {
        this.data = argData;
    }

    public String[] getCategory() {
        return this.category;
    }

    public void setCategory(String[] argCategory) {
        this.category = argCategory;
    }

    public String[] getSeries() {
        return this.series;
    }

    public void setSeries(String[] argSeries) {
        this.series = argSeries;
    }

    public String getFileName() {
        return this.fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    /**
    *paintseries,category,data ????
    */
    private CategoryDataset getDataSet(String[] series, String[] category, double[][] data) {
        return DatasetUtilities.createCategoryDataset(series, category, data);
    }

    public static void main(String[] args) throws IOException {

        /**
        *
        */
        double[][] data = new double[][] { { 10.0, 4.0, 15.0, 14.0 }, { 5.0, 7.0, 14.0, 3.0 },
                { 6.0, 17.0, 12.0, 7.0 }, { 7.0, 15.0, 11.0, 0.0 }, { 8.0, 6.0, 10.0, 9.0 }, { 9.0, 8.0, 0.0, 6.0 },
                { 10.0, 9.0, 7.0, 7.0 }, { 11.0, 13.0, 9.0, 9.0 }, { 3.0, 7.0, 11.0, 10.0 } };
        String[] series = new String[] { "", "", "", "", "", "", "",
                "", "?" };
        String[] category = new String[] { "", "", "", "" };
        MyBarChart bar = new MyBarChart();
        bar.setTitle("");
        bar.setAxisXLabel("");
        bar.setAxisYLabel("?");
        bar.setFileName("fruit.jpg");
        bar.setWidth(400);
        bar.setHeight(300);
        bar.setData(data);
        bar.setSeries(series);
        bar.setCategory(category);

        bar.paint();
    }

}