Search code examples
c#wpfchartscartesianlivecharts

How to dynamically create a cartesian chart using livecharts


Can anyone tell me how to create cartesian chart dynamically in C# code? I created an instance of cartesian chart with CartesianChart ch = new CartesianChart(); but do I need to add series, margins, etc? Basically I need to create wpf cartesian chart in code which will then be showed in wpf application. Thanks in advance.


Solution

  • Please find below a simple example to programmatically create a CartesianChart instance and apply it to a named WPF element. As a minimum your CartesianChart needs some data to display, which is defined within a SeriesCollection and set to the Series property.

    Code behind:

        CartesianChart ch = new CartesianChart();
        ch.Series = new SeriesCollection
        {
            new LineSeries
            {
                Title = "Series 1",
                Values = new ChartValues<double> { 1, 1, 2, 3 ,5 }
            }
        };
        TestGrid.Children.Add(ch);
    

    XAML:

    <Grid Name="TestGrid"/>
    

    enter image description here