Charting: How to add a LineSeries programmatically?

Aug 11, 2010 at 10:21 AM

How does one add LineSeries to a graph programmatically (i.e. I don't know how many lines on the graph there will be at compile time)?

I tried adding the following test code to some existing code, to test programmatically adding a 2nd line (with no additional changes to XAML) however this doesn't work (an exception occurs)

 

        private ObservableCollection<RtData> _rTDataCol = new ObservableCollection<RtData>();

        public MainWindow()
        {
            InitializeComponent();
            InitializeBackgroundWorker();

            var lineSeries = (LineSeries)(RealTimeChart2.Series[0]);
            lineSeries.ItemsSource = _rTDataCol;
            lineSeries.IndependentValuePath = "Second";
            lineSeries.DependentValuePath = "Kbps";

            // Populate window's worth of data with 0's
            for (int i = 0; i < 60; i++)
            {
                var rt = new RtData();
                rt.Second = i;
                rt.Kbps = 0;
                _rTDataCol.Add(rt);
            }

            // Test only stuff
            LineSeries testSeries = new LineSeries();
            testSeries.ItemsSource = _rTDataCol;
            testSeries.IndependentValuePath = "testing 123";
            testSeries.DependentValuePath = "Kbps";
            RealTimeChart2.Series.Add(testSeries);

        }

The associated section in my XAML is:

 

       <Grid Grid.Row="2">
            <chartingToolkit:Chart Name="RealTimeChart2" Title="Real Time Usage" BorderThickness="0" MinHeight="400">
                <chartingToolkit:LineSeries Title="Main" 
                                            IsSelectionEnabled="True"
                                            PolylineStyle="{StaticResource GooglePolylineStyle}" 
                                            DataPointStyle="{StaticResource GoogleLineDataPointStyle}" 
                                            >


                    <!--<Setter Property="Template" Value="{x:Null}"/>-->

                    <chartingToolkit:LineSeries.DependentRangeAxis>
                        <chartingToolkit:LinearAxis Orientation="Y" Title="kbps"/>
                    </chartingToolkit:LineSeries.DependentRangeAxis>

                </chartingToolkit:LineSeries>
            </chartingToolkit:Chart>
        </Grid>