Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Multiple Varying Length Plots in CSharp

What is the best method of plotting variable length *and* variable increment data. The data that we are dealing with never starts with zero, may have a fixed increment, but most often won't. For example, one x-y data set will range from 30000 to 300000 with a fixed increment of 500. The next x-y data set will contain only 20 x-y points, not evenly spaced. What is the best method of plotting this data simultaneously? What is the best NI graphing control. Ideally, this data will be read in from files, so how would I go about keeping track of which plots came from which file? Do the graphing controls automatically scale to the data in the plots? Also, do the graphing controls automatically change the color of the plots? I
couldn't find any samples that really covered this type of application in C# or VB.NET. I understand this is a loaded question, but I do have familiarity with the v7.0 controls and I really need a fresh view on this. Any help is greatly appreciated!

Thanks,
Derek
0 Kudos
Message 1 of 9
(5,588 Views)
What is the best method of plotting this data simultaneously?

If you're using the WaveformGraph, you can use the WaveformPlot.PlotYAppend(double[] yData, double increment) method. Using this WaveformPlot method with the WaveformGraph is the easiest way to do this as long as your data is linear. Otherwise, you can use the ScatterGraph and use the ScatterPlot.PlotXYAppend(double[] xData, double[] yData) method and explicitly specify both the X and Y values.

Ideally, this data will be read in from files, so how would I go about keeping track of which plots came from which file?

I don't think I understand the question. Could you please provide more information about this?

Do the graphing controls automatically scale to the data in
the plots?


You can configure this independently on each axis. If you want this behavior, set the XAxis/YAxis Mode property to AxisMode.AutoScaleLoose or AxisMode.AutoScaleExact. The default value for both XAxis.Mode and YAxis.Mode when you drop a graph on a Form is AxisMode.AutoScaleLoose.

Also, do the graphing controls automatically change the color of the plots?

The graph controls do not automatically do this, but there are ways to easily get this behavior. Please post an example scenario of what you're trying to do and I'll post an example that demonstrates how to achieve the desired effect.

- Elton
Message 2 of 9
(5,588 Views)
Hi Elton,

Thanks for the help! Here are answers to your questions and a few more (sorry).

Is there any disadvantage of using a ScatterGraph over a WaveformGraph?

I was trying to figure out how to differentiate my x-y data sets once they were added to the graph. I now realize that I could use the Tag property to set a unique identifier for each x-y plot, so I could track each one for easy removal.

I'm a little concerned about how to change the colors for each plot automatically. For example, say I read in five files of x-y data and I want to plot them all simultaneously. As I create and add each plot, I need a scheme of not using the same color for any plot. I'm assuming that I would want to re-use colors when a plot was removed. What do you r
ecommend for this situation?

Thanks,
Derek
0 Kudos
Message 3 of 9
(5,588 Views)
Is there any disadvantage of using a ScatterGraph over a WaveformGraph?

It depends on your data. If your data is linear and has a consistent increment between points, WaveformGraph is easier. If you have X/Y data pairs or you have non-linear data, ScatterGraph is easier.

As I create and add each plot, I need a scheme of not using the same color for any plot. I'm assuming that I would want to re-use colors when a plot was removed. What do you recommend for this situation?

There are several ways you could do this. One suggestion would be to create a list of colors that you want to use for your plots and take a color off the list and assign it to the LineColor property of the plot when it's added to the collection. For example, you could create a Queue of colors in your form like this:

private static readonly Queue plotColors = new Queue();

static MyForm()
{
plotColors.Enqueue(Color.Red);
plotColors.Enqueue(Color.LightBlue);
plotColors.Enqueue(Color.Yellow);
plotColors.Enqueue(Color.Orange);
plotColors.Enqueue(Color.Green);
// Add additional colors ...
}

Then you could add an event handler for the graph's PlotsChanged event and handle the assignment of the plot color like this:

