开发者

How to live update chart values in Silverlight 4

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.


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 } );


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