2 Dec 2013

jfreechart bar chart in java swings with example

/* jfreechart bar chart in java swings with example, jfreechart example code in java */

package cost;
import cost.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import java.io.*;
/**
 * A simple demonstration application showing how to create a bar chart.
 *
 */
public class graph extends ApplicationFrame {

    /**

     * Creates a new demo instance.
     *
     * @param title  the frame title.
     */
    public graph(final String title) {

        super(title);


        final CategoryDataset dataset = createDataset();

        final JFreeChart chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(chartPanel);

    }


public String read(String st)
{
String er="";
try
{
File fileDir = new File(st);
BufferedReader in1 = new BufferedReader(new InputStreamReader(new FileInputStream(fileDir)));
String str;
while ((str = in1.readLine()) != null) {
   System.out.println(str);
er=str;
}
in1.close();
  }
catch(Exception e)
{
e.printStackTrace();
}
return er;
}

    /**

     * Returns a sample dataset.
     * 
     * @return The dataset.
     */
    private CategoryDataset createDataset() {
        
String s1=read("cocomo81.txt");
String s2=read("cocomoa.txt");
String s3=read("cocomoi.txt");
String s4=read("slim.txt");
String s5=read("fp.txt");
String s6=read("algo.txt");
String s7=read("rev.txt");

Anova ano=new Anova();

String tot1=ano.calc("cocomo81.txt");
String tot2=ano.calc("cocomoa.txt");
String tot3=ano.calc("cocomoi.txt");
String tot4=ano.calc("slim.txt");
String tot5=ano.calc("fp.txt");
String tot6=ano.calc("algo.txt");
String tot7=ano.calc("rev.txt");

String total=tot1+"##"+tot2+"##"+tot3+"##"+tot4+"##"+tot5+"##"+tot6+"##"+tot7;
new Store().store(total,"errors.txt");

String s11[]=s1.split("##");
String s21[]=s2.split("##");
String s31[]=s3.split("##");
String s41[]=s4.split("##");
String s51[]=s5.split("##");
String s61[]=s6.split("##");
String s71[]=s7.split("##");

double v1 = Double.parseDouble(s11[0]);
double v2 = Double.parseDouble(s21[0]);
double v3 = Double.parseDouble(s31[0]);
double v4 = Double.parseDouble(s41[0]);
double v5 = Double.parseDouble(s51[0]);
double v6 = Double.parseDouble(s61[0]);
double v7 = Double.parseDouble(s71[0]);

double v11 = Double.parseDouble(s11[1]);
double v21 = Double.parseDouble(s21[1]);
double v31 = Double.parseDouble(s31[1]);
double v41 = Double.parseDouble(s41[1]);
double v51 = Double.parseDouble(s51[1]);
double v61 = Double.parseDouble(s61[1]);
double v71 = Double.parseDouble(s71[1]);

double v12 = Double.parseDouble(s11[2]);
double v22 = Double.parseDouble(s21[2]);
double v32 = Double.parseDouble(s31[2]);
double v42 = Double.parseDouble(s41[2]);
double v52 = Double.parseDouble(s51[2]);
double v62 = Double.parseDouble(s61[2]);
double v72 = Double.parseDouble(s71[2]);

double v13 = Double.parseDouble(s11[3]);
double v23 = Double.parseDouble(s21[3]);
double v33 = Double.parseDouble(s31[3]);
double v43 = Double.parseDouble(s41[3]);
double v53 = Double.parseDouble(s51[3]);
double v63 = Double.parseDouble(s61[3]);
double v73 = Double.parseDouble(s71[3]);

double v14 = Double.parseDouble(s11[4]);
double v24 = Double.parseDouble(s21[4]);
double v34 = Double.parseDouble(s31[4]);
double v44 = Double.parseDouble(s41[4]);
double v54 = Double.parseDouble(s51[4]);
double v64 = Double.parseDouble(s61[4]);
double v74 = Double.parseDouble(s71[4]);
        // row keys...
        final String series1 = "Hospital";
        final String series2 = "Software";
        final String series3 = "Mobile";
final String series4 = "Telecome";
final String series5 = "Banking";

        // column keys...

        final String category1 = "COCOMO 81";
        final String category2 = "COCOMO II(basic)";
        final String category3 = "COCOMO II(Intermediate)";
        final String category4 = "SLIM";
        final String category5 = "Function Points";
final String category6 = "Algorithmic Cost Model";
final String category7 = "REVIREVIC";

        // create the dataset...

        final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        dataset.addValue(v1, series1, category1);

        dataset.addValue(v2, series1, category2);
        dataset.addValue(v3, series1, category3);
        dataset.addValue(v4, series1, category4);
        dataset.addValue(v5, series1, category5);
dataset.addValue(v6, series1, category6);
dataset.addValue(v7, series1, category7);

        dataset.addValue(v11, series2, category1);

        dataset.addValue(v21, series2, category2);
        dataset.addValue(v31, series2, category3);
        dataset.addValue(v41, series2, category4);
        dataset.addValue(v51, series2, category5);
dataset.addValue(v61, series2, category6);
dataset.addValue(v71, series2, category7);

        dataset.addValue(v12, series3, category1);

        dataset.addValue(v22, series3, category2);
        dataset.addValue(v32, series3, category3);
        dataset.addValue(v42, series3, category4);
        dataset.addValue(v52, series3, category5);
dataset.addValue(v62, series3, category6);
dataset.addValue(v72, series3, category7);

dataset.addValue(v13, series4, category1);
        dataset.addValue(v23, series4, category2);
        dataset.addValue(v33, series4, category3);
        dataset.addValue(v43, series4, category4);
        dataset.addValue(v53, series4, category5);
dataset.addValue(v63, series4, category6);
dataset.addValue(v73, series4, category7);

dataset.addValue(v14, series5, category1);
        dataset.addValue(v24, series5, category2);
        dataset.addValue(v34, series5, category3);
        dataset.addValue(v44, series5, category4);
        dataset.addValue(v54, series5, category5);
dataset.addValue(v64, series5, category6);
dataset.addValue(v74, series5, category7);

        

        return dataset;
        
    }

