如何在 wpf 工具包中将更多的线系列动态添加到折线图中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15253576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:55:36  来源:igfitidea点击:

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

wpfchartswpftoolkitlinechartlineseries

提问by SH.IN

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...

我正在尝试使用 wpf 工具包图表线,我需要在图表中有不止一条线,但我无法弄清楚如何做到这一点,我试图在此处和谷歌中查看,但我总是找到一个 XAML 代码,我需要在 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);

}

I tried this but it is not working ...

我试过这个,但它不起作用......

please help!:(

请帮忙!:(

回答by Santux

XAML:

XAML:

<chartingToolkit:Chart Name="lineChart" />

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

您可以将 valueList 定义为: 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));

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