JFreeChart Gantt根据描述为子任务绘制不同的颜色

时间:2014-02-04 16:33:51

标签: java jfreechart

我正在尝试在甘特图中显示我的日志。每个日志都表示为一个任务,每个任务有7个子任务,表示部分时间。如果它们的值不为0,则子图将显示在图表中(否则它们将不会显示...)。每个子任务的颜色取决于它们的描述。

我发现了一些接近我想要的东西(http://www.jfree.org/phpBB2/viewtopic.php?f=3&t=25206&sid=e506218a236895b1018d40d586b9fb7c),但在这个例子中,它仅用于根据描述改变任务的颜色,而不是子任务。我还读过某个地方,要改变子任务颜色,可能需要修改drawTasks()方法...但我真的不知道在这个方法中要修改什么......如果有人可以,我真的很感激在这帮帮我......

这就是我的代码目前的样子:

public class Chart extends ApplicationFrame {

private static final long serialVersionUID = -8599149330893077525L;

private List<ChartEntry> logs = new ArrayList<ChartEntry>();
private ChartEntry chartEntry;
static IntervalCategoryDataset dataset;
final JFreeChart chart;

public Chart(final String title) {

    super(title);

    // create new dataset and chart
    dataset = createDataset();
    chart = createChart(dataset);

    // create plot and renderer
    // 04.02.2014 hier weitermachen, wenn MyGanttRenderer ist fertig

    // save as PNG-File
    Date today = new Date();
    SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yy:HH:mm:SS");
    File fileName = new File("/home/rinay/Dokumente/Auswertung Chart/"
            + DATE_FORMAT.format(today) + "_" + TestFirefox.testName
            + "_Chart.png");

    try {
        ChartUtilities.saveChartAsPNG(fileName, chart, 600, 900, null);
    } catch (IOException e) {
        throw new RuntimeException("Error saving a file", e);
    }

    // final ChartPanel chartPanel = new ChartPanel(chart);
    // chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    // setContentPane(chartPanel);
}

private static class MyGanttRenderer extends GanttRenderer {

    @Override
    public Paint getItemPaint(int row, int col) {
        Paint result;
        // String subDescription = null;

        XYTaskDataset tds = (XYTaskDataset) dataset;
        TaskSeriesCollection tsc = tds.getTasks();
        TaskSeries ts = tsc.getSeries(row);
        Task t = ts.get(col);
        // int count = t.getSubtaskCount();
        // if (count == 0)
        // return result;
        // for(int i= 0; i< count;i++){
        // subDescription = t.getSubtask(i).getDescription();
        // }
        result = getCategoryPaint(t.getDescription());
        return result;
    }

    private Paint getCategoryPaint(String description) {
        Paint result = Color.black;

        switch (description) {
        case "blocked":
            result = Color.red;
            break;
        case "dns":
            result = Color.orange;
            break;
        case "connect":
            result = Color.yellow;
            break;
        case "ssl":
            result = Color.green;
            break;
        case "send":
            result = Color.cyan;
            break;
        case "wait":
            result = Color.blue;
            break;
        case "receive":
            result = Color.magenta;
            break;
        }
        return result;
    }
}
 ....
 ....
private IntervalCategoryDataset createDataset() {

    // copy relevant information of each entry to a ChartEntry-Object
    saveAsChartEntry(HarCreator.har);

    // sort logs-List by date
    sortLogsByDate();

    // add log-List objects to JFreeChart-Dataset
    final TaskSeries taskSeries = new TaskSeries("Zeiten");
    // List<Task> tasks = new ArrayList<Task>();

    // Test1: Logs-Anzahl ausgeben
    System.out.println("Test 1: Anzahl Logs = " + logs.size());
    // Test1

    for (ChartEntry log : logs) {
        // description of task
        String description = log.getMethod() + " " + log.getURL();

        // Start date
        Date start = log.getDate();

        // add total time of request to start date
        Calendar cal = Calendar.getInstance();
        Calendar timeCounter = Calendar.getInstance();
        cal.setTime(start);
        timeCounter.setTime(start);
        cal.add(Calendar.MILLISECOND, (int) log.getTime());
        Date end = cal.getTime();

        // create new task
        Task task = new Task(description, start, end);

        for (int i = 0; i < log.getTimings().length; i++) {
            String subDescription;

            switch (i) {
            case 0:
                subDescription = "blocked";
                break;
            case 1:
                subDescription = "dns";
                break;
            case 2:
                subDescription = "connect";
                break;
            case 3:
                subDescription = "ssl";
                break;
            case 4:
                subDescription = "send";
                break;
            case 5:
                subDescription = "wait";
                break;
            case 6:
                subDescription = "receive";
                break;
            default:
                subDescription = "";
                break;
            }

            if (log.getTimingElement(i) != 0) {
                Date subStart = timeCounter.getTime();

                timeCounter.add(Calendar.MILLISECOND,
                        (int) log.getTimingElement(i));
                Date subEnd = timeCounter.getTime();

                Task subTask = new Task(subDescription, subStart, subEnd);

                task.addSubtask(subTask);

            } else
                continue;
        }
        // add the new task above to tasks-List
        taskSeries.add(task);
    }

    // Test 3: anzahl von tasks
    System.out.println("Test 3: Anzahl von Tasks= " + taskSeries.getItemCount());
    // Test 3

    TaskSeriesCollection collection = new TaskSeriesCollection();
    collection.add(taskSeries);

    return collection;
}
....
....

方法getCategoryPaint假设根据给定的描述确定子任务的颜色......但我不知道在哪里实现这个方法......

1 个答案:

答案 0 :(得分:1)

  

如果您能够向我解释这些方法在为子任务设置不同颜色方面实际做了什么,那将是很好的。

在此example中,initClut()创建一个List<Color>,其中包含对给定行和列的超类getItemPaint()结果的不同称呼。自定义getItemPaint()实现返回给定行和列中每个子任务的列表中的下一个元素。在您的示例中,渲染器将使用相同的七种颜色列表,并且只会更改索引。

private static class MyRenderer extends GanttRenderer {

    private static final int PASS = 2; // assumes two passes
    private final List<Color> clut = new ArrayList<Color>();
    private final TaskSeriesCollection model;
    private int row;
    private int col;
    private int index;

    public MyRenderer(TaskSeriesCollection model) {
        this.model = model;
        // initialize clut
    }

    @Override
    public Paint getItemPaint(int row, int col) {
        if (this.row != row || this.col != col) {
            this.row = row;
            this.col = col;
            index = 0;
        }
        int clutIndex = index++ / PASS;
        return clut.get(clutIndex);
    }
}