Scale Y-axis based on live data with fixed number of axis labels

I want to show a chart where the Primary axis is a DateTimeAxis and the Secondary axis is a NumericalAxis. This chart will show live data for the last 5 hours (the data source will be trimmed continuously) and I have the following requirements on that -


  1. My Y-axis will be scaled based on the live data. The labels value should be always an integer and the label will always start with "0"
  2. The Y-axis will always show exactly 3 labels (The first one is fixed ["0"], other two will be based on the new range)
  3. My data will never exceed 200. So I do not want to show any label with value more than 220

I have tried to achieve these for a while, but couldn't able to come up with any implementation which meets my requirements. Can you suggest me any cleaner approach to do that?


2 Replies

DD Devakumar Dhanapoosanam Syncfusion Team February 8, 2022 05:33 PM UTC

Hi HRahman, 
 
We are currently working on your requirement at our end, and we will update the complete details by tomorrow February 9, 2022. 
 
Regards, 
Devakumar D 



DD Devakumar Dhanapoosanam Syncfusion Team February 9, 2022 02:45 PM UTC

Hi HRahman, 
 
We have achieved your requirement by using Minimum property of chart axis to set the start position as zero always and remaining two requirements are achieved by using the custom NumericalAxisExt and overriding the GenerateVisibleLabels method to modify the y-axis VisibleLabels collection as per the below code example.  
 
<chart:SfChart.SecondaryAxis> 
    <local:NumericalAxisExt Minimum="0"/> 
</chart:SfChart.SecondaryAxis> 
 
public class NumericalAxisExt : NumericalAxis 
{ 
    protected override void GenerateVisibleLabels() 
    { 
        base.GenerateVisibleLabels(); 
        VisibleLabels.Clear(); 
        var median = Math.Round(VisibleRange.Median); 
        var end = Math.Round(VisibleRange.End); 
 
        VisibleLabels.Add(new ChartAxisLabel(0, 0)); 
        VisibleLabels.Add(new ChartAxisLabel(median, median)); 
 
        if (end <= 220) 
        { 
            VisibleLabels.Add(new ChartAxisLabel(end, end)); 
        } 
        else 
        { 
            VisibleLabels.Add(new ChartAxisLabel(220, 220)); 
        } 
    } 
} 
 
 
 
Please let us know if you need any further assistance on this. 
 
Regards, 
Devakumar D 


Loader.
Up arrow icon