Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Multiplot ScatterGraph - snap cursor to nearest point AND plot

Hello.

 

I have a small task to complete.

I want to configure scatter graph with 2+ plots and one cursor.

Cursor has to snap to nearest point on nearest plot.

 

This is the code I use to do that:

//filling graph with data
int cplt = 0;
for (int i = 0; i < ChannelGUIDsList.Length; i++)
{
    ChannelGUIDsList[i] = ChannelGUIDsList[i].Substring(1,ChannelGUIDsList[i].Length-2);
    CSQL.GetMeasurementData(ChannelGUIDsList[i], StartTime.CalendarDate.ToUniversalTime(), StopTime.CalendarDate.ToUniversalTime(), ref dtDataTable);
    double[] dataX = new double[dtDataTable.Rows.Count];
    double[] dataY = new double[dtDataTable.Rows.Count];
    for (int j = 0; j < dtDataTable.Rows.Count; ++j)
    {
        dataX[j] = (double)DataConverter.Convert(Convert.ToDateTime(dtDataTable.Rows[j]["DateTime"]), typeof(double));
        dataY[j] = Convert.ToDouble(dtDataTable.Rows[j]["Value"]);
    }
    if (dataY.Length > 0)
    {
        ScatterPlot Plot1 = new ScatterPlot();
        Plot1 = (ScatterPlot)xyDataGraph.Plots[0].Clone();
        xyDataGraph.Plots.Add(Plot1);
        xyDataGraph.Plots[cplt++].PlotXY(dataX, dataY);
    }
}
  
// mouse click event
protected void xyDataGraph_PlotAreaClick(object sender, ClickEventArgs e)
{

    double xValue, yValue;
    XAxis xAxis1;
    YAxis yAxis1;
    xyDataGraph.Cursors[0].LabelVisible = true;
    xyDataGraph.Cursors[0].Visible = true;
    xyDataGraph.Cursors[0].SnapMode = CursorSnapMode.Floating;
   
    xAxis1 = xyDataGraph.XAxes[0];
    yAxis1 = xyDataGraph.YAxes[0];
    xyDataGraph.Plots[0].InverseMapDataPoint(xyDataGraph.PlotAreaBounds,new PointF(e.X, e.Y), out xValue, out yValue);
    xyDataGraph.Cursors[0].MoveCursor(xValue, yValue);
    xyDataGraph.Cursors[0].SnapMode = CursorSnapMode.NearestPoint;
}

 First the Cursor is on plot[0] and I double click near Plot[1]. The Cursor jumps correctly on Plot[1] on nearest point. after that I click near Plot[0] and I get this error:

XYCursor.Plot and XYCursor do not belong to the same graph.

Additional hint. Plots are always connected together. What is this problem?

See attached photo.

Any help would be great.

Beno.

0 Kudos
Message 1 of 15
(7,628 Views)
Hi Beno,

I'm having trouble reproducing what you describe... I modified the code a little to eliminate the datatypes that you were using but it is basically the same (see attached solution).
 
Also, I am using the PlotAreaMouseDown event.
 
From the look of your screenshot however, it really looks like you only have one plot. This would likely explain both issues.
 
I would check and see if somehow you are putting all the points into a single plot. The easiest way to do this is to put a breakpoint before your 'for loop' and check the value of ChannelGUIDsList.Length. You could also check to see how many times xyDataGraph.Plots.Add(Plot1); is run using a breakpoint.
 
Finally, where exactly (which line) are you getting the error?
Jervin Justin
NI TestStand Product Manager
0 Kudos
Message 2 of 15
(7,600 Views)

Hi.

 

I forgot to tell we use MStudio fro .NET

 

Few minutes ago we found that if the plots are loaded one at a time they are not connected. It’s like this:

Push button - load plot 0 with data

Push button - load plot 1 with data

Push button - load plot 2 with data

 

In test’s we do are around 30 point’s per plot.

 

Source of the error is:    

xyDataGraph.Cursors[0].SnapMode = CursorSnapMode.NearestPoint;

 

What could this be?

 

Thnx,

Beno

0 Kudos
Message 3 of 15
(7,597 Views)

I'm really sorry.

