Restricting ToolTipText to Chart Line Only

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
gw1500se
Posts: 23
Joined: Tue Nov 21, 2017 3:36 pm
antibot: No, of course not.

Restricting ToolTipText to Chart Line Only

Post by gw1500se » Mon Aug 27, 2018 2:13 pm

I have the following for displaying a tool tip at specific locations on a Timeline Series chart.

Code: Select all

class ToolTipChart extends ChartPanel {
		
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public ToolTipChart(JFreeChart chart) {
		super(chart);
	}
		
	@Override
	public String getToolTipText(MouseEvent e) {
		int x = e.getX();
		int y = e.getY();
		Point2D p = chartPanel.translateScreenToJava2D(new Point(x, y));
		XYPlot plot = chartPanel.getChart().getXYPlot();
		Rectangle2D dataArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea();
		if(!dataArea.contains(p)) return "";
		double xx = plot.getDomainAxis().java2DToValue(p.getX(), dataArea, plot.getDomainAxisEdge());
		double yy = plot.getRangeAxis().java2DToValue(p.getY(), dataArea, plot.getRangeAxisEdge());
		NumberFormat nformat = NumberFormat.getIntegerInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		return ("Time: " + sdf.format(xx) + ", Temperature: " + nformat.format(yy) +"\u00B0"+scale ) ;
	}
}
This pretty much works and displays the time and temperature at the location of the mouse. This behavior isn't bad but I'd like to refine it. I don't know if this is possible but I'd like to only display the tool tip when the mouse hovers over a plotted line. This is a multi-line chart. I guess the question is, is there a way to determine if the mouse is hovering over a line on the chart rather than just getting the data of the current mouse location? TIA.

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Restricting ToolTipText to Chart Line Only

Post by John Matthews » Mon Aug 27, 2018 6:09 pm

Apply an XYToolTipGenerator such as the StandardXYToolTipGenerator to your renderer, as shown in ChartFactory.createTimeSeriesChart(). Your implementation of generateToolTip() will have access to the relevant dataset, series and item.

gw1500se
Posts: 23
Joined: Tue Nov 21, 2017 3:36 pm
antibot: No, of course not.

Re: Restricting ToolTipText to Chart Line Only

Post by gw1500se » Mon Aug 27, 2018 7:13 pm

Thanks for the reply. Would this be "instead of" or "in addition to?"

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Restricting ToolTipText to Chart Line Only

Post by John Matthews » Tue Aug 28, 2018 2:50 am

gw1500se wrote:Would this be "instead of" or "in addition to?"
Instead. ChartPanel::getToolTipText queries the relevant ChartEntity for you, so you can focus on formatting.

Locked