1

I'm trying to display my logs in a Gantt Chart. Each log is represented as a Task and each Task has 7 subtasks, which represents the partiall timings. The subtasks are to be shown in the chart if their value is not 0 (otherwise they won't be shown...). The color of each subtask depends on their description.

I've found something close to what I want (http://www.jfree.org/phpBB2/viewtopic.php?f=3&t=25206&sid=e506218a236895b1018d40d586b9fb7c) but in this example it's only being used to change the colors of the task depending on their description, soo not the subtask. I've also read somewhere that to change the subtask color it might be necessary to modify the drawTasks() method...but I really have no clue what to modify in this method... I'd be really thankful if someone could help me here...

This is how my code looks like at the moment:

public class Chart extends ApplicationFrame {

private static final long serialVersionUID = -8599149330893077525L;

private List<ChartEntry> logs = new ArrayList<ChartEntry>();
private ChartEntry chartEntry;
static IntervalCategoryDataset dataset;
final JFreeChart chart;

public Chart(final String title) {

    super(title);

    // create new dataset and chart
    dataset = createDataset();
    chart = createChart(dataset);

    // create plot and renderer
    // 04.02.2014 hier weitermachen, wenn MyGanttRenderer ist fertig

    // save as PNG-File
    Date today = new Date();
    SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yy:HH:mm:SS");
    File fileName = new File("/home/rinay/Dokumente/Auswertung Chart/"
            + DATE_FORMAT.format(today) + "_" + TestFirefox.testName
            + "_Chart.png");

    try {
        ChartUtilities.saveChartAsPNG(fileName, chart, 600, 900, null);
    } catch (IOException e) {
        throw new RuntimeException("Error saving a file", e);
    }

    // final ChartPanel chartPanel = new ChartPanel(chart);
    // chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    // setContentPane(chartPanel);
}

private static class MyGanttRenderer extends GanttRenderer {

    @Override
    public Paint getItemPaint(int row, int col) {
        Paint result;
        // String subDescription = null;

        XYTaskDataset tds = (XYTaskDataset) dataset;
        TaskSeriesCollection tsc = tds.getTasks();
        TaskSeries ts = tsc.getSeries(row);
        Task t = ts.get(col);
        // int count = t.getSubtaskCount();
        // if (count == 0)
        // return result;
        // for(int i= 0; i< count;i++){
        // subDescription = t.getSubtask(i).getDescription();
        // }
        result = getCategoryPaint(t.getDescription());
        return result;
    }

    private Paint getCategoryPaint(String description) {
        Paint result = Color.black;

        switch (description) {
        case "blocked":
            result = Color.red;
            break;
        case "dns":
            result = Color.orange;
            break;
        case "connect":
            result = Color.yellow;
            break;
        case "ssl":
            result = Color.green;
            break;
        case "send":
            result = Color.cyan;
            break;
        case "wait":
            result = Color.blue;
            break;
        case "receive":
            result = Color.magenta;
            break;
        }
        return result;
    }
}
 ....
 ....
private IntervalCategoryDataset createDataset() {

    // copy relevant information of each entry to a ChartEntry-Object
    saveAsChartEntry(HarCreator.har);

    // sort logs-List by date
    sortLogsByDate();

    // add log-List objects to JFreeChart-Dataset
    final TaskSeries taskSeries = new TaskSeries("Zeiten");
    // List<Task> tasks = new ArrayList<Task>();

    // Test1: Logs-Anzahl ausgeben
    System.out.println("Test 1: Anzahl Logs = " + logs.size());
    // Test1

    for (ChartEntry log : logs) {
        // description of task
        String description = log.getMethod() + " " + log.getURL();

        // Start date
        Date start = log.getDate();

        // add total time of request to start date
        Calendar cal = Calendar.getInstance();
        Calendar timeCounter = Calendar.getInstance();
        cal.setTime(start);
        timeCounter.setTime(start);
        cal.add(Calendar.MILLISECOND, (int) log.getTime());
        Date end = cal.getTime();

        // create new task
        Task task = new Task(description, start, end);

        for (int i = 0; i < log.getTimings().length; i++) {
            String subDescription;

            switch (i) {
            case 0:
                subDescription = "blocked";
                break;
            case 1:
                subDescription = "dns";
                break;
            case 2:
                subDescription = "connect";
                break;
            case 3:
                subDescription = "ssl";
                break;
            case 4:
                subDescription = "send";
                break;
            case 5:
                subDescription = "wait";
                break;
            case 6:
                subDescription = "receive";
                break;
            default:
                subDescription = "";
                break;
            }

            if (log.getTimingElement(i) != 0) {
                Date subStart = timeCounter.getTime();

                timeCounter.add(Calendar.MILLISECOND,
                        (int) log.getTimingElement(i));
                Date subEnd = timeCounter.getTime();

                Task subTask = new Task(subDescription, subStart, subEnd);

                task.addSubtask(subTask);

            } else
                continue;
        }
        // add the new task above to tasks-List
        taskSeries.add(task);
    }

    // Test 3: anzahl von tasks
    System.out.println("Test 3: Anzahl von Tasks= " + taskSeries.getItemCount());
    // Test 3

    TaskSeriesCollection collection = new TaskSeriesCollection();
    collection.add(taskSeries);

    return collection;
}
....
....

The method getCategoryPaint is suppose to determine the color of the subtask based on the given description...but I don't know where to implement this method...

user3006009
  • 103
  • 1
  • 2
  • 9
  • 1
    Possible duplicate of [Code for changing the color of subtasks in Gantt Chart](http://stackoverflow.com/questions/8938690/code-for-changing-the-color-of-subtasks-in-gantt-chart) – trashgod Feb 04 '14 at 19:48
  • trashgod, thanks for the quick reply...I did see that post but since I can't even understand what the getItemPaint and initClut-Methods are supposed to do, I can't adjust them to my needs. It would be nice if you could explain me what these to methods are actually doing in terms of setting different colors for subtasks... – user3006009 Feb 05 '14 at 07:52

1 Answers1

1

It would be nice if you could explain to me what these to methods are actually doing in terms of setting different colors for subtasks.

In this example, initClut() creates a List<Color> containing varying salutations of the superclass's getItemPaint() result for a given row and column. The custom getItemPaint() implementation returns the next element in the list for each sub-task in a given row and column. In your example, the renderer would use the same list of seven colors, and only the index would change.

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;
        // initialize clut
    }

    @Override
    public Paint getItemPaint(int row, int col) {
        if (this.row != row || this.col != col) {
            this.row = row;
            this.col = col;
            index = 0;
        }
        int clutIndex = index++ / PASS;
        return clut.get(clutIndex);
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • See also the complete example posted [here](http://www.jfree.org/forum/viewtopic.php?f=3&t=116822#p177465). – trashgod Feb 18 '14 at 12:56