JMSL Chart Programmer’s Guide
The pick mechanism allows MouseEvents to be associated with chart nodes. The pick mechanism follows the Java listener pattern.
The class that is to handle the pick events implements the PickListener interface. This requires the implementation of the method pickPerformed(PickEvent).
The PickListener is then added to a Chart node, using the node’s addPickListener method.
A pick is fired by calling the method pick(MouseEvent) in the top-level Chart node. Normally this is done in response to a mouse click. Mouse clicks can be detected by implementing the MouseListener interface and adding it to the chart’s container.
In this example, when a bar is clicked it toggles its color between blue and red.
A MouseListener is implemented as an inner class. Its mouseClicked method calls Chart.pick(MouseEvent).
The class SamplePick implements the PickListener interface. It is not an inner class only because of the size of the pickPerformed method. The pick listener is added to the chart node bar. This means that it is active for that node and its children but is not active for the axes and other nodes.
View code file
import com.imsl.chart.*;
import java.awt.Color;
public class SamplePick extends JFrameChart implements PickListener {
static private final double x[] = {0, 1, 2, 3};
static private final double y[] = {4, 1, 2, 5};
public SamplePick() {
// Create a bar chart
Chart chart = getChart();
AxisXY axis = new AxisXY(chart);
Bar bar = new Bar(axis, x, y);
bar.setLabels(new String[]{"A","B","C"});
// Add the pick listener to the bar
// Add the mouse listener
addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent event) {
public void pickPerformed(PickEvent event) {
// Track the number of hits on this bar
ChartNode node = event.getNode();
int count = node.getIntegerAttribute("pick.count", 0);
node.setAttribute("pick.count", new Integer(count));
// Change the bar's color depending on the number of times
// it has been picked.
Color color = ((count%2==0) ? :;
public static void main(String argv[]) {
new SamplePick().setVisible(true);