We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Export a Chart using MVVM

OK this is probably simple, but I can't seem to wrap my mind around it. I'm using PRISM with a WINRT application that uses SFChart. The chart is created in XAML, it's data is all set via data binding to an observablecollection. Now I see there is a function on the chart that lets me save it using something like chart.save(). I get to thinking that is awesome! I could add a feature to my app that would let me have a user click a button where it saves the chart out, attaches it to an email object and then calls the OS's send email function. Instant ability to send charts to anyone! However, the chart on it's own though isn't very helpful without something that explains it. Luckily I've already got data on the page (also bound from the viewmodel) that I can add to the email to make the chart make sense. So simple steps, call save function, attach to email object, add text to the email from strings that exists in the viewmodel, and send it.
The problem: with MVVM I need to have that button fire off a delegate command which resides in the ViewModel, which knows nothing about the chart...which means I can't call the save function. Now I suppose I could walk the visual tree somehow (which I don't know how to do yet either) and find the chart object so I can call its save function, but now I'm making my viewmodel depend on the view which is not great design. I could could call the save function directly from the code behind, which would be fine except (since I'm not manipulating data for view) but then I have the problem of the fact that I also need to add strings to the email and those strings exist in the ViewModel. Also I don't understand if I do it that way if I have to create the chart in codebehind vs in XAML, or if there is a way to link some sort of chart object to the existing object created in XAML just to use the print function.
What is the "proper" way to accomplish this scenario using MVVM design principles?
Thanks!

2 Replies

JD Jeff Dalby October 30, 2015 08:37 PM UTC

Or maybe this just can't be done because for testing I've added a SfChart object just in code behind, and it doesn't have a Save function on it.  I've tried referencing both the 13.3.0.7 windows 8.1 universal extension or the WP extension, both claim not to have a save or print function.  Is the documentation that says it has one just wrong?


SJ Sumathi Jayaraj Syncfusion Team November 2, 2015 12:51 PM UTC

Hi Jeff,

Thanks for contacting Syncfusion products.

Query 1: MVVM I need to have that button fire off a delegate command which resides in the ViewModel, which knows nothing about the chart.

We can achieve your requirement by setting CommandParameter as per the below code snippet.

Code Snippet [XAML]:


<Button Height="50" Width="100" Margin="3" Content="Save" VerticalAlignment="Center"
                Command="{Binding SaveCommand}" CommandParameter="{Binding ElementName=chart}" Grid.Column="1" HorizontalAlignment="Center"/>



Code Snippet [c#]:


public void Execute(object parameter)
        {
            var chart = parameter as SfChart;
            chart.Save();
        }


We have prepared a sample based on this. You can download the sample from the following location.

Sample: MVVM_WinRTSample

Query 2: windows 8.1 universal extension or the WP extension, both claim not to have a save or print function

At present there is no support to save, and we have logged this as  Feature Request. We have planned to implement this feature in our upcoming main release volume. So we request you to check with the product page in our website regarding this feature in the upcoming release. We will get back to you once this feature got implemented.
Printing API is not available for windows phones.

Please let us know if you have any queries.

Thanks,
Sumathi J

Loader.
Live Chat Icon For mobile
Up arrow icon