Working with annotations

Overview

AnyChart Stock doesn't have a full-featured GUI for adding, editing, removing and other operations with annotations, but, at the same time, provides a large API, that can be used to create an interface you need for your application.

This article describes how to work with annotations in HTML using methods, properties or events from AnyChart Stock: JavaScript Library.

Before you start reading this article we recommend to study the basics about annotations in Drawing Tools and Annotations article and play around with this online demo: Online Demos: Drawing Tools and Annotations.

to top

Adding annotation on client side

As we have already said in overview, AnyChart Stock doesn't have any GUI for adding annotations, because interfaces vary greatly from application to application we have decided to give developer an ability to control the process of adding annotations and give one an ability to implement whatever is needed.

To add annotation into a chart you need to initiate a special mode, in which user can add an annotation in interactive mode. To initiate this mode you need to launch startDrawingAnnotation() method, which you can bind to some button or menu.

Sample code below shows how to launch adding Line annotation mode:

As you can see, you should always specify what type of annotation is added and also you can provide a second parameter with settings to be applied to the annotation added. Full list of annotations and ways to provide settings can be found at AnyChart Stock JavaScript API: startDrawingAnnotation() method.

Live sample below demonstrates this method:

Online HTML/JavaScript Sample

to top

Getting config of added annotation

If you want to obtain configuration of any annotation in the chart you can use two special methods:

Live sample below shows how to obtain the config of annotation:

Online HTML/JavaScript Sample

to top

Getting a list of all added annotations

If you want to obtain a list of all annotations in the chart you can use two special methods:

Live sample below shows how to obtain the list of annotations:

Online HTML/JavaScript Sample

to top

Adding annotation programmatically

If you want to add annotation to the chart programmatically you can use addAnnotation() method, which accepts annotation config both in XML or JSON formats, like that:

or like that:


Note: You always have to specify id of the chart to which annotation should be added; you can omit id of annotation: it will be generated automatically in such case; you can invoke addAnnotation() only after onChartDraw is fired.

Live sample below shows how to add annotations programmatically:

Online HTML/JavaScript Sample

to top

Altering and update of existing annotation

If you want to alter an existing annotation programmatically you can use updateAnnotation() method, which accepts annotation config both in XML or JSON formats, like that:

updateAnnotation method updates annotation by the given id, which should be somehow obtained. In the sample above getSelectedAnnotationId() is used for that.

Live sample below shows how to update annotations using updateAnnotation method:

Online HTML/JavaScript Sample

to top

Removing annotations

To remove annotation from a chart you can use the following methods and mechanism:

Live sample which shows how to remove annotations:

Online HTML/JavaScript Sample

to top

Event handling

Handling events with annotations is the same as handling all other AnyStock events. If you are new to AnyStock, please study Working with charts in HTML: Events Handling article, which described events handling basics.

The following events can be used, when you work with annotations:

Live sample below shows how to handle annotation events:

Online HTML/JavaScript Sample

to top

Using context menu

AnyChart Stock gives developer an ability to add custom items into Flash-player context menu, you can read about this at Working with charts in HTML: Context Menu.

Context menu can be used to work with annotations without creating any GUI at all, you just need to add several commands into menu. Live sample below shows how this can be done:

Online HTML/JavaScript Sample

to top