Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

How to bind plots on WPF graph

Hi,

Is there a way to bind plots (plot collection) to a graph.

 

I'm using a graph with multiple plots. I bind the datasource  with an observablecollection for the data values => the default plots are shown, perfect.

but now I want to manage these plots (color, axis etc..). For that I have to use plots. I can define manually plots in XAML.

 

but I want to add or remove plots at runtime. The datasource is perfect for data values but I have not found the same for plots. In the MVVM concept, is there a way to bind plots with a collection.

Thanks for any help.

 

O .Daniere

0 Kudos
Message 1 of 6
(7,825 Views)

There is no equivalent to DataSource for plots. There is a DefaultPlotRenderers collection for customizing the default appearance of plots, but nothing for configuring other properties like associated axes.


There is a primitve type available that can be used to synchronize a model collection with e.g. the Plots collection on a graph:


    var plots = new ObservableCollection<Plot>( ... );
    var synchronizer = new NationalInstruments.Controls.Primitives.CollectionViewSynchronizer( );
    synchronizer.SourceCollection = CollectionViewSource.GetDefaultView( plots );
    synchronizer.TargetCollection = graph.Plots;


You could use this in code, or create an attached "GraphExtensions.PlotsSource" dependency property that performs the same action when the property value changes.

~ Paul H
0 Kudos
Message 2 of 6
(7,808 Views)

 

Thanks for your response Paul.

 

I have another question regarding the PlotRenderer binding :

 

Using XAML I can bind the plot color to a class property like this :

<ni:Graph x:Name="MyGraph">

   <ni:Graph.Plots>

      <ni:Plot Label="Plot 1">

         <ni:LinePlotRenderer Stroke="{Binding PlotLineColor}" />

      </ni:Plot>

   </ni:Graph.Plots>

</ni:Graph>

With this code behind in VB.NET 2012 (MyGraphVM contains the PlotLineColor property):

        MyGraph.Plots(0).DataContext = MyGraphVM

 

And it works.

 

 

But, I can't do the same binding by code (for example using Plot.setBinding()).

Do you have an example?

What is the best way to bind plot's properties (color, shape, etc)?

 

O. Daniere.

 

0 Kudos
Message 3 of 6
(7,766 Views)

You can use the BindingOperations.SetBinding method to set a binding on a plot renderer:


    var renderer = plot.Renderer;
    var binding = new Binding( "PlotLineColor" ) { Source = MyGraphVM };
    BindingOperations.SetBinding( renderer, LinePlotRenderer.StrokeProperty, binding );

~ Paul H
0 Kudos
Message 4 of 6
(7,743 Views)

Thanks Paul, it works perfectly.

 

O. Daniere

0 Kudos
Message 5 of 6
(7,718 Views)

By the way, I have created the "GraphExtensions.PlotsSource" example I mentioned earlier in an answer to another question.

~ Paul H
0 Kudos
Message 6 of 6
(7,641 Views)