Barchart Tooltip and URL

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
raj_jfree
Posts: 20
Joined: Tue Sep 29, 2009 8:32 am
antibot: No, of course not.

Barchart Tooltip and URL

Post by raj_jfree » Wed Sep 30, 2009 11:38 am

Hi,


I have used Jfreechart 1.0.13 for generating the bar chart using spring mvc.
i am using ChartUtilities.writeChartAsJPEG(response.getOutputStream(), jfreechart1, 400, 400); to display the image in the browser.

problem is i am not able to set the generated Tool tip and URL i have marked in red where i am generating the tool tip and url.
How to setURL and tool tip for the bar chart.
below is my code and the barchart generated.
.
where can i set the url and tool tip for the below code to display and on click it should open the page of the url generated.




Image





package com.jasper.web;

import java.awt.Color;
import java.awt.Font;
import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.IntervalMarker;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.urls.CategoryURLGenerator;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.Layer;
import org.jfree.ui.RectangleAnchor;
import org.jfree.ui.TextAnchor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class GraphController implements Controller {

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setContentType("image/png");

class LabelGenerator extends StandardCategoryItemLabelGenerator {

public String generateLabel(CategoryDataset categorydataset, int i, int j) {
return categorydataset.getRowKey(i).toString();
}

LabelGenerator() {
}
}
final double[][] data = new double[][] { { 210, 300, 320, 265 }, { 200, 304, 201, 201 } };
String[] seriesNames = new String[] { "Actual", "Budget" };
String[] categoryNames = new String[] { "Loan", "Trade", "Cash", "Treasury" };
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(seriesNames, categoryNames, data);

JFreeChart jfreechart1 = ChartFactory.createBarChart("Bar Chart Demo", "", " ", dataset, PlotOrientation.VERTICAL, true, true, true);
CategoryPlot categoryplot = (CategoryPlot) jfreechart1.getPlot();
categoryplot.setRangePannable(true);
IntervalMarker intervalmarker = new IntervalMarker(4.5D, 7.5D);
intervalmarker.setLabel("Target Range");
intervalmarker.setLabelFont(new Font("SansSerif", 2, 11));
intervalmarker.setLabelAnchor(RectangleAnchor.LEFT);
intervalmarker.setLabelTextAnchor(TextAnchor.CENTER_LEFT);
intervalmarker.setPaint(new Color(222, 222, 255, 128));
categoryplot.addRangeMarker(intervalmarker, Layer.BACKGROUND);
NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer();
barrenderer.setDrawBarOutline(false);
barrenderer.setItemMargin(0.10000000000000001D);
barrenderer.setBaseItemLabelGenerator(new LabelGenerator());
barrenderer.setBaseItemLabelsVisible(true);
ItemLabelPosition itemlabelposition = new ItemLabelPosition(ItemLabelAnchor.INSIDE12, TextAnchor.CENTER_RIGHT, TextAnchor.CENTER_RIGHT,
-1.5707963267948966D);
barrenderer.setBasePositiveItemLabelPosition(itemlabelposition);
ItemLabelPosition itemlabelposition1 = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.CENTER_LEFT, TextAnchor.CENTER_LEFT,
-1.5707963267948966D);
barrenderer.setPositiveItemLabelPositionFallback(itemlabelposition1);
CategoryAxis categoryaxis = categoryplot.getDomainAxis();
categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
barrenderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator("www.google.com", seriesNames[1], categoryNames[2]));
String url = new StandardCategoryURLGenerator("www.google.com", seriesNames[1], categoryNames[2]).generateURL(dataset, 1, 1);
barrenderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
String tooltip = new StandardCategoryToolTipGenerator().generateToolTip(dataset, 1, 1);
ChartUtilities.writeChartAsJPEG(response.getOutputStream(), jfreechart1, 400, 400);

response.getOutputStream().close();

return null;

}
}

class StandardCategoryURLGenerator implements CategoryURLGenerator, Cloneable, Serializable {
private static final long serialVersionUID = 1L;
private String actionName;
private String seriesParameterName;
private String categoryParameterName;

public StandardCategoryURLGenerator(String actionName, String seriesParameterName, String categoryParameterName) {
if (actionName != null)
this.actionName = actionName;
if (seriesParameterName != null)
this.seriesParameterName = seriesParameterName;
if (categoryParameterName != null)
this.categoryParameterName = categoryParameterName;
}

public StandardCategoryURLGenerator() {
// TODO Auto-generated constructor stub
}

public String generateURL(CategoryDataset categoryDataset, int series, int category) {
String url = actionName;
Comparable categoryKey = categoryDataset.getColumnKey(category);
boolean firstParameter = url.indexOf("?") == -1;
url += firstParameter ? "?" : "&";
url += this.seriesParameterName + "=" + series;
url += "&" + this.categoryParameterName + "=" + categoryKey.toString();

return url;
}

}

