SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, iOS Chart, Android Chart and JavaScript Chart Components
I’m adding an annotation to a chart with a DateTime x-axis. Without a label the annotation is successfully added. However, if I specify a label I get an InvalidOperationException when Add() is called. The exception message is “The Type this is a test is not a valid Comparable Type”}:
var annotation = new VerticalLineAnnotation
{
VerticalAlignment = VerticalAlignment.Stretch,
ShowLabel = true,
X1 = time.DateTime,
Stroke = System.Windows.Media.Brushes.Orange,
LabelPlacement = LabelPlacement.TopRight,
LabelValue = "this is a test",
LabelsOrientation = Orientation.Vertical
};
MyChart.Annotations.Add(annotation);
The LabelValue is obviously of type string and non-null and indeed of type IComparable. Any reason I can’t add it?
Hi Dan,
The VerticalLineAnnotation.LabelValue is expecting a numeric value of IComparable. Unfortunately there is no interface in .NET for IComparableExceptForString 🙂 so we had to use the lowest common denominator interface (IComparable) and hope nobody set the value to a string, or custom class .. Values such as 1.234, or ’22 MAR 2016′ will be valid and will be parsed, but string values won’t.
If you want to place a string value on a VerticalLineAnnotation Axis Label, you can use this code.
<s:VerticalLineAnnotation VerticalAlignment="Stretch"
FontSize="12"
FontWeight="Bold"
ShowLabel="False"
Stroke="Brown"
StrokeThickness="2"
X1="9.5"
Y1="3">
<s:AnnotationLabel LabelPlacement="Axis" Text="Hello Thar!" />
</s:VerticalLineAnnotation>
Best regards,
Andrew
I had the same issue as Dan, wanting to display a non-date string when using a DateTime X axis. The workaround of adding child AnnotationLabels doesn’t work when using the view model, i.e. VerticalLineAnnotationViewModel. No labels appear.
An alternative workaround is to use the LabelTextFormatting property to assign your desired text – you may want to escape some or all of it (single quotes) to prevent it being parsed as a DateTime format string.
E.g. if setting in code
vm.LabelTextFormatting = "'My desired label'";
or equivalent if setting in XAML.
Please login first to submit.