2012-01-20 1 views
1

Gantt chart의 하위 작업 색을 변경해야합니다. 내 예제는 다음 데이터 집합과 렌더러가있는 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); 
     } 
    } 
} 
+0

+1 for sscce; 저작권 소스에 대한 링크를 추가했습니다. – trashgod

답변

4

으로 사용자 정의 렌더러가 getItemPaint()에 의해 반환되는 결과를 상태로 모델을 조회 할 수 있습니다, here 제안했다. 이 예제에서 서브 타스크는 주어진 계열에 대한 기본 색상의 다양한 채도 팔레트를 사용하여 렌더링됩니다. 이 접근법에서는 렌더러가 두 번 통과한다고 가정합니다. 종속성을 문서화하는 데 몇 가지주의를 기울여야합니다.

GanttSubtaskDemo

/** @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])); 
     } 
    } 
} 
+0

NB :이 기하학적 회귀 분석에서 0으로 나누기가 자동으로 최대 채도로 고정됩니다. 채도 및/또는 밝기를 변화시키는 다른 방식이 가능하다. – trashgod

+0

관련 [예제] (http://stackoverflow.com/a/9875534/230513)도 참조하십시오. – trashgod

+0

이해 하겠지만이 문제의 전체 소스 코드를 얻을 수 있습니까? 그래서, 나는 더 잘 이해할 수있다. 내가 여기서 본 것을 정확히 필요로한다. –

0

아니면 작업을 확장하고 렌더러에 의해 액세스 마지막 항목을 추적하기 위해 스레드 로컬 변수를 사용할 수 있습니다

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의 변경.

관련 문제