com.main.controller.ChartDIG.java Source code

Java tutorial

Introduction

Here is the source code for com.main.controller.ChartDIG.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.main.controller;

import com.main.dto.FrequencyDTO;
import java.io.File;
import java.util.ArrayList;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.omg.PortableInterceptor.INACTIVE;

/**
 *
 * @author Ashish
 */

public class ChartDIG {

    String chartTitle;
    String xTitle;
    String yTitle;
    ArrayList<String> arrayList;
    int width;
    int height;

    public ChartDIG(String chartTitle, String xTitle, String yTitle, ArrayList<String> arrayList, int width,
            int height) {
        this.chartTitle = chartTitle;
        this.xTitle = xTitle;
        this.yTitle = yTitle;
        this.arrayList = arrayList;
        this.width = width;
        this.height = height;
    }

    public File generateBarChart() throws Exception {
        final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for (String string : arrayList) {
            dataset.addValue(Integer.parseInt(string), string, string);
        }

        JFreeChart barChart = ChartFactory.createBarChart(chartTitle, xTitle, yTitle, dataset,
                PlotOrientation.VERTICAL, true, true, false);

        //        int width = 640;
        //        /* Width of the image */
        //        int height = 480;
        /* Height of the image */
        File BarChart = new File("chart/" + chartTitle + ".jpeg");
        ChartUtilities.saveChartAsJPEG(BarChart, barChart, width, height);
        return BarChart;
    }

    public File generatePieChart() throws Exception {
        DefaultPieDataset dataset = new DefaultPieDataset();
        for (String arrayList1 : arrayList) {
            dataset.setValue(arrayList1, Double.parseDouble(arrayList1));
        }

        JFreeChart chart = ChartFactory.createPieChart(chartTitle, // chart title
                dataset, // data
                true, // include legend
                true, false);

        File pieChart = new File("chart/" + chartTitle + ".jpeg");
        ChartUtilities.saveChartAsJPEG(pieChart, chart, width, height);
        return pieChart;
    }
}