如何在 Silverlight 4 中实时更新图表值
我有一个像这样创建的 LineChart:
<Grid.Resources>
<local:EngineMeasurementCollection x:Key="EngineMeasurementCollection"/>
</Grid.Resources>
<charting:Chart x:Name="ahorasi" Title="Engine Performance" Margin="-2,0,384,0">
<!-- Power curve -->
<charting:LineSeries
Title="Power"
ItemsSource="{StaticResource EngineMeasurementCollection}"
IndependentValueBinding="{Binding Speed}"
DependentValueBinding="{Binding Power}"
>
<!-- Vertical axis for power curve -->
<charting:LineSeries.DependentRangeAxis>
<charting:LinearAxis
Orientation="Y"
Title="Power (hp)"
Minimum="0"
Maximum="250"
Interval="50"
ShowGridLines="True"/>
</charting:LineSeries.DependentRangeAxis>
</charting:LineSeries>
<!-- Torque curve -->
<charting:LineSeries
Title="Torque"
ItemsSource="{StaticResource EngineMeasurementCollection}"
IndependentValueBinding="{Binding Speed}"
DependentValueBinding="{Binding Torque}">
<!-- Vertical axis for torque curve -->
<charting:LineSeries.DependentRangeAxis>
<charting:LinearAxis
Orientation="Y"
Title="Torque (lb-ft)"
Minimum="50"
Maximum="300"
Interval="50"/>
</charting:LineSeries.DependentRangeAxis>
</charting:LineSeries>
<charting:Chart.Axes>
<!-- Shared horizontal axis -->
<charting:LinearAxis
Orientation="X"
Title="Speed (rpm)"
Interval="1000"
ShowGridLines="True"/>
</charting:Chart.Axes>
</charting:Chart>
然后
public class EngineMeasurementCollection : ObservableCollection<EngineMeasurement>
{
public EngineMeasurementCollection()
{
Add(new EngineMeasurement { Speed = 1000, Torque = 100, Power = 20 });
Add(new EngineMeasurement { Speed = 2000, Torque = 160, Power = 60 });
Add(new EngineMeasurement { Speed = 3000, Torque = 210, Power = 125 });
Add(new EngineMeasurement { Speed = 4000, Torque = 220, Power = 160 });
Add(new EngineMeasurement { Speed = 5000, Torque = 215, Power = 205 });
Add(new EngineMeasurement { Speed = 6000, Torque = 200, Power = 225 });
Add(new EngineMeasurement { Speed = 7000, Torque = 170, Power = 200});
}
}
public class EngineMeasurement
{
public int Speed { get; set; }
public int Torque { get; set; }
public int Power { get; set; }
}`
当我运行我的项目时,图表工作完美,显示在构造函数中创建的值,但这是硬编码的,我找不到添加或删除元素来更新图表的方法。
我什至不明白为什么图表显示构造函数中提供的项目,是否创建了 EngineMeasurementCollection 的实例?自动地?我不创建新的 EngineMeasurementCollection。
我尝试过类似的操作:
EngineMeasurement littleitem = new EngineMeasurement();
EngineMeasurementCollection fullitems = new EngineMeasurementCollection();
littleitem.Power = 10;
littleitem.Speed = 1000;
littleitem.Torque = 50;
fullitems.Add(littleitem);
//Up to this point everything works perfect, now I want to update my chart with fullitem values in whatever possible way.
我不能做 ahorasi.ItemsSource = fullitems 因为 .ItemsSource 不存在,我真的很困惑,因为图表在刚刚创建时效果很好,具有硬编码值,我认为更新它必须只是一个步骤,但找不到如何。
I have a LineChart created like this:
<Grid.Resources>
<local:EngineMeasurementCollection x:Key="EngineMeasurementCollection"/>
</Grid.Resources>
<charting:Chart x:Name="ahorasi" Title="Engine Performance" Margin="-2,0,384,0">
<!-- Power curve -->
<charting:LineSeries
Title="Power"
ItemsSource="{StaticResource EngineMeasurementCollection}"
IndependentValueBinding="{Binding Speed}"
DependentValueBinding="{Binding Power}"
>
<!-- Vertical axis for power curve -->
<charting:LineSeries.DependentRangeAxis>
<charting:LinearAxis
Orientation="Y"
Title="Power (hp)"
Minimum="0"
Maximum="250"
Interval="50"
ShowGridLines="True"/>
</charting:LineSeries.DependentRangeAxis>
</charting:LineSeries>
<!-- Torque curve -->
<charting:LineSeries
Title="Torque"
ItemsSource="{StaticResource EngineMeasurementCollection}"
IndependentValueBinding="{Binding Speed}"
DependentValueBinding="{Binding Torque}">
<!-- Vertical axis for torque curve -->
<charting:LineSeries.DependentRangeAxis>
<charting:LinearAxis
Orientation="Y"
Title="Torque (lb-ft)"
Minimum="50"
Maximum="300"
Interval="50"/>
</charting:LineSeries.DependentRangeAxis>
</charting:LineSeries>
<charting:Chart.Axes>
<!-- Shared horizontal axis -->
<charting:LinearAxis
Orientation="X"
Title="Speed (rpm)"
Interval="1000"
ShowGridLines="True"/>
</charting:Chart.Axes>
</charting:Chart>
Then I have
public class EngineMeasurementCollection : ObservableCollection<EngineMeasurement>
{
public EngineMeasurementCollection()
{
Add(new EngineMeasurement { Speed = 1000, Torque = 100, Power = 20 });
Add(new EngineMeasurement { Speed = 2000, Torque = 160, Power = 60 });
Add(new EngineMeasurement { Speed = 3000, Torque = 210, Power = 125 });
Add(new EngineMeasurement { Speed = 4000, Torque = 220, Power = 160 });
Add(new EngineMeasurement { Speed = 5000, Torque = 215, Power = 205 });
Add(new EngineMeasurement { Speed = 6000, Torque = 200, Power = 225 });
Add(new EngineMeasurement { Speed = 7000, Torque = 170, Power = 200});
}
}
public class EngineMeasurement
{
public int Speed { get; set; }
public int Torque { get; set; }
public int Power { get; set; }
}`
When I run my project the chart works perfect, showing the values created in the constructor, but that's hardcoded, I cant find the way add or remove elements to update the chart.
I don't even understand why the chart shows the provided items at constructor, does a instance of EngineMeasurementCollection is created? automatically? I don't create a new EngineMeasurementCollection.
I tried something like:
EngineMeasurement littleitem = new EngineMeasurement();
EngineMeasurementCollection fullitems = new EngineMeasurementCollection();
littleitem.Power = 10;
littleitem.Speed = 1000;
littleitem.Torque = 50;
fullitems.Add(littleitem);
//Up to this point everything works perfect, now I want to update my chart with fullitem values in whatever possible way.
I can NOT do ahorasi.ItemsSource = fullitems because .ItemsSource does not exist, I am really confused because, the chart works wonderful when just created, with the hardcoded values, I think update it must be just a single step, but cant find how.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Xaml 中的这一行:-
正在为
EngineMeasurementCollection
创建一个实例,并将图表绑定到该实例。因此,如果您想向图表添加新元素,则需要修改此实例。假设这是一个资源的
Grid
的名称为“LayoutRoot”,您可以通过向您的UserControl
添加此属性来访问该集合:-现在您添加一个附加的条目:-
This line in your Xaml:-
is creating an instance to the
EngineMeasurementCollection
and its this instance your chart is bound to.So if you want add new elements to your chart its this instance you need to modify. Assuming the
Grid
that this is a resource of has the name "LayoutRoot" you can gain access to the collection by adding this property to youUserControl
:-Now you add an additional entry with:-
我的特定问题已经解决,但对于进一步的读者来说,我的 UserControl 页面与我用于图表的页面不同,所以我添加
到我的图表页面,并且它工作完美,不知道是否对代码有任何影响就这样。
My particular problem has been solved, but for futher readers, my UserControl page was a different page than the one I use for the chart, so I added
to my chart page, and it works perfect, dont know if theres any implications involved for code it that way.