Adding Tooltip to the XYLine chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Post Reply
mithun
Posts: 2
Joined: Thu Feb 02, 2012 10:07 am
antibot: No, of course not.

Adding Tooltip to the XYLine chart

Post by mithun » Mon Feb 06, 2012 5:49 am

Hi all,
i am new to this forum. i am using JFreechart for the first time. i have created a XYLineChart with the help of examples from this forum.

i need one help from you. i want to add tooltip to my chart created. i have set the tooltip boolean value to true while creating the instance of chart.
i have attached the code snippet here. pls give me the solution for adding the tooltip for the chart which gives the value of "X" and "Y" axis in the tooltip.

My code is as follows,

Code: Select all

package examples;

import java.awt.Color;
import java.awt.Insets;
import java.awt.Paint;
import java.awt.Shape;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.plot.DefaultDrawingSupplier;
import org.jfree.chart.plot.Marker;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.Hour;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;

public class ChartExample extends ApplicationFrame {

	public ChartExample(final String title) {

		super(title);
		XYSeriesCollection dataset = new XYSeriesCollection();
		XYSeries series = new XYSeries("sdf");
		series.add(100, 200);
		series.add(150, 250);
		series.add(160, 260);
		series.add(170, 270);
		series.add(190, 290);
		dataset.addSeries(series);

		final String chartTitle = "\u20A2\u20A2\u20A3\u20A4\u20A5\u20A6\u20A7\u20A8\u20A9\u20AA";
		final JFreeChart chart = ChartFactory.createXYLineChart(chartTitle,
				"Time", "Value", dataset, PlotOrientation.VERTICAL, true, true, false);

		final XYPlot plot = chart.getXYPlot();
		final Marker marker = new ValueMarker(700.0);
		marker.setPaint(Color.blue);
		marker.setAlpha(0.8f);
		plot.addRangeMarker(marker);
		plot.setBackgroundPaint(null);

		final XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot
				.getRenderer();
		renderer.setSeriesShape(0,
				DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE[0]);

		renderer.setSeriesPaint(0, new Color(165, 42, 42));
		renderer.setBaseShapesFilled(true);
		renderer.setBaseItemLabelsVisible(true);
		renderer.setBaseShapesVisible(true);
		renderer.setBaseItemLabelPaint(Color.red);

		final ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
		chartPanel.setMouseZoomable(true, false);
		setContentPane(chartPanel);

	}

	public static void main(final String[] args) {

		final ChartExample demo = new ChartExample("Time Series Demo 4");
		demo.pack();
		RefineryUtilities.centerFrameOnScreen(demo);
		demo.setVisible(true);

	}

}
Thanks in Advance.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Adding Tooltip to the XYLine chart

Post by paradoxoff » Mon Feb 06, 2012 7:00 pm

Add this

Code: Select all

renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());

mithun
Posts: 2
Joined: Thu Feb 02, 2012 10:07 am
antibot: No, of course not.

Re: Adding Tooltip to the XYLine chart

Post by mithun » Tue Feb 07, 2012 5:53 am

Thanks :)

i got what i required.

kreska
Posts: 6
Joined: Tue Nov 13, 2018 12:51 pm
antibot: No, of course not.

Re: Adding Tooltip to the XYLine chart

Post by kreska » Wed Dec 19, 2018 11:36 am

Could you post the effect of this code with and without adding tooltips line? I don't see any difference.

Post Reply