Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Getting value of a cursor on scatter graph with multiple plots

Hi there,

 

I have a scatter graph with multiple plots. I have only one cursor that is associated with plot[0]. I get the (x,y) value of plot[0], according to the cursor movement on the plot[0]. What I want is to get the (x,y) values from other plots as well according to the cursor position (shown in figure attached with ?). All the plots share the same x axes data but different y axes data.

 

Kindly provide some sample code.

 

Regards,

Naureen.

0 Kudos
Message 1 of 4
(6,495 Views)

Hello there,

 

Here is a sample code that does the trick.

 

using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication17
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            scatterPlot1.LineColor = Color.Gray;
            scatterPlot2.LineColor = Color.Gray;
            scatterPlot3.LineColor = Color.Gray;

            double[] xData = new double[] { 1, 2, 5, 9, 12, 13, 14, 17, 21, 24, 27, 33, 38, 44, 46, 52, 55, 59, 60, 67 };
            double[] yData1 = new double[] { 11, 2, 2, 47, 28, 34, 62, 3, 1, 4, 7, 3, 8, 4, 2, 2, 5, 9, 56, 7 };
            double[] yData2 = new double[] { 61, 22, 55, 43, 23, 30, 27, 8, 21, 4, 7, 3, 8, 4, 2, 2, 5, 9, 56, 7 };
            double[] yData3 = new double[] { 13, 2, 25, 68, 43, 35, 22, 7, 1, 4, 7, 3, 8, 4, 2, 2, 5, 9, 56, 7 };

            scatterPlot1.PlotXY(xData, yData1);
            scatterPlot2.PlotXY(xData, yData2);
            scatterPlot3.PlotXY(xData, yData3);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MyXYCursor c = new MyXYCursor(scatterGraph1, scatterPlot1);
            c.Color = Color.White;
            scatterGraph1.Cursors.Add(c);
            c.LabelVisible = true;

            c.AddNewCursorToPlot(scatterPlot2);
        }
    }

    class MyXYCursor : XYCursor
    {
        ScatterGraph _graph;
        List<XYCursor> _cursors = new List<XYCursor>();

        public MyXYCursor(ScatterGraph graph, ScatterPlot plot)
        {
            _graph = graph;
            base.Plot = plot;
        }

        public void AddNewCursorToPlot(ScatterPlot plot)
        {
            if (_graph.Plots.Contains(plot))
            {
                XYCursor c = CreateCursor();
                c.Plot = plot;
                _graph.Cursors.Add(c);
                _cursors.Add(c);
            }
        }

        private XYCursor CreateCursor()
        {
            XYCursor c = new XYCursor();

            c.HorizontalCrosshairMode = CursorCrosshairMode.None;
            c.SnapMode = CursorSnapMode.ToPlot;
            c.Color = Color.Red;
            c.LabelVisible = true;

            // The trick is to move all the cursors whenever one of the cursor is moved.
            this.AfterMove += MyXYCursor_AfterMove;
            c.AfterMove += MyXYCursor_AfterMove;

            return c;
        }

        void MyXYCursor_AfterMove(object sender, AfterMoveXYCursorEventArgs e)
        {
            XPosition = e.XPosition;
            foreach(XYCursor c in _cursors)
            {
                c.XPosition = e.XPosition;
            }
        }
    }
}

 

 

Hope this helps.

 

Vijet Patankar,

National Instruments

0 Kudos
Message 2 of 4
(6,490 Views)

Hello,

 

In my application I am displaying the Y values (yData1,yData2 and yData3) on my form through YPosition, When the user displaces the cursor those values get changed accordingly.

 

Please can you tell me, if I have y values of variable lengths (as shown below), then I will get the exception when the use places the cursor at the point where yData1 has the valid value but yData2 and yData3 has not.

 

double[] yData1 = new double[] { 11, 2, 2, 47, 28, 34, 62, 3, 1, 4, 7, 3, 8, 4, 2, 2, 5, 9, 56, 7 };
            double[] yData2 = new double[] { 61, 22, 55, 43, 23, 30, 27, 8, 21, 4, 7, 3 };
            double[] yData3 = new double[] { 13, 2, 25, 68, 43, 35, 22, 7, 1, 4, 7, 3, 8, 4, 2, 2, 5, 9};

 

I just want when such case arise I just display NA (Not Available) on the form for those yData that has no value.

 

Thanks,

Naureen.

0 Kudos
Message 3 of 4
(6,444 Views)

Can you send us an image of how you want to display "NA"? Because if the there is not enough data on the plot, then the multicursor (e.g MyXYCursor) does not know where to diplay the "NA" along vertical line of the cursor.

 

For example, take a look at the attached image. On the cursor we would have no input to display the value for Plot1 and Plot2.

 

What I would do is in the MyXYCursor_AfterMove method posted above, I would handle for moving the cursors within the range of the x-data. And if the actual cursor that user can move is out of the range of x-data then simply hide the cursor. This way you are just showing enough information on the graph that is necessary for your user.

0 Kudos
Message 4 of 4
(6,424 Views)