SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, iOS Chart, Android Chart and JavaScript Chart Components
SciChart WPF ships with hundreds of WPF Chart Examples which you can browse, play with, view the source-code and even export each WPF Chart Example to a stand-alone Visual Studio solution. All of this is possible with the new and improved SciChart WPF Examples Suite, which ships as part of the SciChart WPF SDK.
Using the AnnotationCreationModifier you can give your users the ability to create annotations dynamically via mouse-clicks.
Tips!
To create TextAnnotations, AxisMarkerAnnotations, CustomAnnotations, – HorizontalLineAnnotations and – VerticalLineAnnotations, simply click once.
To create BoxAnnotations, LineAnnotations and LineArrowAnnotations click and drag.
After creating and selecting an annotation, you can drag, resize and delete it.
Documentation Links
– AnnotationCreationModifier Type
– Annotations API Documentation
– Databinding Annotations with MVVM
The C#/WPF source code for the WPF Chart Create Annotations Dynamically example is included below (Scroll down!).
Did you know you can also view the source code from one of the following sources as well?
<UserControl x:Class="SciChart.Examples.Examples.AnnotateAChart.CreateAnnotationsDynamically.CreateAnnotationsDynamically"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ext="http://schemas.abtsoftware.co.uk/scichart/exampleExternals"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:s="http://schemas.abtsoftware.co.uk/scichart"
Loaded="CreateAnnotationsDynamicallyLoaded"
d:DesignHeight="400"
d:DesignWidth="600"
mc:Ignorable="d">
<UserControl.Resources>
<!-- Defines Default Colors for annotations via implicit styles -->
<Style x:Key="TextAnnotationStyle" TargetType="s:TextAnnotation">
<Setter Property="Background" Value="#22B22020" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Foreground" Value="#666666" />
</Style>
<Style x:Key="BoxAnnotationStyle" TargetType="s:BoxAnnotation">
<Setter Property="BorderBrush" Value="#279B27" />
<Setter Property="Background" Value="#551964FF" />
<Setter Property="BorderThickness" Value="1" />
</Style>
<Style x:Key="AxisMarkerAnnotationStyle" TargetType="s:AxisMarkerAnnotation">
<Setter Property="Background" Value="#FF6600" />
<Setter Property="FontSize" Value="11" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Foreground" Value="#EEE" />
</Style>
<Style x:Key="HorizontalLineAnnotationStyle" TargetType="s:HorizontalLineAnnotation">
<Setter Property="Stroke" Value="#FFFF6600" />
<Setter Property="StrokeThickness" Value="2" />
<Setter Property="ShowLabel" Value="True" />
<Setter Property="LabelPlacement" Value="Axis" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
</Style>
<Style x:Key="VerticalLineAnnotationStyle" TargetType="s:VerticalLineAnnotation">
<Setter Property="Stroke" Value="#FFFF6600" />
<Setter Property="StrokeThickness" Value="2" />
<Setter Property="ShowLabel" Value="True" />
<Setter Property="LabelPlacement" Value="Axis" />
<Setter Property="VerticalAlignment" Value="Stretch" />
</Style>
</UserControl.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/SciChart.Examples.ExternalDependencies;component/Resources/Styles/ToolbarButtonsCommon.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Grid.Resources>
<!-- The SciChartInteractionToolbar adds zoom, pan, zoom extents and rotate functionality -->
<!-- to the chart and is included for example purposes. -->
<!-- If you wish to know how to zoom and pan a chart then do a search for Zoom Pan in the Examples suite! -->
<ext:SciChartInteractionToolbar TargetSurface="{Binding Source={x:Reference Name=sciChart}}">
<ToggleButton x:Name="pointerButton"
Margin="2"
Checked="OnEditingEnabled"
Content="Pointer"
Style="{StaticResource PointerButtonStyle}"
Tag="LineAnnotation"
ToolTip="Pointer"
Unchecked="OnEditDisabled"
s:ToggleButtonExtensions.GroupName="Modifiers" />
<Button Click="OnDeleteSelectedAnnotationsClick"
Style="{StaticResource DefaultButtonStyle}"
Padding="0"
Content="DEL"
ToolTip="Delete Selected"/>
<ext:FlyoutSeparator />
<ext:FlyoutMenuButton x:Name="AnnotationCreation"
HorizontalAlignment="Left"
Content="Annotation"
Style="{StaticResource AnnotationButtonStyle}"
ToolTipService.ToolTip="Annotation">
<ext:FlyoutMenuButton.PopupContent>
<StackPanel MinWidth="150" Orientation="Vertical">
<TextBlock Text="Annotation Type" />
<ComboBox x:Name="AnnotationTypes"
SelectedIndex="0"
SelectionChanged="AnnotationTypes_OnSelectionChanged" />
<ext:FlyoutSeparator />
<Button Width="auto"
HorizontalAlignment="Stretch"
Click="AddAnnotation_OnClick"
Content="Add Annotation"
Style="{StaticResource DefaultButtonStyle}" />
</StackPanel>
</ext:FlyoutMenuButton.PopupContent>
</ext:FlyoutMenuButton>
</ext:SciChartInteractionToolbar>
<!-- Defines the SciChartSurface, with CreateAnnotationModifier -->
<s:SciChartSurface x:Name="sciChart"
Grid.Column="1"
BorderThickness="0"
ClipModifierSurface="True"
Padding="0">
<s:SciChartSurface.RenderableSeries>
<s:FastCandlestickRenderableSeries />
</s:SciChartSurface.RenderableSeries>
<s:SciChartSurface.XAxis>
<s:CategoryDateTimeAxis DrawMajorBands="True" />
</s:SciChartSurface.XAxis>
<s:SciChartSurface.YAxis>
<s:NumericAxis DrawMajorBands="True" TextFormatting="0.0#" />
</s:SciChartSurface.YAxis>
<s:SciChartSurface.ChartModifier>
<s:ModifierGroup>
<s:AnnotationCreationModifier x:Name="annotationCreation" AnnotationCreated="OnAnnotationCreated" />
<s:YAxisDragModifier />
<s:XAxisDragModifier />
</s:ModifierGroup>
</s:SciChartSurface.ChartModifier>
<s:SciChartSurface.Annotations>
<s:CustomAnnotation CoordinateMode="Relative"
HorizontalAnchorPoint="Left"
VerticalAnchorPoint="Center"
Margin="5,60,0,0"
X1="0"
Y1="0" >
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="20"
Opacity="0.7"
Background="#232323"
Margin="5 0"
Text="To select the desired annotation type, click"
Foreground="#FFF" />
<Image Width="32"
Height="32"
Source="/SciChart.Examples;component/Resources/Images/annotation_icon.png"
Stretch="None" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="20"
Opacity="0.7"
Background="#232323"
Margin="5 0"
Text="Now you can add an annotation of the chosen type on the chart. Just click on the surface!"
Foreground="#FFF" />
<Image Width="32"
Height="32"
Source="/SciChart.Examples;component/Resources/Images/pointbutton_icon.png"
Stretch="None" />
</StackPanel>
<TextBlock FontSize="20"
Opacity="0.7"
Background="#232323"
Margin="5 0"
Text="For some annotation types, like BoxAnnotation, it may require more than 1 click to finish it."
Foreground="#FFF" />
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="20"
Opacity="0.7"
Background="#232323"
Margin="5 0"
Text="A single click on an annotation makes it selected. You can delete the selected annotation, clicking"
Foreground="#FFF" />
<Image Width="32"
Height="32"
Source="/SciChart.Examples;component/Resources/Images/del_button.png"
Stretch="None" />
</StackPanel>
</StackPanel>
</s:CustomAnnotation>
</s:SciChartSurface.Annotations>
</s:SciChartSurface>
</Grid>
</UserControl>
// *************************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2021. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales: sales@scichart.com
//
// CreateAnnotationsDynamically.xaml.cs is part of the SCICHART® Examples. Permission is hereby granted
// to modify, create derivative works, distribute and publish any part of this source
// code whether for commercial, private or personal use.
//
// The SCICHART® examples are distributed in the hope that they will be useful, but
// without any warranty. It is provided "AS IS" without warranty of any kind, either
// expressed or implied.
// *************************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using SciChart.Charting.Model.DataSeries;
using SciChart.Charting.Visuals.Annotations;
using SciChart.Examples.ExternalDependencies.Data;
namespace SciChart.Examples.Examples.AnnotateAChart.CreateAnnotationsDynamically
{
public partial class CreateAnnotationsDynamically : UserControl
{
private readonly IDictionary<string, Type> _annotationTypes = new Dictionary<string, Type>()
{
{ "LineAnnotation", typeof(LineAnnotation)},
{ "LineArrowAnnotation", typeof(LineArrowAnnotation)},
{ "TextAnnotation", typeof(TextAnnotation)},
{ "BoxAnnotation", typeof(BoxAnnotation)},
{ "HorizontalLineAnnotation", typeof(HorizontalLineAnnotation)},
{ "VerticalLineAnnotation", typeof(VerticalLineAnnotation)},
{ "AxisMarkerAnnotation", typeof(AxisMarkerAnnotation)},
{ "MyCustomAnnotation", typeof(MyCustomAnnotation)}
};
public CreateAnnotationsDynamically()
{
InitializeComponent();
}
void CreateAnnotationsDynamicallyLoaded(object sender, RoutedEventArgs e)
{
var dataSeries = new OhlcDataSeries<DateTime, double>();
var marketDataService = new MarketDataService(DateTime.Now, 5, 5);
var data = marketDataService.GetHistoricalData(200);
dataSeries.Append(data.Select(x => x.DateTime), data.Select(x => x.Open), data.Select(x => x.High), data.Select(x => x.Low), data.Select(x => x.Close));
sciChart.RenderableSeries[0].DataSeries = dataSeries;
pointerButton.IsChecked = true;
AnnotationTypes.ItemsSource = _annotationTypes.Keys;
}
private void OnDeleteSelectedAnnotationsClick(object sender, RoutedEventArgs e)
{
var selectedAnnotations = sciChart.Annotations.Where(annotation => annotation.IsSelected).ToList();
foreach (var selectedAnnotation in selectedAnnotations)
{
sciChart.Annotations.Remove(selectedAnnotation);
}
}
private void OnAnnotationCreated(object sender, EventArgs e)
{
var newAnnotation = (annotationCreation.Annotation as AnnotationBase);
if (newAnnotation != null)
{
newAnnotation.IsEditable = true;
newAnnotation.CanEditText = true;
}
pointerButton.IsChecked = true;
}
private void OnEditingEnabled(object sender, RoutedEventArgs e)
{
if (annotationCreation != null)
annotationCreation.IsEnabled = false;
foreach (var annotation in sciChart.Annotations)
{
annotation.IsEditable = true;
}
}
private void OnEditDisabled(object sender, RoutedEventArgs e)
{
annotationCreation.IsEnabled = true;
}
private void AnnotationTypes_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
pointerButton.IsChecked = false;
var type = _annotationTypes[(string)AnnotationTypes.SelectedValue];
var resourceName = String.Format("{0}Style", type.Name);
if (Resources.Contains(resourceName))
{
annotationCreation.AnnotationStyle = (Style)Resources[resourceName];
}
annotationCreation.AnnotationType = type;
}
private void AddAnnotation_OnClick(object sender, RoutedEventArgs e)
{
pointerButton.IsChecked = false;
}
}
}
<s:CustomAnnotation x:Class="SciChart.Examples.Examples.AnnotateAChart.CreateAnnotationsDynamically.MyCustomAnnotation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:s="http://schemas.abtsoftware.co.uk/scichart"
Background="Transparent"
d:DesignHeight="400"
d:DesignWidth="600"
mc:Ignorable="d">
<Image Width="178"
Height="74"
Source="/SciChart.Examples.ExternalDependencies;Component/Resources/Images/SciChartLogo_Dark.png"
Stretch="None"/>
</s:CustomAnnotation>
// *************************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2021. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales: sales@scichart.com
//
// MyCustomAnnotation.xaml.cs is part of the SCICHART® Examples. Permission is hereby granted
// to modify, create derivative works, distribute and publish any part of this source
// code whether for commercial, private or personal use.
//
// The SCICHART® examples are distributed in the hope that they will be useful, but
// without any warranty. It is provided "AS IS" without warranty of any kind, either
// expressed or implied.
// *************************************************************************************
using SciChart.Charting.Visuals.Annotations;
namespace SciChart.Examples.Examples.AnnotateAChart.CreateAnnotationsDynamically
{
public partial class MyCustomAnnotation : CustomAnnotation
{
public MyCustomAnnotation()
{
InitializeComponent();
}
}
}