I can't set ChartAnnotationLabelDefinition.Template.

3 posts, 1 answers
  1. Yaroslav
    Yaroslav avatar
    123 posts
    Member since:
    Jan 2016

    Posted 03 Oct Link to this post

    Hello. I try to set ChartAnnotationLabelDefinition.Template in the way where the label of annotation has red color. But I havn't got the required result yet. I wrote the following XAML markup:

    <telerik:RadCartesianChart Visibility="{Binding IsAbsoluteBarChartVisible}">
                <!--The definition of an annotation line-->
                <telerik:RadCartesianChart.Annotations>
                    <telerik:CartesianGridLineAnnotation Axis="{Binding ElementName=verticalAxis}" Value="{Binding AnnotationValue}" Label="{Binding AnnotationLabel}" Stroke="Red" StrokeThickness="2" DashArray="8 2">
                        <telerik:CartesianGridLineAnnotation.LabelDefinition>
                            <telerik:ChartAnnotationLabelDefinition Location="Inside"  VerticalAlignment="Bottom"  HorizontalAlignment="Center">
                                <!--HERE IS THE TEMPLATE DEFINITION-->
                                <telerik:ChartAnnotationLabelDefinition.Template>
                                    <DataTemplate>
                                        <TextBlock Foreground="Red" FontWeight="DemiBold"/>
                                    </DataTemplate>
                                </telerik:ChartAnnotationLabelDefinition.Template>
                            </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="Децибелы [Дб]" Minimum="{Binding ChartMinimum}" Maximum="{Binding ChartMaximum}" MajorStep="{Binding CurrentStep}"/>
                </telerik:RadCartesianChart.VerticalAxis>
                <!--The chart itself-->
                <telerik:RadCartesianChart.Series>
                    <telerik:BarSeries ShowLabels="True" CategoryBinding="Category" ValueBinding="Value" ItemsSource="{Binding Data}"/>
                </telerik:RadCartesianChart.Series>
            </telerik:RadCartesianChart>

    But when I run my application and navigate to the View where charts are displayed then I see only annotation without label (please see 'AnnotationWithoutLabels.PNG' file attached). When I comment the following part of XAML markup

    <telerik:ChartAnnotationLabelDefinition.Template>
        <DataTemplate>
            <TextBlock Foreground="Red" FontWeight="DemiBold"/>
        </DataTemplate>
    </telerik:ChartAnnotationLabelDefinition.Template>

    then I see annotation with black colored label (please see 'AnnotationWithBlackColoredLabel.PNG' file attached). How do I get a bright red color for the annotation label?

  2. Answer
    Martin
    Admin
    Martin avatar
    1101 posts

    Posted 04 Oct Link to this post

    Hello Yaroslav,

    The label is not displayed when you set the Template property, because the TextBlock inside the DataTemplate doesn't have its Text property set. 
    <telerik:ChartAnnotationLabelDefinition.Template>
        <DataTemplate>
            <TextBlock Text="{Binding}" Foreground="Red" FontWeight="DemiBold"/>
        </DataTemplate>
    </telerik:ChartAnnotationLabelDefinition.Template>
    Note that the data context passed in the template is the value of the annotation's Label property.

    If you don't want to use a template you can achieve the same result by setting the DefaultVisualStyle instead of the Template property.
    <telerik:ChartAnnotationLabelDefinition.DefaultVisualStyle>
        <Style TargetType="TextBlock">
            <Setter Property="Foreground" Value="Red" />
        </Style>
    </telerik:ChartAnnotationLabelDefinition.DefaultVisualStyle>

    Regards,
    Martin
    Telerik by Progress
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Yaroslav
    Yaroslav avatar
    123 posts
    Member since:
    Jan 2016

    Posted 05 Oct in reply to Martin Link to this post

    Martin, fthank you for your help.
Back to Top