用于更改甘特图中子任务的颜色的代码 [英] Code for changing the color of subtasks in Gantt Chart

查看:279
本文介绍了用于更改甘特图中子任务的颜色的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  private IntervalCategoryDataset createSampleDataset ){

final TaskSeries s1 = new TaskSeries(Scheduled);

final任务t1 = new任务(
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任务t2 =新任务(
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 Subask1Color = 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);
}
}
}


解决方案>

此处所示,自定义渲染器可以查询模型以对由 getItemPaint() 。在本示例中,使用给定系列的默认颜色的变化饱和度的调色板来渲染子任务。该方法假定渲染器进行两次通过;应该注意记录依赖性。



  / ** @see http://stackoverflow.com/questions/8938690 * / 
private static class MyRenderer extends GanttRenderer {

private static final int PASS = 2; //假设两遍
private final List< Color> clut = new ArrayList< Color>();
private final TaskSeriesCollection模型;
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! | 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();
颜色c =(颜色)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 clut.add(Color.getHSBColor(a [0],a [1] / i,a [2]))
}
}
}


I need to change the color of subtasks in a Gantt chart. My example is based on GanttDemo2 with the following dataset and renderer. In different forums I found some discussions related to this topic, but I did not find a clear simple working example. In particular, I can change the color of tasks, but I don't know how to extract subtasks.

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

解决方案

As suggested here, a custom renderer can query the model to condition the result returned by getItemPaint(). In this example, subtasks are rendered using a palette of varying saturations of the default color for a given series. The approach assumes that the renderer makes two passes; some care should be given to documenting the dependency.

/** @see http://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]));
        }
    }
}

这篇关于用于更改甘特图中子任务的颜色的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