silverlight 在运行时以编程方式简单填充 lineSeries
我有一个名为“lineSeries1”的折线系列图表。
我希望在运行时根据两个变量填充和更新该图表。 “currentPos”将是 X 轴,“budget”将是 Y 轴。此外,“预算”可以在运行时更改值。
基本上,我只想知道如何以编程方式设置图表中的值。例如,每当创建新的 currentPos/budget 时,或者修改现有预算时。
我怎样才能在 Silverlight C# 中实现这一点?网上的文档很少...
编辑:经过一些研究,我发现了这个教程: http://www.a2zdotnet.com/View.aspx?Id=136。受到该网站的一些启发,我想出了自己的代码,但它不起作用...
这是我的 XAML:
<toolkit:Chart x:Name="theChart">
<toolkit:LineSeries x:Name="lineSeries1" DependentValuePath="CurrentPos" IndependentValuePath="Budget"></toolkit:LineSeries>
</toolkit:Chart>
这是我的类:
public class oneEvent
{
private int _CurrentPos;
private int _Budget;
public oneEvent(int currentPos, int budget)
{
_CurrentPos = currentPos;
_Budget = budget;
}
public int CurrentPos
{
get { return _CurrentPos; }
set { _CurrentPos = value; }
}
public int Budget
{
get { return _Budget; }
set { _Budget = value; }
}
}
这是我填充折线图的代码:
public MainPage()
{
InitializeComponent();
List<oneEvent> list = new List<oneEvent>();
list.Add(new oneEvent(0, 8000));
list.Add(new oneEvent(1, 9000));
list.Add(new oneEvent(2, 10000));
list.Add(new oneEvent(3, 11000));
list.Add(new oneEvent(4, 12000));
list.Add(new oneEvent(5, 9000));
list.Add(new oneEvent(6, 500));
list.Add(new oneEvent(7, 1000));
try
{
lineSeries1.ItemsSource = list;
}
catch (System.Exception excep)
{
MessageBox.Show(excep.Message);
}
}
当我运行此代码时,我收到一条消息框显示“对象引用未设置到对象的实例”。我做错了什么?
编辑: 好的,解决了我的问题。 而不是编写 lineSeries1.ItemsSource = list;
, 我应该将 LineSerieslineseries = theChart.Series[0] 写为 LineSeries; lineseries.ItemsSource = 列表; 我不知道为什么,但它有效......
I have a line series chart called "lineSeries1".
I would like that chart to be filled and updated during runtime, depending of two variables. "currentPos" would be the X axis, and "budget" would be the Y axis. Also, "budget" is possible to change value during runtime.
Basically, I would just like to know how to set values in the chart, programmatically. For example, whenever a new currentPos/budget is created, or when an existing budget is modified.
How can I pull that off in Silverlight C#? there is very little documentation on that online...
EDIT: after some research, I came upon this tutorial: http://www.a2zdotnet.com/View.aspx?Id=136. With some inspiration from that site, I came up with my own code, which doesn't work...
Here's my XAML:
<toolkit:Chart x:Name="theChart">
<toolkit:LineSeries x:Name="lineSeries1" DependentValuePath="CurrentPos" IndependentValuePath="Budget"></toolkit:LineSeries>
</toolkit:Chart>
Here's my class:
public class oneEvent
{
private int _CurrentPos;
private int _Budget;
public oneEvent(int currentPos, int budget)
{
_CurrentPos = currentPos;
_Budget = budget;
}
public int CurrentPos
{
get { return _CurrentPos; }
set { _CurrentPos = value; }
}
public int Budget
{
get { return _Budget; }
set { _Budget = value; }
}
}
and here's my code which populates the lines chart:
public MainPage()
{
InitializeComponent();
List<oneEvent> list = new List<oneEvent>();
list.Add(new oneEvent(0, 8000));
list.Add(new oneEvent(1, 9000));
list.Add(new oneEvent(2, 10000));
list.Add(new oneEvent(3, 11000));
list.Add(new oneEvent(4, 12000));
list.Add(new oneEvent(5, 9000));
list.Add(new oneEvent(6, 500));
list.Add(new oneEvent(7, 1000));
try
{
lineSeries1.ItemsSource = list;
}
catch (System.Exception excep)
{
MessageBox.Show(excep.Message);
}
}
When I run this, I get a message box saying "object reference not set to an instance of an object". What am I doing wrong?
EDIT:
ok, figured out my problem.
Instead of writing lineSeries1.ItemsSource = list;
,
I should've written LineSeries lineseries = theChart.Series[0] as LineSeries;
lineseries.ItemsSource = list;
I don't know why, but it works...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,解决了我的问题。 我不应该写
lineSeries1.ItemsSource = list;
,而应该写
LineSerieslineseries = theChart.Series[0] as LineSeries;
lineseries.ItemsSource = list;
我不知道为什么,但它有效......
ok, figured out my problem. Instead of writing
lineSeries1.ItemsSource = list;
, I should've written
LineSeries lineseries = theChart.Series[0] as LineSeries;
lineseries.ItemsSource = list;
I don't know why, but it works...