Thursday

JFreeChart example - create charts in Java and save them as jpeg

Download jcommon-1.0.16.jar and add to the build path.

https://mvnrepository.com/artifact/jfree/jcommon/1.0.16

For maven:
<dependency>
    <groupId>jfree</groupId>
    <artifactId>jcommon</artifactId>
    <version>1.0.16</version>
</dependency>
Download jfreechart-1.0.13.jar and add to the build path.

https://mvnrepository.com/artifact/jfree/jfreechart/1.0.13
For maven:
<dependency>
    <groupId>jfree</groupId>
    <artifactId>jfreechart</artifactId>
    <version>1.0.13</version>
</dependency>

import java.io.IOException;

public class Main {

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

              PieChart3D pieChart3D = new PieChart3D("Comparison", "Which operating system are you using?");
              pieChart3D.pack();
              pieChart3D.setVisible(true);

              PieChart pieChart = new PieChart("Comparison", "Which operating system are you using?");
              pieChart.pack();
              pieChart.setVisible(true);

              XYAreaChart xyAreaChart = new XYAreaChart("Comparison", "Sample XY Area Chart");
              xyAreaChart.pack();
              xyAreaChart.setVisible(true);

              XYLineChart xyLineChart = new XYLineChart("Comparison", "Sample XY Line Chart");
              xyLineChart.pack();
              xyLineChart.setVisible(true);

              XYStepAreaChart xyStepAreaChart = new XYStepAreaChart("Comparison", "Sample XY Step Area Chart");
              xyStepAreaChart.pack();
              xyStepAreaChart.setVisible(true);

              //We will save the following chart to a file named chart.jpg
              VerticalBarChart3D verticalBarChart3D = new VerticalBarChart3D("Comparison", "Sample Vertical Bar Chart");
       }
}


import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;


public class PieChart extends JFrame {

    private static final long serialVersionUID = 1L;

    public PieChart(String applicationTitle, String chartTitle) {
        super(applicationTitle);
        PieDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset, chartTitle);

        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);
    }

    private  PieDataset createDataset() {
        DefaultPieDataset result = new DefaultPieDataset();
        result.setValue("Linux", 29);
        result.setValue("Mac", 20);
        result.setValue("Windows", 51);
        return result;
    }

    private JFreeChart createChart(PieDataset dataset, String title) {
        JFreeChart chart = ChartFactory.createPieChart(
            title,                  // chart title
            dataset,                // data
            true,                   // include legend
            true,
            false
        );


        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setStartAngle(290);
        plot.setDirection(Rotation.CLOCKWISE);
        plot.setForegroundAlpha(0.5f);
        return chart;
    }
}



import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;


public class PieChart3D extends JFrame {

    private static final long serialVersionUID = 1L;

    public PieChart3D(String applicationTitle, String chartTitle) {
        super(applicationTitle);
        PieDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset, chartTitle);

        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);
    }

    private  PieDataset createDataset() {
        DefaultPieDataset result = new DefaultPieDataset();
        result.setValue("Linux", 29);
        result.setValue("Mac", 20);
        result.setValue("Windows", 51);
        return result;
    }

    private JFreeChart createChart(PieDataset dataset, String title) {
        JFreeChart chart = ChartFactory.createPieChart3D(
            title,                  // chart title
            dataset,                // data
            true,                   // include legend
            true,
            false
        );

        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setStartAngle(290);
        plot.setDirection(Rotation.CLOCKWISE);
        plot.setForegroundAlpha(0.5f);
        return chart;
    }
}



import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;


public class VerticalBarChart3D extends JFrame {

    private static final long serialVersionUID = 1L;

    public VerticalBarChart3D(String applicationTitle, String chartTitle) throws IOException {
        super(applicationTitle);
        DefaultCategoryDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset, chartTitle);