raj_jfree
Posts: 20
Joined: Tue Sep 29, 2009 8:32 am
antibot: No, of course not.

Re: Barchart Tooltip and URL

Post by raj_jfree » Thu Oct 01, 2009 10:46 am

My issue was solved..thanks for reviewing ..
if any one wants the solution can reply me...

anjanas
Posts: 5
Joined: Fri Mar 28, 2008 5:41 am

Re: Barchart Tooltip and URL

Post by anjanas » Thu Oct 15, 2009 11:32 am

Can u post the solution?

raj_jfree
Posts: 20
Joined: Tue Sep 29, 2009 8:32 am
antibot: No, of course not.

Re: Barchart Tooltip and URL

Post by raj_jfree » Mon Oct 19, 2009 4:32 am

Code: Select all

	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map mapCustomer = new HashMap();
		mapCustomer.put("success", true);
		List list = new ArrayList();
		try {
			class LabelGenerator extends StandardCategoryItemLabelGenerator {

				private static final long serialVersionUID = 1L;

				public String generateLabel(CategoryDataset categorydataset, int i, int j) {
					return categorydataset.getRowKey(i).toString();
				}

				LabelGenerator() {
				}
			}
			
	List al = new ArrayList();
	    	List al2 = new ArrayList();
			Collection categoryNames = new ArrayList();

	    	
	    	
	 // if the values are required in Integer format  ....................................	
//	    	
	    	al.add(100);
	    	al.add(20);
	    	al.add(39);
	    	al.add(39);
	    	
	    	al2.add(40);
	    	al2.add(20);
	    	al2.add(60);
	    	al2.add(39);
	    	
	    	int [][]data = new int[2][];
	    	data[0] =ArrayUtils.toPrimitive((Integer[]) al.toArray(new Integer[0]));
	    	data[1]=ArrayUtils.toPrimitive((Integer[]) al2.toArray(new Integer[0]));
	    	

			String[] seriesNames = new String[] { "Actual", "Budget" };
//	
			categoryNames.add("Loan");
			categoryNames.add("Trade");
			categoryNames.add("Cash");
			categoryNames.add("Treasury");

			String sCategoryNames[] = (String[]) categoryNames.toArray(new String[0]);
			
	

			
			// Overridden the datset utility method to pass Integer array.....................
			
			CategoryDataset dataset = createCategoryDataset(seriesNames, sCategoryNames,data);
			JFreeChart jfreechart = ChartFactory.createBarChart("Actual vs Budget", "", " ", dataset, PlotOrientation.VERTICAL, true, true, true);
			jfreechart.setBackgroundPaint(java.awt.Color.white);
			CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
			NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
			numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
			BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer();

			// set up gradient paints for series...
			final GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue, 0.0f, 0.0f, Color.blue);
			final GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.red, 0.0f, 0.0f, Color.red);
			barrenderer.setSeriesPaint(0, gp0);
			barrenderer.setSeriesPaint(1, gp1);
			barrenderer.setMaximumBarWidth(0.1);
			barrenderer.setDrawBarOutline(true);
			barrenderer.setItemMargin(0.0);
			barrenderer.setBaseItemLabelGenerator(new LabelGenerator());
			barrenderer.setBaseItemLabelsVisible(false);
			barrenderer.setBaseLegendTextPaint(gp0);
			


			ItemLabelPosition itemlabelposition = new ItemLabelPosition(ItemLabelAnchor.INSIDE12, TextAnchor.CENTER_RIGHT, TextAnchor.CENTER_RIGHT,
					-1.5707963267948966D);
			barrenderer.setBasePositiveItemLabelPosition(itemlabelposition);
			ItemLabelPosition itemlabelposition1 = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.CENTER_LEFT, TextAnchor.CENTER_LEFT,
					-1.5707963267948966D);
			barrenderer.setPositiveItemLabelPositionFallback(itemlabelposition1);
			CategoryAxis categoryaxis = categoryplot.getDomainAxis();
			categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
			categoryaxis.setLabelPaint(gp0);
			barrenderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
			barrenderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator(" ", "series","category"));
		
			final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());

			final File file = new File(request.getRealPath("/BarChart.png"));

			ChartUtilities.saveChartAsPNG(file, jfreechart, 600, 400, info);

			PrintWriter writer = response.getWriter();
			writer.println("<HTML>");
			writer.println("<HEAD><TITLE>Bar Chart</TITLE><script type=\"text/JavaScript\"  src=\"overlib.js\"></script></HEAD>");
			writer.println("<BODY>");
			ChartUtilities.writeImageMap(writer, "chart", info, true);
			writer.println("<IMG SRC=\"BarChart.png\" " + "WIDTH=\"600\" HEIGHT=\"400\" BORDER=\"0\" USEMAP=\"#chart\">");
			writer.println("</BODY>");
			writer.println("</HTML>");
			writer.close();
			
		} catch (Exception e) {
			mapCustomer.put("success", false);
		}

		return new ModelAndView(getSuccessView(), mapCustomer);
	}

