Code for changing the color of subtasks in Gantt Chart (JFre

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Klausos
Posts: 1
Joined: Fri Jan 20, 2012 10:22 am
antibot: No, of course not.

Code for changing the color of subtasks in Gantt Chart (JFre

Post by Klausos » Fri Jan 20, 2012 10:26 am

I need to change color of subtasks in Gantt chart. Below you can find my SSCCE. 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 color of tasks, but I don´t know how to extract subtasks.

Code: Select all

import java.awt.Color;
import java.awt.Paint;
import java.util.Calendar;
import java.util.Date;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.GanttRenderer;
import org.jfree.data.category.IntervalCategoryDataset;
import org.jfree.data.gantt.Task;
import org.jfree.data.gantt.TaskSeries;
import org.jfree.data.gantt.TaskSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class testtt extends ApplicationFrame {

    public static void main(final String[] args) {

        final testtt demo = new testtt("Gantt Chart Demo 2");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }
    public testtt(final String title) {

        super(title);

        final IntervalCategoryDataset dataset = createSampleDataset();

        final JFreeChart chart = ChartFactory.createGanttChart(
            "Gantt Chart Demo",  // chart title
            "Task",              // domain axis label
            "Date",              // range axis label
            dataset,             // data
            true,                // include legend
            true,                // tooltips
            false                // urls
        );
        final CategoryPlot plot = (CategoryPlot) chart.getPlot();
        MyRenderer renderer = new MyRenderer();
        plot.setRenderer(renderer);
        
        for (int row=0; row<dataset.getRowCount(); row++) 
        	for (int col=0; col<dataset.getColumnCount(); col++)
        			renderer.getItemPaint(row,col);
        
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);

    }
    
    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 Task t3 = new Task(
                "TTT", date(1, Calendar.JULY, 2001), date(1, Calendar.AUGUST, 2001)
            ); 
        t3.addSubtask(new Task("Proposal 1", date(1, Calendar.JULY, 2001), date(15, Calendar.JULY, 2001)));
        t3.addSubtask(new Task("Proposal 2", date(16, Calendar.JULY, 2001), date(25, Calendar.JULY, 2001)));
        t3.addSubtask( new Task("Proposal 3", date(26, Calendar.JULY, 2001), date(1, Calendar.AUGUST, 2001)));
        s1.add(t3);
        
        final TaskSeriesCollection collection = new TaskSeriesCollection();
        collection.add(s1);

        return collection;
    }

    /**
     * Utility method for creating <code>Date</code> objects.
     *
     * @param day  the date.
     * @param month  the month.
     * @param year  the year.
     *
     * @return a date.
     */
    private static Date date(final int day, final int month, final int year) {

        final Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, day);
        final Date result = calendar.getTime();
        return result;

    }

}

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 (col == 0) {
            return subtask1Color;
        } else if (col == 1) {
        	return subtask2Color;
        } else if (col == 2) {
        	return subtask3Color;
        } else
            return super.getItemPaint(row, col);
        }
  }

Locked