2

我正在尝试使用 wpf 工具包图表线,并且我需要在图表中有不止一条线,但我不知道该怎么做在 c# 中动态执行。在程序中,我不知道我需要多少图表以及每个图表中有多少行,这是我在 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);

}

我试过这个,但它不工作......

请帮忙!:(

4

1 回答 1

5

XAML:

<chartingToolkit:Chart Name="lineChart" />

代码隐藏:

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

您可以将 valueList 定义为: List<KeyValuePair<string, int>> valueList = new List<KeyValuePair<string, int>>();

并将所需的值插入为valueList.Insert(0, new KeyValuePair<string, int>(key, value));

于 2013-03-06T17:27:45.900 回答