Series Last value Label on Y Axis

In SfCartesianChart for any series especially for Line, Column and/or Financial i.e CandleStick Is possible to show last value of the Series on Y Axis to the Right


I was able to make the Axis show on the Right side 

 CrossesAt="{Static x:Double.MaxValue}"


Is is possible to show last value of the series to see on Y Axis?

Example screenshot here

Image_5487_1724972689673




7 Replies 1 reply marked as answer

NT Nitheeshkumar Thangaraj Syncfusion Team August 30, 2024 08:06 AM UTC

Hi Praveen,



We would like to let you know that there is an option to display the last value of a series on the Y-axis in the SfCartesianChart. This can be achieved by using a HorizontalLineAnnotation with its axis label. To accomplish this, we initialize a property in the ViewModel to hold the last value of the series' data collection and bind that property to the Y1 property of the HorizontalLineAnnotation. It supports both load and dynamic time.


By using this method, you can customize the display of the last value according to your needs. Below, we have attached a code snippet and a video demonstrating for your reference:

public class ViewModel : INotifyPropertyChanged

 {

     private double _lastYValue;

     public double LastYValue

     {

         get => _lastYValue;

         set

         {

             if (_lastYValue != value)

             {

                 _lastYValue = value;

                 OnPropertyChanged(nameof(LastYValue));

             }

         }

     }

     public ObservableCollection<Model> Data { get; set; }

 

public ViewModel()

 {

  // your Data collection

  Data.CollectionChanged += Data_CollectionChanged;

 UpdateLastYValue();

}

 

private void Data_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)

 {

     UpdateLastYValue();

 }

 

 private void UpdateLastYValue()

 {

     if (Data.Any())

     {

         LastYValue = Data.Last().YValue;  // Assign the last YValue

     }

 }


<chart:SfCartesianChart.Annotations>

    <chart:HorizontalLineAnnotation Y1="{Binding LastYValue}" ShowAxisLabel="True">

        <chart:HorizontalLineAnnotation.AxisLabelStyle>

            <chart:ChartAxisLabelStyle Background="Orange"/>

        </chart:HorizontalLineAnnotation.AxisLabelStyle>

    </chart:HorizontalLineAnnotation>

</chart:SfCartesianChart.Annotations>



We hope this helps. If you need any further assistance or concerns, feel free to ask.


Regards,

Nitheeshkumar.



Attachment: HorizontalAnnotation_cfe63c0.zip


PD Praveen D October 5, 2024 10:56 PM UTC

I was able to use the suggest option is display Last value with Transparent Stroke on Horizontal Line.


1)

I have more request is it possible use  Y1="{Binding LastYValue}"  but display a different Value in Axis Label 

ex: LastValue from ViewModel to display label at correct Y Axis position but display a different Value?


2)

One minor workaround would be great when the HorizontalLine can be send behind the other chartSeries such that it does have gaps in drawing the Chart due to Transparent Stroke


Image_7763_1728168891152



AJ Arul Jenith Berkmans Syncfusion Team October 7, 2024 02:53 PM UTC

Hi Praveen,

 

We have reviewed your queries and would like to address them as follows:

 

Query 1: Displaying a Different Value in the Axis Label

 

We have investigated your query and would like to let you know that we are unable to fully understand your requirement. Could you please share more details about your specific need on how to display the axis label with the annotation? This information helps us to provide you with a better solution.

 

Query 2: Sending the Horizontal Line Behind Other Chart Series

 

Regarding the query about the sending horizontal line annotation behind the chart series. You can be able to achieve that by using CanAnnotationUnderPlotArea property which was the internal property of chart.

 

We have attached a sample demonstrating how to change the order of annotations. Please review the sample and let us know if you have any questions or need further assistance.

 

Thank you for your time, and we look forward to your response.

 

Regards,

Arul Jenith B.



Attachment: CustomViewSample_bd264030.zip


PD Praveen D October 8, 2024 03:06 AM UTC

Query 2 : Attached examples works fine. Thank you


Query1: In the Sample code attached, the last value of 20 in orange label is at show at Y Axis Value 20. 


We would like to customize the text displayed in the label example 20 (5%) i.e. Custom Text from Binding of different property but Y Axis at which text( eg: 20 5%)  will be shown will still be at 20 i.e. based on Y1 Binding Value


Mock up screenshot -

Image_2146_1728356172479

 

Hope this clarifies 


Please let me know


thanks





AJ Arul Jenith Berkmans Syncfusion Team October 8, 2024 01:36 PM UTC

Hi Praveen,


We have analyzed your requirement and are pleased to inform you that you can achieve it by using the LabelFormat property in ChartAxisLabelStyle. We have prepared a sample for you, which you can review for further details.


Here’s a code snippet for XAML:

 <local:SfCartesianChartExt.Annotations>

     <chart:HorizontalLineAnnotation  Y1="{Binding LastYValue}"  ShowAxisLabel="True" Stroke="Transparent"  >

<chart:HorizontalLineAnnotation.AxisLabelStyle>

             <chart:ChartAxisLabelStyle Background="Orange" LabelFormat="{Binding Label}" />

</chart:HorizontalLineAnnotation.AxisLabelStyle>

     </chart:HorizontalLineAnnotation>

 </local:SfCartesianChartExt.Annotations>


ViewModel code snippet:


  public class ViewModel : INotifyPropertyChanged

  {

      public double Percentage = 5; // Modify the value as you requirement

      public int DataCount = 300000;

      private readonly Random randomNumber;

 

      private double _lastYValue;

      public double LastYValue

      {

          get => _lastYValue;

          set

          {

              if (_lastYValue != value)

              {

                  _lastYValue = value;

                      OnPropertyChanged(nameof(LastYValue));

              }

          }

      }

 

      public string Label => $"{LastYValue} ({Percentage}%)";

}




Output Image:
Screenshot 2024-10-08 173046.png

If you need further assistance or have any questions, feel free to reach out.

Thank you for your patient and understanding.


Regards,

ARUL JENITH B.


Attachment: CustomViewSample_80999525.zip


PD Praveen D October 8, 2024 05:09 PM UTC

Excellent. That works. Thank you



PR Preethi Rajakandham Syncfusion Team October 9, 2024 05:07 AM UTC

Hi Praveen,

You're welcome. 

We are glad that the provided response meets your requirement. We will mark this thread as solved. Please let us know if you need further assistance. As always, we are happy to help you out. 

Regards,

Preethi R


Marked as answer
Loader.
Up arrow icon