Hi,
I have a standard XY line chart with visible and filled items. What I need is to change the item shape color whenever the user clicks on it. I wrote a mouse listener below and registered it. The code looks like this:
ChartPanel panel = new ChartPanel(chart);
frame.add(panel);
panel.addChartMouseListener(new ChartMouseClicksListener());
public class ChartMouseClicksListener implements ChartMouseListener {
public void chartMouseClicked(ChartMouseEvent event) {
XYItemEntity entity = (XYItemEntity) event.getEntity();
int itemIndex = entity.getItem();
//XYPlot plot = event.getChart().getXYPlot();
//XYLineAndShapeRenderer renderer =
// (XYLineAndShapeRenderer) plot.getRenderer();
// WHAT NEXT?
}
public void chartMouseMoved(ChartMouseEvent event) {
// this event is not needed for now
}
}
The question is how to proceed from here and access the specific item shape and then change its color? I guess, I'd like this color to be persistent to zooming, so each redraw should not reset it back to defaults.
Thanks,
Alex
How to change the color of a single data item shape?
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
You need to subclass the XYLineAndShapeRenderer and override getItemPaint(). By default, getItemPaint() returns the series color, but you can have it return anything you want.
In your subclass, store the selected item and return a custom color for it. For all other items, just return super.getItemPaint().
In your subclass, store the selected item and return a custom color for it. For all other items, just return super.getItemPaint().
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader

