Annotations

An annotation marks a level or a span that matters: a support price, a reporting window, a single candle worth calling out. They are anchored in data space, so they stay put through zoom, pan, an update(), a theme switch and a chart-type switch.

chart.addAnnotation({ type: 'yLine', y: 187.4, label: 'Resistance' })
chart.addAnnotation({ type: 'xBand', x: '2024-02-01', x2: '2024-02-15', label: 'Earnings window' })
MethodDoes
addAnnotation(config)Adds one, or replaces it if id already exists. Returns the id, or null on invalid input
updateAnnotation(id, patch)Patches one
removeAnnotation(id)Removes one
clearAnnotations()Removes all
getAnnotation(id) / getAnnotations()Reads them back, meta included

Everything is managed by id, so removing an annotation never disturbs a trading price line or an indicator's own annotations.

Six types

TypeAnchors onDraws
yLineyA horizontal price level
yBandy, y2A shaded price range
xLinexA vertical time marker
xBandx, x2A shaded time span
pointx, yA marker at one coordinate
textx, yA text label at one coordinate

x takes a timestamp, a date string, or a Date.

Config

KeyApplies toWhat it is
idallA stable id. Auto-generated as anno-N when omitted
labelallThe label text. text is an alias, which reads better for type: 'text'
colorallLine, marker and label color. Defaults from the theme
fillColor, opacitybandsThe fill, defaulting to color
textColorallLabel text color
width, strokeDashArraylinesStroke width and dash length
labelPositiony annotations'left' or 'right'
markerpointMarker overrides, such as { size, shape, fillColor }
metaallAnything of yours, returned by getAnnotation and getAnnotations

Colors default from the active theme, so an annotation with no styling stays legible after a mode or preset switch.

Three overlay layers, and which to use

Annotations are one of three ways to put something on the chart, and they are not interchangeable:

AnnotationsTrading price linesDrawings
Placed byYour codeYour codeThe user, or your code
DraggableNoYesYes
CallbacksNoonCross, onMove, onRemoveEvents only
ForEditorial marks and reference levelsOrders, stops, targets and alertsTechnical analysis

Use an annotation when the mark is a statement rather than a control. Reach for a price line when the reader is meant to move it or when a streamed bar crossing it should fire something.

Persistence

Annotations are captured by getState() and restored by setState(), meta included. Unlike price lines they carry no callbacks, so nothing has to be re-bound after a restore.

See also