@amcharts/amcharts5-mcp 1.9.3 → 1.9.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -2
- package/extended/concepts/common-elements/images.md +1 -1
- package/extended/examples/column-bar/flame-chart.md +328 -0
- package/extended/examples/column-bar/network-waterfall-chart.md +320 -0
- package/extended/examples/column-bar/time-zones-and-daylight-saving.md +242 -0
- package/extended/examples/line-area/cpu-and-memory-timeline.md +348 -0
- package/extended/examples/maps/jupiter-and-its-moons.md +209 -0
- package/extended/examples/maps/mars-landing-sites.md +248 -0
- package/extended/examples/maps/rotating-moon.md +182 -0
- package/extended/examples/maps/saturn-and-its-rings.md +286 -0
- package/extended/examples/radar-polar/radial-ridgeline-chart.md +326 -0
- package/extended/examples/stock/changing-data-granularity.md +1 -1
- package/extended/examples/stock/live-stock-data.md +1 -1
- package/extended/examples/stock/separate-volume-panel.md +2 -2
- package/extended/examples/stock/stock-chart-comparing-prices.md +1 -1
- package/extended/examples/stock/stock-chart-with-bollinger-bands-indicator.md +1 -1
- package/extended/examples/stock/stock-chart-with-relative-strength-index.md +1 -1
- package/extended/examples/stock/stock-chart-with-volume-profile.md +1 -1
- package/extended/examples/stock/stock-chart.md +1 -1
- package/extended/examples/stock/stock-data-grouping.md +2 -2
- package/extended/examples/stock/stock-intraday-data.md +1 -1
- package/package.json +1 -1
- package/src/index.js +2 -5
- package/src/tools.js +116 -20
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Network Waterfall Chart"
|
|
3
|
+
source: "https://www.amcharts.com/demos/network-waterfall-chart/"
|
|
4
|
+
category: "column-bar"
|
|
5
|
+
scraped: "2026-10-25"
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
The requests a shop's home page makes as it loads, the way the browser's network panel draws them: each request a row, split into its phases, with its DOMContentLoaded and Load moments marked. Throttle the network with Chrome's presets to see what a slow connection does.
|
|
9
|
+
|
|
10
|
+
Reading a waterfall: A network waterfall lines up every request on one clock, so the shape of the page load shows at once: a long first bar is a slow server, steps down the chart are requests that wait for each other, and DNS and connection time appear only where the page talks to a new server. Throttling stretches every round trip and download, and shows which requests stand between the visitor and a usable page.
|
|
11
|
+
|
|
12
|
+
Good for:
|
|
13
|
+
- Page loads, API calls and other request timings
|
|
14
|
+
- Any process of phases per item: builds, pipelines, jobs
|
|
15
|
+
- Finding what waits on what
|
|
16
|
+
|
|
17
|
+
Think twice when:
|
|
18
|
+
- Hundreds of requests: group or filter them first
|
|
19
|
+
- Totals per phase: a stacked bar chart is clearer
|
|
20
|
+
|
|
21
|
+
Prompt: Create a network waterfall chart like Chrome DevTools' network panel, with generated data: the 17 requests of a shop's home page (the HTML, styles, scripts, fonts, images, API calls, analytics, favicon) down the side in request order, time in milliseconds along the top. Each request's phases are floating bars on its row, one column series per phase (Queueing in faint gray, then DNS lookup, Connecting, Waiting for server and Content download in theme colors) with a legend; DNS and connecting only on the first request to each server; later requests to it queue until that connection is up. Mark DOMContentLoaded and Load with dashed vertical lines labeled with their times; Load waits for the files, images and the analytics script, not for fetch() calls. Tooltips: the file, the phase and its duration, the type, size and server. Buttons switch the network speed with Chrome's throttling presets (off; fast 4G, 8.1 Mbps and 165 ms; slow 4G, 1.44 Mbps and 562.5 ms; 3G, 400 kbps and 2,000 ms) and recompute the timings. On phones hide the legend and the milestone labels. Use the amCharts 5 library with its Responsive theme.
|
|
22
|
+
|
|
23
|
+
## JavaScript
|
|
24
|
+
|
|
25
|
+
```javascript
|
|
26
|
+
// Create root element
|
|
27
|
+
// https://docs.amcharts.com/v5/getting-started/#root-element
|
|
28
|
+
var root = am5.Root.new("chartdiv");
|
|
29
|
+
|
|
30
|
+
// Set themes
|
|
31
|
+
// https://docs.amcharts.com/v5/concepts/themes/
|
|
32
|
+
var responsive = am5themes_Responsive.new(root); // kept in a variable: a rule of its own is added below
|
|
33
|
+
root.setThemes([
|
|
34
|
+
am5themes_Animated.new(root),
|
|
35
|
+
responsive
|
|
36
|
+
]);
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
// ── Sample data: the requests a shop's home page makes as it loads, like the network panel of the browser's tools.
|
|
40
|
+
// Each request goes through phases: waiting in the queue, finding the server (DNS), connecting to it, waiting for its
|
|
41
|
+
// answer and downloading it. A server's first request pays for the DNS lookup and the connection; later ones to the
|
|
42
|
+
// same server reuse them ──
|
|
43
|
+
|
|
44
|
+
// The requests: file, type, server, size (KB), the server's own time to answer (ms), and what has to happen before
|
|
45
|
+
// the browser asks for it
|
|
46
|
+
var REQUESTS = [
|
|
47
|
+
{ url: "index.html", type: "Document", host: "shop.example.com", size: 32, server: 180, after: "start" },
|
|
48
|
+
{ url: "styles.css", type: "Stylesheet", host: "shop.example.com", size: 48, server: 25, after: "html" },
|
|
49
|
+
{ url: "vendor.js", type: "Script", host: "shop.example.com", size: 420, server: 20, after: "html" },
|
|
50
|
+
{ url: "app.js", type: "Script", host: "shop.example.com", size: 310, server: 20, after: "html" },
|
|
51
|
+
{ url: "fonts.css", type: "Stylesheet", host: "fonts.example.net", size: 2, server: 30, after: "html" },
|
|
52
|
+
{ url: "inter.woff2", type: "Font", host: "fonts.example.org", size: 38, server: 15, after: "fonts.css" },
|
|
53
|
+
{ url: "inter-bold.woff2", type: "Font", host: "fonts.example.org", size: 41, server: 15, after: "fonts.css" },
|
|
54
|
+
{ url: "logo.svg", type: "Image", host: "shop.example.com", size: 6, server: 10, after: "html" },
|
|
55
|
+
{ url: "hero.webp", type: "Image", host: "img.example-cdn.com", size: 180, server: 30, after: "html" },
|
|
56
|
+
{ url: "product-1.webp", type: "Image", host: "img.example-cdn.com", size: 64, server: 25, after: "html" },
|
|
57
|
+
{ url: "product-2.webp", type: "Image", host: "img.example-cdn.com", size: 58, server: 25, after: "html" },
|
|
58
|
+
{ url: "product-3.webp", type: "Image", host: "img.example-cdn.com", size: 71, server: 25, after: "html" },
|
|
59
|
+
{ url: "product-4.webp", type: "Image", host: "img.example-cdn.com", size: 49, server: 25, after: "html" },
|
|
60
|
+
{ url: "api/user", type: "Fetch", host: "api.example.com", size: 8, server: 90, after: "app.js" },
|
|
61
|
+
{ url: "api/products", type: "Fetch", host: "api.example.com", size: 24, server: 140, after: "app.js" },
|
|
62
|
+
{ url: "analytics.js", type: "Script", host: "stats.example.io", size: 45, server: 40, after: "dcl" },
|
|
63
|
+
{ url: "favicon.ico", type: "Image", host: "shop.example.com", size: 4, server: 10, after: "load" }
|
|
64
|
+
];
|
|
65
|
+
|
|
66
|
+
// Network speeds: a round trip's latency (ms) and the download speed (kilobits/s). Without throttling a fast office
|
|
67
|
+
// connection; the others are Chrome's own throttling presets
|
|
68
|
+
var PROFILES = {
|
|
69
|
+
none: { rtt: 20, kbps: 50000 },
|
|
70
|
+
fast4g: { rtt: 165, kbps: 8100 },
|
|
71
|
+
slow4g: { rtt: 562.5, kbps: 1440 },
|
|
72
|
+
slow3g: { rtt: 2000, kbps: 400 }
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
// A seeded random number generator (mulberry32): the same timings on every visit
|
|
76
|
+
function makeRandom(seed) {
|
|
77
|
+
return function() {
|
|
78
|
+
seed = seed + 0x6D2B79F5 | 0;
|
|
79
|
+
var t = Math.imul(seed ^ seed >>> 15, 1 | seed);
|
|
80
|
+
t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
|
|
81
|
+
return ((t ^ t >>> 14) >>> 0) / 4294967296;
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Each request's phases at a speed, and when the page's DOMContentLoaded and load events fire
|
|
86
|
+
function simulate(profile) {
|
|
87
|
+
var random = makeRandom(2026);
|
|
88
|
+
var kbPerMs = profile.kbps / 8 / 1000; // kilobytes a millisecond
|
|
89
|
+
var connectedAt = {}; // when the connection to each server was up
|
|
90
|
+
var done = {}; // when each request finished
|
|
91
|
+
var phases = [];
|
|
92
|
+
var events = { start: 0 };
|
|
93
|
+
|
|
94
|
+
REQUESTS.forEach(function(request) {
|
|
95
|
+
// when the browser can ask: at the start, once the HTML has started to arrive (the parser finds the tags), once
|
|
96
|
+
// a file it depends on is in, or after the page's own events
|
|
97
|
+
var ready = request.after === "html" ? events.html + 5 + 30 * random() : (events[request.after] !== undefined ? events[request.after] : done[request.after]);
|
|
98
|
+
var t = ready;
|
|
99
|
+
var parts = [];
|
|
100
|
+
function phase(name, ms) {
|
|
101
|
+
parts.push({ url: request.url, phase: name, start: Math.round(t * 10) / 10, end: Math.round((t + ms) * 10) / 10, duration: Math.round(ms) });
|
|
102
|
+
t += ms;
|
|
103
|
+
}
|
|
104
|
+
var queue = 1 + (request.type === "Image" ? 15 : 3) * random(); // images wait behind more urgent requests
|
|
105
|
+
// a request to a server that is still being connected to waits for that connection (the browser calls it stalled)
|
|
106
|
+
if (connectedAt[request.host] !== undefined) {
|
|
107
|
+
queue = Math.max(queue, connectedAt[request.host] - t);
|
|
108
|
+
}
|
|
109
|
+
phase("Queueing", queue);
|
|
110
|
+
if (connectedAt[request.host] === undefined) {
|
|
111
|
+
// the first request to a server finds it and connects to it; later ones reuse the connection
|
|
112
|
+
phase("DNS lookup", profile.rtt * (0.5 + 0.5 * random()));
|
|
113
|
+
phase("Connecting", profile.rtt * 2); // TCP, then TLS
|
|
114
|
+
connectedAt[request.host] = t;
|
|
115
|
+
}
|
|
116
|
+
phase("Waiting for server", profile.rtt + request.server);
|
|
117
|
+
// a bit under the line's speed: the other downloads share it
|
|
118
|
+
phase("Content download", Math.max(1, request.size / kbPerMs * (1.1 + 0.4 * random())));
|
|
119
|
+
parts.forEach(function(p) {
|
|
120
|
+
p.type = request.type;
|
|
121
|
+
p.host = request.host;
|
|
122
|
+
p.size = request.size;
|
|
123
|
+
phases.push(p);
|
|
124
|
+
});
|
|
125
|
+
done[request.url] = t;
|
|
126
|
+
|
|
127
|
+
// the page's milestones: the HTML's first bytes, then DOMContentLoaded once the HTML, the styles and the
|
|
128
|
+
// scripts are in and the scripts have run
|
|
129
|
+
if (request.url === "index.html") { events.html = t - (t - parts[parts.length - 1].start) * 0.7; }
|
|
130
|
+
if (request.url === "app.js") {
|
|
131
|
+
events.dcl = Math.max(done["index.html"], done["styles.css"], done["vendor.js"], done["app.js"]) + 90;
|
|
132
|
+
}
|
|
133
|
+
if (request.url === "analytics.js") {
|
|
134
|
+
// load fires once everything the page itself loads is in: its files and images, and a script added before load,
|
|
135
|
+
// like the analytics. Requests made with fetch() don't hold it
|
|
136
|
+
events.load = Math.max.apply(null, REQUESTS.filter(function(q) {
|
|
137
|
+
return q.type !== "Fetch" && done[q.url] !== undefined;
|
|
138
|
+
}).map(function(q) { return done[q.url]; })) + 15;
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
return { phases: phases, dcl: Math.round(events.dcl), load: Math.round(events.load), end: Math.max.apply(null, phases.map(function(p) { return p.end; })) };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
// Create chart
|
|
146
|
+
// https://docs.amcharts.com/v5/charts/xy-chart/
|
|
147
|
+
var chart = root.container.children.push(am5xy.XYChart.new(root, {
|
|
148
|
+
panX: false,
|
|
149
|
+
panY: false,
|
|
150
|
+
wheelX: "none",
|
|
151
|
+
wheelY: "none",
|
|
152
|
+
paddingRight: 30, // room for the last time label, which is centered on the plot's edge
|
|
153
|
+
layout: root.verticalLayout
|
|
154
|
+
}));
|
|
155
|
+
|
|
156
|
+
// Add legend: the phases
|
|
157
|
+
// https://docs.amcharts.com/v5/charts/xy-chart/legend-xy-series/
|
|
158
|
+
var legend = chart.children.unshift(am5.Legend.new(root, {
|
|
159
|
+
centerX: am5.p50,
|
|
160
|
+
x: am5.p50,
|
|
161
|
+
marginBottom: 10
|
|
162
|
+
}));
|
|
163
|
+
|
|
164
|
+
// Create axes
|
|
165
|
+
// https://docs.amcharts.com/v5/charts/xy-chart/axes/
|
|
166
|
+
// The requests down the side, in the order the browser made them
|
|
167
|
+
var yRenderer = am5xy.AxisRendererY.new(root, {
|
|
168
|
+
inversed: true, // the first request at the top
|
|
169
|
+
minGridDistance: 8, // a label for every request
|
|
170
|
+
cellStartLocation: 0.2, // the bars thinner than the rows
|
|
171
|
+
cellEndLocation: 0.8
|
|
172
|
+
});
|
|
173
|
+
yRenderer.labels.template.setAll({
|
|
174
|
+
fontSize: "0.8em",
|
|
175
|
+
maxWidth: 130,
|
|
176
|
+
oversizedBehavior: "truncate"
|
|
177
|
+
});
|
|
178
|
+
yRenderer.grid.template.set("strokeOpacity", 0.05);
|
|
179
|
+
var yAxis = chart.yAxes.push(am5xy.CategoryAxis.new(root, {
|
|
180
|
+
categoryField: "url",
|
|
181
|
+
renderer: yRenderer
|
|
182
|
+
}));
|
|
183
|
+
yAxis.data.setAll(REQUESTS);
|
|
184
|
+
|
|
185
|
+
// The time since the page was asked for, along the top
|
|
186
|
+
var xRenderer = am5xy.AxisRendererX.new(root, {
|
|
187
|
+
opposite: true,
|
|
188
|
+
minGridDistance: 70
|
|
189
|
+
});
|
|
190
|
+
xRenderer.grid.template.set("strokeOpacity", 0.06);
|
|
191
|
+
var xAxis = chart.xAxes.push(am5xy.ValueAxis.new(root, {
|
|
192
|
+
min: 0,
|
|
193
|
+
numberFormat: "#,###' ms'",
|
|
194
|
+
renderer: xRenderer
|
|
195
|
+
}));
|
|
196
|
+
|
|
197
|
+
// Create series
|
|
198
|
+
// https://docs.amcharts.com/v5/charts/xy-chart/series/
|
|
199
|
+
// One series per phase, so the legend names them and can hide each: a phase is a floating bar on its request's row,
|
|
200
|
+
// from when it started to when it ended. The queue in a faint gray, the rest in the theme's colors
|
|
201
|
+
var gray = root.interfaceColors.get("alternativeBackground");
|
|
202
|
+
var series = ["Queueing", "DNS lookup", "Connecting", "Waiting for server", "Content download"].map(function(name) {
|
|
203
|
+
var settings = {
|
|
204
|
+
name: name,
|
|
205
|
+
xAxis: xAxis,
|
|
206
|
+
yAxis: yAxis,
|
|
207
|
+
openValueXField: "start",
|
|
208
|
+
valueXField: "end",
|
|
209
|
+
categoryYField: "url",
|
|
210
|
+
clustered: false // the phases share their request's row
|
|
211
|
+
};
|
|
212
|
+
if (name === "Queueing") {
|
|
213
|
+
settings.fill = gray;
|
|
214
|
+
settings.stroke = gray;
|
|
215
|
+
}
|
|
216
|
+
var s = chart.series.push(am5xy.ColumnSeries.new(root, settings));
|
|
217
|
+
s.columns.template.setAll({
|
|
218
|
+
strokeOpacity: 0,
|
|
219
|
+
fillOpacity: name === "Queueing" ? 0.25 : 1,
|
|
220
|
+
tooltipText: "[bold]{url}[/]\n" + name + ": {duration} ms\n{type}, {size} KB from {host}",
|
|
221
|
+
tooltipY: 0
|
|
222
|
+
});
|
|
223
|
+
legend.data.push(s);
|
|
224
|
+
return s;
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
// The page's two milestones as lines across the requests: DOMContentLoaded (the HTML read and its scripts run) and
|
|
228
|
+
// load (everything the page asked for is in)
|
|
229
|
+
// https://docs.amcharts.com/v5/charts/xy-chart/axes/axis-ranges/
|
|
230
|
+
function milestone(color, side) {
|
|
231
|
+
var range = xAxis.createAxisRange(xAxis.makeDataItem({ value: 0 }));
|
|
232
|
+
range.get("grid").setAll({
|
|
233
|
+
forceHidden: false,
|
|
234
|
+
stroke: color,
|
|
235
|
+
strokeOpacity: 1,
|
|
236
|
+
strokeWidth: 1.5,
|
|
237
|
+
strokeDasharray: [4, 3]
|
|
238
|
+
});
|
|
239
|
+
range.get("label").setAll({
|
|
240
|
+
forceHidden: false,
|
|
241
|
+
fill: color,
|
|
242
|
+
fontSize: "0.75em",
|
|
243
|
+
fontWeight: "600",
|
|
244
|
+
dy: 24, // down into the plot, below the time axis's own labels
|
|
245
|
+
paddingTop: 2,
|
|
246
|
+
paddingBottom: 2,
|
|
247
|
+
paddingLeft: 4,
|
|
248
|
+
paddingRight: 4,
|
|
249
|
+
background: am5.RoundedRectangle.new(root, {
|
|
250
|
+
fill: root.interfaceColors.get("background"), // the chart's background, so the bars behind don't show through
|
|
251
|
+
fillOpacity: 0.9
|
|
252
|
+
}),
|
|
253
|
+
centerY: 0,
|
|
254
|
+
// DOMContentLoaded's name to the left of its line, Load's to the right, so the two never overlap
|
|
255
|
+
centerX: side < 0 ? am5.p100 : 0,
|
|
256
|
+
dx: 5 * side
|
|
257
|
+
});
|
|
258
|
+
return range;
|
|
259
|
+
}
|
|
260
|
+
var dclRange = milestone(root.interfaceColors.get("primaryButton"), -1);
|
|
261
|
+
var loadRange = milestone(root.interfaceColors.get("negative"), 1);
|
|
262
|
+
|
|
263
|
+
// On a narrow chart (a phone) the legend would take most of the height: it is hidden there, and so are the milestones' labels (their lines stay). The rule is
|
|
264
|
+
// undone on a wider chart
|
|
265
|
+
responsive.addRule({
|
|
266
|
+
relevant: am5themes_Responsive.widthM, // the chart is 600px wide or less
|
|
267
|
+
applying: function() {
|
|
268
|
+
legend.set("forceHidden", true);
|
|
269
|
+
dclRange.get("label").set("forceHidden", true); // the lines stay
|
|
270
|
+
loadRange.get("label").set("forceHidden", true);
|
|
271
|
+
},
|
|
272
|
+
removing: function() {
|
|
273
|
+
legend.set("forceHidden", false);
|
|
274
|
+
dclRange.get("label").set("forceHidden", false);
|
|
275
|
+
loadRange.get("label").set("forceHidden", false);
|
|
276
|
+
}
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
// Shows the page loading at a network speed: "none", "fast4g", "slow4g" or "slow3g"
|
|
280
|
+
function showRequests(name) {
|
|
281
|
+
var result = simulate(PROFILES[name]);
|
|
282
|
+
series.forEach(function(s) {
|
|
283
|
+
s.data.setAll(result.phases.filter(function(p) { return p.phase === s.get("name"); }));
|
|
284
|
+
});
|
|
285
|
+
dclRange.set("value", result.dcl);
|
|
286
|
+
dclRange.get("label").set("text", "DOMContentLoaded " + result.dcl.toLocaleString("en-US") + " ms");
|
|
287
|
+
loadRange.set("value", result.load);
|
|
288
|
+
loadRange.get("label").set("text", "Load " + result.load.toLocaleString("en-US") + " ms");
|
|
289
|
+
xAxis.set("max", Math.ceil(result.end * 1.04 / 50) * 50); // the last request, with a little room
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
showRequests("none");
|
|
293
|
+
|
|
294
|
+
// Make stuff animate on load
|
|
295
|
+
// https://docs.amcharts.com/v5/concepts/animations/
|
|
296
|
+
chart.appear(1000, 100);
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
## HTML
|
|
300
|
+
|
|
301
|
+
```html
|
|
302
|
+
<div id="chartdiv"></div>
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
## CSS
|
|
306
|
+
|
|
307
|
+
```css
|
|
308
|
+
#chartdiv {
|
|
309
|
+
width: 100%;
|
|
310
|
+
height: 500px;
|
|
311
|
+
font-size: 0.875rem;
|
|
312
|
+
}
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
## Required resources
|
|
316
|
+
|
|
317
|
+
- https://cdn.amcharts.com/lib/5/index.js
|
|
318
|
+
- https://cdn.amcharts.com/lib/5/xy.js
|
|
319
|
+
- https://cdn.amcharts.com/lib/5/themes/Animated.js
|
|
320
|
+
- https://cdn.amcharts.com/lib/5/themes/Responsive.js
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Time Zones and Daylight Saving"
|
|
3
|
+
source: "https://www.amcharts.com/demos/time-zones-and-daylight-saving/"
|
|
4
|
+
category: "column-bar"
|
|
5
|
+
scraped: "2026-10-25"
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
Two offices open 9 to 5 on their own clocks. The chart shows them in Berlin time, or any zone you pick: its days start at that zone's midnight, and where its clocks change, a day has 25 hours.
|
|
9
|
+
|
|
10
|
+
Dates belong to a time zone: A timestamp is a moment; which day and hour it falls on depends on where it is read. Set root.timezone and the chart reads every moment in that zone, whatever the viewer's own: the date axis puts its day lines at the zone's midnight, writes labels and tooltips in its time, and stretches a day to 25 hours, or shrinks it to 23, where the zone's clocks change. Between Europe's change on October 25 and America's on November 1, the two offices are five hours apart instead of six, and share three hours a day instead of two.
|
|
11
|
+
|
|
12
|
+
Good for:
|
|
13
|
+
- Teams, markets and servers in several time zones
|
|
14
|
+
- Schedules that must read in one city's time
|
|
15
|
+
- Hourly data across a daylight saving change
|
|
16
|
+
|
|
17
|
+
Think twice when:
|
|
18
|
+
- Data about each viewer's own day: leave the zone unset
|
|
19
|
+
- Plain UTC: root.utc is simpler and faster
|
|
20
|
+
|
|
21
|
+
Prompt: Create a schedule chart on a date axis, shown in Berlin time with root.timezone: three rows (a Berlin office, a New York office, and the hours both are open), with a bar for each weekday's 9:00 to 17:00 on each office's own clock, over three weeks around the 2026 autumn daylight saving changes. Mark the days that are not 24 hours long in the chart's time zone. Add a cursor and a horizontal scrollbar. Use the amCharts 5 library with its Responsive theme.
|
|
22
|
+
|
|
23
|
+
## JavaScript
|
|
24
|
+
|
|
25
|
+
```javascript
|
|
26
|
+
// Create root element
|
|
27
|
+
// https://docs.amcharts.com/v5/getting-started/#root-element
|
|
28
|
+
var root = am5.Root.new("chartdiv");
|
|
29
|
+
|
|
30
|
+
// Show every date and time in Berlin time, wherever the viewer is. Any IANA time zone name works ("America/New_York",
|
|
31
|
+
// "Asia/Kathmandu"...): the date axis then puts its day lines at that zone's midnight, writes its labels and tooltips
|
|
32
|
+
// in its time, and follows its daylight saving changes
|
|
33
|
+
// https://docs.amcharts.com/v5/getting-started/root-element/#time-zone
|
|
34
|
+
root.timezone = am5.Timezone.new("Europe/Berlin");
|
|
35
|
+
|
|
36
|
+
// Set themes
|
|
37
|
+
// https://docs.amcharts.com/v5/concepts/themes/
|
|
38
|
+
root.setThemes([
|
|
39
|
+
am5themes_Animated.new(root),
|
|
40
|
+
am5themes_Responsive.new(root)
|
|
41
|
+
]);
|
|
42
|
+
|
|
43
|
+
// The data: three weeks around the autumn clock changes of 2026, Europe's on Sunday, October 25 and America's on
|
|
44
|
+
// Sunday, November 1
|
|
45
|
+
var start = Date.UTC(2026, 9, 19); // Monday, October 19
|
|
46
|
+
var end = Date.UTC(2026, 10, 7); // Saturday, November 7
|
|
47
|
+
|
|
48
|
+
// Two offices, each open 9:00 to 17:00 on its own clock, Monday to Friday
|
|
49
|
+
var offices = [
|
|
50
|
+
{ name: "Berlin office", timezone: "Europe/Berlin" },
|
|
51
|
+
{ name: "New York office", timezone: "America/New_York" }
|
|
52
|
+
];
|
|
53
|
+
|
|
54
|
+
// An office's working days as moments in time: the hours when its own clock reads 9:00 and 17:00. A Timezone object
|
|
55
|
+
// reads any moment's date and hour in its zone, so the hours follow the office's clock changes, not the viewer's
|
|
56
|
+
function workdays(office) {
|
|
57
|
+
var zone = am5.Timezone.new(office.timezone);
|
|
58
|
+
var days = [];
|
|
59
|
+
var opened = null;
|
|
60
|
+
// an hour at a time: note when the office opens, and the day when it closes
|
|
61
|
+
for (var time = start; time < end; time += 3600000) {
|
|
62
|
+
var clock = zone.parseDate(new Date(time)); // { year, month, day, hour, minute, weekday... } on its clock
|
|
63
|
+
var open = clock.weekday >= 1 && clock.weekday <= 5 && clock.hour >= 9 && clock.hour < 17;
|
|
64
|
+
if (open && opened === null) {
|
|
65
|
+
opened = time;
|
|
66
|
+
}
|
|
67
|
+
if (!open && opened !== null) {
|
|
68
|
+
days.push({ row: office.name, open: opened, close: time });
|
|
69
|
+
opened = null;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return days;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
var berlin = workdays(offices[0]);
|
|
76
|
+
var newYork = workdays(offices[1]);
|
|
77
|
+
|
|
78
|
+
// The hours both offices are open: where a Berlin day and a New York day overlap
|
|
79
|
+
var both = [];
|
|
80
|
+
berlin.forEach(function(b) {
|
|
81
|
+
newYork.forEach(function(n) {
|
|
82
|
+
var open = Math.max(b.open, n.open), close = Math.min(b.close, n.close);
|
|
83
|
+
if (open < close) {
|
|
84
|
+
both.push({ row: "Both open", open: open, close: close });
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// Create chart
|
|
90
|
+
// https://docs.amcharts.com/v5/charts/xy-chart/
|
|
91
|
+
var chart = root.container.children.push(am5xy.XYChart.new(root, {
|
|
92
|
+
panX: true, // drag the plot sideways to move along the dates
|
|
93
|
+
panY: false,
|
|
94
|
+
wheelX: "panX", // a sideways wheel or swipe moves along the dates...
|
|
95
|
+
wheelY: "zoomX", // ...and the wheel zooms in on the hours
|
|
96
|
+
paddingLeft: 0
|
|
97
|
+
}));
|
|
98
|
+
|
|
99
|
+
// The rows: one per office, and the hours they share
|
|
100
|
+
// https://docs.amcharts.com/v5/charts/xy-chart/axes/category-axis/
|
|
101
|
+
var yRenderer = am5xy.AxisRendererY.new(root, {
|
|
102
|
+
inversed: true, // the first row at the top
|
|
103
|
+
minGridDistance: 20 // every row gets its label
|
|
104
|
+
});
|
|
105
|
+
yRenderer.grid.template.set("location", 1); // grid lines between the rows
|
|
106
|
+
|
|
107
|
+
var yAxis = chart.yAxes.push(am5xy.CategoryAxis.new(root, {
|
|
108
|
+
categoryField: "row",
|
|
109
|
+
renderer: yRenderer
|
|
110
|
+
}));
|
|
111
|
+
yAxis.data.setAll([{ row: "Berlin office" }, { row: "New York office" }, { row: "Both open" }]);
|
|
112
|
+
|
|
113
|
+
// The time: the date axis reads every moment in the chart's time zone
|
|
114
|
+
// https://docs.amcharts.com/v5/charts/xy-chart/axes/date-axis/
|
|
115
|
+
var xAxis = chart.xAxes.push(am5xy.DateAxis.new(root, {
|
|
116
|
+
baseInterval: { timeUnit: "hour", count: 1 }, // the data changes on the hour
|
|
117
|
+
renderer: am5xy.AxisRendererX.new(root, {
|
|
118
|
+
minGridDistance: 70 // at least 70px between labels: hours when zoomed in, days when zoomed out
|
|
119
|
+
}),
|
|
120
|
+
tooltip: am5.Tooltip.new(root, {}),
|
|
121
|
+
// the cursor's time, with the zone's offset from UTC, which changes with its clocks
|
|
122
|
+
tooltipDateFormat: "EEE, MMM d, HH:mm (UTC ZZ)"
|
|
123
|
+
}));
|
|
124
|
+
|
|
125
|
+
// One series per row: a bar from opening to closing time
|
|
126
|
+
// https://docs.amcharts.com/v5/charts/xy-chart/series/column-series/
|
|
127
|
+
function addRow(name, data) {
|
|
128
|
+
var series = chart.series.push(am5xy.ColumnSeries.new(root, {
|
|
129
|
+
name: name,
|
|
130
|
+
xAxis: xAxis,
|
|
131
|
+
yAxis: yAxis,
|
|
132
|
+
openValueXField: "open", // a bar starts at the opening time...
|
|
133
|
+
valueXField: "close", // ...and ends at the closing time
|
|
134
|
+
categoryYField: "row",
|
|
135
|
+
clustered: false, // each bar fills its own row
|
|
136
|
+
tooltip: am5.Tooltip.new(root, {
|
|
137
|
+
// the hours in the chart's time zone
|
|
138
|
+
labelText: "{categoryY}: {openValueX.formatDate('EEE HH:mm')} to {valueX.formatDate('HH:mm')}"
|
|
139
|
+
})
|
|
140
|
+
}));
|
|
141
|
+
series.columns.template.setAll({
|
|
142
|
+
height: am5.percent(56), // a bar a little over half the row high
|
|
143
|
+
cornerRadiusTL: 4,
|
|
144
|
+
cornerRadiusTR: 4,
|
|
145
|
+
cornerRadiusBL: 4,
|
|
146
|
+
cornerRadiusBR: 4,
|
|
147
|
+
strokeOpacity: 0
|
|
148
|
+
});
|
|
149
|
+
series.data.setAll(data);
|
|
150
|
+
return series;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
addRow("Berlin office", berlin);
|
|
154
|
+
addRow("New York office", newYork);
|
|
155
|
+
addRow("Both open", both);
|
|
156
|
+
|
|
157
|
+
// Days that are not 24 hours long in the chart's time zone: 25 hours where its clocks go back, 23 where they go
|
|
158
|
+
// forward. getDateIntervalDuration gives a day's real length in a time zone; the band and its label mark that day
|
|
159
|
+
var dayMarks = [];
|
|
160
|
+
function markDayLengths() {
|
|
161
|
+
// the old marks go: the chart's time zone may have changed
|
|
162
|
+
dayMarks.forEach(function(mark) {
|
|
163
|
+
xAxis.axisRanges.removeValue(mark);
|
|
164
|
+
});
|
|
165
|
+
dayMarks = [];
|
|
166
|
+
var oneDay = { timeUnit: "day", count: 1 };
|
|
167
|
+
// from the midnight before the first moment, one local day at a time
|
|
168
|
+
var day = am5.time.round(new Date(start), "day", 1, 1, root.utc, undefined, root.timezone);
|
|
169
|
+
while (day.getTime() < end) {
|
|
170
|
+
var length = am5.time.getDateIntervalDuration(oneDay, day, 1, root.utc, root.timezone);
|
|
171
|
+
if (length !== 86400000) {
|
|
172
|
+
var mark = xAxis.makeDataItem({ value: day.getTime(), endValue: day.getTime() + length });
|
|
173
|
+
xAxis.createAxisRange(mark);
|
|
174
|
+
mark.get("axisFill").setAll({
|
|
175
|
+
visible: true,
|
|
176
|
+
fill: root.interfaceColors.get("alternativeBackground"),
|
|
177
|
+
fillOpacity: 0.06 // a faint band over the whole day
|
|
178
|
+
});
|
|
179
|
+
// the day's length, low in the plot: the clocks change on a Sunday, when both offices are closed
|
|
180
|
+
mark.get("label").setAll({
|
|
181
|
+
text: "[bold]" + Math.round(length / 3600000) + " hours[/]\n" + (length > 86400000 ? "clocks go back" : "clocks go forward"),
|
|
182
|
+
inside: true, // over the plot, not among the axis labels...
|
|
183
|
+
location: 0.5, // ...in the middle of the day...
|
|
184
|
+
centerY: am5.p100, // ...standing on the axis line
|
|
185
|
+
dy: -6,
|
|
186
|
+
textAlign: "center",
|
|
187
|
+
fontSize: "0.8em",
|
|
188
|
+
fillOpacity: 0.7
|
|
189
|
+
});
|
|
190
|
+
dayMarks.push(mark);
|
|
191
|
+
}
|
|
192
|
+
day = new Date(day.getTime() + length);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
markDayLengths();
|
|
196
|
+
|
|
197
|
+
// Add cursor
|
|
198
|
+
// https://docs.amcharts.com/v5/charts/xy-chart/cursor/
|
|
199
|
+
chart.set("cursor", am5xy.XYCursor.new(root, {
|
|
200
|
+
behavior: "zoomX", // drag across the plot to zoom in on those hours
|
|
201
|
+
xAxis: xAxis
|
|
202
|
+
})).lineY.set("visible", false);
|
|
203
|
+
|
|
204
|
+
// Add scrollbar
|
|
205
|
+
// https://docs.amcharts.com/v5/charts/xy-chart/scrollbars/
|
|
206
|
+
chart.set("scrollbarX", am5.Scrollbar.new(root, {
|
|
207
|
+
orientation: "horizontal" // above the plot: drag it along the three weeks
|
|
208
|
+
}));
|
|
209
|
+
|
|
210
|
+
// No zoom-out button over the first row's bars: the scrollbar and the wheel zoom out
|
|
211
|
+
chart.zoomOutButton.set("forceHidden", true);
|
|
212
|
+
|
|
213
|
+
// Make stuff animate on load, then zoom in on the days around Europe's clock change, Friday to Tuesday. The bars lie
|
|
214
|
+
// along the date axis, so the axis fits itself to them while the chart appears: the zoom comes after
|
|
215
|
+
// https://docs.amcharts.com/v5/concepts/animations/
|
|
216
|
+
chart.appear(1000, 100).then(function() {
|
|
217
|
+
xAxis.zoomToDates(new Date(Date.UTC(2026, 9, 22, 22)), new Date(Date.UTC(2026, 9, 27, 23)));
|
|
218
|
+
});
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## HTML
|
|
222
|
+
|
|
223
|
+
```html
|
|
224
|
+
<div id="chartdiv"></div>
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## CSS
|
|
228
|
+
|
|
229
|
+
```css
|
|
230
|
+
#chartdiv {
|
|
231
|
+
width: 100%;
|
|
232
|
+
height: 500px;
|
|
233
|
+
font-size: 0.875rem;
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## Required resources
|
|
238
|
+
|
|
239
|
+
- https://cdn.amcharts.com/lib/5/index.js
|
|
240
|
+
- https://cdn.amcharts.com/lib/5/xy.js
|
|
241
|
+
- https://cdn.amcharts.com/lib/5/themes/Animated.js
|
|
242
|
+
- https://cdn.amcharts.com/lib/5/themes/Responsive.js
|