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()

            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;

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


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" 
                                            PolylineStyle="{StaticResource GooglePolylineStyle}" 
                                            DataPointStyle="{StaticResource GoogleLineDataPointStyle}" 

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

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