IMSL C# Programmer’s Guide
Actions >> Picking  Previous Page  Contents  Next Page

Picking

The pick mechanism allows mouse events to be associated with chart nodes. The pick mechanism follows the .NET delegate pattern.

A PickEventHandler is added to a chart node, using the node’s PickPerformed method using the delegate += syntax to define the method to be called when the event occurs.

A pick is fired by calling the method Pick(MouseEventArgs) in the top-level Chart node. Normally this is done in response to a mouse click. Mouse clicks can be detected by adding a MouseEventHandler to the MouseDown property of the chart’s container.

Example

In this example, when a bar is clicked it toggles its color between blue and red.

A MouseEventHandler is added to the Panel, which calls method Chart.Pick(MouseEventArgs).

The pick delegate 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


(Download Code)

using System.Windows.Forms;
using System.Drawing;
using Imsl.Chart2D;

public class SamplePick : FrameChart
{
    private Chart chart;
    private double[] x = new double[]{0, 1, 2, 3};
    private double[] y = new double[]{4, 1, 2, 5};

    public SamplePick()
    {
        chart = this.Chart;
        AxisXY axis = new AxisXY(chart);
        Bar bar = new Bar(axis, x, y);
        bar.BarType = Bar.BAR_TYPE_VERTICAL;
        bar.SetLabels(new string[]{“A”,”B”,”C”, “D”});
        bar.FillColor = Color.Blue;

        // Add the event handler delegates
        bar.PickPerformed += new PickEventHandler(SamplePick_PickPerformed);
        this.Panel.MouseDown += new MouseEventHandler(SamplePick_MouseDown);
    }

    private void SamplePick_MouseDown(object sender, MouseEventArgs e)
    {
        chart.Pick(e);
    }

    private void SamplePick_PickPerformed(PickEventArgs param)
    {
        ChartNode node = param.Node;
        int count = node.GetIntegerAttribute(“pick.count”, 0);
        count++;
        node.SetAttribute(“pick.count”, (System.Object) (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.FillColor = color;
        Refresh();
    }

    public static void Main(string[] argv)
    {
        System.Windows.Forms.Application.Run(new SamplePick());
    }
}




©  Visual Numerics, Inc.  All rights reserved.  Previous Page  Contents  Next Page