我尝试从字典集合中设置图表 XValueMember 和 YValueMember:
public partial class GraphWindow : Window
{
Dictionary<int, double> value;
public GraphWindow(Dictionary<int, double> values)
{
InitializeComponent();
value = new Dictionary<int, double>(values);
Plot();
}
public void Plot()
{
Chart chart = this.FindName("Chart") as Chart;
if (chart != null)
{
chart.DataSource = value;
chart.Series["series"].XValueMember = "Key";
chart.Series["series"].YValueMembers = "Value";
}
}
}
轴已构建,但为空 ( screenshot )。之前,我尝试使用KeyValuePair,但chart不支持它。如何从图表格式的字典中获取值和键?感谢您的帮助!
最佳答案
您只缺少一个小piece谜题:
chart.DataSource = value;
chart.Series[0].XValueMember = "Key";
chart.Series[0].YValueMembers = "Value";
chart.DataBind();
最后一行将在其他人设置后建立DataBinding
。
每当您的数据发生更改时,您都需要再次调用它。
注意:根据上面的 MSDN 链接..:
In cases where a data source is set to a chart and no other data operations are required, the DataBind method does not have to be explicitly called. In these cases, the Chart itself will data bind to the data source prior to being rendered.
.. 事实上,您可以通过调用
来强制渲染chart.Refresh();
但是由于字典需要设置值成员某些东西似乎是使其工作所必需的..
使用这些数据
value.Clear();
value.Add(1,12);
value.Add(2,32);
value.Add(3,52);
value.Add(6,42);
此图表结果:
关于c# - 如何从字典中设置图表 XValue 和 YValue 成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35607905/