One of the powerful ways of drawing your user’s attention to chart data is through annotations. As the name suggests, annotations let you add textual notes, shapes and images to highlight important data points of the plotted data. When we visualize data in charts, it is not just about depicting the data; it's about making it meaningful. Annotation provide additional information that may not be represented by basic X and Y values. With the 2016 v3 release of C1Studio, Annotations have been made available in FlexChart for WinForms, WPF and UWP to further empower your data visualization needs.
In this blog, we'll walk you through on how you can use Annotations in FlexChart:
Annotations in FlexChart are added as layers, so you can add multiple annotations of different shapes and sizes. Adding annotations to FlexChart is simple two-step process:
Here's how to add the AnnotationLayer to desktop apps. WinForms
var annotationLayer = new AnnotationLayer(flexChart);
WPF/UWP
var annotationLayer = new AnnotationLayer();
flexChart.Layers.Add(annotationLayer);
Each annotation object provides an API to specify the annotation's text, location, and characteristics based on the annotation type. The following is a snippet to create Annotation object and add to the Annotation Layer:
var annoObject = new C1.Win.Chart.Annotation.Text()
{
//Configure Annotation here
Content = "y = log x",
Attachment = AnnotationAttachment.Relative,
Location = new PointF(0.1f, 0.05f)
};
annotationLayer.Annotations.Add(annoObject);
Basic FlexChart with Annotation
Let's look at the various options available for adding annotations based on your requirements. FlexChart comes with eight different types of annotations that can be broadly classified in following three categories.
If you need to provide a detailed description of the chart, highlight additional information for a specific data point, or let user know about the mathematical formula that generates the drawn series, Text annotations allow you to add single or multi-line notes to your chart Here’s an example to add Text annotations to the FlexChart:
var dangerText = new Text();
dangerText.Content = "Dangerous";
dangerText.Attachment = C1.Chart.Annotation.AnnotationAttachment.DataIndex;
dangerText.PointIndex = 5;
annotationlayer.Annotations.Add(dangerText);
var description = new Text();
description.Content = "Research suggests that safe exposure limit is 85 decibels for 8 hours a day." + Environment.NewLine + "Your hearing could be at risk of damage if you are continually exposed " + Environment.NewLine + "to high volume sounds";
description.Attachment = C1.Chart.Annotation.AnnotationAttachment.Relative;
description.Location = new PointF(0.2f, 0.1f);
annotationlayer.Annotations.Add(description);
Single- and multi-line text annotations in FlexChart
Shape-based annotations allow you to draw your user's attention to an individual or a group of data points. Shapes (especially geometric) are essential for the visual impact. FlexChart provides you with six different shape annotations:
Along with the shape, you can also display the textual information by specifying Annotation's Content property. Here's how to add a Rectangle-shaped annotation to the FlexChart:
//_data is used as chart's DataSource
var maxSales = _data.Select(x => x.Sales).Max();
var item = _data.First(x => x.Sales == maxSales);
var rect = new C1.Win.Chart.Annotation.Rectangle
{
Attachment = C1.Chart.Annotation.AnnotationAttachment.DataCoordinate,
Content = string.Format("Maximum Sales Recorded in {0}\\nAmount:{1:C}", item.Month, item.Sales),
Location = new PointF { X = _data.IndexOf(item), Y = (float)item.Sales },
Height = 50,
Width = 200,
Position = C1.Chart.Annotation.AnnotationPosition.Top
};
rect.Style.FillColor = Color.Transparent;
annotationLayer.Annotations.Add(rect);
The chart clearly indicates which month had maximum sales: Shape-based annotations in FlexChart
Image annotations are helpful when you want to display images on the chart surface to let the end user quickly relate with the plotted data, like up/down arrows icons for profit/loss, or company logos to easily associate with the brand. The Image annotation can be made more informative by displaying Tooltip next to it. You can specify TooltipText property in any annotation object. Here's how to add an Image annotation with tooltip to the FlexChart:
var android = new C1.Win.Chart.Annotation.Image
{
Attachment = C1.Chart.Annotation.AnnotationAttachment.DataIndex,
PointIndex = 0,
SourceImage = Properties.Resources.Img_Android,
Height = 64,
Width = 64,
Position = C1.Chart.Annotation.AnnotationPosition.Bottom,
TooltipText = "Sales of Android smartphones grew by 15%."
};
annotationLayer.Annotations.Add(android);
With image annotations in the following chart, you can see how much easier it becomes to visualize mobile sales data of each operating system. The logos are more visually compelling than text would be. Image annotations in FlexChart Check out the AnnotationExplorer sample (Win | WPF | UWP) for details on adding Annotations of all types.
You may have a requirement with regards annotation placement: at a fixed or relative position, or attached to specific value or series. FlexChart offers you complete flexibility by allowing you to specify how you want to attach the annotations via AnnotationAttachment property. The different annotation position requirement and options available with FlexChart are:
Positioning annotations in FlexChart In addition to these, an annotation can also be positioned relative to the point it has been attached to, i.e you can place the annotation to the Top/Left/Right/Bottom/Center of the point.
Being able to customize the appearance of a chart element is one of most basic and important needs for a chart user. FlexChart continues to provide the same with the Annotation feature as well. You have options to customize the appearance of annotations as well as its content via the Style and ContentStyle properties. Following is an example on how to customize the appearance of a shape based annotation:
var rect = new C1.Win.Chart.Annotation.Rectangle();
//TODO: configure the annotation
//Customize the Rectangle
rect.Style.FillColor = Color.CornflowerBlue;
rect.Style.StrokeColor = Color.Blue;
rect.Style.StrokeDashPattern = new[] { 2f, 3f };
//Customize the Rectangle's Content
rect.ContentStyle.StrokeColor = Color.White;
rect.ContentStyle.Font = new Font(flexChart.Font.FontFamily,9,FontStyle.Bold);
var annotationLayer = new AnnotationLayer(flexChart);
annotationLayer.Annotations.Add(rect);
Styling annotations in FlexChart
When you've been studying and manipulating data for a long time, you may be able to understand a chart immediately. But you'll also want to make the chart as instantly understandable for your users. It's important to review your chart from the perspective of your audience, who may be viewing the data for the first time. Let's understand this by visualizing a store footfall data and how annotations can help you facilitate the understanding of your audience. The following chart visualizes the trend of the number of visitors in a store. We can make two instant observations:
Store footfall graph without annotations While these observations were easy to make, the chart doesn't communicate the reason for the spikes in data, and annotations can play an important role. Here's the same chart with annotations added. We can instantly see that footfall is directly related to the day of week: weekends, holidays and discounts drive customer traffic.
Store footfall graph with annotations