        BufferedImage image = chart.createBufferedImage(500,300);
        JLabel lblChart = new JLabel();
        lblChart.setIcon(new ImageIcon(image));
        ChartUtilities.saveChartAsJPEG(new File("chart.jpg"), chart, 500, 300);
    }

    private  DefaultCategoryDataset createDataset() {
        DefaultCategoryDataset categoryDataset = new DefaultCategoryDataset();
        categoryDataset.setValue(6,"Marks" ,"Aditi" ); 
        categoryDataset.setValue(3,"Marks" ,"Pooja" ); 
        categoryDataset.setValue(10,"Marks" ,"Ria" ); 
        categoryDataset.setValue(5,"Marks" ,"Twinkle" ); 
        categoryDataset.setValue(20,"Marks" ,"Rutvi" );

        return categoryDataset;
    }

    private JFreeChart createChart(DefaultCategoryDataset dataset, String title) {

        JFreeChart chart = ChartFactory.createBarChart(
            title,                  // chart title
            "Students",              // X-Axis label
            "Marks",                 // Y-Axis label,
            dataset,
            PlotOrientation.VERTICAL,      //Plot orientation 
            false,                // Show legend 
            true,                // Use tooltips 
            false                // Generate URLs 
        );

        chart.getTitle().setPaint(Color.BLUE);    // Set the colour of the title 
        chart.setBackgroundPaint(Color.WHITE);    // Set the background colour of the chart
        CategoryPlot cp = (CategoryPlot ) chart.getPlot();
        cp.setBackgroundPaint(Color.CYAN);      
        cp.setRangeGridlinePaint(Color.RED);

        return chart;
    }
}



import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;


public class XYAreaChart extends JFrame {

    private static final long serialVersionUID = 1L;

    public XYAreaChart(String applicationTitle, String chartTitle) {
        super(applicationTitle);

        XYSeries series = createDataset();
        XYDataset xydataset = new XYSeriesCollection(series);
        JFreeChart chart = createXYAreaChart(xydataset, chartTitle);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);
    }

    private  XYSeries createDataset() {
        XYSeries result = new XYSeries ("Average Size");
        result.add(20, 29);
        result.add(40, 20);
        result.add(78, 51);

        return result;
    }

    private JFreeChart createXYAreaChart(XYDataset xydataset, String title) {
 
        JFreeChart chart = ChartFactory.createXYAreaChart(
            title,                  // chart title
            "Height",           // X-Axis label
            "Weight",           // Y-Axis label
            xydataset,          // Dataset
            PlotOrientation.VERTICAL,
            true,            // include legend
            true,            // tooltips
            true             // urls
        );

        XYPlot plot = (XYPlot) chart.getXYPlot();
        plot.setForegroundAlpha(0.5f);
        return chart;
    }
}



import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;


public class XYLineChart extends JFrame {

    private static final long serialVersionUID = 1L;

    public XYLineChart(String applicationTitle, String chartTitle) {
        super(applicationTitle);

        XYSeries series = createDataset();
        XYDataset xydataset = new XYSeriesCollection(series);
        JFreeChart chart = createXYLineChart(xydataset, chartTitle);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);
    }

    private  XYSeries createDataset() {
        XYSeries result = new XYSeries ("Average Size");
        result.add(20, 29);
        result.add(40, 20);
        result.add(78, 51);

        return result;
    }

    private JFreeChart createXYLineChart(XYDataset xydataset, String title) {

        JFreeChart chart = ChartFactory.createXYLineChart(
            title,                  // chart title
            "Height",           // X-Axis label
            "Weight",           // Y-Axis label
            xydataset,          // Dataset
            PlotOrientation.VERTICAL,
            true,            // include legend
            true,            // tooltips
            true             // urls
        );

        XYPlot plot = (XYPlot) chart.getXYPlot();
        plot.setForegroundAlpha(0.5f);
  
        return chart;
    }
}



import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;


public class XYStepAreaChart extends JFrame {

    private static final long serialVersionUID = 1L;

    public XYStepAreaChart(String applicationTitle, String chartTitle) {
        super(applicationTitle);

        XYSeries series = createDataset();
        XYDataset xydataset = new XYSeriesCollection(series);

        JFreeChart stepAreaChart = createXYStepAreaChart(xydataset, chartTitle);
        ChartPanel stepAreaChartPanel = new ChartPanel(stepAreaChart);
        stepAreaChartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(stepAreaChartPanel);
    }

    private  XYSeries createDataset() {
        XYSeries result = new XYSeries ("Average Size");

        result.add(20, 29);
        result.add(40, 20);
        result.add(78, 51);
  
        return result;
    }

    private JFreeChart createXYStepAreaChart(XYDataset xydataset, String title) {
        JFreeChart chart = ChartFactory.createXYStepAreaChart(
            title,                  // chart title
            "Height",           // X-Axis label
            "Weight",           // Y-Axis label
            xydataset,          // Dataset
            PlotOrientation.VERTICAL,
            true,            // include legend
            true,            // tooltips
            true             // urls
        );

        XYPlot plot = (XYPlot) chart.getXYPlot();
        plot.setForegroundAlpha(0.5f);
        return chart;
    }
}

No comments:

Post a Comment