How to show the Tooltips

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
senthil
Posts: 12
Joined: Thu Apr 17, 2008 8:48 am

How to show the Tooltips

Post by senthil » Tue May 27, 2008 7:48 am

Hi
i need a clarification regarding tooltips


first i just to tell you how i handle the creation of chart .

i am saving a chart using servletUtilities and displaying the chart in


<img src=servlet name ? file name = ? />

my requirement is to show the tool tips

is it possible ...


i have created the chart giving the boolean true for tool tips


final JFreeChart chart = ChartFactory.createBarChart(
"Sample", // chart title
"", // domain axis label
"Performance", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // the plot orientation
false, // legend
true, // tooltips
false // urls
);

but tool tips is not displayed in the page


could you guide me ?


by
senthilkumar.A

rahul
Posts: 6
Joined: Thu Jun 26, 2008 1:08 pm

Post by rahul » Tue Jul 01, 2008 1:30 pm

Hi,
I also have the same problem. I am calling the servlet from the src attribute of the img tag in a jsp file. This is the part of the servlet

Code: Select all

JFreeChart chart = ChartFactory.createBarChart
		  ("BAR CHART","Hour", "Volume", dataset,PlotOrientation.VERTICAL, true,true,false);
CategoryPlot plot = chart.getCategoryPlot();
CategoryItemRenderer renderer = plot.getRenderer();
StandardCategoryToolTipGenerator generator =
          new StandardCategoryToolTipGenerator("{1}, {2}", NumberFormat.getInstance());
renderer.setBaseToolTipGenerator(generator);
And I am returning the chart back. The chart is getting displayed but am not getting the tooltips.

Hi senthil..did you get any solution for this?

Thanks

Wetzerk
Posts: 14
Joined: Thu Feb 07, 2008 3:54 pm

Post by Wetzerk » Tue Jul 01, 2008 5:15 pm

If this is being rendered as html then you need to ensure the map tag is populated. If you jsp only has the img tag then you won't get what you're looking for.

Check out:
http://www.jfree.org/phpBB2/viewtopic.p ... +image+map
http://www.jfree.org/phpBB2/viewtopic.p ... +image+map
http://www.jfree.org/phpBB2/viewtopic.p ... +image+map

rahul
Posts: 6
Joined: Thu Jun 26, 2008 1:08 pm

Post by rahul » Wed Jul 02, 2008 4:07 am

Thanks Wetzerk for your reply..

So if I am using a jsp with an image tag, then I wont be able to get tooltips. Is this because the tolltips require ChartPanel or ChartRenderingInfo, which cannot be added in this case?

Thanks.

samaeinder
Posts: 10
Joined: Thu Jun 12, 2008 6:34 am

Post by samaeinder » Wed Jul 02, 2008 6:06 am

plz use code give below .....i hope this is able to solve ur problems.
if not , plz don't hesitate to ping me back
thanks
SAM

Code: Select all

	package src;

	import java.io.PrintWriter;
	import java.sql.Connection;
	import java.sql.DriverManager;
	import java.sql.ResultSet;
	import java.sql.Statement;
	import java.text.NumberFormat;
	import java.text.SimpleDateFormat;
	import java.util.Locale;

	import javax.servlet.http.HttpSession;


	import org.jfree.chart.ChartFactory;
	import org.jfree.chart.ChartRenderingInfo;
	import org.jfree.chart.ChartUtilities;
	import org.jfree.chart.JFreeChart;



import org.jfree.chart.axis.CategoryAxis;
	import org.jfree.chart.axis.DateAxis;
	import org.jfree.chart.axis.NumberAxis;
	import org.jfree.chart.axis.ValueAxis;
	import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
	import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.Plot;
	import org.jfree.chart.plot.PlotOrientation;
	import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.BarRenderer;
	import org.jfree.chart.renderer.StandardXYItemRenderer;
	import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.urls.StandardCategoryURLGenerator;
	import org.jfree.chart.urls.TimeSeriesURLGenerator;

import org.jfree.data.DefaultCategoryDataset;
	import org.jfree.data.XYDataset;
	import org.jfree.data.XYSeries;
import org.jfree.data.XYSeriesCollection;



	public class tooltipForString {
		public static String generateXYChart(String section, HttpSession session, PrintWriter pw) {
		String filename = null;
		try{
		final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		final String type1 = "DRE";
    /*	while(rs.next()){
    		dataset.addValue(rs.getInt(1),type1, rs.getString(2));
    	}*/
		dataset.addValue(3, type1, "2.1.1");
		dataset.addValue(6, type1, "2.1.5");
		dataset.addValue(8, type1, "2.1.7");
		
		CategoryAxis categoryAxis = new CategoryAxis("");
		ValueAxis valueAxis = new NumberAxis("");
		BarRenderer renderer = new BarRenderer();
		renderer.setItemURLGenerator(new StandardCategoryURLGenerator("xy_chart.jsp","series","section"));
        renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());

		Plot plot = new CategoryPlot(dataset, categoryAxis, valueAxis, renderer);;
		JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, false);
		chart.setBackgroundPaint(java.awt.Color.white);

		//  Write the chart image to the temporary directory
		ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
		filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session);

		//  Write the image map to the PrintWriter
		ChartUtilities.writeImageMap(pw, filename, info);
		pw.flush();

	
	} catch (Exception e) {
		System.out.println("Exception - " + e.toString());
		e.printStackTrace(System.out);
	 filename = "public_error_500x300.png";
	}
	return filename;
	}
}


jsp used

Code: Select all

<%@ page import = "src.tooltipForString" %>

<%@ page import = "java.io.PrintWriter" %>
<%@ page import = "java.util.ArrayList" %>
<%@ page import = "java.util.Iterator" %>
<%
	String section = request.getParameter("section");
	if (section == null ? false : section.equals("All")) section = null;
	String filename = tooltipForString.generateXYChart(section, session, new PrintWriter(out));
	String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
	
%>
<html>
<head>
<link rel="stylesheet" href="sample.css" type="text/css"/>
<title>Timeseries Chart Example</title>
</head>
<body>
<img src="images/top_bar.png" width=1004 height=75 border=0>
<table border=0>
	<tr>
	<td width=170><img src="images/spacer.png" width=170 height=1></td>
	<td>
	<h2>Timeseries Chart Example</h2>
	Section [<%= section == null ? "All sections" : section %>]<br>
    <br>

	<img src="<%= graphURL %>" width=500 height=300 border=0 usemap="#<%= filename %>">

</body>
</html>
web.xml

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
  <servlet>
    <servlet-name>DisplayChart</servlet-name>
    <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
  </servlet>
   <servlet-mapping>
        <servlet-name>DisplayChart</servlet-name>
        <url-pattern>/servlet/DisplayChart</url-pattern>
    </servlet-mapping>
</web-app>

Locked