Binde Entire SfChart to viewmodel, not just series collection

Hello,
I wonder if it is possible to bind the SfChart element to a representation in the view model? I know I can bind the seriescollection but I wan to be able to add axis, series, header title, etc dynamically in code. Something like this:



Then on my VM

public SfChart Chart {get;}

Chart.Series.add(...);
Chart.PrimaryAxis = ...
Chart.Legend =....

Any hints are appreciated =]


3 Replies

MK Muneesh Kumar G Syncfusion Team March 29, 2018 07:41 AM UTC

Hi Juliano, 
 
Thanks for using Syncfusion products.  
 
We can achieve your requirement by setting SfChart as content of ContentControl and creating chart in view model. Using this solution you can able to add axis, series, header to chart dynamically. Please refer the below code snippet. 
 
Code snippet [XAML]: 
 
<Grid > 
        <Grid.DataContext> 
            <local:ViewModel/> 
        </Grid.DataContext> 
        <ContentControl Content="{Binding Chart}"/> 
 
    </Grid> 
 
 
Code snippet [C#]: 
 
public SfChart Chart { get; set; } 
        public ViewModel() 
        { 
            GenerateData(); 
 
            Chart = new SfChart(); 
            Chart.PrimaryAxis = new NumericalAxis(); 
            Chart.SecondaryAxis = new NumericalAxis(); 
 
            SplineAreaSeries series = new SplineAreaSeries(); 
            series.XBindingPath = "XValue"; 
            series.YBindingPath = "YValue"; 
            series.ItemsSource = Data; 
 
            Chart.Series.Add(series); 
        } 
 
 
We have prepared a sample based on this, please find the sample from the following location. 
 
 
Please let us know if you have any queries.  
 
Thanks, 
Muneesh Kumar G. 



JF Juliano Franz March 29, 2018 12:02 PM UTC

Perfect, thank you!
I was struggling to figure out what was the correct property. Content it is =]


MK Muneesh Kumar G Syncfusion Team March 29, 2018 01:56 PM UTC

Hi Juliano,  
 
Thanks for the update.  
 
We are glad to know that the given solution works. Please let us know if you need any further assistance. 
 
Thanks, 
Muneesh Kumar G. 
 


Loader.
Up arrow icon