SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, iOS Chart, Android Chart and JavaScript Chart Components
Hi,
I’m using the following code to create a text annotation:
private void AddLabel(DateTime StartTime, DateTime EndTime, string label)
{
TextAnnotation text = new TextAnnotation();
text.YAxisId = "LeftAxis";
text.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
text.Text = label;
text.CoordinateMode = AnnotationCoordinateMode.RelativeY;
text.X1 = StartTime;
text.X2 = EndTime;
text.Y1 = .05;
text.Y2 = 0;
text.TextAlignment = TextAlignment.Center;
text.TextStretch = Stretch.UniformToFill;
text.Background = new SolidColorBrush(Color.FromArgb(0x80, 0x80, 0x80, 0x80));
text.BorderBrush = new SolidColorBrush(Color.FromArgb(0xC0, 0xD0, 0xD0, 0xD0));
text.BorderThickness = new Thickness(2);
sciChart.Annotations.Add(text);
}
Unfortunately the text doesn’t scale like I want it to though. The size of the text stays the same even when the chart is small, making the height greater than .05. Is there a way to make the text get smaller as the chart is shrunk so that it stays within the .05 Y boundary?
I attached 2 images to illustrate the issue.
Thanks,
Greg
Hi Greg,
Hm…in this case I can suggest you changing the FontSize of annotations dynamically or modifying a Template of TextAnnotaion. Please, try using following control template:
<ControlTemplate TargetType="s:TextAnnotation"> <Border x:Name="PART_TextAnnotationRoot" Margin="{TemplateBinding Margin}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="{TemplateBinding CornerRadius}"> <Viewbox Stretch="{TemplateBinding TextStretch}"> <TextBox x:Name="PART_InputTextArea" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalAlignment}" AcceptsReturn="True" Background="Transparent" BorderThickness="0" FontFamily="{TemplateBinding FontFamily}" FontSize="{TemplateBinding FontSize}" FontWeight="{TemplateBinding FontWeight}" Foreground="{TemplateBinding Foreground}" IsEnabled="{Binding CanEditText, RelativeSource={RelativeSource TemplatedParent}}" IsTabStop="{TemplateBinding IsEditable}" Style="{StaticResource AnnotationTextBoxStyle}" Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" TextAlignment="{TemplateBinding TextAlignment}" TextWrapping="Wrap" /> </Viewbox> </Border> </ControlTemplate>
Also you can find info about how to apply a template to a control in code in How to generate RolloverMarkerTemplate in code topic.
Please, let us know if this helps,
Best regards,
Yuriy
Another solution to make the TextAnnotation size small when the chart is resized is presented here:
Text Annotation Resizing with FontSize Binding
This is a very simple solution requiring just one binding per annotation plus a converter.
Hope this helps!
Best regards,
Andrew
Please login first to submit.