gantt, subtask and item label

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
syella
Posts: 4
Joined: Fri May 30, 2008 9:25 am

gantt, subtask and item label

Post by syella » Fri May 30, 2008 9:31 am

Hi all,
I have a problem with item label in gantt chart. I have the code above :

Code: Select all

final JFreeChart chart = ChartFactory.createGanttChart(...);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
GanttRenderer gr = (GanttRenderer) plot.getRenderer();
gr.setItemLabelGenerator(new IntervalCategoryItemLabelGenerator("{0}-{1}-{2}-{3}", new SimpleDateFormat(
                        "HH:mm:ss")));
gr.setItemLabelsVisible(true);
Items labels are displayed when i have some task, but not when my task has some subtask. Why? How can i do to display label on subtasks?
Thanks

arydin
Posts: 1
Joined: Mon Jun 09, 2008 3:07 pm

Post by arydin » Mon Jun 09, 2008 3:10 pm

Did you find out the solution on this? I have the exact same problem and don't know how to solve it.

I have a interval period with start and end date that I create a Task for and to this Task I add intervals (subtasks) that I want to display tooltip for.

Does anyone know how to do this? The tooltip is only showing for the main task and the TastSeries description - and this is not of interest. Thanks for any help.

fabian
Posts: 1
Joined: Tue Jun 10, 2008 10:43 am

Solution

Post by fabian » Tue Jun 10, 2008 10:53 am

After a really long and frustrating time I found a solution for displaying labels in subtasks and I don't want you to whithold this solution...

The problem is that in the exisiting drawTasks function in GanttRenderer the code for displaying labels is not there (know the hell why). So we have to extend the GanttRenderer class with our new drawTasks function:

Code: Select all

	public class MyGanttRenderer extends GanttRenderer {
		private transient Paint completePaint;
		private transient Paint incompletePaint;
		private double startPercent;
		private double endPercent;

		public MyGanttRenderer() {
			super();
			setIncludeBaseInRange(false);
			this.completePaint = Color.green;
			this.incompletePaint = Color.red;
			this.startPercent = 0.35;
			this.endPercent = 0.65;
		}

		protected void drawTasks(Graphics2D g2,
				CategoryItemRendererState state,
				Rectangle2D dataArea,
				CategoryPlot plot,
				CategoryAxis domainAxis,
				ValueAxis rangeAxis,
				GanttCategoryDataset dataset,
				int row,
				int column) {

			int count = dataset.getSubIntervalCount(row, column);
			if (count == 0) {
				drawTask(g2, state, dataArea, plot, domainAxis, rangeAxis, 
						dataset, row, column);
			}

			for (int subinterval = 0; subinterval < count; subinterval++) {

				RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge();

				// value 0
				Number value0 = dataset.getStartValue(row, column, subinterval);
				if (value0 == null) {
					return;
				}
				double translatedValue0 = rangeAxis.valueToJava2D(
						value0.doubleValue(), dataArea, rangeAxisLocation);

				// value 1
				Number value1 = dataset.getEndValue(row, column, subinterval);
				if (value1 == null) {
					return;
				}
				double translatedValue1 = rangeAxis.valueToJava2D(
						value1.doubleValue(), dataArea, rangeAxisLocation);

				if (translatedValue1 < translatedValue0) {
					double temp = translatedValue1;
					translatedValue1 = translatedValue0;
					translatedValue0 = temp;
				}

				double rectStart = calculateBarW0(plot, plot.getOrientation(), 
						dataArea, domainAxis, state, row, column);
				double rectLength = Math.abs(translatedValue1 - translatedValue0);
				double rectBreadth = state.getBarWidth();

				// DRAW THE BARS...
				Rectangle2D bar = null;

				if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
					bar = new Rectangle2D.Double(translatedValue0, rectStart, 
							rectLength, rectBreadth);
				}
				else if (plot.getOrientation() == PlotOrientation.VERTICAL) {
					bar = new Rectangle2D.Double(rectStart, translatedValue0, 
							rectBreadth, rectLength);
				}

				Rectangle2D completeBar = null;
				Rectangle2D incompleteBar = null;
				Number percent = dataset.getPercentComplete(row, column, 
						subinterval);
				double start = getStartPercent();
				double end = getEndPercent();
				if (percent != null) {
					double p = percent.doubleValue();
					if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
						completeBar = new Rectangle2D.Double(translatedValue0, 
								rectStart + start * rectBreadth, rectLength * p, 
								rectBreadth * (end - start));
						incompleteBar = new Rectangle2D.Double(translatedValue0 
								+ rectLength * p, rectStart + start * rectBreadth, 
								rectLength * (1 - p), rectBreadth * (end - start));
					}
					else if (plot.getOrientation() == PlotOrientation.VERTICAL) {
						completeBar = new Rectangle2D.Double(rectStart + start 
								* rectBreadth, translatedValue0 + rectLength 
								* (1 - p), rectBreadth * (end - start), 
								rectLength * p);
						incompleteBar = new Rectangle2D.Double(rectStart + start 
								* rectBreadth, translatedValue0, rectBreadth 
								* (end - start), rectLength * (1 - p));
					}

				}

				Paint seriesPaint = getItemPaint(row, column);
				g2.setPaint(seriesPaint);
				g2.fill(bar);

				if (completeBar != null) {
					g2.setPaint(getCompletePaint());
					g2.fill(completeBar);
				}
				if (incompleteBar != null) {
					g2.setPaint(getIncompletePaint());
					g2.fill(incompleteBar);
				}
				if (isDrawBarOutline() 
						&& state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD) {
					g2.setStroke(getItemStroke(row, column));
					g2.setPaint(getItemOutlinePaint(row, column));
					g2.draw(bar);
				}

				CategoryItemLabelGenerator generator = getItemLabelGenerator(row, column);
				if (generator != null && isItemLabelVisible(row, column)) {
					drawItemLabel(g2, dataset, row, column, plot, generator, bar, false);
				}

				// collect entity and tool tip information...
				if (state.getInfo() != null) {
					EntityCollection entities = state.getEntityCollection();
					if (entities != null) {
						String tip = null;
						if (getToolTipGenerator(row, column) != null) {
							tip = getToolTipGenerator(row, column).generateToolTip(
									dataset, row, column);
						}
						String url = null;
						if (getItemURLGenerator(row, column) != null) {
							url = getItemURLGenerator(row, column).generateURL(
									dataset, row, column);
						}
						CategoryItemEntity entity = new CategoryItemEntity(
								bar, tip, url, dataset, dataset.getRowKey(row), 
								dataset.getColumnKey(column));
						entities.add(entity);
					}
				}
			}
		}
	}
