Search code examples
wpfchartswpftoolkitlinechartlineseries

How to add dynamically more line series into a line chart in wpf toolkit?


I'm trying to use wpf toolkit chart line and I need to have more then one line in the chart but i cant figer out how to do that I tried to look in here and Google but I always found an XAML code and I need to do it dynamically in c#. In the program I cant know how much charts i will need and how many line in every chart this is way I can't do it in the XAML...

for (int j = 0; j < 4; j++) //its just so i cant check it
{
    ColumnDefinition cd = new ColumnDefinition();
    myGrid.ColumnDefinitions.Add(cd);

    Chart chart1 = new Chart();
    LineSeries lineChart = new LineSeries();
    chart1.Height = 200;
    chart1.Width = 300;
    chart1.Title = (j);

    //((LineSeries)chart1.Series[0]).ItemsSource = valueList;

    lineChart.DependentValuePath = "Value";
    lineChart.IndependentValuePath = "Key";
    lineChart.ItemsSource = valueList;
    lineChart.IsSelectionEnabled = true;
    chart1.Series.Add(lineChart);
   lineChart.ItemsSource = valueList1;
    chart1.Series.Add(lineChart); <---

    myGrid.Children.Add(chart1);
    Grid.SetColumn(chart1, (j));  
    Grid.SetRow(chart1, 0);

}

I tried this but it is not working ...

please help!:(


Solution

  • XAML:

    <chartingToolkit:Chart Name="lineChart" />

    Code-behind:

    private void showChart(List<KeyValuePair<string, int>> valueList)
        {
            LineSeries lineSeries1 = new LineSeries();
            lineSeries1.Title = "Title";
            lineSeries1.DependentValuePath = "Value";
            lineSeries1.IndependentValuePath = "Key";
            lineSeries1.ItemsSource = valueList;
            lineChart.Series.Add(lineSeries1);
        }
    

    Where you can define valueList as: List<KeyValuePair<string, int>> valueList = new List<KeyValuePair<string, int>>();

    and insert the desired values as valueList.Insert(0, new KeyValuePair<string, int>(key, value));