0

I have two WPF toolkit charts (column and pie).

<-- Column Chart -->

<DVC:Chart Name="mcChartPie" Title="{Binding ChartName}"       
DataContext="{Binding SelectedChart}">
<DVC:Chart.Series>
<DVC:PieSeries ItemsSource="{Binding Columns}" Title="Some Chart"
IndependentValueBinding="{Binding Path=Name}" DependentValueBinding="{Binding 
Path=Value}"></DVC:PieSeries>
</DVC:Chart.Series>
</DVC:Chart>

<-- Pie Chart -->

    <DVC:Chart Name="mcChart" Title="{Binding ChartName}"    
DataContext="{Binding SelectedChart}" Style="{DynamicResource Info>
<DVC:Chart.Series>
<DVC:ColumnSeries ItemsSource="{Binding Columns}" Title="Some Chart"  
IndependentValueBinding="{Binding Path=Name}" DependentValueBinding="{Binding 
Path=Value}" Background="Black" AnimationSequence="FirstToLast" ></DVC:ColumnSeries>
</DVC:Chart.Series>
</DVC:Chart>

There is a combobox that allows user to select the chart type. When user selects the "Column Chart Type", the column chart template should be visible and when user selects "Pie Chart Type", the pie chart template should be visible. How can I do this by overriding SelectTemplate() method in the DataTemplateSelector class?

  • The only one way is to create 2 usercontrols with 2 datatemplates and switch them. How to use the `DataTemplateSelector` class I've described in my answer to this question: http://stackoverflow.com/questions/5309099/changing-the-view-for-a-viewmodel/5310213#5310213. – vortexwolf Nov 03 '11 at 12:19
  • Can you write the code that match to series types? – sari k Jun 11 '15 at 10:39

1 Answers1

0

Try something like this, just a simple method which creates a new serie (Pie, Bar, Column, Lines , Area), and then adds the serie to the chart control after clearing the previous serie loaded on the chart control.

Regards

    void loadPieSerie()
    {
        System.Windows.Controls.DataVisualization.Charting.PieSeries pieSerie = new System.Windows.Controls.DataVisualization.Charting.PieSeries();
        chartControl.Series.Clear();

        List<KeyValuePair<string, int>> valueList= new List<KeyValuePair<string, int>>();
        valueList.Add(new KeyValuePair<string, int>("Dogs", 50));
        valueList.Add(new KeyValuePair<string, int>("Cats", 20));
        valueList.Add(new KeyValuePair<string, int>("Dinosaurs", 30));
        pieSerie.DependentValuePath = "Value";
        pieSerie.IndependentValuePath = "Key";
        pieSerie.ItemsSource = values;
        chartControl.Series.Add(pieSerie);
    }
Wolver
  • 36
  • 3