11-16-2011 02:34 PM - edited 11-16-2011 02:37 PM
Hello there i need help visual .net c# :
I want to know arithmetic mean of ploty values, between two user selected points.
I think selection of points will be easy with cursor, when user click on data point, cursor will snap pointy.
But how i can select whole data points in these user selected region and get arithmetic mean of these y values?
Thanks for your kindly help.
Solved! Go to Solution.
11-16-2011 03:37 PM
Hello there,
I quickly worked on what you had asked and got it working for me. Here is the code sample.
Things to note:
private void xyCursor1_AfterMove(object sender, NationalInstruments.UI.AfterMoveXYCursorEventArgs e)
{
double value = GetAverage();
label1.Text = value.ToString(); //Use the average value to display
}
private void xyCursor2_AfterMove(object sender, NationalInstruments.UI.AfterMoveXYCursorEventArgs e)
{
double value = GetAverage();
label1.Text = value.ToString(); //Use the average value to display
}
// Method that calculates avg (or something that you want)
private double GetAverage()
{
// Note that the SnapMode of both cursors are set to ToPlot.
int startIndex = (int)(xyCursor1.XPosition * waveformPlot1.DefaultIncrement);
int endIndex = (int)(xyCursor2.XPosition * waveformPlot1.DefaultIncrement);
if (startIndex > endIndex)
{
int temp = endIndex;
endIndex = startIndex;
startIndex = temp;
}
double[] data = waveformPlot1.GetYData();
data = GetMyDataInterval(data, startIndex, endIndex);
double value = CalculateAverage(data);
return value;
}
// Method to do some arithmetic on the array values
private double CalculateAverage(double[] data)
{
// Here you performe the calculation of Average or Mean or what ever you want
double avg = 0;
for (int i = 0; i < data.Length; i++ )
{
avg += data[i];
}
avg = avg / data.Length;
return avg;
}
// Method that returns valid data between two indices
private double[] GetMyDataInterval(double[] originalData, int startIndex, int endIndex)
{
double[] data = new double[endIndex - startIndex + 1];
for (int i = startIndex; i <= endIndex; i++)
{
data[i - startIndex] = originalData[i];
}
return data;
}
I hope this helps for you. Let us know if you wanted something else.
Vijet Patankar
National Instruments
11-17-2011 06:08 AM - edited 11-17-2011 06:10 AM
Thanks a lot Vijet it works fine. But now i want to give number to regions. At first, user select two points of regions. After selecting regions program gives averages like reg1average, reg2average... In addition, user must see regions after selection. So i want to change regions visual styles. Also there can be labels above regions like area 1, area 2...
For example if there are 8 region, there will be 16 xycursor. It means mess. And how can know region count before user state them? I think after selecting 2 points with xycursor, program must change color and size of points. And finally, i think it will be useful if keep reg1average values in array.
Thanks
11-17-2011 04:44 PM
Hello there,
I saw your requirement requirement and you are right for the fact that using curors is going to be a lot of mess. Becuase when you are dealing with many cursors, it is difficult to pair the curosrs even though the cursors pairs have different colors. It would look bad.
This is the part where you could use the XYRangeAnnotations. The only issue with the annotations that you can only drag the caption but, you cannot resize or move the annotations. However, if you can achieve the resize and move for the annotations, you can make the application look a lot better when you have many annotations in the graph.
Here is the screenshot that I implemented for with the annotations (replacing the cursors).
Misc:
I have attached the code using the annotations.
Hope this helps.
Vijet Patankar,
National Instruments
11-19-2011 12:26 PM
i will try it.
how i can add user clicked data points to double[] ?
11-21-2011 11:37 AM
Use above methods to do all the tricks you want to perform.
For example, To get the x and y data where the mouse clicked, you just use the WaveformPlot.InverseMapDataPoint() method passing the location of the mouse click.
11-23-2011 07:45 AM
I have one question, how i can find maximum and minimum Y points between x1-x2 ?
11-23-2011 07:48 AM
i find it thanks.