Tool tip not shown in the graph

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Narayan
Posts: 30
Joined: Wed Apr 11, 2007 11:20 am
Location: Bangalore

Tool tip not shown in the graph

Post by Narayan » Thu May 17, 2007 9:44 am

Hi All,

In my graph, I cant see the tooltip when I take the mouse the line series. Can you help me out in this.
The code is below

final JFreeChart chart = ChartFactory.createTimeSeriesChart(
chartTitle, // Chart Title
domainLabel, // X-Axis Label
unit, // Y-Axis Label
dataset, // Dataset
true, // Show Legend
true, // Show Tooltip
false); // URL

chart.setBackgroundPaint(Color.white);

XYPlot plot = (XYPlot) chart.getPlot();
DateAxis dateAxis = (DateAxis)plot.getDomainAxis();

final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

XYItemRenderer renderer = plot.getRenderer();
Color[] color = new Color[]{new Color(0,0,153), new Color(255,0,204), new Color(0,102,0), new Color(255,0,0), new Color(0,0,0), new Color(102,0,0), new Color(153,0,51)};

LegendItemCollection chartLegend = new LegendItemCollection();
Shape shape = new Rectangle(10, 10);

int arraySize = fieldsArray.length;
for(int i=0; i<arraySize; i++){
renderer.setSeriesPaint(i, color);
renderer.setSeriesStroke(i, new BasicStroke(1.0f));

chartLegend.add(new LegendItem(fieldsArray, null, null, null, shape, color));
}
plot.setFixedLegendItems(chartLegend);

TickUnitSource tickUnits = createDateTickUnits(TimeZone.getDefault(),type);
dateAxis.setStandardTickUnits(tickUnits);

return chart;

Thanks in advance for any assistance.

Regards,
Narayan

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Thu May 17, 2007 2:01 pm

I can't see any reason why that won't work. Of course, if you'd posted a small self-contained demo, I could have run it and actually tried it out.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Narayan
Posts: 30
Joined: Wed Apr 11, 2007 11:20 am
Location: Bangalore

Post by Narayan » Fri May 18, 2007 11:42 am

Hi Dave, I appreciate your quick response.

Here is the sample self running code for the timrseries chart. Can you please let me know what can I do to get the tooltip for the graph.


import java.awt.Color;
import java.awt.Dimension;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;



public class SecondLineChart extends ApplicationFrame{

private String chartTitle;

public SecondLineChart(String title){
super(title);
chartTitle = title;
TimeSeriesCollection dataset = null;
dataset = createDailyDataset();

final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new Dimension(550, 250));
setContentPane(chartPanel);
}



private TimeSeriesCollection createDailyDataset(){

TimeSeriesCollection dataset = new TimeSeriesCollection();

TimeSeries [] time = new TimeSeries[2];

Regards,
Narayan

time[0] = new TimeSeries("Pers Msg", Day.class);
time[1] = new TimeSeries("Non Pers Msg", Day.class);

Day day1 = null;
Day day2 = null;
Day day3 = null;
Day day4 = null;
Day day5 = null;
Day day6 = null;
try {
day1 = new Day(new SimpleDateFormat("MMM dd, yyyy").parse("Jun 12, 2006"));
day2 = new Day(new SimpleDateFormat("MMM dd, yyyy").parse("Jun 13, 2006"));
day3 = new Day(new SimpleDateFormat("MMM dd, yyyy").parse("Jun 14, 2006"));
day4 = new Day(new SimpleDateFormat("MMM dd, yyyy").parse("Jun 15, 2006"));
day5 = new Day(new SimpleDateFormat("MMM dd, yyyy").parse("Jun 16, 2006"));
day6 = new Day(new SimpleDateFormat("MMM dd, yyyy").parse("Jun 17, 2006"));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


time[0].add(day1, 12);
time[1].add(day1, 25);
time[0].add(day2, 3242);
time[1].add(day2, 2523);
time[0].add(day3, 1243);
time[1].add(day3, 2534);
time[0].add(day4, 162);
time[1].add(day4, 2345);
time[0].add(day5, 1562);
time[1].add(day5, 235);
time[0].add(day6, 162);
time[1].add(day6, 225);

dataset.addSeries(time[0]);
dataset.addSeries(time[1]);
return dataset;
}

private JFreeChart createChart(final TimeSeriesCollection dataset){

final JFreeChart chart = ChartFactory.createTimeSeriesChart(
chartTitle, // Chart Title
"Date", // X-Axis Label
"Message", // Y-Axis Label
dataset, // Dataset
true, // Show Legend
true, // Show Tooltip
false);

chart.setBackgroundPaint(Color.white);

return chart;
}

public static void main(String [] args){
SecondLineChart chartType = new SecondLineChart("First Line Chart Sample");
chartType.pack();
RefineryUtilities.centerFrameOnScreen(chartType);
chartType.setVisible(true);
}
}

