09-18-2012 05:40 PM - edited 09-18-2012 06:00 PM
I am trying to display a WPF graph where each data point corresponds to a constant fractional delta along the x asis. E..g. (0.15, 1), (0.30, 5), (0.45, 3.6), ...
My best guess is that I want to plot an array of Points containing values such as above since I do not understand how to inform the graph control that each value to plot represents a delta-X value that is not 1 (e.g. 0.15).
When I try to update the graph's DataSource dependency property via data-binding (e.g. invoking the property-changed event handler with the name of the code-behind property) I get the following exception:
I searched for examples of 2D scatter graphs using the WPF graph control without success. Note that this is a different problem than how to display multiple plots on the same graph.
Thanks in advance for your help!
- Paul.
Solved! Go to Solution.
09-19-2012 09:15 AM
I would need to see some code in order to figure out what the error you are seeing is from. But if you want to plot scattergraph data, try using an array of Points.
Point[] myData = new Point[10]; for (int i = 0; i < 10; i++) myData[i] = new Point(1.5 / 10 * i, i % 3); myGraph.DataSource = myData;
09-19-2012
10:08 AM
- last edited on
11-20-2024
04:38 PM
by
Content Cleaner
Currently, we have one example called "XYPlotting" that shows scatter data in the WPF Graph. You can find this with the WPF examples that were installed with Measurement Studio.
Point is the most common type for providing scatter data, though other types like ComplexDouble or ChartCollection would also work (see How to: Plot and Chart topic for a list of all the supported data types).
Regarding the exception, this is expected behavior during initialization for a data type. Note that this is a "first chance exception", meaning this dialog appears immediately when the exception is thrown, even if it is handled later on. If you continue execution, you will find that the exception is handled and the graph displays the data.
Exception warnings are controlled in Visual Studio through the Exceptions dialog, reached via the Debug » Exceptions... menu. "First chance" corresponds to the Thrown column; User-unhandled represents exceptions that bubble up to your code:
To explain a little more about the reason why the exception occurs: the set of types supported by the graph controls is extensible and not fixed before hand. When a data type is encountered for the first time, we perform a search to determine the best descriptor to use. For the initial release of the WPF controls, we chose to rely on the .NET type system's checking of generic arguments, but since there is no "would this type be valid?" method we have to construct the type and handle any type load failures that occur. Because we cache the result, these exceptions do not appear often, but this is something we plan to improve in the future.
09-19-2012 10:12 AM
I am doing pretty much exactly what your code snippet does.
Here is my markup:
<ni:Graph DataSource="{Binding RgptBeforeX}" Grid.Row="1" Margin="4" Loaded="OnBeforeXGraphLoaded" x:Name="graphBeforeX">
<ni:Graph.Axes>
<ni:AxisDouble Orientation="Horizontal" Adjuster="None" Range="0, 50, System.Double" Label="X Component (Hz)"/>
</ni:Graph.Axes>
<ni:Graph.Plots>
<ni:Plot Label="X">
<ni:LinePlotRenderer Stroke="Red" />
</ni:Plot>
</ni:Graph.Plots>
</ni:Graph>
Here is the code for the RgptBeforeX property used for the data binding:
private Point[] m_rgptBeforeX;
public Point[] RgptBeforeX
{
get
{
return m_rgptBeforeX;
}
set
{
if (m_rgptBeforeX != value)
{
m_rgptBeforeX = value;
NotifyPropertyChanged("RgptBeforeX");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string str)
{
if (PropertyChanged != null)
{
PropertyChanged(this, newPropertyChangedEventArgs(str));
}
}
This all works great when I use an array of doubles for the DataSource but not for the array of points. Maybe its the data binding. To see if the problem could be due to WPF data-binding I changed my code behind property to set the DataSource directly but till see the same problem:
public Point[] RgptBeforeX
{
set
{
if (m_rgptBeforeX != value)
{
m_rgptBeforeX = value;
//NotifyPropertyChanged("RgptBeforeX");
if (m_grfBeforeX != null)
{
m_grfBeforeX.DataSource = m_rgptBeforeX;
}
}
} }
Thanks - Paul.
09-19-2012 10:17 AM
Thanks Paul!
I usually leave first chance excpections as a matter of course to catch exceptions when my code throws them. You are correct that the exception is handled and not returned to my code. Once I get past the exceptions the data displays exactly as I had hoped it would.
- Paul.
08-11-2015
10:35 AM
- last edited on
11-20-2024
04:38 PM
by
Content Cleaner
Just wanted to let you know this issue was fixed in the Measurement Studio 2015 release.