Search code examples
javajfreechart

Why isn't the correct data appears in the gant chart?


Hi Everybody how are you ?

I made ​​a java application to manage tasks, and I intend to create gant charts of subtasks.

I did the following, but the data do not appear correctly.

As you can see from the picture I posted, although they appear 3 bars appear only data from a single subtask. Nothing appears on the left. I do not know why this happens.

Does anyone can help me solve this little problem please?

Result of query:

SubTask    StartDate      EndDate
T3.1      2014-04-29   2014-06-05
T3.2      2014-06-05   2014-06-05
T3.3      2014-06-09   2014-06-09

Code

public class GantDemo {

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

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

   public static void main(String[] args) {

    Connection con = null;
    TaskSeriesCollection collection = new TaskSeriesCollection();

    /* MySQL */
    String databaseURL = "jdbc:mysql://localhost:3306/tasks1.5";
    String driverName = "com.mysql.jdbc.Driver";
    String user = "****";
    String password = "*****";
    try {
        Class.forName(driverName).newInstance();
    } catch (Exception ex) {
        System.out.println("");
    }

    try {
        con = DriverManager.getConnection(databaseURL, user, password);

        if (!con.isClosed()) {
            System.out.println("Successfully connected to the DataBase Server...");
        }

        Statement statement;
        statement = con.createStatement();
        String selectQuery = "SELECT idSubTask, startDate, endDate FROM tasks where idTask like 'T3' ";
        ResultSet resultSet = null;
        resultSet = statement.executeQuery(selectQuery);
        if (resultSet != null) // Success
        {
            while (resultSet.next()) {


                String idSubTask = (String) resultSet.getObject("idSubTask ");
                String startDate= (String) resultSet.getObject("startDate");
                String endDate = (String) resultSet.getObject("endDate ");

                SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
                Date dateS = sdf2.parse(dataInicio);
                SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd");
                Date dateF = sdf3.parse(dataFim);

                //DELETE
                System.out.println("" + idSubTask + " " + startDate+ "   " + endDate );

                TaskSeries schedule1 = new TaskSeries("Scheduled Tasks");
                schedule1.add(new Task(idSubTask,
                        new SimpleTimePeriod(dateS, dateE)));

                collection.add(schedule1);

            }
        }
        resultSet.close();

    } catch (Exception e) {
        System.out.println("" + e);
    }
    IntervalCategoryDataset dataset = collection;
    JFreeChart chart = ChartFactory.createGanttChart(
            "Gantt Chart Example", // chart Heading
            "SubTask", // X-axis label
            "Date", // Y-axis label
            dataset, // dataset
            true, // legend
            true, // tooltips
            false // urls
            );
    chart.setBackgroundPaint(new Color(0xff, 0xff, 0xcc));
    ChartFrame frame = new ChartFrame("Gantt Chart", chart);
    frame.setVisible(true);
    frame.setSize(400, 350);
    }
    }

enter image description here


Solution

  • You're overriding your TaskSeries every iteration. Add TaskSeries object to collection only once.

    Example:

    public class GantDemo {
        public static void main(String[] args) throws ParseException {
    
            TaskSeriesCollection collection = new TaskSeriesCollection();
            List<SubTask> list = new ArrayList<SubTask>();
            list.add(new SubTask("T3.1", "2014-04-29", "2014-06-05"));
            list.add(new SubTask("T3.2", "2014-06-05", "2014-06-15"));
            list.add(new SubTask("T3.3", "2014-06-09", "2014-06-19"));
            TaskSeries schedule1 = new TaskSeries("Scheduled Tasks");
            for (SubTask task : list) {
                Date dateS = new SimpleDateFormat("yyyy-MM-dd")
                        .parse(task.startDate);
                Date dateE = new SimpleDateFormat("yyyy-MM-dd").parse(task.endDate);
    
                schedule1.add(new Task(task.taskId, new SimpleTimePeriod(dateS,
                        dateE)));
    
            }
            collection.add(schedule1);
            System.out.println(collection.toString());
    
            IntervalCategoryDataset dataset = collection;
            JFreeChart chart = ChartFactory.createGanttChart("Gantt Chart Example", 
                    "SubTask", // X-axis label
                    "Date", // Y-axis label
                    dataset, // dataset
                    true, // legend
                    true, // tooltips
                    false // urls
                    );
            chart.setBackgroundPaint(new Color(0xff, 0xff, 0xcc));
            ChartFrame frame = new ChartFrame("Gantt Chart", chart);
            frame.setVisible(true);
            frame.setSize(400, 350);
        }
    
        private static class SubTask {
            private String taskId;
            private String startDate;
            private String endDate;
    
            public SubTask(String taskId, String startDate, String endDate) {
                super();
                this.taskId = taskId;
                this.startDate = startDate;
                this.endDate = endDate;
            }
        }
    }
    

    Another thing is that your dates are covered so charts won't plot( subtask 2 and 3)