How to change the color of a single data item shape?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
exo
Posts: 4
Joined: Tue Oct 31, 2006 1:49 am
Location: Israel

How to change the color of a single data item shape?

Post by exo » Tue Oct 31, 2006 2:27 am

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

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Tue Oct 31, 2006 3:16 pm

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().
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked