Java 用于更改甘特图中子任务颜色的代码

Java 用于更改甘特图中子任务颜色的代码,java,colors,jfreechart,gantt-chart,Java,Colors,Jfreechart,Gantt Chart,我需要更改任务中子任务的颜色。我的示例基于以下数据集和渲染器。在不同的论坛上,我发现了一些与这个主题相关的讨论,但我没有找到一个清晰简单的工作示例。特别是,我可以更改任务的颜色,但我不知道如何提取子任务 private IntervalCategoryDataset createSampleDataset() { final TaskSeries s1 = new TaskSeries("Scheduled"); final Task t1 = new Task(

我需要更改任务中子任务的颜色。我的示例基于以下数据集和渲染器。在不同的论坛上,我发现了一些与这个主题相关的讨论,但我没有找到一个清晰简单的工作示例。特别是,我可以更改任务的颜色,但我不知道如何提取子任务

private IntervalCategoryDataset createSampleDataset() {

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

    final Task t1 = new Task(
        "Design", date(1, Calendar.APRIL, 2001), date(1, Calendar.MAY, 2001));
    t1.addSubtask(new Task("Design 1", date(1, Calendar.APRIL, 2001), date(15, Calendar.APRIL, 2001)));
    t1.addSubtask(new Task("Design 2", date(16, Calendar.APRIL, 2001), date(25, Calendar.APRIL, 2001)));
    t1.addSubtask(new Task("Design 3", date(26, Calendar.APRIL, 2001), date(1, Calendar.MAY, 2001)));
    s1.add(t1);

    final Task t2 = new Task(
        "Proposal", date(1, Calendar.JUNE, 2001), date(1, Calendar.JULY, 2001));
    t2.addSubtask(new Task("Proposal 1", date(1, Calendar.JUNE, 2001), date(15, Calendar.JUNE, 2001)));
    t2.addSubtask(new Task("Proposal 2", date(16, Calendar.JUNE, 2001), date(25, Calendar.JUNE, 2001)));
    t2.addSubtask(new Task("Proposal 3", date(26, Calendar.JUNE, 2001), date(1, Calendar.JULY, 2001)));
    s1.add(t2);

    final TaskSeriesCollection collection = new TaskSeriesCollection();
    collection.add(s1);
    return collection;
}

class MyRenderer extends GanttRenderer {

    private static final Color subtask1Color = Color.blue;
    private static final Color subtask2Color = Color.cyan;
    private static final Color subtask3Color = Color.green;
    private static final long serialVersionUID = 1L;

    public MyRenderer() {
        super();
    }

    @Override
    public Paint getItemPaint(int row, int col) {
        System.out.println(row + " " + col + " " + super.getItemPaint(row, col));
        if (row == 0) {
            return subtask1Color;
        } else if (row == 1) {
            return subtask2Color;
        } else if (row == 2) {
            return subtask3Color;
        } else {
            return super.getItemPaint(row, col);
        }
    }
}
正如所建议的那样,自定义渲染器可以查询模型,以调整由返回的结果。在本例中,使用给定系列的默认颜色的不同饱和度调色板渲染子任务。该方法假设渲染器进行两次传递;应该注意记录依赖关系

/** @see https://stackoverflow.com/questions/8938690 */
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;
    }

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

    private void initClut(int row, int col) {
        clut.clear();
        Color c = (Color) super.getItemPaint(row, col);
        float[] a = new float[3];
        Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), a);
        TaskSeries series = (TaskSeries) model.getRowKeys().get(row);
        List<Task> tasks = series.getTasks(); // unchecked
        int taskCount = tasks.get(col).getSubtaskCount();
        taskCount = Math.max(1, taskCount);
        for (int i = 0; i < taskCount; i++) {
            clut.add(Color.getHSBColor(a[0], a[1] / i, a[2]));
        }
    }
}
正如所建议的那样,自定义渲染器可以查询模型,以调整由返回的结果。在本例中,使用给定系列的默认颜色的不同饱和度调色板渲染子任务。该方法假设渲染器进行两次传递;应该注意记录依赖关系

/** @see https://stackoverflow.com/questions/8938690 */
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;
    }

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

    private void initClut(int row, int col) {
        clut.clear();
        Color c = (Color) super.getItemPaint(row, col);
        float[] a = new float[3];
        Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), a);
        TaskSeries series = (TaskSeries) model.getRowKeys().get(row);
        List<Task> tasks = series.getTasks(); // unchecked
        int taskCount = tasks.get(col).getSubtaskCount();
        taskCount = Math.max(1, taskCount);
        for (int i = 0; i < taskCount; i++) {
            clut.add(Color.getHSBColor(a[0], a[1] / i, a[2]));
        }
    }
}

或者可以扩展任务并使用线程局部变量跟踪渲染器访问的最后一项:

private ThreadLocal<Integer> lastSubTask = new ThreadLocal<Integer>();
...
private class MyTask extends Task {
    ...
    public Task getSubtask(int index) {
        lastSubTask.set(index);
        return super.getSubtask(index);
    }
}
...
private class MyRenderer extends GanttRenderer {
    ...
    public Paint getCompletePaint() {
        Integer index = lastSubTask.get();
        return getColorForSubTask(index);
    }
    ...
}

这可能对jfreechart中的更改更具弹性。

或者您可以扩展任务并使用线程局部变量跟踪渲染器访问的最后一个项目:

private ThreadLocal<Integer> lastSubTask = new ThreadLocal<Integer>();
...
private class MyTask extends Task {
    ...
    public Task getSubtask(int index) {
        lastSubTask.set(index);
        return super.getSubtask(index);
    }
}
...
private class MyRenderer extends GanttRenderer {
    ...
    public Paint getCompletePaint() {
        Integer index = lastSubTask.get();
        return getColorForSubTask(index);
    }
    ...
}

这可能对jfreechart中的变化更具弹性。

+1适用于sscce;添加到版权来源的链接。sscce为+1;添加了到版权所有source.NB的链接:在这个几何回归中,被零除会自动固定到最大饱和度。其他改变饱和度和/或亮度的方案是可能的。我确实理解,但我能有一个完整的源代码吗?因此,我可以更好地理解它,以便进一步处理。我需要的正是我在这里看到的东西。@ParthBhayani:为了获得更好的理解,看一个新问题;举出这一点,;使用标签;使用,包括一个显示你被卡住的地方。注意:在这个几何回归中,被零除会自动固定到最大饱和度。其他改变饱和度和/或亮度的方案是可能的。我确实理解,但我能有一个完整的源代码吗?因此,我可以更好地理解它,以便进一步处理。我需要的正是我在这里看到的东西。@ParthBhayani:为了获得更好的理解,看一个新问题;举出这一点,;使用标签;使用,包括一个显示你被卡住的地方。