Search code examples
c#wpflivecharts

LvCharts.Wpf - How to update label values (redraw labels)


Trying to get a hang on framework called Live Charts. I'm reading my values and labels from MS SQL database. The first time it reads values everything is shown. I've added a new event when I select another item from a list it updates query and brings other values.

The problem is when I bring other values my Labels don't renew. Now I debugged and variables contain everything as expected but Label values don't update. Maybe there is some command to force update on chart so that I can redraw labels?

Labels are binding to a variable

<lvc:Axis x:Name="axisX"
          FontFamily="Arial" 
          Foreground="Black" 
          FontSize="10" 
          MinValue="0" 
          MaxValue="9" 
          LabelsRotation="25" 
          Title="" 
          Labels="{Binding Labels}">

I update all values and push to chart

public void ChartDraw()
{
   Labels = new[] { "" };
   Labels = Str.ToArray(); //Labels get values from MS SQL, values are correct.
   DataContext = this;

   YFormatter = value => value + " %";

   foreach (string sqlread in Sql_Koncentracija)
   {
      SeriesCollection[0].Values.Add(Convert.ToDouble(sqlread)); // this adds values to //chart and it updates automatically, but labels dont update.
   }

}

Solution

  • put your DataContext binding value in loading or in class constructor:

    ...
    public YourClass() { 
        InitializeComponent();
        DataContext = this; 
    } 
    
    

    or if you need init data context after loading set your context after your control/form loaded :

    public YourClass() { 
        InitializeComponent();
        this.Loaded += (s, e) => {
                    this.DataContext = this; ;
                };
    }