private void OnPlotsChanged(object sender, CollectionChangeEventArgs e)
{
XYPlot plot = e.Element as XYPlot;

Debug.Assert(plot != null, "CollectionChangeEventArgs.Element is not an XYPlot.");
Debug.Assert(plotColors.Count > 0, "The queue of plots colors is empty.");

switch (e.Action)
{
case CollectionChangeAction.Add:
// Pick the next color in the queue for the added plot.
plot.LineColor = (Color)plotColors.Dequeue();
break;

case CollectionChangeAction.Remove:
// Put the removed plot color back into the queue.
plotColors.Enqueue(plot.LineColor);
break;

default:
break;
}
}

Now when you add plots to the graph's Plots collection, new differentiating colors should automatically be assigned to the plot, and when you remove plots from the graph's Plots collection, the color should be added back to the queue and can be recycled in a future plot. Hope this helps.

- Elton
0 Kudos
Message 4 of 9
(5,588 Views)
Hi Elton,

Thanks for the excellent code! I'll have to try this out. I wish someone would write a book on using Measurement Studio controls. Sometimes I feel like it's explore and hit or miss...

Thanks again,
Derek
0 Kudos
Message 5 of 9
(5,588 Views)
There are conceptual topics in the Measurement Studio Reference that you may find useful. Take a look at Using the Measurement Studio .NET Class Libraries for starters. You can also find this in the table of contents by navigating to NI Measurement Studio Help->NI Measurement Studio .NET Class Library->Using the Measurement Studio .NET Class Libraries. In particular you may find the Using the Measurement Studio Graph .NET Controls section useful.

There will be many more topics added to these sections in upcoming releases. If there are any particular topics that you would like to see, please post a reply to this message with
your list and they will be taken into consideration as the new topics are developed. Thanks.

- Elton
0 Kudos
Message 6 of 9
(5,588 Views)
Hi Elton,

I have another question about this solution. Our data is not directly plotted, rather *three* separate calculations are plotted on three separate tab pages with a waveformgraph on each tab page. Think of plotting position on the first graph, velocity on the second, and acceleration on the third. The catch is that all three plots must have the same color that the calculations were based on (original x-y data). How would this scheme work in this case?

Thanks again!
Derek
0 Kudos
Message 7 of 9
(5,588 Views)
Hi Elton,

I've reviewed the help, but it's not always easy to infer a solution from a reference. How many programming books have you purchased because Microsoft's help didn't cut it. To be honest, I buy the books because of the author's experience and wisdom and this kind of information is invaluable. I think of Chris Sells' recent WinForms book. Everything he wrote about is in the help somewhere, but he eloquently puts it all together with some really great examples. This is similar to the code that you demostrated above based on your experience.

I guess after my ramblings, I would like to see a book with "real world" (sorry for the cliche) examples. Or focused articles like you would see in a magazine like MSDN
, Visual Studio, etc.

And can someone please update the NI website to include a C# option for support!

Thanks,
Derek
0 Kudos
Message 8 of 9
(5,588 Views)
The same idea could work, but you wouldn't want to use the PlotsChanged event. You could still use the Queue for holding the colors, but then assign the LineColor property at the time that you create the plots and add them to the collection, and similarly add them back to the queue when you remove the plots from the collection. For example (assuming that you're using the WaveformGraph):

// Adding plots ...
WaveformPlot plot1 = new WaveformPlot(graph1.XAxes[0], graph1.YAxes[0]);
WaveformPlot plot2 = new WaveformPlot(graph2.XAxes[0], graph2.YAxes[0]);
WaveformPlot plot3 = new WaveformPlot(graph3.XAxes[0], graph3.YAxes[0]);

// ...
plot1.LineColor = plot2.LineColor = plot3.LineColor = (Color)plotColors.Dequeue();
graph1
.Plots.Add(plot1);
graph2.Plots.Add(plot2);
graph3.Plots.Add(plot3);

// Removing plots ...
graph1.Plots.Remove(plot1);
graph2.Plots.Remove(plot2);
graph3.Plots.Remove(plot3);
plotColors.Enqueue(plot1.LineColor);

- Elton
0 Kudos
Message 9 of 9
(5,588 Views)