one.TimeLinePanel.java Source code

Java tutorial

Introduction

Here is the source code for one.TimeLinePanel.java

Source

package one;

import TableModel.TaskModel;
import java.awt.Component;
import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.category.IntervalCategoryDataset;
import org.jfree.data.gantt.Task;
import org.jfree.data.gantt.TaskSeries;
import org.jfree.data.gantt.TaskSeriesCollection;
import org.jfree.data.time.SimpleTimePeriod;

/*
 * 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.
 */

/**
 *
 * @author jas
 */
public class TimeLinePanel extends javax.swing.JPanel {

    /**
     * Creates new form TimeLinePanel
     */
    public static HashMap<Date, String> hm;// = new HashMap<>();
    public static String[] task;// = new String[hm.size()];
    public static Date[] time;// = new Date[hm.size()];

    public static IntervalCategoryDataset dataset;
    public static JFreeChart chart;
    public static ChartPanel chartPanel;
    String title;
    TaskModel tm;

    public TimeLinePanel() {
        initComponents();

        hm = new HashMap<>();
        tm = new TaskModel();
        hm = tm.timeLineChart(101);

        task = new String[hm.size()];
        time = new Date[hm.size()];

        dataset = createDataset();
        chart = createChart(dataset);

        // add the chart to a panel...
        chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        myInit();
        //this.add(chartPanel);
        //setContentPane(chartPanel);
    }

    public void myInit() {

        this.add(chartPanel);

    }

    public static IntervalCategoryDataset createDataset() {

        final TaskSeries s1 = new TaskSeries("Scheduled");

        int i = 0;
        for (Map.Entry<Date, String> pairs : TimeLinePanel.hm.entrySet()) {
            time[i] = (Date) pairs.getKey();
            task[i] = (String) pairs.getValue();
            s1.add(new Task(task[i], new SimpleTimePeriod(date(14, Calendar.OCTOBER, 2014), time[i])));
            i = i + 1;
        }
        System.out.println(task[1]);
        s1.add(new Task(task[1], new SimpleTimePeriod(date(14, Calendar.OCTOBER, 2014), time[1])));
        s1.add(new Task("Database set up",
                new SimpleTimePeriod(date(15, Calendar.OCTOBER, 2014), date(30, Calendar.OCTOBER, 2014))));
        final TaskSeriesCollection collection = new TaskSeriesCollection();
        collection.add(s1);
        return collection;
    }

    private static Date date(final int day, final int month, final int year) {

        final Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, day);
        final Date result = calendar.getTime();
        return result;

    }

    private JFreeChart createChart(final IntervalCategoryDataset dataset) {
        final JFreeChart chart = ChartFactory.createGanttChart("TimeLine Chart Demo", // chart title
                "Task", // domain axis label
                "Date", // range axis label
                dataset, // data
                true, // include legend
                true, // tooltips
                false // urls
        );
        //        chart.getCategoryPlot().getDomainAxis().setMaxCategoryLabelWidthRatio(10.0f);
        return chart;
    }

    /**
     * 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() {

        jDateChooser1 = new com.toedter.calendar.JDateChooser();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        jLabel1.setText("Choose a TimeLine Date");

        jButton1.setText("enter");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(112, Short.MAX_VALUE).addComponent(jLabel1).addGap(18, 18, 18)
                        .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1).addGap(108, 108, 108)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGap(31, 31, 31)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jButton1)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel1).addComponent(jDateChooser1,
                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(313, Short.MAX_VALUE)));
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        String st_date = jDateChooser1.getDateFormatString();
        SimpleDateFormat sdf1 = new SimpleDateFormat("MM-dd-yyyy");
        System.out.println(st_date);
        //        java.util.Date date = sdf1.parse(st_date);
        //        java.sql.Date sqlStartDate = new java.sql.Date(date.getTime());
        //        TimeLinePanel.dataset = createDataset();
        //        TimeLinePanel.chart = createChart(dataset);
        //        hm = new HashMap<>();
        //        tm = new TaskModel();
        //        hm = tm.timeLineChart(101,);
        //        
        //        task = new String[hm.size()];
        //        time = new Date[hm.size()];
        //
        //        // add the chart to a panel...
        //        final ChartPanel chartPanel = new ChartPanel(chart);
        //        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        //        this.add(chartPanel);
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private com.toedter.calendar.JDateChooser jDateChooser1;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
}