Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 807
Akul123 Avatar asked Dec 02 '25 20:12

Akul123


1 Answers

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

like image 103
AlBal Avatar answered Dec 05 '25 10:12

AlBal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!