Control a chart from your code
BC.chart returns a handle. Use it to move the visible window, swap the spec while keeping the zoom, and put your own content in the tooltip.
The handle
The buttons call setView, update and getView. With a mouse or a trackpad, scroll with Ctrl held, or drag, to zoom on the chart itself, then press Log view. Press Points / lines while zoomed: update keeps the zoom.
…
HTML
<script src="/release/kits/timeseries.js"></script>
<div id="chart"></div>
<div class="bar">
<button id="march">Zoom to March</button>
<button id="reset">Reset</button>
<button id="kind">Points / lines</button>
<button id="view">Log view</button>
</div>
<pre class="out" id="out">…</pre>
<script>
// 90 days of visits for three channels
function makeRows() {
var next = (function (s) { return function () { s = (s * 1664525 + 1013904223) % 4294967296; return s / 4294967296; }; })(7);
var rows = [];
['web', 'app', 'email'].forEach(function (channel, c) {
var level = 200 - c * 60;
for (var d = 0; d < 90; d++) {
level = Math.max(20, level + (next() - 0.47) * 24);
rows.push({ day: new Date(Date.UTC(2024, 0, 1 + d)).toISOString().slice(0, 10), channel: channel, visits: Math.round(level) });
}
});
return rows;
}
BC.data('visits', makeRows());
BC.defineFn('tip', function (d) { // custom tooltip: an array of lines, inserted as text
return [d.values.channel, d.values.day + ': ' + d.values.visits + ' visits'];
});
var spec = {
"data": "visits",
"size": [720, 320],
"scales": {
"x": { "type": "time" },
"y": { "type": "linear", "zero": true },
"color": { "type": "color" }
},
"guides": [
{ "type": "grid", "scale": "y" },
{ "type": "axis", "scale": "x" },
{ "type": "axis", "scale": "y", "title": "Visits" },
{ "type": "legend", "scale": "color", "position": "top" }
],
"marks": [{ "type": "line", "x": "day", "y": "visits", "color": "channel" }],
"interaction": [
{ "type": "zoom", "fit": ["y"] },
{ "type": "brush", "fit": ["y"] },
{ "type": "legend-filter" },
{ "type": "tooltip", "content": "tip" }
]
};
var chart = BC.chart(document.getElementById('chart'), spec);
var out = document.getElementById('out');
document.getElementById('march').onclick = function () {
chart.setView('x', [Date.UTC(2024, 2, 1), Date.UTC(2024, 2, 31)]);
};
document.getElementById('reset').onclick = function () { chart.setView('x', null); };
document.getElementById('kind').onclick = function () {
var mark = spec.marks[0];
mark.type = mark.type === 'line' ? 'point' : 'line';
chart.update(spec); // the zoom stays
};
document.getElementById('view').onclick = function () {
out.textContent = JSON.stringify(chart.getView());
};
</script> What the handle offers
host, spec and diagnostics are read-only; ready resolves after the first draw (a dataset that is still being decoded delays it); update(spec, { resetView }), setView(scale, domain | null), getView() and destroy() do the work. Register functions with BC.defineFn(name, fn) and refer to them by name from the spec, which is how a JSON spec gets custom code without carrying any.