如何在 Silverlight 4 中实时更新图表值

发布于 2024-11-09 07:24:00 字数 3758 浏览 0 评论 0原文

我有一个像这样创建的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

药祭#氼 2024-11-16 07:24:00

Xaml 中的这一行:-

               <local:EngineMeasurementCollection x:Key="EngineMeasurementCollection"/>

正在为 EngineMeasurementCollection 创建一个实例,并将图表绑定到该实例。

因此,如果您想向图表添加新元素,则需要修改此实例。假设这是一个资源的 Grid 的名称为“LayoutRoot”,您可以通过向您的 UserControl 添加此属性来访问该集合:-

 public EngineMeasurementCollection CurrentMeasurements
 {
      get { return (EngineMeasurementCollection)LayoutRoot.Resources["EngineMeasurementCollection"]; }
 }

现在您添加一个附加的条目:-

CurrentMeasurements.Add(new EngineMeasurement() { Power = 10, Speed = 1000, Torque = 50 } );

This line in your Xaml:-

               <local:EngineMeasurementCollection x:Key="EngineMeasurementCollection"/>

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 you UserControl:-

 public EngineMeasurementCollection CurrentMeasurements
 {
      get { return (EngineMeasurementCollection)LayoutRoot.Resources["EngineMeasurementCollection"]; }
 }

Now you add an additional entry with:-

CurrentMeasurements.Add(new EngineMeasurement() { Power = 10, Speed = 1000, Torque = 50 } );
不必了 2024-11-16 07:24:00

我的特定问题已经解决,但对于进一步的读者来说,我的 UserControl 页面与我用于图表的页面不同,所以我添加

public EngineMeasurementCollection CurrentMeasurements

到我的图表页面,并且它工作完美,不知道是否对代码有任何影响就这样。

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

public EngineMeasurementCollection CurrentMeasurements

to my chart page, and it works perfect, dont know if theres any implications involved for code it that way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文