1

I need to visualize overlapping of subtasks. In my case, a task is one person1, and subtasks of each task have descriptions "SignIn", "SignOut", "Click" (short-duration tasks), "Call" (long-duration task). What I want is to be able to: 1. See the overlap of 2 calls and see overlap of shortTasks and longTasks. Something similar to this enter image description here

My thoughts:

  • Lower the transparency of subtasks, then the overlapping areas will be rendered as darker. (In real life, if there is a green object with alpha 20 overlap or laying on top of a red object with alpha 100, we should see a render of green + red in the overlapped area?)
  • Render the shortTasks on a overlay above longTasks, so that the short tasks will displayed above the long task. <-- Still haven't figured out how to do this in code

I've followed this Changing saturation of subtasks, changing transparency in getItemPaint

Color rbg = clut.get(clutIndex);
return new Color(rbg.getRed(), rbg.getGreen(), rbg.getBlue(), 150);

but it's not exactly what I'm looking for, since the overlapped areas are not demonstrated.

private @NotNull IntervalCategoryDataset createDataset() {

    final TaskSeries s1 = new TaskSeries("Scheduled");
    Task person1 = new Task("Person1",
            new SimpleTimePeriod(date(1, Calendar.APRIL, 2001),
                    date(30, Calendar.APRIL, 2001)));

    // Subtasks
    Task subTask1 = new Task("SignIn",
            new SimpleTimePeriod(date(1, Calendar.APRIL, 2001),
                    date(2, Calendar.APRIL, 2001))); // no overlap
    Task subTask2 = new Task("Call1",
            new SimpleTimePeriod(date(5, Calendar.APRIL, 2001),
                    date(20, Calendar.APRIL, 2001))); // day 5 - 20
    Task subTask3 = new Task("Click",
            new SimpleTimePeriod(date(16, Calendar.APRIL, 2001),
                    date(17, Calendar.APRIL, 2001))); // overlap with call1 and call2
    Task subTask4 = new Task("Call2",
            new SimpleTimePeriod(date(15, Calendar.APRIL, 2001),
                    date(30, Calendar.APRIL, 2001))); // day 15 - 30

    person1.addSubtask(subTask1);
    person1.addSubtask(subTask2);
    person1.addSubtask(subTask3);
    person1.addSubtask(subTask4);
    s1.add(person1);
    final TaskSeriesCollection collection = new TaskSeriesCollection();
    collection.add(s1);

    return collection;
}

Overlap demo

How to code using my approach or is there any better approaches to achieve this? Thank you!

Elly
  • 145
  • 7
  • @trashgod thanks for the info! Can `AlphaComposite` be used within a class extending JFrame? I'm having difficulties as to where I should call `(Graphics2D)demo.getGraphics().setComposite(AlphaComposite.Xor);` Currently call it in `static void main` after declaring `final GanttChart demo = new GanttChart("Demo");` but got the same output in the post – Elly Oct 26 '22 at 16:18
  • I also tried calling `this` in `public GanttChart` constructor `(Graphics2D)this.getGraphics().setComposite(AlphaComposite.Xor)` but got NullPointerException – Elly Oct 26 '22 at 16:29
  • The graphics context is invalid at that point; I've elaborated below. – trashgod Oct 26 '22 at 17:43

1 Answers1

1

While GanttRenderer doesn't support AlphaComposite directly, you can adjust the hue, saturation and brightness; saturation is shown here, but ⍺ < 1 may be worth exploring. You can set the mode in your implementation of drawItem(); Xor is pictured. Use the AlphaCompositeDemo cited here to find the desired mode and colors.

private static class MyRenderer extends GanttRenderer {
…
    @Override
    public void drawItem(Graphics2D g2, CategoryItemRendererState state, Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis, ValueAxis rangeAxis, CategoryDataset dataset, int row, int column, int pass) {
        g2.setComposite(AlphaComposite.Xor);
        super.drawItem(g2, state, dataArea, plot, domainAxis, rangeAxis, dataset, row, column, pass);
    }
}

image

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thank you, it works for me. There is a weird grey shadow after a subtask but I guess it's a Gantt Task thing? Now the rest is for me to implement color by description and achieve what I need :D Also, can you elaborate on what `Pass` means in [this](https://stackoverflow.com/a/8949913/20161220) Is it okay if I make no changes to `Pass`? – Elly Oct 26 '22 at 21:41
  • If I understand correctly, `drawItem()` is called in multiple passes by the enclosing plot; print the `pass` parameter to see. – trashgod Oct 26 '22 at 22:16