3

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.

jsanalytics
  • 13,058
  • 4
  • 22
  • 43
LBajlo
  • 95
  • 3
  • 12

1 Answers1

2

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

jsanalytics
  • 13,058
  • 4
  • 22
  • 43
AlBal
  • 146
  • 7
  • Please can you tell me how to add Legend on chart but programmatically? I am new to this and there is no too much documentation about creating dynamically (in code) live charts :/ – LBajlo Nov 08 '17 at 18:29
  • There is a LegendLocation property on the CartesianChart class - for example add "ch.LegendLocation = LegendLocation.Right" to make the legend appear on the right hand side of the chart. Hope this helps! If you have any further queries please add as a new question. – AlBal Nov 08 '17 at 21:08
  • can you tellme the flow if I get a value from a textbox in XAML and then the control comes to button_click() function in xaml.cs and write the chart generation code there but then how do I inject the chart below the textfeld in .xaml file – zavy mola Sep 29 '19 at 01:04
  • Zavy - do you want the chart to appear only once an entry has been made in the textbox? – AlBal Oct 04 '19 at 20:27