How do you refresh VisibleLabels in a NumericalAxis?

I have two labels on my Y NumericalAxis that can be changed by the user. I can't figure out how to refresh the NumericalAxis or tell SyncFusion to re-draw the axis.

public class MyAxis : NumericalAxis
{
private TrendGraphViewModel _viewModel;

protected override void OnCreateLabels()
{
base.OnCreateLabels();

VisibleLabels.Clear();

VisibleLabels.Add(new ChartAxisLabel((double)_viewModel.UserSettings.UpperTarget, _viewModel.UserSettings.UpperTarget.ToString()));
VisibleLabels.Add(new ChartAxisLabel((double)_viewModel.UserSettings.LowerTarget, _viewModel.UserSettings.LowerTarget.ToString()));
}

protected override void OnBindingContextChanged()
{
// since these properties are not technically children, the inheritedBindingContext needs to be manually set
base.OnBindingContextChanged();

_viewModel = BindingContext as MyViewModel;
_viewModel.UserSettings.PropertyChanged += UserSettingsOnPropertyChanged;
}

private void UserSettingsOnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(_viewModel.UserSettings.UpperTarget) ||
e.PropertyName == nameof(_viewModel.UserSettings.LowerTarget))
{
// TODO: What to do here? Calling OnCreateLabels() doesn't refresh the Axis
OnCreateLabels();
}
}
}



4 Replies 1 reply marked as answer

YP Yuvaraj Palanisamy Syncfusion Team November 10, 2020 01:17 PM UTC

Hi Adam Scroggin,  
   
Greetings from Syncfusion.  
   
We have analyzed your query and example code, the OnCreateLabels() method called when only chart load time and changing the ItemsSource property of Series.  
   
If you want to dynamically change the YAxis visible label, please set the series ItemsSource to null and then set the ItemsSource for Series in UserSettingsOnPropertyChanged. 
 
  
private void UserSettingsOnPropertyChanged(object sender, PropertyChangedEventArgs e)
   {
      if (e.PropertyName == nameof(_viewModel.UserSettings.UpperTarget) ||
          e.PropertyName == nameof(_viewModel.UserSettings.LowerTarget))
      {
         var Data = _viewModel.ChartData;
  
         _viewModel.ChartData = null;  
         _viewModel.ChartData = Data;
      }
   }
  
   
Or else if you want to change the axis range start and end value, you can directly bind the value to the axis Minimum & Maximum property of the chart axis.

Could you share your use case scenario of modifying the axis label, which will be helpful to serve you a better solution at the earliest.
  
 
Regards, 
Yuvaraj. 



PP Paul Pringle November 10, 2020 04:09 PM UTC

On my Y axis, I have the min (0) and the max (16.6). But I also have two labels 7.8 and 4.4.  After the graph is drawn, the user could change 7.8 to 8.1. Thus, when I update VisibileLabels to the new value, how do I get the Y axis to redraw.



PP Paul Pringle November 10, 2020 04:20 PM UTC

MyAxis inherits from Syncfusion's NumericalAxis. There is no ItemsSource property or Series in this class.


YP Yuvaraj Palanisamy Syncfusion Team November 11, 2020 03:49 PM UTC

Hi Adam Scroggin,   
  
Query : There is no ItemsSource property or Series in this class.  
  
Thanks for your update. We have analyzed your query and we have achieved your requirement “Update the visible labels dynamically for axis” by using the below code snippet since we just null the ViewModel class ItemsSource populated binding property. So, It will work at your end.  
   
Code Snippet[C#]  
private void UserSettingsOnPropertyChanged(object sender, PropertyChangedEventArgs e)  
{  
   
     if (e.PropertyName == nameof(_viewModel.UpperTarget) ||  
         e.PropertyName == nameof(_viewModel.LowerTarget))  
     {  
         var data = _viewModel.Data;  
         _viewModel.Data = null;  
         _viewModel.Data = data;  
         OnCreateLabels();  
     }  
}  
   
Also, we have prepared the sample for your reference, please find the sample from the below link.  
   
  
Regards, 
Yuvaraj. 


Marked as answer
Loader.
Up arrow icon