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

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


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

        }

    }

}


慕虎7371278
浏览 597回答 2
2回答

四季花海

或者,您可以扩展任务并使用线程局部变量来跟踪渲染器访问的最后一项:private ThreadLocal<Integer> lastSubTask = new ThreadLocal<Integer>();...private class MyTask extends Task {&nbsp; &nbsp; ...&nbsp; &nbsp; public Task getSubtask(int index) {&nbsp; &nbsp; &nbsp; &nbsp; lastSubTask.set(index);&nbsp; &nbsp; &nbsp; &nbsp; return super.getSubtask(index);&nbsp; &nbsp; }}...private class MyRenderer extends GanttRenderer {&nbsp; &nbsp; ...&nbsp; &nbsp; public Paint getCompletePaint() {&nbsp; &nbsp; &nbsp; &nbsp; Integer index = lastSubTask.get();&nbsp; &nbsp; &nbsp; &nbsp; return getColorForSubTask(index);&nbsp; &nbsp; }&nbsp; &nbsp; ...}这可能对jfreechart的更改更具弹性。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java