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
Highlight selected data point
-
- Posts: 47
- Joined: Thu Dec 11, 2008 7:59 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:
2) Create your ChartMouseListener implementation
1) Add your ChartMouseListener to your ChartPanel:
Code: Select all
chartPanel.addChartMouseListener(new MyChartMouseListener());
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)
{
}
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?
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?
-
- Posts: 47
- Joined: Thu Dec 11, 2008 7:59 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
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
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
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.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.
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.