trouble generating tooltips with CustomXYToolTipGenerator

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
jusepi
Posts: 16
Joined: Tue May 09, 2006 1:18 pm
Location: USA

trouble generating tooltips with CustomXYToolTipGenerator

Post by jusepi » Sun May 14, 2006 7:26 pm

I've been trying to get custom tool tips working on an XYScatter plot for a few days now, but I'm doing something wrong. :?

Basically I create a XYSeriesCollection with 2 series and 20 items in each. Then I use the following code to create a chart:

Code: Select all

		JFreeChart chart = ChartFactory.createScatterPlot(
				"Events", // chart title
				"Distance (mi)", // domain axis label
				"Time (s)", // range axis label
				dataset, // data
				PlotOrientation.VERTICAL,
				true, // include legend
				true, // tooltips
				false // url's
		);
Then I call

Code: Select all

 generateToolTips(dataset, chart.getXYPlot()); 
Here's the code for that method:

Code: Select all

	private void generateToolTips(XYSeriesCollection collection, XYPlot plot)
	{
		ArrayList ttList;
		String tt;
		XYSeries series;
		CustomXYToolTipGenerator ttGen;
		XYItemRenderer renderer;
		
		int i, imax, s, smax = collection.getSeriesCount();
		for (s=0; s<smax; s++)
		{
			ttList = new ArrayList();
			series = collection.getSeries(s);
			imax = series.getItemCount();
			for (i=0; i<imax; i++)
			{
				tt = "Event " + (s+i) + " \n Distance: " + series.getDataItem(i).getX()
                          + "\n Time: " + series.getDataItem(i).getY();
				ttList.add(tt);
			}
			ttGen = new CustomXYToolTipGenerator();
//			ttGen.generateToolTip(collection, s, i);
			ttGen.addToolTipSeries(ttList);
			renderer = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, ttGen);
			plot.setRenderer(i, renderer);
		}
	}

I get an error:
java.lang.IllegalArgumentException: Index 'index' out of bounds.
at org.jfree.chart.plot.XYPlot.getDomainAxisForDataset(XYPlot.java:2679)
...
org.jfree.chart.servlet.ServletUtilities.saveChartAsPNG(ServletUtilities.java:187)
...
when I call this method:

Code: Select all

String filename = ServletUtilities.saveChartAsPNG(chart, CHART_WIDTH, CHART_HEIGHT, new ChartRenderingInfo(new StandardEntityCollection()), request.getSession());
I tried changing

Code: Select all

plot.setRenderer(i, renderer);
to

Code: Select all

plot.setRenderer(renderer);
This change does not throw any exceptions, but the tool tips don't show up either. Any help with this is much appreciated.

jusepi
Posts: 16
Joined: Tue May 09, 2006 1:18 pm
Location: USA

trouble generating tooltips with CustomXYToolTipGenerator

Post by jusepi » Sun May 14, 2006 7:30 pm

Also is there a better way of creating custom tooltips? I'd like to be able to add them as I generate the data, so in that case, I'd only have access to the dataset, not the plot. Thanks again.

angel
Posts: 899
Joined: Thu Jan 15, 2004 12:07 am
Location: Germany - Palatinate

Post by angel » Mon May 15, 2006 8:30 am

This is getting a bit messy.

1) I never tried creating the tooltips in an ArrayList, but your method might work.

Code: Select all

         renderer = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, ttGen); 
         plot.setRenderer(i, renderer); 
2) Why do you create and set a renderer (plot.setRenderer)???ChartFactory.createScatterPlot did this already.

Code: Select all

String filename = ServletUtilities.saveChartAsPNG(chart, CHART_WIDTH, CHART_HEIGHT, new ChartRenderingInfo(new StandardEntityCollection()), request.getSession()); 
3) Is this all the code for obtaing the image-maps for the tooltip? If yes, this will never work.

jusepi
Posts: 16
Joined: Tue May 09, 2006 1:18 pm
Location: USA

Post by jusepi » Mon May 15, 2006 2:05 pm

angel wrote:This is getting a bit messy.
sorry about the mess...

angel wrote:2) Why do you create and set a renderer (plot.setRenderer)???ChartFactory.createScatterPlot did this already.
Ya figured it out. I was thinking I needed to set a generator for each renderer with one renderer per series. I was thinking in terms of a CategoryDataset where you have multiple datasets and renderers. I forgot XYCollection is one dataset with multiple series, so there should only be one renderer. Also, I didn't understand how the addToolTipSeries method worked. You have to keep adding to the tooltiplist (one for each series).

I changed the code to:

Code: Select all

	private CustomXYToolTipGenerator generateToolTips(XYSeriesCollection dataSet, XYPlot plot)
	{
		ArrayList ttList;
		String tt;
		XYSeries series;
		CustomXYToolTipGenerator ttGen = new CustomXYToolTipGenerator();

		nf.setMaximumFractionDigits(3);

		int i, imax, s, smax = dataSet.getSeriesCount();
		for (s = 0; s < smax; s++)
		{
			ttList = new ArrayList();
			series = dataSet.getSeries(s);
			imax = series.getItemCount();
			for (i = 0; i < imax; i++)
			{
				tt = "Event: " + series.getKey() + "\nDistance: " + nf.format(series.getDataItem(i).getX())
						+ "\nTime: " + series.getDataItem(i).getY().intValue() + " s";
				ttList.add(tt);
			}
			ttGen.addToolTipSeries(ttList);
		}

		return ttGen;
	}
the key was to take CustomXYToolTipGenerator ttGen = new CustomXYToolTipGenerator(); out of the for loop. And then the resulting CustomXYToolTipGenerator has to be added to the single plot renderer.

Code: Select all

String filename = ServletUtilities.saveChartAsPNG(chart, CHART_WIDTH, CHART_HEIGHT, new ChartRenderingInfo(new StandardEntityCollection()), request.getSession()); 
3) Is this all the code for obtaing the image-maps for the tooltip? If yes, this will never work.
Yes, I also call String map = ChartUtilities.getImageMap(filename, info);

I finally got it to work. Thanks for the help

Locked