Highlight selected data point

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
snoopygee
Posts: 18
Joined: Wed Jan 28, 2009 3:10 pm

Highlight selected data point

Post by snoopygee » Thu Feb 12, 2009 12:02 pm

Hi All,

When I mouse over a data point in an YXSeries chart I would like the point to highlight somehow (by changing colour and/or become encircled) to indicate that a particular point is selected. Would someone please be able to point me in the right direction on how to perform this action.

Thanks
Snoopygee

MitchBuell
Posts: 47
Joined: Thu Dec 11, 2008 7:59 pm

Post by MitchBuell » Thu Feb 12, 2009 3:55 pm

The ChartPanel has a ChartMouseListener interface that you can implement that provides feedback about mouse moves and mouse clicks. You can create your own ChartMouseListener that is called when the mouse moves or mouse clicks. It then gets the data point you care about, and you can do whatever you need to. I don't know how to tell a Renderer to change the color of a specific point, but that's all that is left.

1) Add your ChartMouseListener to your ChartPanel:

Code: Select all

chartPanel.addChartMouseListener(new MyChartMouseListener());
2) Create your ChartMouseListener implementation

Code: Select all

public void chartMouseClicked(ChartMouseEvent event)
{
	ChartEntity entity = event.getEntity();
	if (entity == null)
		return;
	
	// Get entity details
	String tooltip = ((XYItemEntity)entity).getToolTipText();
	XYDataset dataset = ((XYItemEntity)entity).getDataset();
	int seriesIndex = ((XYItemEntity)entity).getSeriesIndex();
	int item = ((XYItemEntity)entity).getItem();

	// You have the dataset the data point belongs to, the index of the series in that dataset of the data point, and the specific item index in the series of the data point.
	XYSeries series = ((XYSeriesCollection)dataset).getSeries(seriesIndex);
	XYDataItem xyItem = series.getDataItem(item);
}

public void chartMouseMoved(ChartMouseEvent event)
{
}

snoopygee
Posts: 18
Joined: Wed Jan 28, 2009 3:10 pm

Post by snoopygee » Fri Feb 13, 2009 10:21 am

Thanks for the Tip Mitch. But what I really need is the method for altering the selected point's colour and shape (renderer).

Anyone have any idea's?

mkivinie
Posts: 51
Joined: Wed Jul 06, 2005 8:35 am

Post by mkivinie » Fri Feb 13, 2009 11:46 am

You could use Mitch's code to mark that item selected with your own code.
Then create a special Renderer that notices the selected item and renders it with your desired attributes.

Or alternatively: without modifying the underlying JFreeChart, use Mitch's code to find the selected item. Then find out its coordinates within the plot (probably not the ones that the MouseEvent provides). Then place an Annotation of your choice on top of it (e.g. XYShapeAnnotation). And when the selected item is unselected then remove the Annotation.

More ideas from someone else?

MitchBuell
Posts: 47
Joined: Thu Dec 11, 2008 7:59 pm

Post by MitchBuell » Fri Feb 13, 2009 3:34 pm

snoopygee wrote:But what I really need is the method for altering the selected point's colour and shape (renderer).
That's all that is left to do with my code, and unfortunately I don't know how to do that.

snoopygee
Posts: 18
Joined: Wed Jan 28, 2009 3:10 pm

Post by snoopygee » Fri Feb 13, 2009 3:56 pm

Thanks for all your replies guys.

The tricky bit for me is how to alter the renderer. I understand how to receive the mouse events and to retrieve the underline data for the selected points, but not sure how to actually alter the paint method to redraw the specific point differently. Extra brownie points for anyone who can define how this is done.

Thanks

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

Post by paradoxoff » Fri Feb 13, 2009 5:46 pm

snoopygee wrote: The tricky bit for me is how to alter the renderer. I understand how to receive the mouse events and to retrieve the underline data for the selected points, but not sure how to actually alter the paint method to redraw the specific point differently.
Write your own renderer and override getItemPaint(int,int) and/or getItemOutlinePaint(int,int) and/or getItemOutlineStroke(int,int) to return specific colors/strokes based on whether the item is selected or not.
The renderer has to know from somewhere whether it should the return the "normal" paints/strokes or the "selected" paints/strokes. Thus, you will have to store the selection information in the renderer as well (probably as boolean[seriesCount][itemCount]). This renderer would also need to have a method "setItemSelected(int seriesIndex,int itemIndex,boolean selected)" that can be invoked when a data item selection event occurs.

Locked