0
votes

I am a new-bee to WPF, I want to know how to add charts by using WPF data visualization toolkit from behind code.

public void LoadChart()
    {
        List<KeyValuePair<string, int>> valueList = new List<KeyValuePair<string, int>>();
        valueList.Add(new KeyValuePair<string, int>("FreeSpace", 60));
        valueList.Add(new KeyValuePair<string, int>("UsedSpace", 20));

        List<KeyValuePair<string, int>> list = new List<KeyValuePair<string, int>>();
        valueList.Add(new KeyValuePair<string, int>("x", 40));
        valueList.Add(new KeyValuePair<string, int>("y", 400));
        PieSeries pie = new PieSeries();


        pie.Title = "Disk Memory";
        pie.ItemsSource = valueList;
        pie.IndependentValuePath = "Key";
        pie.DependentValuePath = "Value";
        chart.Series.Add(pie);

        Chart x = new Chart();

        PieSeries series = new PieSeries();
        series.Title = "Disk Memory";
        series.ItemsSource = list;
        series.IndependentValuePath = "Key";
        series.DependentValuePath = "Value";
        x.Series.Add(series);


    }
}

wherein I am trying to add both charts to a grid. How can we add?

1

1 Answers

0
votes

If you have defined the Grid in your XAML markup, you can give it an x:Name:

<Grid x:Name="theGrid" />

You can then add the charts to the Grid's Children collection programmatically:

theGrid.RowDefinitions.Add(new RowDefinition()); //add a row the Grid
theGrid.RowDefinitions.Add(new RowDefinition());
theGrid.Children.Add(chart);
Grid.SetRow(1, x); //add "x" to the second row
theGrid.Children.Add(x);