iad_2_gg.gui.ChartDialog.java Source code

Java tutorial

Introduction

Here is the source code for iad_2_gg.gui.ChartDialog.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 iad_2_gg.gui;

import java.awt.Dimension;
import java.util.List;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author glabg
 */
public class ChartDialog extends javax.swing.JDialog {

    public ChartDialog(java.awt.Frame parent, boolean modal, String chartTitle, String xLabel, String yLabel,
            List<Double> values, String name, int interval) {
        super(parent, modal);

        JFreeChart lineChart = ChartFactory.createXYLineChart(chartTitle, xLabel, yLabel,
                createDataset(values, name, interval), PlotOrientation.VERTICAL, true, true, false);

        ChartPanel chartPanel = new ChartPanel(lineChart);
        chartPanel.setPreferredSize(new Dimension(1200, 600));
        setContentPane(chartPanel);
        initComponents();
    }

    public ChartDialog(java.awt.Frame parent, boolean modal, String chartTitle, String xLabel, String yLabel,
            List<Double> values1, String sName1, List<Double> values2, String sName2, int interval) {
        super(parent, modal);

        JFreeChart lineChart = ChartFactory.createXYLineChart(chartTitle, xLabel, yLabel,
                createDataset(values1, sName1, values2, sName2, interval), PlotOrientation.VERTICAL, true, true,
                false);

        ChartPanel chartPanel = new ChartPanel(lineChart);
        chartPanel.setPreferredSize(new Dimension(1200, 600));
        setContentPane(chartPanel);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0,
                1210, Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 610,
                Short.MAX_VALUE));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    private XYDataset createDataset(List<Double> values, String name, int interval) {
        XYSeries serie = new XYSeries(name);

        int i = 1;
        for (double v : values) {
            serie.add((i * interval), v);
            i++;
        }
        XYSeriesCollection data = new XYSeriesCollection();
        data.addSeries(serie);
        return data;
    }

    private XYDataset createDataset(List<Double> values1, String sName1, List<Double> values2, String sName2,
            int interval) {
        XYSeriesCollection data = new XYSeriesCollection();
        XYSeries serie1 = new XYSeries(sName1);
        int i = 1;
        for (double v : values1) {
            serie1.add((i * interval), v);
            i++;
        }

        data.addSeries(serie1);

        XYSeries serie2 = new XYSeries(sName2);
        i = 1;
        for (double v : values2) {
            serie2.add((i * interval), v);
            i++;
        }
        data.addSeries(serie2);

        return data;
    }

}