How to bind Chart segment's Y axis value to ChartAdornmentInfo's LabelTempate ?

I would like to create a LabelTemplate for ChartAdornmentInfo and bind segment's Y Axis values to it. I have empty values in my series in ChartTemplate , and I need to hide the visibility of the LabelTemplate if the Y Axis value is '0'.  ChartSeries and it's label logic is implemented in code behind.


3 Replies 1 reply marked as answer

AJ Arul Jenith Berkmans Syncfusion Team December 18, 2024 10:11 AM UTC

Hi Santhosh Kumar,


We have validated your query and would like to inform you that you can collapse specific data labels in a label template using the IValueConverter. By setting the converter in the data label template, you can modify the visibility of data labels based on your conditions.


Here is a simple example code snippet.


C#:


var columnSeries = new ColumnSeries

{

    ItemsSource = vm.Data,

    XBindingPath = "Name",

    YBindingPath = "Height",

    AdornmentsInfo = new ChartAdornmentInfo()

    {

        ShowLabel = true,

        LabelTemplate = CreateDataLabelTemplateWithConverter(),

        SegmentLabelContent = LabelContent.LabelContentPath

    }

};

 

 private DataTemplate CreateDataLabelTemplateWithConverter()

 {

     var template = new DataTemplate();

 

     // Create StackPanel

     var stackPanelFactory = new FrameworkElementFactory(typeof(StackPanel));

 

     // Bind StackPanel.Visibility to YData using the converter

     var visibilityBinding = new Binding("YData")

     {

         Converter = new DataLabelVisibilityConverter()

     };

     stackPanelFactory.SetBinding(StackPanel.VisibilityProperty, visibilityBinding);

 

     var yValue = new FrameworkElementFactory(typeof(TextBlock));

     yValue.SetBinding(TextBlock.TextProperty, new Binding("YData")  {});

     yValueStack.AppendChild(yValue);

 

 

     stackPanelFactory.AppendChild(yValueStack);

 

     template.VisualTree = stackPanelFactory;

     return template;

 }


Converter code snippet:


public class DataLabelVisibilityConverter : IValueConverter

 {

     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

     {

         if (value is double doubleValue)

         {

             // Custom condition: Hide labels for values equal to 0

             return doubleValue == 0 ? Visibility.Collapsed : Visibility.Visible;

         }

         return Visibility.Visible;

     }

 

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

     {

         return null;

     }

 }


For more details, please visit the document: How to collapse the visibility of specific data label in WPF Chart (SfChart)?


Additionally, we have prepared a sample and attached it for your reference. If you need further assistance, feel free to contact us.


Thank you for your understanding.


Best regards,

Arul Jenith B.


Attachment: DataLabelSample_ae90dae.zip

Marked as answer

SK Santhosh Kumar Bathruswamy December 18, 2024 04:03 PM UTC

Thank you Arul Jenith Berkmans, It works as expected.



PR Preethi Rajakandham Syncfusion Team December 19, 2024 04:55 AM UTC

Hi Santhosh,

You're welcome. We are glad to know that the reported problem has been resolved. Please let us know if you require any further assistance on this. We will be happy to assist you.

Regards,

Preethi R


Loader.
Up arrow icon