Before  CSQL.GetMeasurementData(.... line of code we have to clear DataTable like this

dtDataTable.Clear();

Now this is solved but we still have problem to get cursor snap to nearest plot and point on this plot.....

                xyDataGraph.Plots.Add(Plot1);

                xyDataGraph.Plots[cplt].PlotXY(dataX, dataY);

If plot data is added this way "snap to" is working but after a few clicks ERROR

 

                xyDataGraph.Plots.Add(Plot1);

                Plot1.PlotXY(dataX, dataY);

Snap to point is not working.

 

Try attached example with both options.

 

Beno

0 Kudos
Message 4 of 15
(7,584 Views)

Hi Beno,

I'm seeing the same behavior on my end (not snapping to the plot), let me research it a little more and I'll get back to you.

I don't see the error message though... Do you get this even when you use a Windows Form instead of a WebForm?

Jervin Justin
NI TestStand Product Manager
0 Kudos
Message 5 of 15
(7,569 Views)

Hi.

When I use windows forms it's working superb.

Used your example and this one http://forums.ni.com/ni/board/message?board.id=232&message.id=1580&query.id=18839#M1580

It has, also, allot more functionality as in asp.net. Smiley Mad

Thank you for your help.

Beno

0 Kudos
Message 6 of 15
(7,559 Views)

Hi Beno,

Glad you were able to get your application up and running.

I did find out what was wrong in the previous case though. A xyGraph has an empty plot on it by default, and this is xyDataGraph.Plots[0], and your cursor is by default mapped to this plot.

When you do

xyDataGraph.Plots.Add(Plot1);
xyDataGraph.Plots[cplt].PlotXY(dataX, dataY);

you are actually adding a 2nd plot, but then plotting the data to the 1st one. The snap works in this case because there is data on the 1st ([0]th) plot.

When you do

xyDataGraph.Plots.Add(Plot1);
Plot1.PlotXY(dataX, dataY);

you are adding a 2nd plot, but the first plot is empty, therefore the snap does not work. You can fix this by removing the 1st (default) plot and remapping the cursor to the new plot like this:

xyDataGraph.Plots.Remove(scatterPlot1);
xyDataGraph.Cursors[0].Plot = xyDataGraph.Plots[0];

See the attached program.

Jervin Justin
NI TestStand Product Manager
0 Kudos
Message 7 of 15
(7,546 Views)

Thnx.

Now we use this:

                ScatterPlot Plot1 = new ScatterPlot();

                Plot1 = (ScatterPlot)xyDataGraph.Plots[0].Clone();

                xyDataGraph.Plots.Add(Plot1);

                Plot1.PlotXY(dataX, dataY);

 

I added your code on the end of "for" loop, but i still get this error:

                XYCursor.Plot and XYCursor do not belong to the same graph.

 

I had to change suggested code to this:

                xyDataGraph.Plots.Remove(xyDataGraph.Plots[0]);

                xyDataGraph.Cursors[0].Plot = xyDataGraph.Plots[0];

 

Don't know how else to get the first plot of scattergraph in asp.net.

Should I do it differently?

 

Beno

0 Kudos
Message 8 of 15
(7,543 Views)

I have some new information to share.

 

If scattergraph is loaded on "PageLoad" we don't get error with snap cursor to nearest point.

But this we can't use because zoom is reset.

 

Attached are two projects.

 

First, with button to load graph and error after few clicks that move cursor around plots and second one with graph load on page load and no error till now.

Cursor error could not be reproduced.

We really need it to work with "the button".

 

Is it possible to be some timeout problem with graph data?

Can we make a workaround so the graph is reloaded after every cursor move but the zoom and other settings stay unchanged?

 

Thanks.

 

 

0 Kudos
Message 9 of 15
(7,431 Views)
Hi Beno,
 
It looks like the NoError copy of the project isn't totally correct either. I ran it on my end and what it does is alternate which plot the cursor snaps to. For instance if you click on the same point multiple times, it cycles between all three plots.
 
Also, I noticed that you assign the colors incorrectly (you set cplt to 0 and then check 1, 2 and 3... You should be checking 0,1 and 2)
 
Finally I found something that might be the source of the problem, I'll have to investigate a little further...
I noticed that when I change
xyDataGraph.Cursors[0].Plot = xyDataGraph.Plots[0];    to
xyDataGraph.Cursors[0].Plot = xyDataGraph.Plots[1]; //Changed Plot #
it always crashes even on the first click... I will look into this a little more, I just wanted to give you a heads up on what I found.
Jervin Justin
NI TestStand Product Manager
0 Kudos
Message 10 of 15
(7,392 Views)