Articles in this section
Category / Section

How to Collapse the Visibility of Specific Data Label in WPF Chart?

4 mins read

This article describes how to collapse the visibility of the specific data label in our WPF Charts.

You can hide the data label by using the LabelTemplate property and a converter, as shown in the following steps:

Step 1: Define a data label template for the series.

<DataTemplate x:Key="labelTemplate">
      <StackPanel Orientation="Vertical" 
             Background="{Binding Interior}">
             <StackPanel Orientation="Horizontal">
                   <TextBlock Text="XValue:" />
                   <TextBlock Text="{Binding XData}" />
              </StackPanel>
              <StackPanel Orientation="Horizontal">
                    <TextBlock Text="YValue: " />
                   <TextBlock Text="{Binding YData}" />
              </StackPanel>
      </StackPanel>
</DataTemplate>

 

Step 2: Create a value to visibility converter to control the visibility of the data label based on the Y-value. For example, in the following converter, we have collapsed the visibility of the data label for values less than 50.

public class VisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        var labelValue = (double)value;
        if (labelValue < 50)
            return Visibility.Collapsed;
         return Visibility.Visible;        
    }
    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
            return value;
    }
}

 

Step 3: To control the visibility, bind the converter to the DataTemplate Visibility property.

<chart:SfChart.Resources>
      <local:VisibilityConverter x:Key="visibilityConverter"/>
      <DataTemplate x:Key="labelTemplate">
            <StackPanel Orientation="Vertical" 
                   Visibility="{Binding YData,Converter={StaticResource visibilityConverter}}"
                   Background="{Binding Interior}">
                   <StackPanel Orientation="Horizontal">
                        <TextBlock Text="XValue:" />
                        <TextBlock Text="{Binding XData}" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="YValue: " />
                        <TextBlock Text="{Binding YData}" />
                    </StackPanel>
              </StackPanel>
       </DataTemplate>
 </chart:SfChart.Resources>

 

Step 4: Set the defined DataTemplate to the LabelTemplate property of ChartAdornmentInfo in the series.

<chart:SfChart x:Name="chart" Header="Efficiency of oil-fired power production">
            . . .
  <chart:ColumnSeries XBindingPath="XValue" 
      YBindingPath="Y1Value" Palette="Custom"
      ItemsSource="{Binding Data}">
                
      <chart:ColumnSeries.AdornmentsInfo>
         <chart:ChartAdornmentInfo SegmentLabelContent="LabelContentPath"  
              ShowLabel="True" LabelPosition="Outer" LabelTemplate="{StaticResource labelTemplate}">
          </chart:ChartAdornmentInfo>
       </chart:ColumnSeries.AdornmentsInfo>
   </chart:ColumnSeries>
</chart:SfChart>

Output

DataLabel in WPF chart

See also

How to draw dotted line in WPF Chart?

How to display chart area alone in WPF Chart (SfChart)?

How to display trackball label or tooltip over chart annotation?


Conclusion

I hope you enjoyed learning about how to collapse the visibility of specific data label in WPF Chart.

You can refer to our WPF Chart feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF Chart example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!



Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied