The dynamic chart state can be saved, restored and updated at runtime.
Save & Restore Copy Link
import {
AgCartesianChartOptions,
AgCartesianSeriesTooltipRendererParams,
AgChartState,
AgCharts,
AnimationModule,
AreaSeriesModule,
ContextMenuModule,
CrosshairModule,
LegendModule,
ModuleRegistry,
NavigatorModule,
NumberAxisModule,
UnitTimeAxisModule,
ZoomModule,
} from "ag-charts-enterprise";
import { DataType, getData } from "./data";
const dateFormatter = new Intl.DateTimeFormat("en-GB");
const tooltip = {
renderer: ({
datum,
yKey,
}: AgCartesianSeriesTooltipRendererParams<DataType>) => {
const value = `${Math.round(Number(datum[yKey]) / 100) / 10 + "k"}`;
return { data: [{ label: dateFormatter.format(datum.date), value }] };
},
};
let state: AgChartState = {
version: "11.0.0",
zoom: {
rangeX: {
start: {
__type: "date",
value: new Date("2021-01-01").getTime(),
},
},
},
legend: [
{
seriesId: "tate-modern",
visible: false,
},
{
seriesId: "tate-liverpool",
visible: false,
},
],
};
ModuleRegistry.registerModules([
AnimationModule,
AreaSeriesModule,
CrosshairModule,
LegendModule,
NavigatorModule,
NumberAxisModule,
UnitTimeAxisModule,
ZoomModule,
ContextMenuModule,
]);
const options: AgCartesianChartOptions<DataType> = {
title: {
text: "Total Visitors to Tate Galleries",
},
footnote: {
text: "Source: Department for Digital, Culture, Media & Sport",
},
data: getData(),
navigator: {
enabled: true,
},
zoom: {
enabled: true,
},
series: [
{
type: "area",
xKey: "date",
yKey: "Tate Modern",
yName: "Tate Modern",
id: "tate-modern",
tooltip,
},
{
type: "area",
xKey: "date",
yKey: "Tate Britain",
yName: "Tate Britain",
id: "tate-britain",
tooltip,
},
{
type: "area",
xKey: "date",
yKey: "Tate Liverpool",
yName: "Tate Liverpool",
id: "tate-liverpool",
tooltip,
},
{
type: "area",
xKey: "date",
yKey: "Tate St Ives",
yName: "Tate St Ives",
id: "tate-st-ives",
tooltip,
},
],
axes: {
x: {
type: "unit-time",
},
y: {
type: "number",
title: {
text: "Total visitors",
},
label: {
formatter: (params) => {
return params.value / 1000 + "k";
},
},
},
},
};
options.container = document.getElementById("myChart");
const chart = AgCharts.create(options);
function saveState() {
const newState = chart.getState();
state = newState;
console.log("Saved", state);
}
function restoreState() {
chart.setState(state).then(() => {
console.log(`Restored`, state);
});
}
if (typeof window !== "undefined") {
// Attach external event handlers to window so they can be called from index.html
(<any>window).saveState = saveState;
(<any>window).restoreState = restoreState;
}
export interface DataType {
date: Date;
"Tate Britain": number;
"Tate Modern": number;
"Tate Liverpool": number;
"Tate St Ives": number;
}
export function getData(): DataType[] {
return [
{
date: new Date(2015, 0, 1),
"Tate Britain": 103268,
"Tate Modern": 307185,
"Tate Liverpool": 43888,
"Tate St Ives": 3939,
},
{
date: new Date(2015, 1, 1),
"Tate Britain": 145525,
"Tate Modern": 365921,
"Tate Liverpool": 40130,
"Tate St Ives": 8145,
},
{
date: new Date(2015, 2, 1),
"Tate Britain": 166384,
"Tate Modern": 393326,
"Tate Liverpool": 33696,
"Tate St Ives": 10328,
},
{
date: new Date(2015, 3, 1),
"Tate Britain": 148871,
"Tate Modern": 566058,
"Tate Liverpool": 47671,
"Tate St Ives": 20663,
},
{
date: new Date(2015, 4, 1),
"Tate Britain": 125526,
"Tate Modern": 447241,
"Tate Liverpool": 37075,
"Tate St Ives": 16965,
},
{
date: new Date(2015, 5, 1),
"Tate Britain": 122866,
"Tate Modern": 428800,
"Tate Liverpool": 55740,
"Tate St Ives": 24119,
},
{
date: new Date(2015, 6, 1),
"Tate Britain": 107487,
"Tate Modern": 530400,
"Tate Liverpool": 79328,
"Tate St Ives": 28612,
},
{
date: new Date(2015, 7, 1),
"Tate Britain": 81997,
"Tate Modern": 591215,
"Tate Liverpool": 85698,
"Tate St Ives": 32666,
},
{
date: new Date(2015, 8, 1),
"Tate Britain": 118527,
"Tate Modern": 490750,
"Tate Liverpool": 63270,
"Tate St Ives": 21066,
},
{
date: new Date(2015, 9, 1),
"Tate Britain": 164064,
"Tate Modern": 492280,
"Tate Liverpool": 70965,
"Tate St Ives": 16249,
},
{
date: new Date(2015, 10, 1),
"Tate Britain": 144861,
"Tate Modern": 358494,
"Tate Liverpool": 37767,
"Tate St Ives": 7874,
},
{
date: new Date(2015, 11, 1),
"Tate Britain": 106191,
"Tate Modern": 333040,
"Tate Liverpool": 24896,
"Tate St Ives": 5419,
},
{
date: new Date(2016, 0, 1),
"Tate Britain": 107297,
"Tate Modern": 323835,
"Tate Liverpool": 25313,
"Tate St Ives": 5772,
},
{
date: new Date(2016, 1, 1),
"Tate Britain": 77074,
"Tate Modern": 419576,
"Tate Liverpool": 50027,
"Tate St Ives": 9860,
},
{
date: new Date(2016, 2, 1),
"Tate Britain": 92849,
"Tate Modern": 542205,
"Tate Liverpool": 43105,
"Tate St Ives": 15329,
},
{
date: new Date(2016, 3, 1),
"Tate Britain": 92416,
"Tate Modern": 509236,
"Tate Liverpool": 44228,
"Tate St Ives": 25089,
},
{
date: new Date(2016, 4, 1),
"Tate Britain": 100981,
"Tate Modern": 479821,
"Tate Liverpool": 51139,
"Tate St Ives": 26962,
},
{
date: new Date(2016, 5, 1),
"Tate Britain": 106932,
"Tate Modern": 338848,
"Tate Liverpool": 57352,
"Tate St Ives": 26841,
},
{
date: new Date(2016, 6, 1),
"Tate Britain": 152988,
"Tate Modern": 421666,
"Tate Liverpool": 68614,
"Tate St Ives": 41982,
},
{
date: new Date(2016, 7, 1),
"Tate Britain": 163845,
"Tate Modern": 424556,
"Tate Liverpool": 79755,
"Tate St Ives": 35360,
},
{
date: new Date(2016, 8, 1),
"Tate Britain": 137612,
"Tate Modern": 335647,
"Tate Liverpool": 58422,
"Tate St Ives": 30991,
},
{
date: new Date(2016, 9, 1),
"Tate Britain": 148505,
"Tate Modern": 408457,
"Tate Liverpool": 45139,
"Tate St Ives": 19982,
},
{
date: new Date(2016, 10, 1),
"Tate Britain": 103923,
"Tate Modern": 364490,
"Tate Liverpool": 34634,
"Tate St Ives": 12805,
},
{
date: new Date(2016, 11, 1),
"Tate Britain": 93850,
"Tate Modern": 316602,
"Tate Liverpool": 25390,
"Tate St Ives": 8706,
},
{
date: new Date(2017, 0, 1),
"Tate Britain": 99825,
"Tate Modern": 358933,
"Tate Liverpool": 30907,
"Tate St Ives": 8936,
},
{
date: new Date(2017, 1, 1),
"Tate Britain": 112826,
"Tate Modern": 415405,
"Tate Liverpool": 39962,
"Tate St Ives": 1197,
},
{
date: new Date(2017, 2, 1),
"Tate Britain": 105283,
"Tate Modern": 432107,
"Tate Liverpool": 35485,
"Tate St Ives": 1858,
},
{
date: new Date(2017, 3, 1),
"Tate Britain": 97022,
"Tate Modern": 517940,
"Tate Liverpool": 40149,
"Tate St Ives": 4365,
},
{
date: new Date(2017, 4, 1),
"Tate Britain": 86268,
"Tate Modern": 613354,
"Tate Liverpool": 41532,
"Tate St Ives": 18110,
},
{
date: new Date(2017, 5, 1),
"Tate Britain": 91233,
"Tate Modern": 501885,
"Tate Liverpool": 46528,
"Tate St Ives": 26490,
},
{
date: new Date(2017, 6, 1),
"Tate Britain": 98553,
"Tate Modern": 595019,
"Tate Liverpool": 68947,
"Tate St Ives": 36274,
},
{
date: new Date(2017, 7, 1),
"Tate Britain": 116933,
"Tate Modern": 686413,
"Tate Liverpool": 64914,
"Tate St Ives": 42241,
},
{
date: new Date(2017, 8, 1),
"Tate Britain": 117077,
"Tate Modern": 445973,
"Tate Liverpool": 52578,
"Tate St Ives": 28253,
},
{
date: new Date(2017, 9, 1),
"Tate Britain": 165984,
"Tate Modern": 473991,
"Tate Liverpool": 51242,
"Tate St Ives": 20720,
},
{
date: new Date(2017, 10, 1),
"Tate Britain": 151838,
"Tate Modern": 377363,
"Tate Liverpool": 49772,
"Tate St Ives": 11008,
},
{
date: new Date(2017, 11, 1),
"Tate Britain": 115036,
"Tate Modern": 367044,
"Tate Liverpool": 38839,
"Tate St Ives": 8894,
},
{
date: new Date(2018, 0, 1),
"Tate Britain": 150835,
"Tate Modern": 341653,
"Tate Liverpool": 49664,
"Tate St Ives": 9185,
},
{
date: new Date(2018, 1, 1),
"Tate Britain": 85319,
"Tate Modern": 380208,
"Tate Liverpool": 53319,
"Tate St Ives": 10556,
},
{
date: new Date(2018, 2, 1),
"Tate Britain": 97391,
"Tate Modern": 401531,
"Tate Liverpool": 40284,
"Tate St Ives": 14394,
},
{
date: new Date(2018, 3, 1),
"Tate Britain": 96213,
"Tate Modern": 421822,
"Tate Liverpool": 47839,
"Tate St Ives": 23268,
},
{
date: new Date(2018, 4, 1),
"Tate Britain": 94446,
"Tate Modern": 484772,
"Tate Liverpool": 58217,
"Tate St Ives": 21366,
},
{
date: new Date(2018, 5, 1),
"Tate Britain": 89751,
"Tate Modern": 388272,
"Tate Liverpool": 43894,
"Tate St Ives": 24837,
},
{
date: new Date(2018, 6, 1),
"Tate Britain": 116414,
"Tate Modern": 475441,
"Tate Liverpool": 71520,
"Tate St Ives": 33999,
},
{
date: new Date(2018, 7, 1),
"Tate Britain": 124208,
"Tate Modern": 444771,
"Tate Liverpool": 80716,
"Tate St Ives": 37277,
},
{
date: new Date(2018, 8, 1),
"Tate Britain": 112709,
"Tate Modern": 321903,
"Tate Liverpool": 53372,
"Tate St Ives": 29469,
},
{
date: new Date(2018, 9, 1),
"Tate Britain": 144855,
"Tate Modern": 375924,
"Tate Liverpool": 60299,
"Tate St Ives": 5049,
},
{
date: new Date(2018, 10, 1),
"Tate Britain": 105103,
"Tate Modern": 337615,
"Tate Liverpool": 33291,
"Tate St Ives": 1312,
},
{
date: new Date(2018, 11, 1),
"Tate Britain": 85376,
"Tate Modern": 338669,
"Tate Liverpool": 33995,
"Tate St Ives": 1208,
},
{
date: new Date(2019, 0, 1),
"Tate Britain": 102183,
"Tate Modern": 361563,
"Tate Liverpool": 37147,
"Tate St Ives": 1061,
},
{
date: new Date(2019, 1, 1),
"Tate Britain": 103836,
"Tate Modern": 287057,
"Tate Liverpool": 49176,
"Tate St Ives": 1708,
},
{
date: new Date(2019, 2, 1),
"Tate Britain": 98606,
"Tate Modern": 349639,
"Tate Liverpool": 48198,
"Tate St Ives": 3094,
},
{
date: new Date(2019, 3, 1),
"Tate Britain": 85141,
"Tate Modern": 305386,
"Tate Liverpool": 46793,
"Tate St Ives": 4022,
},
{
date: new Date(2019, 4, 1),
"Tate Britain": 86284,
"Tate Modern": 297165,
"Tate Liverpool": 48294,
"Tate St Ives": 5260,
},
{
date: new Date(2019, 5, 1),
"Tate Britain": 71559,
"Tate Modern": 604476,
"Tate Liverpool": 51198,
"Tate St Ives": 6808,
},
{
date: new Date(2019, 6, 1),
"Tate Britain": 72169,
"Tate Modern": 811162,
"Tate Liverpool": 73174,
"Tate St Ives": 7917,
},
{
date: new Date(2019, 7, 1),
"Tate Britain": 78179,
"Tate Modern": 704548,
"Tate Liverpool": 82663,
"Tate St Ives": 8615,
},
{
date: new Date(2019, 8, 1),
"Tate Britain": 76485,
"Tate Modern": 545300,
"Tate Liverpool": 60190,
"Tate St Ives": 6271,
},
{
date: new Date(2019, 9, 1),
"Tate Britain": 107346,
"Tate Modern": 681188,
"Tate Liverpool": 63106,
"Tate St Ives": 4168,
},
{
date: new Date(2019, 10, 1),
"Tate Britain": 109595,
"Tate Modern": 436004,
"Tate Liverpool": 41584,
"Tate St Ives": 1460,
},
{
date: new Date(2019, 11, 1),
"Tate Britain": 90159,
"Tate Modern": 455709,
"Tate Liverpool": 37031,
"Tate St Ives": 1019,
},
{
date: new Date(2020, 0, 1),
"Tate Britain": 95021,
"Tate Modern": 449905,
"Tate Liverpool": 37764,
"Tate St Ives": 1005,
},
{
date: new Date(2020, 1, 1),
"Tate Britain": 230582,
"Tate Modern": 506428,
"Tate Liverpool": 52866,
"Tate St Ives": 1607,
},
{
date: new Date(2020, 2, 1),
"Tate Britain": 261157,
"Tate Modern": 598464,
"Tate Liverpool": 41743,
"Tate St Ives": 4250,
},
{
date: new Date(2020, 3, 1),
"Tate Britain": 267289,
"Tate Modern": 671156,
"Tate Liverpool": 56490,
"Tate St Ives": 27072,
},
{
date: new Date(2020, 4, 1),
"Tate Britain": 281482,
"Tate Modern": 508600,
"Tate Liverpool": 47420,
"Tate St Ives": 24018,
},
{
date: new Date(2020, 5, 1),
"Tate Britain": 79354,
"Tate Modern": 368362,
"Tate Liverpool": 45789,
"Tate St Ives": 27410,
},
{
date: new Date(2020, 6, 1),
"Tate Britain": 76255,
"Tate Modern": 470070,
"Tate Liverpool": 67580,
"Tate St Ives": 34527,
},
{
date: new Date(2020, 7, 1),
"Tate Britain": 73507,
"Tate Modern": 460302,
"Tate Liverpool": 82562,
"Tate St Ives": 39910,
},
{
date: new Date(2020, 8, 1),
"Tate Britain": 92375,
"Tate Modern": 406788,
"Tate Liverpool": 57935,
"Tate St Ives": 9240,
},
{
date: new Date(2020, 9, 1),
"Tate Britain": 94926,
"Tate Modern": 505248,
"Tate Liverpool": 61161,
"Tate St Ives": 32506,
},
{
date: new Date(2020, 10, 1),
"Tate Britain": 125618,
"Tate Modern": 406729,
"Tate Liverpool": 41833,
"Tate St Ives": 14149,
},
{
date: new Date(2020, 11, 1),
"Tate Britain": 100311,
"Tate Modern": 415010,
"Tate Liverpool": 34881,
"Tate St Ives": 10538,
},
{
date: new Date(2021, 0, 1),
"Tate Britain": 118050,
"Tate Modern": 448533,
"Tate Liverpool": 37951,
"Tate St Ives": 9812,
},
{
date: new Date(2021, 1, 1),
"Tate Britain": 96918,
"Tate Modern": 463059,
"Tate Liverpool": 54735,
"Tate St Ives": 16387,
},
{
date: new Date(2021, 2, 1),
"Tate Britain": 142412,
"Tate Modern": 584789,
"Tate Liverpool": 55049,
"Tate St Ives": 19606,
},
{
date: new Date(2021, 3, 1),
"Tate Britain": 134842,
"Tate Modern": 577292,
"Tate Liverpool": 65598,
"Tate St Ives": 30779,
},
{
date: new Date(2021, 4, 1),
"Tate Britain": 94857,
"Tate Modern": 488664,
"Tate Liverpool": 65891,
"Tate St Ives": 27820,
},
{
date: new Date(2021, 5, 1),
"Tate Britain": 93405,
"Tate Modern": 445754,
"Tate Liverpool": 56006,
"Tate St Ives": 29558,
},
{
date: new Date(2021, 6, 1),
"Tate Britain": 89300,
"Tate Modern": 503440,
"Tate Liverpool": 71470,
"Tate St Ives": 38558,
},
{
date: new Date(2021, 7, 1),
"Tate Britain": 114589,
"Tate Modern": 588072,
"Tate Liverpool": 84132,
"Tate St Ives": 46959,
},
{
date: new Date(2021, 8, 1),
"Tate Britain": 88753,
"Tate Modern": 465353,
"Tate Liverpool": 64397,
"Tate St Ives": 35388,
},
{
date: new Date(2021, 9, 1),
"Tate Britain": 95888,
"Tate Modern": 497085,
"Tate Liverpool": 72777,
"Tate St Ives": 24968,
},
{
date: new Date(2021, 10, 1),
"Tate Britain": 105967,
"Tate Modern": 415404,
"Tate Liverpool": 42497,
"Tate St Ives": 13902,
},
{
date: new Date(2021, 11, 1),
"Tate Britain": 97542,
"Tate Modern": 391117,
"Tate Liverpool": 36704,
"Tate St Ives": 9127,
},
{
date: new Date(2022, 0, 1),
"Tate Britain": 107878,
"Tate Modern": 444650,
"Tate Liverpool": 39577,
"Tate St Ives": 10398,
},
{
date: new Date(2022, 1, 1),
"Tate Britain": 170352,
"Tate Modern": 457674,
"Tate Liverpool": 53204,
"Tate St Ives": 13354,
},
{
date: new Date(2022, 2, 1),
"Tate Britain": 163916,
"Tate Modern": 524184,
"Tate Liverpool": 49790,
"Tate St Ives": 18923,
},
{
date: new Date(2022, 3, 1),
"Tate Britain": 224037,
"Tate Modern": 482407,
"Tate Liverpool": 49898,
"Tate St Ives": 28151,
},
{
date: new Date(2022, 4, 1),
"Tate Britain": 182813,
"Tate Modern": 464646,
"Tate Liverpool": 46899,
"Tate St Ives": 25636,
},
{
date: new Date(2022, 5, 1),
"Tate Britain": 161564,
"Tate Modern": 456004,
"Tate Liverpool": 58661,
"Tate St Ives": 28374,
},
{
date: new Date(2022, 6, 1),
"Tate Britain": 184697,
"Tate Modern": 603515,
"Tate Liverpool": 74443,
"Tate St Ives": 35269,
},
{
date: new Date(2022, 7, 1),
"Tate Britain": 163401,
"Tate Modern": 624672,
"Tate Liverpool": 84662,
"Tate St Ives": 40934,
},
{
date: new Date(2022, 8, 1),
"Tate Britain": 111366,
"Tate Modern": 496690,
"Tate Liverpool": 56137,
"Tate St Ives": 31331,
},
{
date: new Date(2022, 9, 1),
"Tate Britain": 140026,
"Tate Modern": 604257,
"Tate Liverpool": 65679,
"Tate St Ives": 25995,
},
{
date: new Date(2022, 10, 1),
"Tate Britain": 122798,
"Tate Modern": 502589,
"Tate Liverpool": 48899,
"Tate St Ives": 12183,
},
{
date: new Date(2022, 11, 1),
"Tate Britain": 75789,
"Tate Modern": 437052,
"Tate Liverpool": 32173,
"Tate St Ives": 8199,
},
{
date: new Date(2023, 0, 1),
"Tate Britain": 118376,
"Tate Modern": 344887,
"Tate Liverpool": 42401,
"Tate St Ives": 5048,
},
];
}
Use the buttons to save and restore both the Legend and Zoom states.
function saveState() {
const newState = chart.getState();
// save to database...
}
function restoreState() {
// retrieve state from database...
chart.setState(state);
}In the above example:
- Zoom and pan the chart, and also toggle some legend items. Then click 'Save' to store the chart state using
chart.getState(). - Click the 'Restore' button to restore a saved state to the chart using
chart.setState(). - This will override the current state.
Financial Charts also allow save, restore and update of annotations and chart type.
import {
AgChartState,
AgCharts,
AgFinancialChartOptions,
ContextMenuModule,
FinancialChartModule,
ModuleRegistry,
} from "ag-charts-enterprise";
import { getData } from "./data";
let state: AgChartState = {
version: "11.0.0",
chartType: "ohlc",
annotations: [
{
type: "parallel-channel",
height: 9.692307692304894,
middle: { strokeWidth: 1, lineDash: [6, 5] },
background: { fill: "#5090dc", fillOpacity: 0.2 },
start: {
x: {
__type: "date",
value: "Thu Mar 21 2024 18:45:00 GMT+0000 (Greenwich Mean Time)",
},
y: 39821.692307692305,
},
end: {
x: {
__type: "date",
value: "Thu Mar 21 2024 18:55:00 GMT+0000 (Greenwich Mean Time)",
},
y: 39838.46153846154,
},
handle: { fill: "white" },
stroke: "#5090dc",
strokeOpacity: 1,
strokeWidth: 2,
},
],
zoom: {
rangeX: {
start: {
__type: "date",
value: "Thu Mar 21 2024 18:43:00 GMT+0000 (Greenwich Mean Time)",
},
},
},
};
ModuleRegistry.registerModules([FinancialChartModule]);
const options: AgFinancialChartOptions = {
data: getData(),
title: {
text: "Dow Jones Industrial Average",
},
rangeButtons: false,
};
options.container = document.getElementById("myChart");
const chart = AgCharts.createFinancialChart(options);
function saveState() {
const newState = chart.getState();
state = newState;
console.log("Saved", state);
}
function restoreState() {
chart.setState(state).then(() => {
console.log(`Restored`, state);
});
}
if (typeof window !== "undefined") {
// Attach external event handlers to window so they can be called from index.html
(<any>window).saveState = saveState;
(<any>window).restoreState = restoreState;
}
export function getData() {
return [
{
date: new Date("2024-03-21T18:24:00.000Z"),
volume: 4244973105000,
open: 39863.58,
high: 39870.44,
low: 39862.47,
close: 39867.41,
},
{
date: new Date("2024-03-21T18:25:00.000Z"),
volume: 2351421483000,
open: 39867.02,
high: 39868.19,
low: 39863.48,
close: 39863.65,
},
{
date: new Date("2024-03-21T18:26:00.000Z"),
volume: 2386696495000,
open: 39864.35,
high: 39865.24,
low: 39858.74,
close: 39858.85,
},
{
date: new Date("2024-03-21T18:27:00.000Z"),
volume: 2055431221000,
open: 39858.04,
high: 39865.13,
low: 39858.04,
close: 39861.59,
},
{
date: new Date("2024-03-21T18:28:00.000Z"),
volume: 2172253124000,
open: 39859.33,
high: 39859.33,
low: 39855.74,
close: 39855.93,
},
{
date: new Date("2024-03-21T18:29:00.000Z"),
volume: 2046722089000,
open: 39851.89,
high: 39854.23,
low: 39851.85,
close: 39854.23,
},
{
date: new Date("2024-03-21T18:30:00.000Z"),
volume: 2111185396000,
open: 39853.09,
high: 39853.09,
low: 39844.98,
close: 39845.26,
},
{
date: new Date("2024-03-21T18:31:00.000Z"),
volume: 1850766477000,
open: 39841.15,
high: 39847.25,
low: 39839.9,
close: 39847.25,
},
{
date: new Date("2024-03-21T18:32:00.000Z"),
volume: 1955913253000,
open: 39847.75,
high: 39850.3,
low: 39846.01,
close: 39850.3,
},
{
date: new Date("2024-03-21T18:33:00.000Z"),
volume: 1977157506000,
open: 39849.21,
high: 39849.21,
low: 39842.77,
close: 39842.77,
},
{
date: new Date("2024-03-21T18:34:00.000Z"),
volume: 2001143711000,
open: 39838.3,
high: 39838.3,
low: 39828.7,
close: 39829.22,
},
{
date: new Date("2024-03-21T18:35:00.000Z"),
volume: 2216839469000,
open: 39829.55,
high: 39829.55,
low: 39829.55,
close: 39829.55,
},
{
date: new Date("2024-03-21T18:36:00.000Z"),
volume: 2157858548000,
open: 39831.68,
high: 39831.68,
low: 39826.62,
close: 39826.62,
},
{
date: new Date("2024-03-21T18:37:00.000Z"),
volume: 2057592847000,
open: 39827.69,
high: 39832.95,
low: 39827.69,
close: 39829.31,
},
{
date: new Date("2024-03-21T18:38:00.000Z"),
volume: 1905849406000,
open: 39828.53,
high: 39832.68,
low: 39823.05,
close: 39832.68,
},
{
date: new Date("2024-03-21T18:39:00.000Z"),
volume: 2013748142000,
open: 39833.53,
high: 39834.54,
low: 39829.58,
close: 39829.68,
},
{
date: new Date("2024-03-21T18:40:00.000Z"),
volume: 2077193771000,
open: 39828.04,
high: 39829.34,
low: 39825.69,
close: 39827.32,
},
{
date: new Date("2024-03-21T18:41:00.000Z"),
volume: 1959845124000,
open: 39827.96,
high: 39828.99,
low: 39819.97,
close: 39819.97,
},
{
date: new Date("2024-03-21T18:42:00.000Z"),
volume: 1669881577000,
open: 39817.52,
high: 39825.81,
low: 39817.52,
close: 39823.98,
},
{
date: new Date("2024-03-21T18:43:00.000Z"),
volume: 1936663046000,
open: 39821.88,
high: 39822.98,
low: 39817.52,
close: 39820.22,
},
{
date: new Date("2024-03-21T18:44:00.000Z"),
volume: 1827645178000,
open: 39821.42,
high: 39824.13,
low: 39821.42,
close: 39821.49,
},
{
date: new Date("2024-03-21T18:45:00.000Z"),
volume: 2352361081000,
open: 39821.4,
high: 39821.4,
low: 39815.71,
close: 39817.62,
},
{
date: new Date("2024-03-21T18:46:00.000Z"),
volume: 1958155311000,
open: 39817.86,
high: 39824.6,
low: 39817.48,
close: 39824.6,
},
{
date: new Date("2024-03-21T18:47:00.000Z"),
volume: 2128341430000,
open: 39824.88,
high: 39828.78,
low: 39821.66,
close: 39828.78,
},
{
date: new Date("2024-03-21T18:48:00.000Z"),
volume: 2138052710000,
open: 39829.22,
high: 39829.78,
low: 39822.64,
close: 39822.64,
},
{
date: new Date("2024-03-21T18:49:00.000Z"),
volume: 2441134922000,
open: 39822.16,
high: 39825.81,
low: 39822.06,
close: 39825.81,
},
{
date: new Date("2024-03-21T18:50:00.000Z"),
volume: 2076441612000,
open: 39828.12,
high: 39831.04,
low: 39828,
close: 39828.55,
},
{
date: new Date("2024-03-21T18:51:00.000Z"),
volume: 2165432434000,
open: 39830.12,
high: 39830.6,
low: 39825.13,
close: 39825.13,
},
{
date: new Date("2024-03-21T18:52:00.000Z"),
volume: 2184137855000,
open: 39825.67,
high: 39829.1,
low: 39823.67,
close: 39825.9,
},
{
date: new Date("2024-03-21T18:53:00.000Z"),
volume: 2268926200000,
open: 39825.5,
high: 39834.47,
low: 39825.5,
close: 39834.47,
},
{
date: new Date("2024-03-21T18:54:00.000Z"),
volume: 2275484003000,
open: 39836.13,
high: 39837.39,
low: 39832.39,
close: 39837.39,
},
{
date: new Date("2024-03-21T18:55:00.000Z"),
volume: 4244973105000,
open: 39838.38,
high: 39838.38,
low: 39831.4,
close: 39833.44,
},
{
date: new Date("2024-03-21T18:56:00.000Z"),
volume: 1932361461000,
open: 39832.39,
high: 39832.39,
low: 39826.3,
close: 39828.56,
},
{
date: new Date("2024-03-21T18:57:00.000Z"),
volume: 2018270144000,
open: 39828.3,
high: 39830.1,
low: 39822.64,
close: 39823.19,
},
{
date: new Date("2024-03-21T18:58:00.000Z"),
volume: 1976745837000,
open: 39824.52,
high: 39826.63,
low: 39818.9,
close: 39818.9,
},
{
date: new Date("2024-03-21T18:59:00.000Z"),
volume: 2298552282000,
open: 39820.9,
high: 39824.11,
low: 39819.72,
close: 39823.18,
},
{
date: new Date("2024-03-21T19:00:00.000Z"),
volume: 2135953365000,
open: 39823.85,
high: 39828.46,
low: 39823.85,
close: 39828.21,
},
{
date: new Date("2024-03-21T19:01:00.000Z"),
volume: 1858119044000,
open: 39827.68,
high: 39830.59,
low: 39827.68,
close: 39830.1,
},
{
date: new Date("2024-03-21T19:02:00.000Z"),
volume: 2129442202000,
open: 39830.63,
high: 39834.28,
low: 39829.71,
close: 39829.71,
},
{
date: new Date("2024-03-21T19:03:00.000Z"),
volume: 2299297267000,
open: 39827.14,
high: 39827.31,
low: 39816.49,
close: 39816.49,
},
{
date: new Date("2024-03-21T19:04:00.000Z"),
volume: 2235764324000,
open: 39817.52,
high: 39817.52,
low: 39810.94,
close: 39811.84,
},
{
date: new Date("2024-03-21T19:05:00.000Z"),
volume: 2411036284000,
open: 39813.32,
high: 39817.11,
low: 39812.82,
close: 39816.44,
},
{
date: new Date("2024-03-21T19:06:00.000Z"),
volume: 2419266832000,
open: 39813.03,
high: 39817.34,
low: 39808.39,
close: 39816.15,
},
{
date: new Date("2024-03-21T19:07:00.000Z"),
volume: 2430973527000,
open: 39815.04,
high: 39820.99,
low: 39813.98,
close: 39816.66,
},
{
date: new Date("2024-03-21T19:08:00.000Z"),
volume: 2329779403000,
open: 39816.36,
high: 39816.36,
low: 39810.55,
close: 39812.57,
},
{
date: new Date("2024-03-21T19:09:00.000Z"),
volume: 2230253462000,
open: 39812.98,
high: 39819.43,
low: 39812.65,
close: 39819.43,
},
{
date: new Date("2024-03-21T19:10:00.000Z"),
volume: 2493423103000,
open: 39819.2,
high: 39826.03,
low: 39817.89,
close: 39826.03,
},
{
date: new Date("2024-03-21T19:11:00.000Z"),
volume: 1991524626000,
open: 39826.19,
high: 39829.06,
low: 39824.82,
close: 39827.21,
},
{
date: new Date("2024-03-21T19:12:00.000Z"),
volume: 2160880932000,
open: 39823.98,
high: 39828.95,
low: 39820.27,
close: 39828.95,
},
{
date: new Date("2024-03-21T19:13:00.000Z"),
volume: 2115648071000,
open: 39829.2,
high: 39829.51,
low: 39826.63,
close: 39827.79,
},
{
date: new Date("2024-03-21T19:14:00.000Z"),
volume: 2344769422000,
open: 39827.64,
high: 39833.08,
low: 39827.64,
close: 39832.18,
},
{
date: new Date("2024-03-21T19:15:00.000Z"),
volume: 2302663368000,
open: 39831.95,
high: 39833.68,
low: 39829.5,
close: 39833.68,
},
{
date: new Date("2024-03-21T19:16:00.000Z"),
volume: 2124400350000,
open: 39835.38,
high: 39835.38,
low: 39833.59,
close: 39833.73,
},
{
date: new Date("2024-03-21T19:17:00.000Z"),
volume: 2355659575000,
open: 39834.57,
high: 39835,
low: 39829.16,
close: 39829.35,
},
{
date: new Date("2024-03-21T19:18:00.000Z"),
volume: 2390659124000,
open: 39828.25,
high: 39828.25,
low: 39817.87,
close: 39818.05,
},
{
date: new Date("2024-03-21T19:19:00.000Z"),
volume: 2635705086000,
open: 39816.43,
high: 39823.57,
low: 39815.17,
close: 39816.26,
},
{
date: new Date("2024-03-21T19:20:00.000Z"),
volume: 2734219191000,
open: 39817.57,
high: 39822.8,
low: 39815.79,
close: 39820.7,
},
{
date: new Date("2024-03-21T19:21:00.000Z"),
volume: 2454581997000,
open: 39821.29,
high: 39827.81,
low: 39820.51,
close: 39827.81,
},
{
date: new Date("2024-03-21T19:22:00.000Z"),
volume: 2472963274000,
open: 39825.8,
high: 39831.08,
low: 39822.18,
close: 39828.73,
},
{
date: new Date("2024-03-21T19:23:00.000Z"),
volume: 2665565303000,
open: 39828.91,
high: 39829.61,
low: 39822.32,
close: 39823.74,
},
{
date: new Date("2024-03-21T19:24:00.000Z"),
volume: 2959734415000,
open: 39823.62,
high: 39823.62,
low: 39818.14,
close: 39818.53,
},
{
date: new Date("2024-03-21T19:25:00.000Z"),
volume: 2784710672000,
open: 39817.25,
high: 39820.73,
low: 39816.06,
close: 39818.79,
},
{
date: new Date("2024-03-21T19:26:00.000Z"),
volume: 2600517109000,
open: 39819.62,
high: 39821.82,
low: 39817.54,
close: 39817.54,
},
{
date: new Date("2024-03-21T19:27:00.000Z"),
volume: 2701786725000,
open: 39817.37,
high: 39824.05,
low: 39817.37,
close: 39820.21,
},
{
date: new Date("2024-03-21T19:28:00.000Z"),
volume: 2804876425000,
open: 39822.27,
high: 39823.76,
low: 39819.22,
close: 39823.76,
},
{
date: new Date("2024-03-21T19:29:00.000Z"),
volume: 1711045140000,
open: 39826.99,
high: 39830.62,
low: 39826.95,
close: 39829.89,
},
{
date: new Date("2024-03-21T19:30:00.000Z"),
volume: 2298552282000,
open: 39830.61,
high: 39839.61,
low: 39830.61,
close: 39838.06,
},
{
date: new Date("2024-03-21T19:31:00.000Z"),
volume: 1711045260000,
open: 39836.44,
high: 39843.47,
low: 39835.66,
close: 39842.69,
},
{
date: new Date("2024-03-21T19:32:00.000Z"),
volume: 2472963274000,
open: 39843.12,
high: 39843.12,
low: 39833.49,
close: 39833.49,
},
{
date: new Date("2024-03-21T19:33:00.000Z"),
volume: 1711045380000,
open: 39833.53,
high: 39833.53,
low: 39831.84,
close: 39833.01,
},
];
}
In the above example:
- Use the toolbar to create annotations and change the chart type, and then click 'Save' to store the chart state using
chart.getState(). - Click the 'Restore' button to restore a saved state to the chart using
chart.setState(). - This will override the current state.
Initial State Copy Link
The initialState chart option allows creating a chart with a saved state already applied.
Additionally, mutating this option at runtime will modify the chart state dynamically.
import {
AgCartesianChartOptions,
AgCartesianSeriesTooltipRendererParams,
AgCharts,
AnimationModule,
AreaSeriesModule,
ContextMenuModule,
CrosshairModule,
LegendModule,
ModuleRegistry,
NavigatorModule,
NumberAxisModule,
UnitTimeAxisModule,
ZoomModule,
} from "ag-charts-enterprise";
import { DataType, getData } from "./data";
const data = getData();
const dateFormatter = new Intl.DateTimeFormat("en-GB");
const tooltip = {
renderer: ({
datum,
yKey,
}: AgCartesianSeriesTooltipRendererParams<DataType>) => {
const value = `${Math.round(Number(datum[yKey]) / 100) / 10 + "k"}`;
return { data: [{ label: dateFormatter.format(datum.date), value }] };
},
};
ModuleRegistry.registerModules([
AnimationModule,
AreaSeriesModule,
CrosshairModule,
LegendModule,
NavigatorModule,
NumberAxisModule,
UnitTimeAxisModule,
ZoomModule,
ContextMenuModule,
]);
const options: AgCartesianChartOptions<DataType> = {
title: {
text: "Total Visitors to Tate Galleries",
},
footnote: {
text: "Source: Department for Digital, Culture, Media & Sport",
},
data,
navigator: {
enabled: true,
},
zoom: {
enabled: true,
},
initialState: {
zoom: {
rangeX: {
start: {
__type: "date",
value: new Date("2021-01-01").getTime(),
},
},
},
legend: [
{
seriesId: "tate-modern",
visible: false,
},
],
},
series: [
{
type: "area",
xKey: "date",
yKey: "Tate Modern",
yName: "Tate Modern",
id: "tate-modern",
tooltip,
},
{
type: "area",
xKey: "date",
yKey: "Tate Britain",
yName: "Tate Britain",
id: "tate-britain",
tooltip,
},
{
type: "area",
xKey: "date",
yKey: "Tate Liverpool",
yName: "Tate Liverpool",
id: "tate-liverpool",
tooltip,
},
{
type: "area",
xKey: "date",
yKey: "Tate St Ives",
yName: "Tate St Ives",
id: "tate-st-ives",
tooltip,
},
],
axes: {
x: {
type: "unit-time",
},
y: {
type: "number",
title: {
text: "Total visitors",
},
label: {
formatter: (params) => {
return params.value / 1000 + "k";
},
},
},
},
};
options.container = document.getElementById("myChart");
const chart = AgCharts.create(options);
function showSixMonths() {
options.initialState!.zoom = {
rangeX: {
start: {
__type: "date",
value:
data[data.length - 1].date.getTime() - 1000 * 60 * 60 * 24 * 30 * 6,
},
},
};
chart.update(options);
}
function show2019() {
options.initialState!.zoom = {
rangeX: {
start: {
__type: "date",
value: new Date("2019-01-01").getTime(),
},
end: {
__type: "date",
value: new Date("2020-01-01").getTime(),
},
},
};
chart.update(options);
}
function showAll() {
options.initialState!.zoom = {};
chart.update(options);
}
function setInitialLegendState() {
options.initialState!.legend = [
{
seriesId: "tate-modern",
visible: false,
},
{
seriesId: "tate-liverpool",
visible: false,
},
];
chart.update(options);
}
function resetInitialLegendState() {
options.initialState!.legend = [
{
seriesId: "tate-modern",
visible: true,
},
{
seriesId: "tate-liverpool",
visible: true,
},
{
seriesId: "tate-britain",
visible: true,
},
{
seriesId: "tate-st-ives",
visible: true,
},
];
chart.update(options);
}
if (typeof window !== "undefined") {
// Attach external event handlers to window so they can be called from index.html
(<any>window).showSixMonths = showSixMonths;
(<any>window).show2019 = show2019;
(<any>window).showAll = showAll;
(<any>window).setInitialLegendState = setInitialLegendState;
(<any>window).resetInitialLegendState = resetInitialLegendState;
}
export interface DataType {
date: Date;
"Tate Britain": number;
"Tate Modern": number;
"Tate Liverpool": number;
"Tate St Ives": number;
}
export function getData(): DataType[] {
return [
{
date: new Date(2015, 0, 1),
"Tate Britain": 103268,
"Tate Modern": 307185,
"Tate Liverpool": 43888,
"Tate St Ives": 3939,
},
{
date: new Date(2015, 1, 1),
"Tate Britain": 145525,
"Tate Modern": 365921,
"Tate Liverpool": 40130,
"Tate St Ives": 8145,
},
{
date: new Date(2015, 2, 1),
"Tate Britain": 166384,
"Tate Modern": 393326,
"Tate Liverpool": 33696,
"Tate St Ives": 10328,
},
{
date: new Date(2015, 3, 1),
"Tate Britain": 148871,
"Tate Modern": 566058,
"Tate Liverpool": 47671,
"Tate St Ives": 20663,
},
{
date: new Date(2015, 4, 1),
"Tate Britain": 125526,
"Tate Modern": 447241,
"Tate Liverpool": 37075,
"Tate St Ives": 16965,
},
{
date: new Date(2015, 5, 1),
"Tate Britain": 122866,
"Tate Modern": 428800,
"Tate Liverpool": 55740,
"Tate St Ives": 24119,
},
{
date: new Date(2015, 6, 1),
"Tate Britain": 107487,
"Tate Modern": 530400,
"Tate Liverpool": 79328,
"Tate St Ives": 28612,
},
{
date: new Date(2015, 7, 1),
"Tate Britain": 81997,
"Tate Modern": 591215,
"Tate Liverpool": 85698,
"Tate St Ives": 32666,
},
{
date: new Date(2015, 8, 1),
"Tate Britain": 118527,
"Tate Modern": 490750,
"Tate Liverpool": 63270,
"Tate St Ives": 21066,
},
{
date: new Date(2015, 9, 1),
"Tate Britain": 164064,
"Tate Modern": 492280,
"Tate Liverpool": 70965,
"Tate St Ives": 16249,
},
{
date: new Date(2015, 10, 1),
"Tate Britain": 144861,
"Tate Modern": 358494,
"Tate Liverpool": 37767,
"Tate St Ives": 7874,
},
{
date: new Date(2015, 11, 1),
"Tate Britain": 106191,
"Tate Modern": 333040,
"Tate Liverpool": 24896,
"Tate St Ives": 5419,
},
{
date: new Date(2016, 0, 1),
"Tate Britain": 107297,
"Tate Modern": 323835,
"Tate Liverpool": 25313,
"Tate St Ives": 5772,
},
{
date: new Date(2016, 1, 1),
"Tate Britain": 77074,
"Tate Modern": 419576,
"Tate Liverpool": 50027,
"Tate St Ives": 9860,
},
{
date: new Date(2016, 2, 1),
"Tate Britain": 92849,
"Tate Modern": 542205,
"Tate Liverpool": 43105,
"Tate St Ives": 15329,
},
{
date: new Date(2016, 3, 1),
"Tate Britain": 92416,
"Tate Modern": 509236,
"Tate Liverpool": 44228,
"Tate St Ives": 25089,
},
{
date: new Date(2016, 4, 1),
"Tate Britain": 100981,
"Tate Modern": 479821,
"Tate Liverpool": 51139,
"Tate St Ives": 26962,
},
{
date: new Date(2016, 5, 1),
"Tate Britain": 106932,
"Tate Modern": 338848,
"Tate Liverpool": 57352,
"Tate St Ives": 26841,
},
{
date: new Date(2016, 6, 1),
"Tate Britain": 152988,
"Tate Modern": 421666,
"Tate Liverpool": 68614,
"Tate St Ives": 41982,
},
{
date: new Date(2016, 7, 1),
"Tate Britain": 163845,
"Tate Modern": 424556,
"Tate Liverpool": 79755,
"Tate St Ives": 35360,
},
{
date: new Date(2016, 8, 1),
"Tate Britain": 137612,
"Tate Modern": 335647,
"Tate Liverpool": 58422,
"Tate St Ives": 30991,
},
{
date: new Date(2016, 9, 1),
"Tate Britain": 148505,
"Tate Modern": 408457,
"Tate Liverpool": 45139,
"Tate St Ives": 19982,
},
{
date: new Date(2016, 10, 1),
"Tate Britain": 103923,
"Tate Modern": 364490,
"Tate Liverpool": 34634,
"Tate St Ives": 12805,
},
{
date: new Date(2016, 11, 1),
"Tate Britain": 93850,
"Tate Modern": 316602,
"Tate Liverpool": 25390,
"Tate St Ives": 8706,
},
{
date: new Date(2017, 0, 1),
"Tate Britain": 99825,
"Tate Modern": 358933,
"Tate Liverpool": 30907,
"Tate St Ives": 8936,
},
{
date: new Date(2017, 1, 1),
"Tate Britain": 112826,
"Tate Modern": 415405,
"Tate Liverpool": 39962,
"Tate St Ives": 1197,
},
{
date: new Date(2017, 2, 1),
"Tate Britain": 105283,
"Tate Modern": 432107,
"Tate Liverpool": 35485,
"Tate St Ives": 1858,
},
{
date: new Date(2017, 3, 1),
"Tate Britain": 97022,
"Tate Modern": 517940,
"Tate Liverpool": 40149,
"Tate St Ives": 4365,
},
{
date: new Date(2017, 4, 1),
"Tate Britain": 86268,
"Tate Modern": 613354,
"Tate Liverpool": 41532,
"Tate St Ives": 18110,
},
{
date: new Date(2017, 5, 1),
"Tate Britain": 91233,
"Tate Modern": 501885,
"Tate Liverpool": 46528,
"Tate St Ives": 26490,
},
{
date: new Date(2017, 6, 1),
"Tate Britain": 98553,
"Tate Modern": 595019,
"Tate Liverpool": 68947,
"Tate St Ives": 36274,
},
{
date: new Date(2017, 7, 1),
"Tate Britain": 116933,
"Tate Modern": 686413,
"Tate Liverpool": 64914,
"Tate St Ives": 42241,
},
{
date: new Date(2017, 8, 1),
"Tate Britain": 117077,
"Tate Modern": 445973,
"Tate Liverpool": 52578,
"Tate St Ives": 28253,
},
{
date: new Date(2017, 9, 1),
"Tate Britain": 165984,
"Tate Modern": 473991,
"Tate Liverpool": 51242,
"Tate St Ives": 20720,
},
{
date: new Date(2017, 10, 1),
"Tate Britain": 151838,
"Tate Modern": 377363,
"Tate Liverpool": 49772,
"Tate St Ives": 11008,
},
{
date: new Date(2017, 11, 1),
"Tate Britain": 115036,
"Tate Modern": 367044,
"Tate Liverpool": 38839,
"Tate St Ives": 8894,
},
{
date: new Date(2018, 0, 1),
"Tate Britain": 150835,
"Tate Modern": 341653,
"Tate Liverpool": 49664,
"Tate St Ives": 9185,
},
{
date: new Date(2018, 1, 1),
"Tate Britain": 85319,
"Tate Modern": 380208,
"Tate Liverpool": 53319,
"Tate St Ives": 10556,
},
{
date: new Date(2018, 2, 1),
"Tate Britain": 97391,
"Tate Modern": 401531,
"Tate Liverpool": 40284,
"Tate St Ives": 14394,
},
{
date: new Date(2018, 3, 1),
"Tate Britain": 96213,
"Tate Modern": 421822,
"Tate Liverpool": 47839,
"Tate St Ives": 23268,
},
{
date: new Date(2018, 4, 1),
"Tate Britain": 94446,
"Tate Modern": 484772,
"Tate Liverpool": 58217,
"Tate St Ives": 21366,
},
{
date: new Date(2018, 5, 1),
"Tate Britain": 89751,
"Tate Modern": 388272,
"Tate Liverpool": 43894,
"Tate St Ives": 24837,
},
{
date: new Date(2018, 6, 1),
"Tate Britain": 116414,
"Tate Modern": 475441,
"Tate Liverpool": 71520,
"Tate St Ives": 33999,
},
{
date: new Date(2018, 7, 1),
"Tate Britain": 124208,
"Tate Modern": 444771,
"Tate Liverpool": 80716,
"Tate St Ives": 37277,
},
{
date: new Date(2018, 8, 1),
"Tate Britain": 112709,
"Tate Modern": 321903,
"Tate Liverpool": 53372,
"Tate St Ives": 29469,
},
{
date: new Date(2018, 9, 1),
"Tate Britain": 144855,
"Tate Modern": 375924,
"Tate Liverpool": 60299,
"Tate St Ives": 5049,
},
{
date: new Date(2018, 10, 1),
"Tate Britain": 105103,
"Tate Modern": 337615,
"Tate Liverpool": 33291,
"Tate St Ives": 1312,
},
{
date: new Date(2018, 11, 1),
"Tate Britain": 85376,
"Tate Modern": 338669,
"Tate Liverpool": 33995,
"Tate St Ives": 1208,
},
{
date: new Date(2019, 0, 1),
"Tate Britain": 102183,
"Tate Modern": 361563,
"Tate Liverpool": 37147,
"Tate St Ives": 1061,
},
{
date: new Date(2019, 1, 1),
"Tate Britain": 103836,
"Tate Modern": 287057,
"Tate Liverpool": 49176,
"Tate St Ives": 1708,
},
{
date: new Date(2019, 2, 1),
"Tate Britain": 98606,
"Tate Modern": 349639,
"Tate Liverpool": 48198,
"Tate St Ives": 3094,
},
{
date: new Date(2019, 3, 1),
"Tate Britain": 85141,
"Tate Modern": 305386,
"Tate Liverpool": 46793,
"Tate St Ives": 4022,
},
{
date: new Date(2019, 4, 1),
"Tate Britain": 86284,
"Tate Modern": 297165,
"Tate Liverpool": 48294,
"Tate St Ives": 5260,
},
{
date: new Date(2019, 5, 1),
"Tate Britain": 71559,
"Tate Modern": 604476,
"Tate Liverpool": 51198,
"Tate St Ives": 6808,
},
{
date: new Date(2019, 6, 1),
"Tate Britain": 72169,
"Tate Modern": 811162,
"Tate Liverpool": 73174,
"Tate St Ives": 7917,
},
{
date: new Date(2019, 7, 1),
"Tate Britain": 78179,
"Tate Modern": 704548,
"Tate Liverpool": 82663,
"Tate St Ives": 8615,
},
{
date: new Date(2019, 8, 1),
"Tate Britain": 76485,
"Tate Modern": 545300,
"Tate Liverpool": 60190,
"Tate St Ives": 6271,
},
{
date: new Date(2019, 9, 1),
"Tate Britain": 107346,
"Tate Modern": 681188,
"Tate Liverpool": 63106,
"Tate St Ives": 4168,
},
{
date: new Date(2019, 10, 1),
"Tate Britain": 109595,
"Tate Modern": 436004,
"Tate Liverpool": 41584,
"Tate St Ives": 1460,
},
{
date: new Date(2019, 11, 1),
"Tate Britain": 90159,
"Tate Modern": 455709,
"Tate Liverpool": 37031,
"Tate St Ives": 1019,
},
{
date: new Date(2020, 0, 1),
"Tate Britain": 95021,
"Tate Modern": 449905,
"Tate Liverpool": 37764,
"Tate St Ives": 1005,
},
{
date: new Date(2020, 1, 1),
"Tate Britain": 230582,
"Tate Modern": 506428,
"Tate Liverpool": 52866,
"Tate St Ives": 1607,
},
{
date: new Date(2020, 2, 1),
"Tate Britain": 261157,
"Tate Modern": 598464,
"Tate Liverpool": 41743,
"Tate St Ives": 4250,
},
{
date: new Date(2020, 3, 1),
"Tate Britain": 267289,
"Tate Modern": 671156,
"Tate Liverpool": 56490,
"Tate St Ives": 27072,
},
{
date: new Date(2020, 4, 1),
"Tate Britain": 281482,
"Tate Modern": 508600,
"Tate Liverpool": 47420,
"Tate St Ives": 24018,
},
{
date: new Date(2020, 5, 1),
"Tate Britain": 79354,
"Tate Modern": 368362,
"Tate Liverpool": 45789,
"Tate St Ives": 27410,
},
{
date: new Date(2020, 6, 1),
"Tate Britain": 76255,
"Tate Modern": 470070,
"Tate Liverpool": 67580,
"Tate St Ives": 34527,
},
{
date: new Date(2020, 7, 1),
"Tate Britain": 73507,
"Tate Modern": 460302,
"Tate Liverpool": 82562,
"Tate St Ives": 39910,
},
{
date: new Date(2020, 8, 1),
"Tate Britain": 92375,
"Tate Modern": 406788,
"Tate Liverpool": 57935,
"Tate St Ives": 9240,
},
{
date: new Date(2020, 9, 1),
"Tate Britain": 94926,
"Tate Modern": 505248,
"Tate Liverpool": 61161,
"Tate St Ives": 32506,
},
{
date: new Date(2020, 10, 1),
"Tate Britain": 125618,
"Tate Modern": 406729,
"Tate Liverpool": 41833,
"Tate St Ives": 14149,
},
{
date: new Date(2020, 11, 1),
"Tate Britain": 100311,
"Tate Modern": 415010,
"Tate Liverpool": 34881,
"Tate St Ives": 10538,
},
{
date: new Date(2021, 0, 1),
"Tate Britain": 118050,
"Tate Modern": 448533,
"Tate Liverpool": 37951,
"Tate St Ives": 9812,
},
{
date: new Date(2021, 1, 1),
"Tate Britain": 96918,
"Tate Modern": 463059,
"Tate Liverpool": 54735,
"Tate St Ives": 16387,
},
{
date: new Date(2021, 2, 1),
"Tate Britain": 142412,
"Tate Modern": 584789,
"Tate Liverpool": 55049,
"Tate St Ives": 19606,
},
{
date: new Date(2021, 3, 1),
"Tate Britain": 134842,
"Tate Modern": 577292,
"Tate Liverpool": 65598,
"Tate St Ives": 30779,
},
{
date: new Date(2021, 4, 1),
"Tate Britain": 94857,
"Tate Modern": 488664,
"Tate Liverpool": 65891,
"Tate St Ives": 27820,
},
{
date: new Date(2021, 5, 1),
"Tate Britain": 93405,
"Tate Modern": 445754,
"Tate Liverpool": 56006,
"Tate St Ives": 29558,
},
{
date: new Date(2021, 6, 1),
"Tate Britain": 89300,
"Tate Modern": 503440,
"Tate Liverpool": 71470,
"Tate St Ives": 38558,
},
{
date: new Date(2021, 7, 1),
"Tate Britain": 114589,
"Tate Modern": 588072,
"Tate Liverpool": 84132,
"Tate St Ives": 46959,
},
{
date: new Date(2021, 8, 1),
"Tate Britain": 88753,
"Tate Modern": 465353,
"Tate Liverpool": 64397,
"Tate St Ives": 35388,
},
{
date: new Date(2021, 9, 1),
"Tate Britain": 95888,
"Tate Modern": 497085,
"Tate Liverpool": 72777,
"Tate St Ives": 24968,
},
{
date: new Date(2021, 10, 1),
"Tate Britain": 105967,
"Tate Modern": 415404,
"Tate Liverpool": 42497,
"Tate St Ives": 13902,
},
{
date: new Date(2021, 11, 1),
"Tate Britain": 97542,
"Tate Modern": 391117,
"Tate Liverpool": 36704,
"Tate St Ives": 9127,
},
{
date: new Date(2022, 0, 1),
"Tate Britain": 107878,
"Tate Modern": 444650,
"Tate Liverpool": 39577,
"Tate St Ives": 10398,
},
{
date: new Date(2022, 1, 1),
"Tate Britain": 170352,
"Tate Modern": 457674,
"Tate Liverpool": 53204,
"Tate St Ives": 13354,
},
{
date: new Date(2022, 2, 1),
"Tate Britain": 163916,
"Tate Modern": 524184,
"Tate Liverpool": 49790,
"Tate St Ives": 18923,
},
{
date: new Date(2022, 3, 1),
"Tate Britain": 224037,
"Tate Modern": 482407,
"Tate Liverpool": 49898,
"Tate St Ives": 28151,
},
{
date: new Date(2022, 4, 1),
"Tate Britain": 182813,
"Tate Modern": 464646,
"Tate Liverpool": 46899,
"Tate St Ives": 25636,
},
{
date: new Date(2022, 5, 1),
"Tate Britain": 161564,
"Tate Modern": 456004,
"Tate Liverpool": 58661,
"Tate St Ives": 28374,
},
{
date: new Date(2022, 6, 1),
"Tate Britain": 184697,
"Tate Modern": 603515,
"Tate Liverpool": 74443,
"Tate St Ives": 35269,
},
{
date: new Date(2022, 7, 1),
"Tate Britain": 163401,
"Tate Modern": 624672,
"Tate Liverpool": 84662,
"Tate St Ives": 40934,
},
{
date: new Date(2022, 8, 1),
"Tate Britain": 111366,
"Tate Modern": 496690,
"Tate Liverpool": 56137,
"Tate St Ives": 31331,
},
{
date: new Date(2022, 9, 1),
"Tate Britain": 140026,
"Tate Modern": 604257,
"Tate Liverpool": 65679,
"Tate St Ives": 25995,
},
{
date: new Date(2022, 10, 1),
"Tate Britain": 122798,
"Tate Modern": 502589,
"Tate Liverpool": 48899,
"Tate St Ives": 12183,
},
{
date: new Date(2022, 11, 1),
"Tate Britain": 75789,
"Tate Modern": 437052,
"Tate Liverpool": 32173,
"Tate St Ives": 8199,
},
{
date: new Date(2023, 0, 1),
"Tate Britain": 118376,
"Tate Modern": 344887,
"Tate Liverpool": 42401,
"Tate St Ives": 5048,
},
];
}
The objects provided to this property should be the same as the object returned from the getState() method.
{
initialState: {
zoom: {
rangeX: {
start: {
__type: 'date',
value: new Date('2021-01-01').getTime(),
},
},
},
legend: [
{
seriesId: 'tate-modern',
visible: false,
},
],
},
}In this example:
- The chart loads with an
initialStatefor both Zoom and Legend. - Clicking the buttons updates the
initialState, changing the Zoom or Legend toggle state. - Note that the same approach applies to Financial Charts.
State Contents Copy Link
zoom- This object contains start and end ranges of the zoom.legend- This array contains the current visibility of each series or item in the legend.legendPagination- This number is the current legend page of a paginated legend.active- This object contains the currently active item, which is the series node or legend item that is highlighted/showing a tooltip.annotations- This object contains the position and style of any displayed drawings or text annotations.collapsed- This array contains the identifiers of the currently collapsed items in an Org Chart.chartType- This string is one of the Chart Types.
Data Selection is not part of the chart state. See Selection API more details.
Note that all the state properties are optional, so a property can be excluded if you do not want to restore it.
Date objects cannot be serialised, so should instead be provided as an AgStateSerializableDate object in the format { __type: 'date', value: string | number } with a value of any date string or a timestamp number.
Dates returned from chart.getState() will be in the ISO-8601 format and UTC timezone.
Zoom Copy Link
Zoom state can be provided as a range or ratio on each axis direction with either the rangeX & rangeY or ratioX & ratioY properties.
If both a range and ratio are provided, only the rangeX and rangeY values will be used.
The start and end properties of rangeX and rangeY should match the type of axis, e.g. a date for an Ordinal Time Axis.
The start and end properties of ratioX and ratioY should be a value between 0 and 1 as a proportion of the width or height of the chart.
Legend Copy Link
The initial legend state can be configured with an array of objects defining legend items for the series in the chart. Each object includes a seriesId to match the series and a visible property to control its visibility.
For series with multiple legend items, such as pie or donut, an itemId specifies data elements by their index in the data array.
A legendItemName can be added to the legend initial state and series options, taking priority over both seriesId and itemId.
Legend Pagination Copy Link
The legendPagination state stores the current legend page as a zero-based index.
If the saved page does not exist due to a change in chart size or legend configuration, it is clamped to the last available page.
Active Copy Link
The active state gives programmatic control over chart highlighting and tooltips, as well as saving and restoring the active state.
The active item's itemId is derived as described in the Item Identifiers section.
import {
AgCartesianChartOptions,
AgChartState,
AgCharts,
BarSeriesModule,
CategoryAxisModule,
LegendModule,
ModuleRegistry,
NumberAxisModule,
} from "ag-charts-community";
import { DataType, getData } from "./data";
let state: AgChartState | undefined = undefined;
ModuleRegistry.registerModules([
BarSeriesModule,
LegendModule,
NumberAxisModule,
CategoryAxisModule,
]);
const options: AgCartesianChartOptions<DataType> = {
data: getData(),
title: {
text: "Transportation Usage Over Time",
},
subtitle: {
text: "Click a bar to save active (highlight/tooltip) state",
},
series: [
{
type: "bar",
xKey: "year",
yKey: "publicTransit",
yName: "Public Transit",
},
{ type: "bar", xKey: "year", yKey: "privateCar", yName: "Private Car" },
{ type: "bar", xKey: "year", yKey: "cycle", yName: "Cycle" },
{ type: "bar", xKey: "year", yKey: "other", yName: "Other" },
],
listeners: {
seriesNodeClick: () => {
state = chart.getState();
console.log("Saved", state);
},
},
axes: {
y: {
type: "number",
title: { text: "Usage (millions of trips)" },
},
},
legend: {
position: "bottom",
toggleSeries: false,
listeners: {
legendItemClick: () => {
state = chart.getState();
console.log("Saved", state);
},
},
},
};
options.container = document.getElementById("myChart");
const chart = AgCharts.create(options);
function restoreState() {
if (state) {
chart.setState(state).then(() => {
console.log("Restored", state);
});
}
}
if (typeof window !== "undefined") {
// Attach external event handlers to window so they can be called from index.html
(<any>window).restoreState = restoreState;
}
export interface DataType {
year: string;
publicTransit: number;
privateCar: number;
cycle: number;
other: number;
}
export function getData(): DataType[] {
return [
{ year: "2020", publicTransit: 120, privateCar: 180, cycle: 60, other: 30 },
{ year: "2021", publicTransit: 140, privateCar: 170, cycle: 75, other: 40 },
{ year: "2022", publicTransit: 160, privateCar: 165, cycle: 90, other: 45 },
{
year: "2023",
publicTransit: 175,
privateCar: 155,
cycle: 110,
other: 50,
},
{
year: "2024",
publicTransit: 190,
privateCar: 150,
cycle: 130,
other: 55,
},
];
}
In this example:
- Click a bar or legend item to store the chart state using
chart.getState(). - Click the 'Restore' button to restore the saved state to the chart using
chart.setState(). - This will make the same item active again, showing its tooltip and highlight.
Frozen Active Copy Link
The active.frozen property can be used to pause user interactions.
import {
AgActiveItemState,
AgCandlestickSeriesTooltipRendererParams,
AgCartesianChartOptions,
AgChartInstance,
AgChartState,
AgCharts,
AnimationModule,
CandlestickSeriesModule,
ContextMenuModule,
CrosshairModule,
LegendModule,
ModuleRegistry,
NumberAxisModule,
TimeAxisModule,
} from "ag-charts-enterprise";
import { TradeDatum, getData } from "./data";
let frozenChart: AgChartInstance | undefined;
function unfreeze() {
if (frozenChart) {
const currentState: AgChartState = frozenChart.getState();
frozenChart.setState({
version: currentState.version,
active: { activeItem: currentState.active?.activeItem, frozen: false },
});
}
frozenChart = undefined;
}
function tooltipRenderer({
datum,
}: AgCandlestickSeriesTooltipRendererParams<TradeDatum>): string {
const date = new Date(datum.date);
const heading = date.toLocaleString("en-GB", {
hour: "2-digit",
minute: "2-digit",
day: "2-digit",
month: "short",
year: "numeric",
});
const format = (v: number) => `$${v.toFixed(2)}`;
(window as any).unfreeze = unfreeze;
return `
<div class="custom-tooltip">
<span>${heading}</span>
<div>
<span>open</span>
<span>${format(datum.open)}</span>
</div>
<div>
<span>high</span>
<span>${format(datum.high)}</span>
</div>
<div>
<span>low</span>
<span>${format(datum.low)}</span>
</div>
<div>
<span>close</span>
<span>${format(datum.close)}</span>
</div>
<div ${frozenChart ? "" : 'style="display: none"'}>
<button type="button" onclick="window.unfreeze()">Unfreeze</button>
</div>
</div>`;
}
ModuleRegistry.registerModules([
AnimationModule,
CandlestickSeriesModule,
ContextMenuModule,
CrosshairModule,
LegendModule,
NumberAxisModule,
TimeAxisModule,
]);
const options: AgCartesianChartOptions<TradeDatum> = {
title: {
text: "Ethereum Prices",
},
data: getData(),
series: [
{
type: "candlestick",
xKey: "date",
openKey: "open",
highKey: "high",
lowKey: "low",
closeKey: "close",
listeners: {
seriesNodeClick: (event) => {
frozenChart = chart;
const currentState: AgChartState = chart.getState();
const activeItem: AgActiveItemState | undefined =
currentState.active?.activeItem?.type === "series-node"
? {
type: "series-node",
// TODO: should event include `itemId`?
itemId: currentState.active.activeItem.itemId,
seriesId: event.seriesId,
}
: undefined;
chart.setState({
version: currentState.version,
active: {
frozen: true,
activeItem,
},
});
},
},
tooltip: {
interaction: { enabled: true },
position: {
anchorTo: "chart",
placement: "top-right",
},
renderer: tooltipRenderer,
},
},
],
axes: {
x: {
type: "time",
},
y: {
type: "number",
label: {
format: "$#{.2f}",
},
},
},
};
options.container = document.getElementById("myChart");
const chart = AgCharts.create(options);
.custom-tooltip {
padding: 10px 12px;
border-radius: 6px;
background: #ffffff;
color: #1f1f1f;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
font-size: 12px;
line-height: 1.4;
min-width: 160px;
display: grid;
grid-template-columns: auto;
gap: 6px;
}
/* Heading */
.custom-tooltip > span:first-child {
font-weight: 600;
margin-bottom: 4px;
}
/* Rows (open/high/low/close) */
.custom-tooltip > div {
display: grid;
grid-template-columns: 1fr auto;
align-items: center;
gap: 12px;
}
/* Labels */
.custom-tooltip > div span:first-child {
opacity: 0.7;
}
/* Values */
.custom-tooltip > div span:last-child {
font-variant-numeric: tabular-nums;
font-weight: 500;
}
/* Button row */
.custom-tooltip button {
justify-self: end;
padding: 4px 8px;
font-size: 12px;
border-radius: 4px;
border: 1px solid #ccc;
background: #f5f5f5;
cursor: pointer;
}
.custom-tooltip button:hover {
background: #eaeaea;
}
export type TradeDatum = {
date: Date;
open: number;
high: number;
low: number;
close: number;
};
export function getData() {
return [
{
date: new Date("2026-02-11T15:00:00Z"),
open: 3630,
high: 3650,
low: 3615,
close: 3642,
},
{
date: new Date("2026-02-11T14:00:00Z"),
open: 3707,
high: 3720,
low: 3625,
close: 3630,
},
{
date: new Date("2026-02-11T13:00:00Z"),
open: 3705,
high: 3728,
low: 3690,
close: 3707,
},
{
date: new Date("2026-02-11T12:00:00Z"),
open: 3725,
high: 3740,
low: 3695,
close: 3705,
},
{
date: new Date("2026-02-11T11:00:00Z"),
open: 3721,
high: 3738,
low: 3710,
close: 3725,
},
{
date: new Date("2026-02-11T10:00:00Z"),
open: 3727,
high: 3745,
low: 3712,
close: 3721,
},
{
date: new Date("2026-02-11T09:00:00Z"),
open: 3665,
high: 3730,
low: 3650,
close: 3727,
},
{
date: new Date("2026-02-11T08:00:00Z"),
open: 3633,
high: 3680,
low: 3620,
close: 3665,
},
{
date: new Date("2026-02-11T07:00:00Z"),
open: 3632,
high: 3650,
low: 3618,
close: 3633,
},
{
date: new Date("2026-02-11T06:00:00Z"),
open: 3635,
high: 3648,
low: 3620,
close: 3632,
},
{
date: new Date("2026-02-11T05:00:00Z"),
open: 3649,
high: 3665,
low: 3622,
close: 3635,
},
{
date: new Date("2026-02-11T04:00:00Z"),
open: 3599,
high: 3660,
low: 3588,
close: 3649,
},
{
date: new Date("2026-02-11T03:00:00Z"),
open: 3617,
high: 3630,
low: 3585,
close: 3599,
},
{
date: new Date("2026-02-11T02:00:00Z"),
open: 3685,
high: 3698,
low: 3605,
close: 3617,
},
{
date: new Date("2026-02-11T01:00:00Z"),
open: 3661,
high: 3695,
low: 3640,
close: 3685,
},
{
date: new Date("2026-02-11T00:00:00Z"),
open: 3709,
high: 3720,
low: 3650,
close: 3661,
},
{
date: new Date("2026-02-10T23:00:00Z"),
open: 3727,
high: 3742,
low: 3698,
close: 3709,
},
{
date: new Date("2026-02-10T22:00:00Z"),
open: 3729,
high: 3750,
low: 3710,
close: 3727,
},
{
date: new Date("2026-02-10T21:00:00Z"),
open: 3738,
high: 3762,
low: 3715,
close: 3729,
},
{
date: new Date("2026-02-10T20:00:00Z"),
open: 3731,
high: 3750,
low: 3712,
close: 3738,
},
{
date: new Date("2026-02-10T19:00:00Z"),
open: 3743,
high: 3770,
low: 3725,
close: 3731,
},
{
date: new Date("2026-02-10T18:00:00Z"),
open: 3727,
high: 3755,
low: 3710,
close: 3743,
},
{
date: new Date("2026-02-10T17:00:00Z"),
open: 3715,
high: 3740,
low: 3702,
close: 3727,
},
{
date: new Date("2026-02-10T16:00:00Z"),
open: 3700,
high: 3725,
low: 3688,
close: 3715,
},
{
date: new Date("2026-02-10T15:00:00Z"),
open: 3682,
high: 3710,
low: 3665,
close: 3700,
},
{
date: new Date("2026-02-10T14:00:00Z"),
open: 3695,
high: 3712,
low: 3660,
close: 3682,
},
{
date: new Date("2026-02-10T13:00:00Z"),
open: 3710,
high: 3728,
low: 3685,
close: 3695,
},
{
date: new Date("2026-02-10T12:00:00Z"),
open: 3728,
high: 3745,
low: 3690,
close: 3710,
},
{
date: new Date("2026-02-10T11:00:00Z"),
open: 3742,
high: 3758,
low: 3715,
close: 3728,
},
{
date: new Date("2026-02-10T10:00:00Z"),
open: 3720,
high: 3755,
low: 3705,
close: 3742,
},
{
date: new Date("2026-02-10T09:00:00Z"),
open: 3698,
high: 3730,
low: 3680,
close: 3720,
},
{
date: new Date("2026-02-10T08:00:00Z"),
open: 3675,
high: 3710,
low: 3660,
close: 3698,
},
{
date: new Date("2026-02-10T07:00:00Z"),
open: 3658,
high: 3690,
low: 3640,
close: 3675,
},
{
date: new Date("2026-02-10T06:00:00Z"),
open: 3665,
high: 3682,
low: 3638,
close: 3658,
},
{
date: new Date("2026-02-10T05:00:00Z"),
open: 3680,
high: 3698,
low: 3650,
close: 3665,
},
{
date: new Date("2026-02-10T04:00:00Z"),
open: 3645,
high: 3690,
low: 3625,
close: 3680,
},
{
date: new Date("2026-02-10T03:00:00Z"),
open: 3628,
high: 3660,
low: 3605,
close: 3645,
},
{
date: new Date("2026-02-10T02:00:00Z"),
open: 3655,
high: 3675,
low: 3610,
close: 3628,
},
{
date: new Date("2026-02-10T01:00:00Z"),
open: 3688,
high: 3700,
low: 3640,
close: 3655,
},
{
date: new Date("2026-02-10T00:00:00Z"),
open: 3715,
high: 3730,
low: 3670,
close: 3688,
},
{
date: new Date("2026-02-09T23:00:00Z"),
open: 3730,
high: 3750,
low: 3695,
close: 3715,
},
{
date: new Date("2026-02-09T22:00:00Z"),
open: 3748,
high: 3765,
low: 3712,
close: 3730,
},
{
date: new Date("2026-02-09T21:00:00Z"),
open: 3722,
high: 3760,
low: 3708,
close: 3748,
},
{
date: new Date("2026-02-09T20:00:00Z"),
open: 3705,
high: 3735,
low: 3680,
close: 3722,
},
{
date: new Date("2026-02-09T19:00:00Z"),
open: 3692,
high: 3715,
low: 3668,
close: 3705,
},
{
date: new Date("2026-02-09T18:00:00Z"),
open: 3710,
high: 3725,
low: 3685,
close: 3692,
},
];
}
In this example:
- Clicking the chart freezes the current Active State. The Highlight & Tooltip remain static when the mouse moves.
- Clicking the 'Unfreeze' button in the Tooltip restores interactivity.
Collapsed Copy Link
The collapsed state stores the identifiers of items collapsed in an Org Chart.
Changes to this state can be listened to with the collapsedChange event. See the Events API page for more details.