Narayan
Posts: 30
Joined: Wed Apr 11, 2007 11:20 am
Location: Bangalore

Post by Narayan » Mon May 21, 2007 5:28 am

Can anyone of you please suggest why the tooltip is not coming for the above code. Is it the problem with the code or the bug in the JFreeChart ??

Thanks in advance.
Narayan

Narayan
Posts: 30
Joined: Wed Apr 11, 2007 11:20 am
Location: Bangalore

Post by Narayan » Mon May 28, 2007 11:10 am

Hello,

This tooltip solution is quite urgent to me. Can you please let me know whether there is any mistake in the code or is it a problem with the JFreeChart code ??

Narayan
Posts: 30
Joined: Wed Apr 11, 2007 11:20 am
Location: Bangalore

Post by Narayan » Mon May 28, 2007 12:24 pm

Hi All,

I am sorry for giving the wrong piece of code. If I use the chartPanel I get the tooltip.
But my requirement is to set the chart in the response and send it. I have given the piece of the which saves it as PNG. When I do this or when I send it in response, I am not gettig the tooltip. Can you please let me know what has to be done so that I get the tooltip when I send it in the response.

Code: Select all

import java.awt.Color;
import java.awt.Dimension;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;



public class SecondLineChart extends ApplicationFrame{

	private String chartTitle;
	
