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.
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.setBarType(Bar.BAR_TYPE_VERTICAL);
bar.setLabels(new String[]{"A","B","C"});
bar.setFillColor(Color.blue);
// Add the pick listener to the bar
bar.addPickListener(this);
// Add the mouse listener
addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent event) {
getChart().pick(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);
count++;
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) ? Color.blue : Color.red);
node.setFillColor(color);
node.setLineColor(color);
repaint();
}
public static void main(String argv[]) {
new SamplePick().setVisible(true);
}
}
PHONE: 713.784.3131 FAX:713.781.9260 |