    private JFreeChart createChart(final CategoryDataset dataset) {

        
        // create the chart...
        final JFreeChart chart = ChartFactory.createBarChart(
            "Chart",         // chart title
            "Category",               // domain axis label
            "Value",                  // range axis label
            dataset,                  // data
            PlotOrientation.VERTICAL, // orientation
            true,                     // include legend
            true,                     // tooltips?
            false                     // URLs?
        );

        // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...


        // set the background color for the chart...

        chart.setBackgroundPaint(Color.white);

        // get a reference to the plot for further customisation...

        final CategoryPlot plot = chart.getCategoryPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);

        // set the range axis to display integers only...

        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        // disable bar outlines...

        final BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false);
        
        // set up gradient paints for series...
        final GradientPaint gp0 = new GradientPaint(
            0.0f, 0.0f, Color.blue, 
            0.0f, 0.0f, Color.lightGray
        );
        final GradientPaint gp1 = new GradientPaint(
            0.0f, 0.0f, Color.green, 
            0.0f, 0.0f, Color.lightGray
        );
        final GradientPaint gp2 = new GradientPaint(
            0.0f, 0.0f, Color.red, 
            0.0f, 0.0f, Color.lightGray
        );
final GradientPaint gp3 = new GradientPaint(
            0.0f, 0.0f, Color.pink, 
            0.0f, 0.0f, Color.lightGray
        );
final GradientPaint gp4 = new GradientPaint(
            0.0f, 0.0f, Color.orange, 
            0.0f, 0.0f, Color.lightGray
        );
final GradientPaint gp5 = new GradientPaint(
            0.0f, 0.0f, Color.gray, 
            0.0f, 0.0f, Color.lightGray
        );
        renderer.setSeriesPaint(0, gp0);
        renderer.setSeriesPaint(1, gp1);
        renderer.setSeriesPaint(2, gp2);
renderer.setSeriesPaint(3, gp3);
renderer.setSeriesPaint(4, gp4);
renderer.setSeriesPaint(5, gp5);

        final CategoryAxis domainAxis = plot.getDomainAxis();

        domainAxis.setCategoryLabelPositions(
            CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
        );
        // OPTIONAL CUSTOMISATION COMPLETED.
        
        return chart;
        
    }
    

    public static void main(final String[] args) {


        final graph demo = new graph("Chart");

        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }


}

How to run:
note: create the following files and also add following jar files to your classpath.

algo.txt with content like 1.928##1.256##2.485##1.021##4.618
cocomo81.txt with content like 5.007##3.263##6.456##2.653##1.199
cocomoa.txt with content like 2.085##1.359##2.688##1.105##4.994, etc
please check the above code

set classpath=./lib/commons-math3-3.0.jar;./lib/jcommon-1.0.13.jar;./lib/jfreechart-1.0.13.jar;./lib/jcommon-0.9.3.jar;.
javac -d . graph.java
java cost.graph

Output:


jfreechart bar chart in java swings with example

0 comments:

Post a Comment