java - 为什么甘特图中没有显示正确的数据?

标签 java jfreechart

大家好,你们好吗?

我做了一个java应用来管理任务,打算做子任务的甘特图。

我做了以下操作,但数据显示不正确。

正如您从我发布的图片中看到的那样,虽然它们出现了 3 个条形,但它们仅显示来自单个子任务的数据。左边什么也没有出现。我不知道为什么会这样。

有谁能帮我解决这个小问题吗?

查询结果:

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

代码

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

最佳答案

您将在每次迭代中覆盖您的 TaskSeries。仅将 TaskSeries 对象添加到集合中一次。

例子:

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;
        }
    }
}

另一件事是你的日期被覆盖所以图表不会绘制(子任务 2 和 3)

关于java - 为什么甘特图中没有显示正确的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24323150/

相关文章:

java - logger java 不通过 System.out

java - org.apache.struts.action.ActionMessage 无法转换为 org.apache.struts.action.ActionError

java数据报套接字未收到(本地主机)

java - Jfreechart - 我们可以在 StackedAreaChart 中为数据点设置形状吗?

java - 将多个透明 JComponents 对象添加到一个 JPanel

java - 为什么在 java 中 'joined' 线程启动之前 join() 方法之前的行不打印?

java - 为什么我的 JToggleButton 不接受我的 setDisabledIcon()?

java - 如何在jfreechart xy图中获取鼠标的y或x轴坐标

java - 一个窗口上的多个 JFreeCharts

java - 在 swing 中单击按钮时显示 JfreeChart