Gantt ItemLabel generation

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Demati
Posts: 12
Joined: Tue Jan 30, 2007 4:04 pm

Gantt ItemLabel generation

Post by Demati » Fri Mar 23, 2007 3:44 pm

Has anyone tried extending the available classes in JFreeChart to allow for the labeling of an individual task versus labeling the entire taskSeries? For example I want to place a different label in the center of each bar (which may or may not be in the same series) on a Gantt chart. This label would be a custom string I would construct for each Task. Is this even possible? Anyone's help is greatly appreciated.

Demati
Posts: 12
Joined: Tue Jan 30, 2007 4:04 pm

Post by Demati » Tue Mar 27, 2007 2:39 pm

Am I the only one who has ever had the need to create a label for individual tasks rather than an entire series? :shock: To the top. Hopefully someone will have some useful information :?

Demati
Posts: 12
Joined: Tue Jan 30, 2007 4:04 pm

Post by Demati » Tue Mar 27, 2007 6:43 pm

So I tried to do the following experiment. I extended Task with TableTask as seen bellow:

Code: Select all

package com.local;

import org.jfree.data.gantt.Task;
import org.jfree.data.time.TimePeriod;

public class TableTask extends Task{
	private int numPeople_m;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7062068940865097525L;

	public TableTask(String description, TimePeriod duration) {
		super(description, duration);
	}
	
	public TableTask(String description, TimePeriod duration, int numPeople) {
		super(description, duration);
		numPeople_m = numPeople;
	}

	public int getNumber(){
		return numPeople_m;
	}
}
The numPeople_m variable is the number of people currently reserved on a table (thus my TableTask). Next I do the following in my custom LabelGenerator:

Code: Select all

package com.local;

import org.jfree.chart.labels.CategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.gantt.TaskSeriesCollection;

/**
 * A custom label generator.
 */
public class LabelGenerator extends StandardCategoryItemLabelGenerator implements CategoryItemLabelGenerator {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3827263549609695569L;
	private TaskSeriesCollection collection_m;
	
	public LabelGenerator(TaskSeriesCollection collection){
		super();
		collection_m = collection;
	}
	
	
	/**
     * Generates an item label.
     * 
     * @param dataset  the dataset.
     * @param series  the series index.
     * @param category  the category index.
     * 
     * @return the label.
     */
    public String generateLabel(CategoryDataset dataset, int series, 
                                int category) {
    	TableTask t1 = (TableTask)collection_m.getSeries(series).getTasks().get(category-1);
    	return Integer.toString(t1.getNumber());
    }
}
This seems to have some peculiar results. There are three possible series I have defined for each table (thus a depth of 3 bars). When I have a situation where there is only one bar on a line in a series the above seems to work. The moment additional bars are introduced to the series at the same table the labeling fails. I have provided an image bellow to illustrate my problem.

Image

Anyone have any input?

Demati
Posts: 12
Joined: Tue Jan 30, 2007 4:04 pm

Post by Demati » Wed Mar 28, 2007 7:51 pm

David, (or anybody) do you have any input?

Demati
Posts: 12
Joined: Tue Jan 30, 2007 4:04 pm

Post by Demati » Fri Mar 30, 2007 4:17 pm

TTT Some help would be greatly appreciated :)

Spyder
Posts: 2
Joined: Fri Mar 30, 2007 6:34 pm

Post by Spyder » Fri Mar 30, 2007 7:15 pm

In GanttRenderer.drawTasks(...), you can see where each subtask is rendered. Specifically, a Rectangle2D named 'bar' is created to indicate the bounds of each subtask. After the bar is drawn:

Code: Select all

Paint seriesPaint = getItemPaint(row, column);
g2.setPaint(seriesPaint);
g2.fill(bar);
...
use 'bar' to render a centered string in each subtask. You have access to the dataset, column and subinterval to determine the label.

Hope this helps.

Demati
Posts: 12
Joined: Tue Jan 30, 2007 4:04 pm

Post by Demati » Mon Apr 02, 2007 12:18 pm

Thank you. I will try that.

Locked