Code: Select all

class StandardCategoryURLGenerator implements CategoryURLGenerator, Cloneable, Serializable {
	private static final long serialVersionUID = 1L;
	private String actionName;
    private String seriesParameterName ;
	private String categoryParameterName ; 


	public StandardCategoryURLGenerator(String actionName, String seriesParameterName, String categoryParameterName) {
		if (actionName != null)
			this.actionName = actionName;
		this.seriesParameterName=seriesParameterName;
		this.categoryParameterName=categoryParameterName;
	}

	public StandardCategoryURLGenerator() {
		// TODO Auto-generated constructor stub
	}
	public String generateURL(CategoryDataset categoryDataset, int series, int category) {
		String url;

		Comparable categoryKey = categoryDataset.getColumnKey(category);
		Comparable seriesKey = categoryDataset.getRowKey(series);

		url = this.seriesParameterName + "=" + seriesKey;
		url += "&" + this.categoryParameterName + "=" + categoryKey.toString();
		url += "&" + "Value" + "=" + categoryDataset.getValue(series, category);

		String javaScript = "javascript:alert('" + url + "')";
//		String javaScript = "javascript:if (opener && !opener.closed){opener.focus();}else{opener = window.open('http://192.168.0.237:10000/iam/','','');location.href='http://192.168.0.237:10000/iam/mygraph.action'}";
		 

		url = javaScript;
		return url;
	} 

is this the one you are looking for ..the bottom class is for generating the URL and onclick it forwards to required..

kasineedis
Posts: 7
Joined: Tue Mar 10, 2009 8:50 pm

Re: Barchart Tooltip and URL

Post by kasineedis » Wed Aug 25, 2010 3:17 pm

Hi Raj ,


My requirment is to create HyperLink on Symbol Axis Tick lables on Y-aix(Ex p1,p2,p3 ...etc).When you click on that hyper link I have to show another window in which I have to show some data by connecting to database.Could you pleaseGuide me or any body in forum.
1)How to make Symbol axis Tick labels as Hyper links.
2)How to create Pop-up window when you click on it.

I always appreciate your help.

Thanks
kasi

upjfree
Posts: 2
Joined: Wed Dec 01, 2010 4:05 pm
antibot: No, of course not.

Re: Barchart Tooltip and URL

Post by upjfree » Wed Dec 01, 2010 4:42 pm

Hi Raj,

I am facing an issue with respect to generating URL's in case of Gantt Chart
For the URL generation part, I have kept the below code.

renderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator("www.google.com"));

This code should ideally create a link of google.com in each of the Bars which Gantt Chart creates.
But this not the actual beahviour.
Kindly tell me if i am missing anything.
Do tell me if you need any more details.
I am using Jfree 1.0.13.

Thanks in Advance.
UpJfree

upjfree
Posts: 2
Joined: Wed Dec 01, 2010 4:05 pm
antibot: No, of course not.

Re: Barchart Tooltip and URL

Post by upjfree » Sat Dec 04, 2010 11:27 am

Hi All,

For the URL's to be enabled it is compulsory to save the Chart as an Image.
Only setting the StandardCategoryURLGenerator wont be enough.
Internally the charts are using image map technicality for displaying the URLS as part of Image.

Locked