	public SecondLineChart(String title){
		super(title);
		chartTitle = title;
		TimeSeriesCollection dataset = null;
		dataset = createDailyDataset();
		
		final JFreeChart chart = createChart(dataset);
		File file1 = new File("E:\\first.png");
		try {
			ChartUtilities.saveChartAsPNG(file1, chart, 500, 200);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

	
	private TimeSeriesCollection createDailyDataset(){

		TimeSeriesCollection dataset = new TimeSeriesCollection();

		TimeSeries [] time = new TimeSeries[2];
		
		time[0] = new TimeSeries("Pers Msg", Day.class);
		time[1] = new TimeSeries("Non Pers Msg", Day.class);
		
		Day day1 = null;
		Day day2 = null;
		Day day3 = null;
		Day day4 = null;
		Day day5 = null;
		Day day6 = null;
		try {
			day1 = new Day(new SimpleDateFormat("MMM dd, yyyy").parse("Jun 12, 2006"));
			day2 = new Day(new SimpleDateFormat("MMM dd, yyyy").parse("Jun 13, 2006"));
			day3 = new Day(new SimpleDateFormat("MMM dd, yyyy").parse("Jun 14, 2006"));
			day4 = new Day(new SimpleDateFormat("MMM dd, yyyy").parse("Jun 15, 2006"));
			day5 = new Day(new SimpleDateFormat("MMM dd, yyyy").parse("Jun 16, 2006"));
			day6 = new Day(new SimpleDateFormat("MMM dd, yyyy").parse("Jun 17, 2006"));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		time[0].add(day1, 12);
		time[1].add(day1, 25);
		time[0].add(day2, 3242);
		time[1].add(day2, 2523);
		time[0].add(day3, 1243);
		time[1].add(day3, 2534);
		time[0].add(day4, 162);
		time[1].add(day4, 2345);
		time[0].add(day5, 1562);
		time[1].add(day5, 235);
		time[0].add(day6, 162);
		time[1].add(day6, 225);
		
		dataset.addSeries(time[0]);
		dataset.addSeries(time[1]);
		return dataset;
	}
	
	private JFreeChart createChart(final TimeSeriesCollection dataset){
		
		final JFreeChart chart = ChartFactory.createTimeSeriesChart(
				chartTitle,  // Chart Title
				"Date", // X-Axis Label
				"Message",        // Y-Axis Label  
				dataset,     // Dataset
				true,        // Show Legend
				true,        // Show Tooltip
				false); 
		
		chart.setBackgroundPaint(Color.white);

		return chart;
	}
	
	public static void main(String [] args){
		SecondLineChart chartType = new SecondLineChart("First Line Chart Sample");
		chartType.pack();
		RefineryUtilities.centerFrameOnScreen(chartType);
		chartType.setVisible(true); 
	}
}

Priya Shivapura
Posts: 59
Joined: Fri Feb 23, 2007 7:41 am

Post by Priya Shivapura » Tue May 29, 2007 5:30 pm

You can retrieve the tooltip information by doing the following.

Code: Select all

ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
ChartUtilities.saveChartAsPNG(file1, chart, width, height, info);
String imageMap = ImageMapUtilities.getImageMap("imagemap", info);	
The imageMap String has the tooltip information.

Regards,
Priya

Narayan
Posts: 30
Joined: Wed Apr 11, 2007 11:20 am
Location: Bangalore

Post by Narayan » Wed May 30, 2007 7:15 am

Thanks Priya. I appreciate your response.

I got that. But I am facing a strange problem.

My requirement is to draw the chart and then put it directly in the jsp without storing it as a PNG file.

I am calling the chart using the following code.

Code: Select all

//in the src call the action class method to get the chart.
<img id="chart" onerror="javascript:errorChart()" src='actionname.action.generateChart.event' border="0" USEMAP="#imageMap"/>
Now in the action class, I am doing the following.

Code: Select all

final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());

OutputStream out = response.getOutputStream();
response.setContentType("image/png");

//get the chart
chart = chartHelper.drawChart(statsContext);

//write the chart as PNG
ChartUtilities.writeChartAsPNG(out, chart, 650, 220, info);

//Write the image map and put to same outputstream.
ChartUtilities.writeImageMap(new PrintWriter(out), "imageMap", info, false);
If I storing it as a image then the code will look

Code: Select all

<map id="imageMap" name="imageMap">
.......
</map>
<img src="chart.png" usemap="#imageMap"/>
The problem comes because I am not having that file with me on the fly and I shouldnt create any temp file. can I give something else instead of the file name in the src ???
Since I am doing both constructing the chart and the imagemap together, I dont know how can I get the above pattern. [/code]

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Wed May 30, 2007 1:55 pm

A workaround would be to draw the chart twice, once to get the image map and again to stream the image bytes to the client.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Narayan
Posts: 30
Joined: Wed Apr 11, 2007 11:20 am
Location: Bangalore

Post by Narayan » Thu May 31, 2007 12:05 pm

Thanks Dave.

I got the answer. I got to fine tune it though.

My jsp will now look like this.

Code: Select all

<%
//here the call to the chart is made
out.println((String)action.getImageMap(request, response));
%>

//src will make the second hit to chart
<img id="chart" onerror="javascript:errorChart()" src="ActionName.action.generateChart.event" border="0"
USEMAP="#imageMap">
I have 2 hits to the backend.
In the first hit, I am doing the following.

Code: Select all

ChartHelper chartHelper = new ChartHelper();
//get the chart
chart = chartHelper.drawChart(statsContext);

//get the rendering info
final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
ByteArrayOutputStream out1 = new ByteArrayOutputStream();
//write the chart.
ChartUtilities.writeChartAsPNG(out1, chart, 650, 220, info);

//get the image map
imageMap = ImageMapUtilities.getImageMap("imageMap", info);

//save the chart as a session attribute
session.setAttribute("chart", chart);

return imageMap;
and the second hit will be to the following.

Code: Select all

//set the response type to png
response.setContentType("image/png");

//get the output stream
OutputStream out = response.getOutputStream();

//get the chart object from the session.
JFreeChart chart = (JFreeChart)session.getAttribute("chart");

//remove chart object from the session
session.removeAttribute("chart");

//get the rendering info
final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
//get the png image
ChartUtilities.writeChartAsPNG(out, chart, 650, 220, info);
			
return null;

Narayan
Posts: 30
Joined: Wed Apr 11, 2007 11:20 am
Location: Bangalore

Post by Narayan » Wed Jul 11, 2007 2:48 pm

Hi Dave,

I am doing the way you had told me, once getting the imageMap and then getting the chart.

But the problem is that if there are lots of records, the chart is not displayed fully, also in the page, some components of the page like left navigation bar will be missing. Can you please tell me how can I have the smooth rendering

Regards,
Narayan

Narayan
Posts: 30
Joined: Wed Apr 11, 2007 11:20 am
Location: Bangalore

Post by Narayan » Wed Jul 11, 2007 3:12 pm

Its urgent because I have the release of the code.

It would be really helpful if I get the solution fast.

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Wed Jul 11, 2007 5:09 pm

Narayan wrote:But the problem is that if there are lots of records, the chart is not displayed fully, also in the page, some components of the page like left navigation bar will be missing. Can you please tell me how can I have the smooth rendering
No I can't, because I have very little information about what you've done. And if I *did* have that information, I'm guessing there would be a few hours of debugging and analysis to figure out what is going wrong...time that I can't afford to spend solving your problem.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Narayan
Posts: 30
Joined: Wed Apr 11, 2007 11:20 am
Location: Bangalore

Post by Narayan » Thu Jul 12, 2007 6:07 am

Ok.. Thanks.

Locked