AxisLabelTemplate

I want to create an AxisLabelTemplate, where the Foreground of the TextBlock matches the Stroke color of the HorizontalLineAnnotation, and the Text corresponds to the Y1 value in the HorizontalLineAnnotation.For example: 

        <DataTemplate x:Key="AxisLabelTemplate">

            <Border

                Width="100"

                Height="30"

                Background="Transparent"

                BorderBrush="Transparent"

                BorderThickness="1"

                CornerRadius="5">

                <StackPanel>

                    <TextBlock

                        Margin="2,2,2,2"

                        HorizontalAlignment="Center"

                        VerticalAlignment="Center"

                        FontFamily="{StaticResource PingFang}"

                        FontSize="14"

                        FontWeight="Bold" />

                </StackPanel>

            </Border>

        </DataTemplate>

                <syncfusion:SfChart.Annotations>

                    <syncfusion:HorizontalLineAnnotation

                        AxisLabelTemplate="{StaticResource AxisLabelTemplate}"

                        HorizontalTextAlignment="Left"

                        ShowAxisLabel="True"

                        Stroke="Green"

                        StrokeDashArray="5,0"

                        Y1="{Binding TargetLine}" />

                </syncfusion:SfChart.Annotations>


3 Replies

PM Priyadharshni Murugan Syncfusion Team August 6, 2025 07:57 AM UTC

Hi Tom,

 

Thank you for your query. Based on your requirements, we have prepared a sample that demonstrates how to create an AxisLabelTemplate where the TextBlock foreground color matches the Stroke color of the HorizontalLineAnnotation, and the label text displays the annotation's Y1 value.

 

Please review the attached sample at your convenience. If you have any further questions or need additional assistance, feel free to reach out to us—we're always here to help.


Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/WpfAppSample

Regards,
Priyadharshni M


Attachment: ChartImage_af8fcbe9.zip


TO Tom August 6, 2025 08:57 AM UTC

Can Foreground inherit the color of Stroke in HorizontalLineAnnotation without direct assignment?

As indicated in my attached image.



Attachment: e43dd72c37eb4119b65c0be405e36228_92e8a22d.png


PM Priyadharshni Murugan Syncfusion Team August 7, 2025 09:42 AM UTC

Hi Tom,

 

We’ve reviewed your exact scenario and prepared a working sample that achieves your requirement. To achieve dynamic color inheritance for the axis label’s Foreground, we used a custom IValueConverter. This converter takes the Y1 value of the annotation and matches it with the corresponding HorizontalLineAnnotation in the chart. Once matched, it returns the Stroke brush, allowing the label text to automatically reflect the annotation’s stroke color—without manual assignment.

 

We’ve shared the sample and output image for your reference. Kindly review and feel free to reach out for further assistance.


Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/WpfAppSample

Regards,
Priyadharshni M


Attachment: Annotation_2c141f10.zip

Loader.
Up arrow icon