Reputation: 105
I am writing an application that uses Jfreechart to plot a simple bar chart, this chart needs to respond to mouse clicks from the user. I would like to change the color of the bar that is clicked on by the user. I know that the event will be a ChartMouseEvent that will be handled by the ChartMouseListener but I am a little confused about how to change the color of the ChartEntity once I have received it from the event.
Any help would be much appreciated.
Upvotes: 1
Views: 1210
Reputation: 205785
You can change the color of an individual bar by overriding the renderer's getItemPaint()
, as shown here and here for other renderer's. Once you receive a ChartEntity
of type CategoryItemEntity
, you can determine which dataset, series, and bar was clicked. Then you can condition your custom renderer's to apply the desired color with the next repaint()
.
Alternatively, display each series and color in a JTable
and use JColorChooser
to select colors, as shown in How to Use Tables.
Upvotes: 3