This is a migrated thread and some comments may be shown as answers.

ChartView live data

1 Answer 246 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Sergey
Top achievements
Rank 1
Sergey asked on 27 Apr 2013, 07:24 AM
Hi, I'm binding my ChartView to RadObservableCollection in a ViewModel, but I'm getting exceptions about trying to add to collection during CollectionChanged event handler working.
I'm suspending collection's notifications and resume them after adding element, but still getting this error (btw, only when chart becoming visible). Doing that from separate thread through Dispatcher.Invoke, also tried without that - not helping.
There are about 5-10 add operations per second. Can be more, can be less sometimes.
I was even trying to lock collection with locker object and then with (Collection as ICollection).SyncRoot. It doesn't help, only changing exception to InvalidArgumentException (index is out of range).
So, I'm frustrated
<telerik:RadCartesianChart x:Name="Chart"
                                   EmptyContent="Нет данных"
                                   Grid.Row="0"
                                   Grid.Column="0"
                                   Grid.ColumnSpan="2">
            <telerik:RadCartesianChart.Grid>
                <telerik:CartesianChartGrid MajorLinesVisibility="None"
                                            StripLinesVisibility="XY">
                    <telerik:CartesianChartGrid.YStripeBrushes>
                        <SolidColorBrush Color="SlateGray" />
                        <SolidColorBrush Color="SlateGray"
                                         Opacity="0.8" />
                    </telerik:CartesianChartGrid.YStripeBrushes>
                </telerik:CartesianChartGrid>
            </telerik:RadCartesianChart.Grid>
            <telerik:RadCartesianChart.VerticalAxis>
                <telerik:LinearAxis Minimum="0"
                                    MajorStep="5"
                                    Maximum="100"
                                    FontFamily="Segoe UI"
                                    FontSize="16"
                                    FontWeight="Bold"
                                    HorizontalLocation="Left"
                                    ElementBrush="Black" />
            </telerik:RadCartesianChart.VerticalAxis>
            <telerik:RadCartesianChart.HorizontalAxis>
                <telerik:DateTimeContinuousAxis MajorStepUnit="Minute"
                                                LabelInterval="10"
                                                FontFamily="Segoe UI"
                                                PlotMode="OnTicks"
                                                LabelFormat="HH:mm:ss.f"
                                                Minimum="{Binding HeatStartTime}"
                                                Maximum="{Binding HeatEndTime}" />
            </telerik:RadCartesianChart.HorizontalAxis>
            <telerik:LineSeries CategoryBinding="Time"
                                ValueBinding="CO"
                                ItemsSource="{Binding GasAnalysisChartData}"
                                Stroke="Red"
                                StrokeThickness="2">
            </telerik:LineSeries>
            <telerik:LineSeries CategoryBinding="Time"
                                ValueBinding="H2"
                                ItemsSource="{Binding GasAnalysisChartData}"
                                Stroke="Lime"
                                StrokeThickness="2">
            </telerik:LineSeries>
            <telerik:LineSeries CategoryBinding="Time"
                                ValueBinding="N2"
                                ItemsSource="{Binding GasAnalysisChartData}"
                                Stroke="Black"
                                StrokeThickness="2">
            </telerik:LineSeries>
            <telerik:LineSeries CategoryBinding="Time"
                                ValueBinding="O2"
                                ItemsSource="{Binding GasAnalysisChartData}"
                                Stroke="Blue"
                                StrokeThickness="2">
            </telerik:LineSeries>
            <telerik:LineSeries CategoryBinding="Time"
                                ValueBinding="Ar"
                                ItemsSource="{Binding GasAnalysisChartData}"
                                Stroke="Fuchsia"
                                StrokeThickness="2">
            </telerik:LineSeries>
            <telerik:LineSeries CategoryBinding="Time"
                                ValueBinding="CO2"
                                ItemsSource="{Binding GasAnalysisChartData}"
                                Stroke="Yellow"
                                StrokeThickness="2">
            </telerik:LineSeries>
        </telerik:RadCartesianChart>

private void ProcessGasAnalysisDataPiece(FlexEvent fex)
        {
            lock ((GasAnalysisChartData as ICollection).SyncRoot)
            {
                var gdata = new GasAnalysisChartDataModel
                {
                    Ar = (double)fex.Arguments["Ar"],
                    CO = (double)fex.Arguments["CO"],
                    CO2 = (double)fex.Arguments["CO2"],
                    H2 = (double)fex.Arguments["H2"],
                    N2 = (double)fex.Arguments["N2"],
                    O2 = (double)fex.Arguments["O2"],
                    Time = fex.Time
                };
                GasAnalysisChartData.SuspendNotifications();
                GasAnalysisChartData.Add( gdata );
                GasAnalysisChartData.ResumeNotifications();
            }

1 Answer, 1 is accepted

Sort by
0
Sergey
Top achievements
Rank 1
answered on 27 Apr 2013, 10:29 AM
Ah, found solution.

You should run method body where you add element in collection (in ViewModel) in Dispatcher.BeginInvoke.
I did in Dispatcher.Invoke or even without, because code was put into delegate and it was somehow wrong and never executes in Dispatcher.BeginInvoke.
I've changed that delegate to Action and everything started working as it should!
Tags
ChartView
Asked by
Sergey
Top achievements
Rank 1
Answers by
Sergey
Top achievements
Rank 1
Share this question
or