Gantt ItemLabel generation
Gantt ItemLabel generation
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.
So I tried to do the following experiment. I extended Task with TableTask as seen bellow:
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:
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.

Anyone have any input?
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;
}
}
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());
}
}

Anyone have any input?
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:
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.
Code: Select all
Paint seriesPaint = getItemPaint(row, column);
g2.setPaint(seriesPaint);
g2.fill(bar);
...
Hope this helps.