如何在 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
How to add dynamically more line series into a line chart in wpf toolkit?
提问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));

