Create WPF Line chart in code behind

Oct 21, 2013 at 10:24 AM
Edited Oct 21, 2013 at 10:30 AM
 I am trying to prepare chart object with line series in code behind without using XAML code.When i am assining the itemsouce property to line series its assigning without giving any error.When i see the points collection for the line series its show null.Can any help me out this??
Is there anything i am missing it??

here is the sample code
        PointCollection datapoints = new PointCollection();
        List<KeyValuePair<double, double>> datapoint = GetPoints();
        foreach (KeyValuePair<double, double> keypair in datapoint)

            datapoints.Add(new Point(keypair.Key, keypair.Value));

        Chart chart = new Chart();
        chart.Name = "areaChart";
        chart.Height = 300;
        chart.Width = 330;
        chart.Title = "Control Voltage";
        chart.VerticalAlignment = VerticalAlignment.Center;
        chart.HorizontalAlignment = HorizontalAlignment.Left;
        LinearAxis xaxis = new LinearAxis();
        xaxis.Name = "XAxis";
        xaxis.Title = "Control Voltage";
        xaxis.Orientation = AxisOrientation.X;
        xaxis.Foreground = new SolidColorBrush(Colors.Black);
        xaxis.ShowGridLines = true;
        LinearAxis yaxis = new LinearAxis();
        yaxis.Name = "YAxis";
        yaxis.Title = "Deviation in ms";
        yaxis.Orientation = AxisOrientation.Y;
        yaxis.ShowGridLines = true;


        Style legendStyle = new Style(typeof(Legend));
        legendStyle.Setters.Add(new Setter(Legend.WidthProperty, 0));
        legendStyle.Setters.Add(new Setter(Legend.BackgroundProperty, new SolidColorBrush(Colors.Transparent)));
        legendStyle.Setters.Add(new Setter(Legend.BorderBrushProperty, new SolidColorBrush(Colors.Transparent)));
        chart.LegendStyle = legendStyle;

        LineSeries ln = new LineSeries();
        ln.Name = "lineGraph";
        ln.Width = 300;
        ln.Height = 200;
        ln.DataContext = datapoint;
        ln.IndependentValueBinding = new Binding("Key");
        ln.DependentValueBinding = new Binding("Value");

        ln.Background = Brushes.White;
        ln.DependentRangeAxis = yaxis;

        Style datapointStyle = new Style(typeof(LineDataPoint));
        datapointStyle.Setters.Add(new Setter(LineDataPoint.WidthProperty, 6.0));
        datapointStyle.Setters.Add(new Setter(LineDataPoint.HeightProperty, 6.0));
        datapointStyle.Setters.Add(new Setter(LineDataPoint.BackgroundProperty, new SolidColorBrush(Colors.Red)));
        ln.DataPointStyle = datapointStyle;