Labels are not visible in BarSeries when DataTemplate for lables is defined.

2 posts, 1 answers
  1. Yaroslav
    Yaroslav avatar
    153 posts
    Member since:
    Jan 2016

    Posted 14 Jul Link to this post

    Hello. I use a DataTemplate for labels in BarSeries. Please see XAML below:

    <telerik:RadCartesianChart Grid.Row="0" Grid.Column="0" Visibility="{Binding IsAbsoluteBarChartVisible}">
        <!--Annotation line-->
        <telerik:RadCartesianChart.Annotations>
            <telerik:CartesianGridLineAnnotation Axis="{Binding ElementName=verticalAxis}" Value="{Binding AnnotationValue}" Label="{Binding AnnotationLabel}" Stroke="Red"
                                             StrokeThickness="2" DashArray="8 2" Visibility="{Binding IsAnnotationVisible}">
                <telerik:CartesianGridLineAnnotation.LabelDefinition>
                    <telerik:ChartAnnotationLabelDefinition Location="Inside"  VerticalAlignment="Bottom"  HorizontalAlignment="Center">
                        <telerik:ChartAnnotationLabelDefinition.DefaultVisualStyle>
                            <Style TargetType="TextBlock">
                                <Setter Property="FontSize" Value="14"/>
                                <Setter Property="FontWeight" Value="DemiBold"/>
                                <Setter Property="Foreground" Value="Red" />
                            </Style>
                        </telerik:ChartAnnotationLabelDefinition.DefaultVisualStyle>
                    </telerik:ChartAnnotationLabelDefinition>
                </telerik:CartesianGridLineAnnotation.LabelDefinition>
            </telerik:CartesianGridLineAnnotation>
        </telerik:RadCartesianChart.Annotations>
        <!--X - axis-->
        <telerik:RadCartesianChart.HorizontalAxis>
            <telerik:CategoricalAxis/>
        </telerik:RadCartesianChart.HorizontalAxis>
        <!--Y - axis-->
        <telerik:RadCartesianChart.VerticalAxis>
            <telerik:LinearAxis x:Name="verticalAxis" Title="{Binding Title_Y}"  Minimum="{Binding AbsoluteMinimum_Y}" Maximum="{Binding AbsoluteMaximum_Y}" MajorStep="{Binding AbsoluteStep_Y}"/>
        </telerik:RadCartesianChart.VerticalAxis>
        <!--Here is bar chart itself-->
        <telerik:RadCartesianChart.Series>
            <telerik:BarSeries ShowLabels="True" CategoryBinding="Category" ValueBinding="Value" ItemsSource="{Binding Data}">
                <telerik:BarSeries.LabelDefinitions>
                    <telerik:ChartSeriesLabelDefinition Margin="0 20 0 0">
                        <telerik:ChartSeriesLabelDefinition.Template>
                            <!-- !!! HERE IS PROBLEMATIC TEMPLATE !!! -->
                            <DataTemplate>
                                <TextBlock Foreground="White"/>
                            </DataTemplate>
                        </telerik:ChartSeriesLabelDefinition.Template>
                    </telerik:ChartSeriesLabelDefinition>
                </telerik:BarSeries.LabelDefinitions>
            </telerik:BarSeries>
        </telerik:RadCartesianChart.Series>
    </telerik:RadCartesianChart>

    But the labels are not visible in Bars in the chart in this case!

    But when I comment the following code:

    <!--telerik:ChartSeriesLabelDefinition.Template>
           <DataTemplate>
                 <TextBlock Foreground="White"/>
           </DataTemplate>
    </telerik:ChartSeriesLabelDefinition.Template-->

    Then the labels are visible nice. Why this has plase. Please help.

  2. Answer
    Dinko
    Admin
    Dinko avatar
    414 posts

    Posted 18 Jul Link to this post

    Hi Yaroslav,

    Looking at the provided code snippet you haven't set the Text property of the TextBlock in the custom template. You can bind it to the Value property (for example) in order to show the label.
    <telerik:ChartSeriesLabelDefinition.Template>
        <DataTemplate>
            <TextBlock Foreground="White" Text="{Binding Value}"/>
        </DataTemplate>
    </telerik:ChartSeriesLabelDefinition.Template>

    Give it a try and let me know if it works in your application.

    Regards,
    Dinko
    Progress Telerik
    Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Back to Top