Skip to content

Main API

This page documents the chart accessor used by chart consumers.

The accessor updates an immutable chart state object (the chart definition) and lets the presenter initialize itself from that state. That gives us:

  1. Easy persistence: serialize state to JSON.
  2. Straightforward undo/redo: immutable state changes are easy to track.
  3. Easy restore: saved chart state can be loaded back directly.

The chart state is a single JavaScript object we call a definition. It is formally described by schema and explained in Chart state.

Accessor methods

Below is a summary of accessor methods (namespace omitted for readability):

  • load(definition, options) loads saved definition JSON. options control merge behavior for annotations and comparison plots. Default: { keepOldAnnotations: false, keepNewAnnotations: true, keepOldCompare: false, keepNewCompare: true }. By default, existing annotations/comparison plots are discarded and incoming ones are kept. Annotation merge logic matches panes by standalone study id. Comparison merge logic de-duplicates old symbols first, enforces max-compare count (4), then maps plots to matching axes.
  • save() returns current state as JSON string.
  • asTemplate(strip) converts current definition to a reusable template. strip.main defaults to true; strip.annotations defaults to false.
  • getDefaultTemplate() returns the default template used during feed initialization.
  • print(options) prints the chart as currently rendered. options.title uses the same shape as setTitle.
  • setTitle(title) sets a runtime chart title. This title currently does not persist with saved state.
  • reset() resets chart state to the first definition loaded after initialization.
  • exportImage(options) returns { width, height, data } where data is a PNG data URI. options can override width/height and pass title.
  • annotate(id, traits = null, data = null) creates annotations; details are on Annotations.
  • change(...actions) applies one or more chart actions in one batch.
  • exportData(options) exports main plot and loaded study data as CSV. Numbers use . as decimal separator; datetime format is YYYY-MM-DD HH:mm:ss (YYYY-MM-DD for EOD).
  • exportTable(options) exports the same loaded chart data as a structured table with columns and rows. Row values are raw values such as Date, number, string, or boolean; use columns to read values by column id.
  • getDataStats() returns { dataRange: { from, to }, barCount } where from/to are UNIX timestamps in milliseconds.

Data export

exportData(options) still returns CSV text. exportTable(options) returns raw structured data for integrations that need to build their own output format, such as Excel workbooks.

const table = chart.exportTable();
for (const row of table.rows) {
for (const column of table.columns) {
console.log(column.name, row[column.id]);
}
}

The package also exports convertTableToCsv(table, options) for callers that want CSV text from a previously exported table.

Export options:

  • excludeHeader: omit CSV header rows.
  • numDecimals: round numeric values during CSV conversion.
  • excludeFields: omit curve fields by field id.
  • visibleRange: export only rows within the current visible x-axis range.
  • sourceOrder: sort exported data sources before columns are built. Sources expose public metadata such as plotUid, plotType, plotTitle, fieldIds, seasonalOffset, paneIndex, axisIndex, plotIndex, and main.

Title object

The title object (used by setTitle, print, and exportImage) has these properties:

  • text: text to render
  • align: left | right | center
  • verticalAlign: top | bottom | middle
  • x, y: pixel coordinates relative to chart viewport
  • style: { fontSize, color }

Change action reference

The change(...actions) API accepts action objects by id. Each action now has a dedicated page under API action details.

Action idSummaryDetails
MainPlot (Symbol deprecated)Changes the primary plot sourceMainPlot
AggregationMerges aggregation valuesAggregation
PeriodSets duration or explicit date rangePeriod
CrosshairEnables/disables and styles crosshair linesCrosshair
TooltipControls tooltip visibility and modeTooltip
EventsToggles dividends/earnings/splits markersEvents
ScaleSets scale mode and axis-related optionsScale
OtherApplies miscellaneous chart optionsOther
PlotAdds/reads/removes/updates plotsPlot
CompareConfigures comparison plotsCompare
AnnotationRemoves/updates/duplicates annotationsAnnotation
ThemeUpdates key chart theme valuesTheme
ClearClears annotations/studies by pane or globallyClear
PreviousReads or updates previous-value line settingsPrevious
MovePlotMoves a plot between panesMovePlot
PinPlotMoves a plot between axes, including detached/no-axis modePinPlot