2

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!:(

kmatyaszek
  • 19,016
  • 9
  • 60
  • 65
SH.IN
  • 71
  • 2
  • 11

1 Answers1

5

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));

Santux
  • 367
  • 1
  • 3
  • 13
  • but my question is how to add more then one line in the same chart , in my code you can see that i have done already what you want me to do . – SH.IN Mar 06 '13 at 17:32
  • Something like http://windowsphone7developerguide.blograby.com/files/2012/08/27-8-2555-16-58-15.jpg ? – Santux Mar 06 '13 at 17:41
  • Keep in mind, you should call the showChart (ok, the name is not the best one, it should be something like addLineSeries) as many times as many line series you pretend – Santux Mar 06 '13 at 18:15