How to change color of particular sub-task in JFreeChart Gantt Chart?
Asked Answered
L

1

3

I have a Gantt Chart with 5 tasks. Each task is divided into 3 sub-tasks. I need to define different color for each sub-task, e.g. Sub-task1: "light blue", Sub-task2: "blue", Sub-task3: "dark blue". I tried to google some examples, but I didn't find any full working example. Thanks.

Update#1: I'm using IntervalCategoryDataset for the dataset.

IntervalCategoryDataset dataset = createDataset(data);

final Task t = new Task("Resource " + i, date(time11), date(time14));
t.addSubtask(new Task("Resource " + i, date(time11), date(time12)));
t.addSubtask(new Task("Resource " + i, date(time12), date(time13)));
t.addSubtask(new Task("Resource " + i, date(time13), date(time14)));
Linneman answered 18/1, 2012 at 17:56 Comment(0)
M
3

You can override the renderer's getItemPaint() method, as discussed here.

Addendum: As a Gnatt chart uses a GanttRenderer, you'd do something like this to see the existing colors. Just return your chosen color for a given row and column.

plot.setRenderer(new MyRenderer());
...
private static class MyRenderer extends GanttRenderer {

    @Override
    public Paint getItemPaint(int row, int col) {
        System.out.println(row + " " + col + " " + super.getItemPaint(row, col));
        return super.getItemPaint(row, col);
    }
}
Moia answered 18/1, 2012 at 19:56 Comment(6)
I read this topic, but I don't understand what shoul be the whole solution. I need some small and clear example.Linneman
I tried this code. But I don´t understand where the Colors of sub-tasks are defined? If I do only this, then all sub-tasks still have the same blue color.Linneman
Where do I need to use getHSBColor()? Inside the getItemPaint or where?Linneman
It looks like getItemPaint() receives the row and column, but you'll have to query the model to getSubtaskCount(). I'm seeing two passes, but you can override drawItem() to be sure.Moia
Could you please provide an example with getSubtaskCount()? I would not like to use drawItem() as it's less flexible method.Linneman
A complete example is beyond the scope of SO. You can probably assume two passes as a start. It's your data model; just invoke getSubtaskCount() on the relevant Task. An sscce that shows representative data and color selection may help.Moia

© 2022 - 2024 — McMap. All rights reserved.