the added code is:

Code: Select all

				CategoryItemLabelGenerator generator = getItemLabelGenerator(row, column);
				if (generator != null && isItemLabelVisible(row, column)) {
					drawItemLabel(g2, dataset, row, column, plot, generator, bar, false);
				}
to display labels just do the following:

Code: Select all

...
			CategoryPlot plot = (CategoryPlot) chart.getPlot();
...
			MyGanttRenderer renderer = new MyGanttRenderer();
			plot.setRenderer(renderer);
...
			renderer.setBaseItemLabelGenerator(new CategoryItemLabelGenerator() {

		        public String generateLabel(CategoryDataset dataSet, int series, int categories) {
					/* your code to get the label */
					return label;
		        }

		        public String generateColumnLabel(CategoryDataset dataset, int categories) {
		            return dataset.getColumnKey(categories).toString();
		        }

		        public String generateRowLabel(CategoryDataset dataset, int series) {
		            return dataset.getRowKey(series).toString();
		        }
			});

			renderer.setBaseItemLabelsVisible(true);
			renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE9, TextAnchor.CENTER_LEFT));
"That's" it. I hope this is helping you to solve your problems with it...

Best,
Fabian

syella
Posts: 4
Joined: Fri May 30, 2008 9:25 am

thanks

Post by syella » Tue Jul 15, 2008 1:53 pm

great! it works perfectly!!!
thanks a lot!!!

jblancx
Posts: 24
Joined: Tue Sep 02, 2008 11:27 am
Location: Germany

Re: thanks

Post by jblancx » Mon Sep 22, 2008 8:05 am

syella wrote:great! it works perfectly!!!
thanks a lot!!!
Hallo syella, pls can you show me how you created subtasks from tasks and the code for inserting the labels. If possible can you post a sample code of a gantt.java .

I want to create subtasks for my tasks and show dependency lines to connect them.

Thanks very much..

yungsharon
Posts: 1
Joined: Tue Dec 17, 2019 7:50 am
antibot: No, of course not.

Re: gantt, subtask and item label

Post by yungsharon » Tue Dec 17, 2019 8:44 am

Hi all,

I have a problem with item label in gantt chart. It shows same label (1/11/2017-1/23/2017) for same category "Geoffrey" which has 2 tasks.
Please help, thanks.

Dataset is follows:

Name#######Leave Start###Leave End####Leave Type
Geoffrey####01/11/2017###01/13/2017###Sick Leave
Geoffrey####01/21/2017###01/23/2017###Sick Leave
Peter#######01/01/2017###01/05/2017###Vacation Leave

_________ 1-Jan-----------11-Jan-----------21-Jan-----------31-Jan
________ |---------------------------------------------------------------------------------
________ | 01/11/2017-01/23/2017______ 01/11/2017-01/23/2017
Geoffrey | ______________XXXXXX_______XXXXXX
________ |
________ |01/01/2017-01/05/2017
Peter___ |XXXXXXXXX
________ |
________ -------------------------------------------------------------------------------------
Here is my related codes:
CategoryPlot plot = (CategoryPlot) chart.getPlot();
MyGanttRenderer renderer = new MyGanttRenderer();
plot.setRenderer(renderer);
renderer.setBaseItemLabelGenerator(new IntervalCategoryItemLabelGenerator("{3}-{4}", new SimpleDateFormat("dd/MM/yyyy"))
renderer.setBaseItemLabelsVisible(true);
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE1, TextAnchor.BOTTOM_CENTER));

Locked