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.

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;
}
}