frequencyassignment.charts.Charts.java Source code

Java tutorial

Introduction

Here is the source code for frequencyassignment.charts.Charts.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 frequencyassignment.charts;

import java.util.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultKeyedValuesDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author Luka
 */
public class Charts {
    public static void displayPie(String name, HashMap<String, Double> categoryValue) {
        DefaultPieDataset data = new DefaultKeyedValuesDataset();
        for (Map.Entry<String, Double> entry : categoryValue.entrySet()) {
            data.setValue(entry.getKey(), entry.getValue());
        }
        JFreeChart chart = ChartFactory.createPieChart("Pie Chart", data, true, true, true);
        ChartFrame frame = new ChartFrame(name, chart);
        frame.pack();
        frame.setVisible(true);
    }

    public static void displayBarChart(String name, HashMap<Integer, Integer> categoryValue) {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for (Map.Entry<Integer, Integer> entry : categoryValue.entrySet()) {
            dataset.addValue(entry.getValue(), name, entry.getKey());
        }
        JFreeChart chart = ChartFactory.createBarChart("Bar Chart", "Category", "Value", dataset);
        ChartFrame frame = new ChartFrame(name, chart);
        frame.pack();
        frame.setVisible(true);
    }

    public static void displayScatterPlot(String name, HashMap<Double, Double> values) {
        XYSeries xyData = new XYSeries(name);
        for (Map.Entry<Double, Double> entry : values.entrySet()) {
            xyData.add(entry.getKey(), entry.getValue());
        }
        XYSeriesCollection xySeriesCollection = new XYSeriesCollection(xyData);
        JFreeChart chart = ChartFactory.createScatterPlot(name, "X", "Y", (XYDataset) xySeriesCollection);
        ChartFrame frame = new ChartFrame(name, chart);
        frame.pack();
        frame.setVisible(true);
    }

}