0

I am running into this problem which I cannot think of an easy way to solve.

Here is my XAML

    <toolkit:Chart Height="250" x:Name="ProductSalesChart" >
        <toolkit:Chart.Series>
            <toolkit:StackedBarSeries>
                <toolkit:SeriesDefinition Title="Series1" ItemsSource="{Binding ProductA}"  IndependentValueBinding="{Binding Milestone}" DependentValueBinding="{Binding Sales}">
                </toolkit:SeriesDefinition>
                <toolkit:SeriesDefinition Title="Series2" ItemsSource="{Binding ProductB}"   IndependentValueBinding="{Binding Milestone}" DependentValueBinding="{Binding Sales}">
                </toolkit:SeriesDefinition>

                <toolkit:StackedBarSeries.IndependentAxis>
                    <toolkit:CategoryAxis Orientation="X"></toolkit:CategoryAxis>
                </toolkit:StackedBarSeries.IndependentAxis>

                <toolkit:StackedBarSeries.DependentAxis>
                    <toolkit:LinearAxis Orientation="Y"  ShowGridLines="True"></toolkit:LinearAxis>
                </toolkit:StackedBarSeries.DependentAxis>

            </toolkit:StackedBarSeries>
        </toolkit:Chart.Series>
    </toolkit:Chart>

So basically, its a stacked chart with milestones on X-axis and Number of sales for each product on Y-axis. I got this working except that the products I retrieve comes from database and it can get changed. In my chart above I have hardcoded it to two (Series1 and Series2). Is there any way to bind this series to a View model object so that it can automatically go up or down ?

Few options based on my findings:

a) Access the chart control from view model and programmatically add series. But doesn't this break MVVM. Also, how can I get a reference to the chart control from view model ? Is there any API for this ? Using event triggers is not working. (at least for the chart OnLoaded)

b) Create your own Chart class inheriting the basic one. How easy is this ? Any good sample ?

Any other ideas ?

Frank Q.
  • 6,001
  • 11
  • 47
  • 62

1 Answers1

-1

I had real problems finding decent documentation when trying to use the ToolKit charts, specifically with regards to binding and how to use in an MVVM way. All examples were pretty much XAML defined series (like yours), or binding fairly trivial aspects of the chart rather than the data.

I don't have all your answers, because I had the same problems, but maybe can help just because I managed to get something working in a datadriven way.

I used Graphite Charts http://www.graphitecharts.com/ mainly because they had decent documentation and so I could actually get started and be productive without trying to sort out the basics of what each class was for and how to use them.

(a) I performed all my data access in the view model, responding to commands from the view. Other actions on the view though would be handled by the code-behind of the view, and it was these actions that would create new series collections, series and data-points from the data exposed by the view model. I never found a way to bind, for example, a series collection to a VM property, but building a series in the code-behind worked just fine. In fact it is probably a cleaner separation as a series (and data point) are very much view objects.

(b) I didn't try to implement my own. Try graphite (or stick with the silverlight one) and try the approach of getting data using VM but building the chart in the view.

Mashton
  • 6,037
  • 2
  • 25
  • 35