bezel-charts 1.6.0 → 1.8.0

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.
@@ -0,0 +1,160 @@
1
+ import { newId, r2, num, has, list, series, bool, fmt, tw, text, small, hit, anim, palette, emptyState, pathOf } from '../core/util.js';
2
+ import { niceScale, legend, styleOf, pattern, marker, DASHES, SHAPES, PATTERNS } from '../core/series.js';
3
+ import { seriesLine, pointMarks } from '../core/lines.js';
4
+ import { tile } from '../core/tile.js';
5
+
6
+ /**
7
+ * Bars and lines with different units on one chart: bars read against the left axis, lines against the right.
8
+ * The right axis gets the same number of steps as the left, so every gridline reads on both sides, and each axis
9
+ * carries a coloured rule matching its data. Several bar series sit side by side, or pile up with `stacked`.
10
+ */
11
+ export default function combo(o, th) {
12
+ var B = series(o.values), L = series(o.line), pal = palette(o, th), k = B.length, m = L.length;
13
+ var labels = list(o.labels), bnames = list(o.series), lnames = list(o.lineSeries), stacked = k > 1 && bool(o.stacked);
14
+ var n = Math.max.apply(null, [0].concat(B.concat(L).map(function (s) { return s.length; })));
15
+ var lo = lineOpts(o), h = num(o.height, 200);
16
+ var barName = function (j) { return bnames[j] || (k === 1 ? 'Bars' : 'Bars ' + (j + 1)); };
17
+ var lineName = function (j) { return lnames[j] || (m === 1 ? 'Line' : 'Line ' + (j + 1)); };
18
+ var barCol = function (j) { return pal[j % pal.length]; }, lineCol = function (j) { return pal[(k + j) % pal.length]; };
19
+ var val = function (s, i) { return i < s.length ? s[i] : 0; };
20
+
21
+ var dashOf = styleOf(o.dash, Object.keys(DASHES), 'solid'), markOf = styleOf(o.markers, SHAPES, 'none');
22
+ var patOf = k > 1 ? styleOf(o.patterns, PATTERNS, 'solid') : null, bg = o.background || th.tile, defs = '', fills = [];
23
+ for (var q = 0; patOf && q < k; q++) {
24
+ if (patOf(q) === 'solid') continue;
25
+ var pid = newId('g');
26
+ defs += pattern(pid, patOf(q), barCol(q), bg);
27
+ fills[q] = 'url(#' + pid + ')';
28
+ }
29
+ // The legend shows a bar swatch for each bar series and a short line for each line.
30
+ var key = function (e, x, y) {
31
+ if (e < k) return '<rect x="' + r2(x) + '" y="' + r2(y + 1) + '" width="10" height="10" rx="2" fill="' + (fills[e] || barCol(e)) + '"/>';
32
+ var j = e - k, c = lineCol(j), dk = dashOf ? DASHES[dashOf(j)] : '', mk = markOf ? markOf(j) : 'none';
33
+ return '<line x1="' + r2(x) + '" x2="' + r2(x + 18) + '" y1="' + r2(y + 6) + '" y2="' + r2(y + 6) + '" stroke="' + c + '" stroke-width="2.5" stroke-linecap="round"' +
34
+ (dk ? ' stroke-dasharray="' + dk + '"' : '') + '/>' + (mk !== 'none' ? marker(mk, x + 9, y + 6, 3.5, ' fill="' + c + '" stroke="' + bg + '" stroke-width="1.5"') : '');
35
+ };
36
+
37
+ // Each column's bar extent: side by side, or piled up with positive parts above zero and negative parts below.
38
+ var tops = [], bottoms = [];
39
+ for (var i = 0; i < n; i++) {
40
+ var up = 0, down = 0;
41
+ B.forEach(function (s) {
42
+ var v = val(s, i);
43
+ if (stacked) { if (v >= 0) up += v; else down += v; }
44
+ else { up = Math.max(up, v); down = Math.min(down, v); }
45
+ });
46
+ tops.push(up); bottoms.push(down);
47
+ }
48
+
49
+ return tile(o, th, h, function (x0, y0, w, h) {
50
+ if (!n || !k) return emptyState(th, x0, y0, w, h);
51
+ var s = defs ? '<defs>' + defs + '</defs>' : '', top = y0;
52
+ var lg = legend(B.map(function (_, j) { return barName(j); }).concat(L.map(function (_, j) { return lineName(j); })), pal, th, x0, top, w, key, 24);
53
+ s += lg.svg;
54
+ top += lg.height;
55
+ var titled = has(o.yLabel) || has(o.lineLabel);
56
+ if (titled) top += 16;
57
+ // Left axis: bars, always including zero. Right axis: lines, with the same number of steps.
58
+ var sc = niceScale(Math.min.apply(null, bottoms), Math.max.apply(null, tops), 4), steps = sc.ticks.length - 1;
59
+ var all = [].concat.apply([], L);
60
+ var lmin = Math.min.apply(null, all), lmax = Math.max.apply(null, all);
61
+ if (bool(o.lineZero)) { lmin = Math.min(0, lmin); lmax = Math.max(0, lmax); }
62
+ var sc2 = m ? matched(lmin, lmax, steps) : null;
63
+ var tl = sc.ticks.map(function (t) { return fmt(t, o, true); }), tr = sc2 ? sc2.ticks.map(function (t) { return fmt(t, lo, true); }) : [];
64
+ var lw = Math.max.apply(null, tl.map(function (t) { return tw(t, 10, true); })) + 10;
65
+ var rw = sc2 ? Math.max.apply(null, tr.map(function (t) { return tw(t, 10, true); })) + 10 : 0;
66
+ var px = x0 + lw, pw = w - lw - rw, ph = h - (top - y0) - (labels.length ? 20 : 4), band = pw / n;
67
+ var X = function (i) { return px + band * (i + 0.5); };
68
+ var Y = function (v) { return top + ph - (v - sc.lo) / (sc.hi - sc.lo) * ph; };
69
+ var Y2 = function (v) { return top + ph - (v - sc2.lo) / (sc2.hi - sc2.lo) * ph; };
70
+ sc.ticks.forEach(function (t, i) {
71
+ var y = Y(t);
72
+ s += '<line x1="' + r2(px) + '" x2="' + r2(px + pw) + '" y1="' + r2(y) + '" y2="' + r2(y) + '" stroke="' + th.grid + '"' + (t === 0 ? '' : ' stroke-dasharray="2 4"') + '/>' +
73
+ text(px - 8, y + 3.5, tl[i], small(th, 10, 'text-anchor="end"')) + (sc2 ? text(px + pw + 8, y + 3.5, tr[i], small(th, 10, 'text-anchor="start"')) : '');
74
+ });
75
+ // A rule down each side in its data's colour, and titles above, so it's clear which axis belongs to what.
76
+ // With several series on one side, the rule takes the theme's text colour instead.
77
+ var leftC = k === 1 ? barCol(0) : th.muted, rightC = m === 1 ? lineCol(0) : th.muted;
78
+ s += '<line x1="' + r2(px) + '" x2="' + r2(px) + '" y1="' + r2(top) + '" y2="' + r2(top + ph) + '" stroke="' + leftC + '" stroke-width="2"/>';
79
+ if (sc2) s += '<line x1="' + r2(px + pw) + '" x2="' + r2(px + pw) + '" y1="' + r2(top) + '" y2="' + r2(top + ph) + '" stroke="' + rightC + '" stroke-width="2"/>';
80
+ if (has(o.yLabel)) s += text(x0, top - 8, o.yLabel, small(th, 10, '', k === 1 ? leftC : th.fg));
81
+ if (has(o.lineLabel) && sc2) s += text(x0 + w, top - 8, o.lineLabel, small(th, 10, 'text-anchor="end"', m === 1 ? rightC : th.fg));
82
+ if (labels.length) {
83
+ var every = Math.max(1, Math.ceil((Math.max.apply(null, labels.map(function (l) { return tw(l, 10, true); })) + 10) / band));
84
+ for (i = 0; i < n; i += every) if (labels[i] != null) s += text(X(i), top + ph + 15, labels[i], small(th, 10, 'text-anchor="middle"'));
85
+ }
86
+ // Bars: side by side within each column, or one pile per column.
87
+ var gw = band * 0.72, bw = stacked ? gw : gw / k;
88
+ for (i = 0; i < n; i++) {
89
+ var up = 0, down = 0;
90
+ B.forEach(function (ser, j) {
91
+ if (i >= ser.length) return;
92
+ var v = ser[i], a, b, x = stacked ? X(i) - gw / 2 : X(i) - gw / 2 + j * bw;
93
+ if (stacked) { if (v >= 0) { a = up; up += v; b = up; } else { a = down; down += v; b = down; } }
94
+ else { a = 0; b = v; }
95
+ var ya = Y(a), yb = Y(b);
96
+ s += '<rect x="' + r2(x + 0.5) + '" y="' + r2(Math.min(ya, yb)) + '" width="' + r2(Math.max(1, bw - 1)) + '" height="' + r2(Math.abs(yb - ya)) + '" rx="2" fill="' + (fills[j] || barCol(j)) + '"' +
97
+ anim(o, v < 0 ? 'bzc-gn' : 'bzc-g', Math.min(0.6, i * 0.04)) + '/>';
98
+ });
99
+ }
100
+ // Lines over the bars, on the right axis.
101
+ L.forEach(function (ser, j) {
102
+ var c = lineCol(j), P = ser.map(function (v, i) { return [X(i), Y2(v)]; }), mk = markOf ? markOf(j) : 'none';
103
+ s += seriesLine(o, pathOf(P, bool(o.straight)), c, j, dashOf ? DASHES[dashOf(j)] : '');
104
+ if (mk !== 'none') s += pointMarks(o, mk, ser, X, Y2, c, bg, j);
105
+ else s += '<circle cx="' + r2(P[P.length - 1][0]) + '" cy="' + r2(P[P.length - 1][1]) + '" r="4" fill="' + c + '" stroke="' + bg + '" stroke-width="2"/>';
106
+ });
107
+ for (i = 0; i < n; i++) {
108
+ var tip = labels[i] || '#' + (i + 1), t = 0;
109
+ B.forEach(function (ser, j) { if (i < ser.length) { tip += '\n' + barName(j) + ': ' + fmt(ser[i], o); t += ser[i]; } });
110
+ if (stacked) tip += '\nTotal: ' + fmt(t, o);
111
+ L.forEach(function (ser, j) { if (i < ser.length) tip += '\n' + lineName(j) + ': ' + fmt(ser[i], lo); });
112
+ s += hit(X(i) - band / 2, top, band, ph, tip);
113
+ }
114
+ return s;
115
+ }, { summary: k + ' bar series' + (stacked ? ' stacked' : '') + ' and ' + m + (m === 1 ? ' line' : ' lines') + ' over ' + n + ' points' });
116
+ }
117
+
118
+ combo.table = function (o) {
119
+ var B = series(o.values), L = series(o.line), labels = list(o.labels), bn = list(o.series), ln = list(o.lineSeries);
120
+ var k = B.length, m = L.length, stacked = k > 1 && bool(o.stacked), lo = lineOpts(o), rows = [];
121
+ var n = Math.max.apply(null, [0].concat(B.concat(L).map(function (s) { return s.length; })));
122
+ for (var i = 0; i < n; i++) {
123
+ var row = [labels[i] || '#' + (i + 1)], t = 0;
124
+ B.forEach(function (s) { row.push(i < s.length ? fmt(s[i], o) : ''); t += i < s.length ? s[i] : 0; });
125
+ if (stacked) row.push(fmt(t, o));
126
+ L.forEach(function (s) { row.push(i < s.length ? fmt(s[i], lo) : ''); });
127
+ rows.push(row);
128
+ }
129
+ return {
130
+ head: ['Label'].concat(B.map(function (_, j) { return bn[j] || (k === 1 ? 'Bars' : 'Bars ' + (j + 1)); }), stacked ? ['Total'] : [],
131
+ L.map(function (_, j) { return ln[j] || (m === 1 ? 'Line' : 'Line ' + (j + 1)); })),
132
+ rows: rows,
133
+ };
134
+ };
135
+
136
+ /** Number formatting for the line's axis and values, from line-prefix, line-suffix and line-decimals. */
137
+ function lineOpts(o) {
138
+ return { prefix: o.linePrefix, suffix: o.lineSuffix, decimals: o.lineDecimals, compact: o.compact, lang: o.lang };
139
+ }
140
+
141
+ /**
142
+ * A round scale over min to max with exactly n steps, so its gridlines match another axis. Steps grow through
143
+ * 1, 2 and 5 times powers of ten until n of them reach from a round start past max.
144
+ */
145
+ function matched(min, max, n) {
146
+ if (min === max) {
147
+ if (min === 0) max = 1;
148
+ else { var p = Math.abs(min) * 0.1; min -= p; max += p; }
149
+ }
150
+ var mag = Math.pow(10, Math.floor(Math.log10((max - min) / n))), steps = [1, 2, 5, 10], i = 0, step, lo;
151
+ for (;;) {
152
+ step = steps[i] * mag;
153
+ lo = Math.floor(min / step) * step;
154
+ if (lo + n * step >= max - step * 1e-9) break;
155
+ if (++i === 4) { i = 1; mag *= 10; }
156
+ }
157
+ var ticks = [];
158
+ for (var t = 0; t <= n; t++) ticks.push(+(lo + t * step).toFixed(10));
159
+ return { lo: ticks[0], hi: ticks[n], ticks: ticks };
160
+ }
@@ -1,6 +1,8 @@
1
- import { newId, r2, num, list, series, bool, fmt, pathOf, text, small, anim, palette, emptyState, parseNotes, statOf } from '../core/util.js';
1
+ import { newId, r2, num, has, list, series, bool, fmt, pathOf, text, small, anim, palette, emptyState, parseNotes, statOf } from '../core/util.js';
2
2
  import { seriesTable, styleOf, DASHES, SHAPES } from '../core/series.js';
3
- import { xyFrame, xyFinish, lineKey, seriesLine, pointMarks } from '../core/xy.js';
3
+ import { xyFrame, xyFinish } from '../core/xy.js';
4
+ import { lineKey, seriesLine, pointMarks } from '../core/lines.js';
5
+ import { parseDates, hasTimes, pointName, datedTable } from '../core/time.js';
4
6
  import { tile } from '../core/tile.js';
5
7
 
6
8
  export default function line(o, th) {
@@ -13,21 +15,34 @@ export default function line(o, th) {
13
15
  // With dashes or markers, the legend shows a short sample of each line instead of a dot.
14
16
  var key = (dashOf || markOf) && lineKey(pal, dashOf, markOf, bg);
15
17
  var nameOf = function (k) { return names[k] || 'Series ' + (k + 1); };
18
+ var T = parseDates(o, n), timed = T && hasTimes(T), lang = o.lang || 'en-GB';
19
+ // A log scale needs positive values; zero and negative ones leave gaps. With none at all, the axis stays linear.
20
+ var all = [].concat.apply([], S), pos = all.filter(function (v) { return v > 0; });
21
+ if (has(o.goal) && +o.goal > 0) pos.push(+o.goal);
22
+ var log = bool(o.log) && pos.length > 0;
16
23
  return tile(o, th, h, function (x0, y0, w, h) {
17
24
  if (!n) return emptyState(th, x0, y0, w, h);
18
- var all = [].concat.apply([], S);
19
25
  var f = xyFrame(o, th, {
20
26
  n: n, names: multi ? S.map(function (_, k) { return nameOf(k); }) : null, pal: pal, key: key, kw: key && 24,
21
27
  lo: Math.min.apply(null, all), hi: Math.max.apply(null, all), zero: bool(o.zero), labels: labels, notes: notes,
22
- tick: function (t) { return fmt(t, o, true); },
28
+ tick: function (t) { return fmt(t, o, true); }, dates: T, min: has(o.min) ? +o.min : NaN, max: has(o.max) ? +o.max : NaN,
29
+ log: log, posLo: log ? Math.min.apply(null, pos) : 0, posHi: log ? Math.max.apply(null, pos) : 0,
23
30
  }, x0, y0, w, h);
24
- var s = f.svg, X = f.X, Y = f.Y, px = f.px, pw = f.pw, top = f.top, ph = f.ph;
31
+ var s = f.svg + (f.clip ? '<g' + f.clip + '>' : ''), X = f.X, Y = f.Y, px = f.px, pw = f.pw, top = f.top, ph = f.ph;
25
32
  S.forEach(function (v, k) {
26
33
  var c = pal[k % pal.length];
27
34
  var P = v.map(function (y, i) { return [X(i), Y(y)]; });
28
35
  if (P.length === 1) P = [[px, P[0][1]], [px + pw, P[0][1]]];
29
- var d = pathOf(P, bool(o.straight));
30
- if (k === 0) {
36
+ // On a log scale the line breaks around points it can't place.
37
+ var runs = log ? P.reduce(function (r, p) {
38
+ if (isFinite(p[1])) r[r.length - 1].push(p); else if (r[r.length - 1].length) r.push([]);
39
+ return r;
40
+ }, [[]]).filter(function (r) { return r.length; }) : [P];
41
+ if (!runs.length) return;
42
+ var d = runs.map(function (r) { return pathOf(r, bool(o.straight)); }).join('');
43
+ P = [].concat.apply([], runs);
44
+ // The soft fill under the first line has no floor to reach on a log scale, so it's left out there.
45
+ if (k === 0 && !log) {
31
46
  var g = newId('g'), last = P[P.length - 1];
32
47
  s += '<defs><linearGradient id="' + g + '" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="' + c + '" stop-opacity=".26"/><stop offset="1" stop-color="' + c + '" stop-opacity="0"/></linearGradient></defs>' +
33
48
  '<path d="' + d + 'L' + r2(last[0]) + ',' + r2(top + ph) + 'L' + r2(P[0][0]) + ',' + r2(top + ph) + 'Z" fill="url(#' + g + ')"' + anim(o, 'bzc-a') + '/>';
@@ -40,14 +55,14 @@ export default function line(o, th) {
40
55
  var e = P[P.length - 1];
41
56
  s += '<circle cx="' + r2(e[0]) + '" cy="' + r2(e[1]) + '" r="4" fill="' + c + '" stroke="' + bg + '" stroke-width="2"/>';
42
57
  }
43
- if (bool(o.showValues)) v.forEach(function (y, i) { s += text(X(i), Y(y) - 8, fmt(y, o, true), small(th, 9, 'text-anchor="middle"')); });
58
+ if (bool(o.showValues)) v.forEach(function (y, i) { if (isFinite(Y(y))) s += text(X(i), Y(y) - 8, fmt(y, o, true), small(th, 9, 'text-anchor="middle"')); });
44
59
  });
45
- return s + xyFinish(o, th, f, n, function (j) {
46
- var tip = labels[j] || '#' + (j + 1);
60
+ return s + (f.clip ? '</g>' : '') + xyFinish(o, th, f, n, function (j) {
61
+ var tip = pointName(labels, T, j, lang, timed);
47
62
  S.forEach(function (v, k) { if (j < v.length) tip += '\n' + (multi ? nameOf(k) + ': ' : '') + fmt(v[j], o); });
48
63
  return tip;
49
64
  });
50
65
  }, { stat: st, summary: S.length + ' series over ' + n + ' points' });
51
66
  }
52
67
 
53
- line.table = function (o) { return seriesTable(o); };
68
+ line.table = function (o) { var t = seriesTable(o); return datedTable(t, o, t.rows.length); };
@@ -0,0 +1,30 @@
1
+ /** Series lines, their markers and legend samples, shared by line, area and combo charts. */
2
+ import { r2, anim } from './util.js';
3
+ import { marker, DASHES } from './series.js';
4
+
5
+ /** A legend swatch for a line: a short sample with its dash and marker. */
6
+ export function lineKey(pal, dashOf, markOf, bg) {
7
+ return function (k, x, y) {
8
+ var c = pal[k % pal.length], dk = dashOf ? DASHES[dashOf(k)] : '', mk = markOf ? markOf(k) : 'none';
9
+ return '<line x1="' + r2(x) + '" x2="' + r2(x + 18) + '" y1="' + r2(y + 6) + '" y2="' + r2(y + 6) + '" stroke="' + c + '" stroke-width="2.5" stroke-linecap="round"' +
10
+ (dk ? ' stroke-dasharray="' + dk + '"' : '') + '/>' + (mk !== 'none' ? marker(mk, x + 9, y + 6, 3.5, ' fill="' + c + '" stroke="' + bg + '" stroke-width="1.5"') : '');
11
+ };
12
+ }
13
+
14
+ /** A series line along path d. The draw-in animation is itself a dash, so dashed lines fade in instead. */
15
+ export function seriesLine(o, d, c, k, dk) {
16
+ return dk
17
+ ? '<path d="' + d + '" fill="none" stroke="' + c + '" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="' + dk + '"' + anim(o, 'bzc-a', k * 0.15) + '/>'
18
+ : '<path d="' + d + '" fill="none" stroke="' + c + '" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" pathLength="1" style="stroke-dasharray:1' + (o.animate ? ';animation-delay:' + k * 0.15 + 's' : '') + '"' + (o.animate ? ' class="bzc-d"' : '') + '/>';
19
+ }
20
+
21
+ /** Markers on a series' points. Long series get one on every few points, and always on the last. */
22
+ export function pointMarks(o, mk, ys, X, Y, c, bg, k) {
23
+ var s = '', every = Math.ceil(ys.length / 40);
24
+ ys.forEach(function (y, i) {
25
+ // On a log scale, zero and negative values have no position, so they get no marker.
26
+ if (!isFinite(Y(y))) return;
27
+ if (i % every === 0 || i === ys.length - 1) s += marker(mk, X(i), Y(y), 3.5, ' fill="' + c + '" stroke="' + bg + '" stroke-width="1.5"' + anim(o, 'bzc-a', 0.6 + k * 0.15));
28
+ });
29
+ return s;
30
+ }
@@ -0,0 +1,101 @@
1
+ /** Real dates along the bottom of line and area charts: parsing, tick steps and labels. Only those charts load this. */
2
+ import { list } from './util.js';
3
+
4
+ var HOUR = 36e5, DAY = 864e5;
5
+
6
+ /**
7
+ * The dates option as UTC times, one per point: "2026-01-05", or with a time, "2026-01-05T14:30". Returns null, so
8
+ * points stay evenly spaced, unless there is a valid date for every point.
9
+ */
10
+ export function parseDates(o, n) {
11
+ var d = list(o.dates);
12
+ if (!n || d.length < n) return null;
13
+ var out = [];
14
+ for (var i = 0; i < n; i++) {
15
+ var m = /^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2}))?)?$/.exec(String(d[i]).trim());
16
+ if (!m) return null;
17
+ out.push(Date.UTC(+m[1], +m[2] - 1, +m[3], +(m[4] || 0), +(m[5] || 0), +(m[6] || 0)));
18
+ }
19
+ return out;
20
+ }
21
+
22
+ function fmt(lang, opt) {
23
+ try { return new Intl.DateTimeFormat(lang, Object.assign({ timeZone: 'UTC' }, opt)); } catch (e) { return new Intl.DateTimeFormat('en-GB', Object.assign({ timeZone: 'UTC' }, opt)); }
24
+ }
25
+
26
+ var pad = function (n) { return (n < 10 ? '0' : '') + n; };
27
+ var clock = function (t) { var d = new Date(t); return pad(d.getUTCHours()) + ':' + pad(d.getUTCMinutes()); };
28
+
29
+ /** A date for tooltips and the data table: "5 Jan 2026", with the time too when the dates have times. */
30
+ export function dateText(t, lang, withTime) {
31
+ return fmt(lang, { day: 'numeric', month: 'short', year: 'numeric' }).format(new Date(t)) + (withTime ? ' ' + clock(t) : '');
32
+ }
33
+
34
+ /** Whether any of the dates has a time of day. */
35
+ export function hasTimes(T) {
36
+ return T.some(function (t) { return t % DAY !== 0; });
37
+ }
38
+
39
+ // Tick steps from an hour to a century, smallest first. Each is a unit and how many of it.
40
+ var STEPS = [['h', 1], ['h', 3], ['h', 6], ['h', 12], ['d', 1], ['d', 2], ['w', 1], ['w', 2], ['m', 1], ['m', 2], ['m', 3], ['m', 6],
41
+ ['y', 1], ['y', 2], ['y', 5], ['y', 10], ['y', 20], ['y', 50], ['y', 100]];
42
+ var ROUGH = { h: HOUR, d: DAY, w: 7 * DAY, m: 30.44 * DAY, y: 365.25 * DAY };
43
+
44
+ /** Every tick of one step between t0 and t1: whole hours, midnights, Mondays, firsts of the month or new years. */
45
+ function ticksFor(t0, t1, unit, k) {
46
+ var out = [], t;
47
+ if (unit === 'h' || unit === 'd' || unit === 'w') {
48
+ var size = unit === 'h' ? HOUR * k : DAY * (unit === 'w' ? 7 * k : k);
49
+ // Weeks start on Mondays: 5 January 1970 was the first Monday after the epoch.
50
+ var from = unit === 'w' ? 4 * DAY : 0;
51
+ for (t = Math.ceil((t0 - from) / size) * size + from; t <= t1; t += size) out.push(t);
52
+ return out;
53
+ }
54
+ var d = new Date(t0), y = d.getUTCFullYear(), mo = unit === 'm' ? d.getUTCMonth() : 0;
55
+ for (;;) {
56
+ t = Date.UTC(y, mo, 1);
57
+ if (t > t1) return out;
58
+ if (t >= t0 && (unit === 'm' ? mo % k === 0 : y % k === 0)) out.push(t);
59
+ if (unit === 'm') { mo++; if (mo === 12) { mo = 0; y++; } } else y++;
60
+ }
61
+ }
62
+
63
+ /**
64
+ * Ticks and labels for the dates t0 to t1 across room pixels: the smallest step that leaves about 64 px per label.
65
+ * Hours read 14:00 (midnight shows the day), days and weeks 5 Jan, months Jan with the year on January and the first
66
+ * tick, and years 2026.
67
+ */
68
+ export function dateTicks(t0, t1, room, lang) {
69
+ var most = Math.max(2, Math.floor(room / 64)), span = t1 - t0;
70
+ for (var i = 0; i < STEPS.length; i++) {
71
+ var unit = STEPS[i][0], k = STEPS[i][1];
72
+ if (span / (ROUGH[unit] * k) > most + 2) continue;
73
+ var ticks = ticksFor(t0, t1, unit, k);
74
+ if (ticks.length > most) continue;
75
+ var dm = fmt(lang, { day: 'numeric', month: 'short' }), my = fmt(lang, { month: 'short', year: 'numeric' }), mm = fmt(lang, { month: 'short' });
76
+ return ticks.map(function (t, j) {
77
+ var d = new Date(t), text;
78
+ if (unit === 'h') text = t % DAY === 0 ? dm.format(d) : clock(t);
79
+ else if (unit === 'd' || unit === 'w') text = dm.format(d);
80
+ else if (unit === 'm') text = d.getUTCMonth() === 0 || j === 0 ? my.format(d) : mm.format(d);
81
+ else text = String(d.getUTCFullYear());
82
+ return { t: t, text: text };
83
+ });
84
+ }
85
+ return [];
86
+ }
87
+
88
+ /** A point's name: its label, else its date, else its number. */
89
+ export function pointName(labels, T, i, lang, timed) {
90
+ return labels[i] || (T ? dateText(T[i], lang, timed) : '#' + (i + 1));
91
+ }
92
+
93
+ /** Puts dates in a data table's first column, for the rows without a label. */
94
+ export function datedTable(t, o, n) {
95
+ var T = parseDates(o, n), labels = list(o.labels);
96
+ if (!T) return t;
97
+ var timed = hasTimes(T);
98
+ t.head[0] = 'Date';
99
+ t.rows.forEach(function (r, i) { if (!labels[i]) r[0] = dateText(T[i], o.lang || 'en-GB', timed); });
100
+ return t;
101
+ }
package/src/core/xy.js CHANGED
@@ -1,15 +1,19 @@
1
- /** The axis frame and series lines line and area charts share. Kept apart so other charts don't load them. */
2
- import { r2, clamp, has, tw, text, small, hit, anim, goalLine } from './util.js';
3
- import { niceScale, legend, marker, DASHES } from './series.js';
1
+ /** The axis frame line and area charts share: dates, fixed ranges and log scales included. Other charts don't load it. */
2
+ import { newId, r2, clamp, has, tw, text, small, hit, goalLine } from './util.js';
3
+ import { niceScale, legend } from './series.js';
4
+ import { dateTicks } from './time.js';
4
5
 
5
6
  /**
6
7
  * The frame line and area charts share: the legend, the value axis with its gridlines, labels along the bottom and notes.
7
8
  * c holds n (points), names (legend names, or null for no legend), pal, key and kw (legend swatches), lo and hi (the data
8
- * range), zero (keep zero on the axis), labels, notes and tick (formats an axis number).
9
- * Returns the SVG so far, the scales X(i) and Y(v), and the plot box: px, pw, top, ph.
9
+ * range), zero (keep zero on the axis), labels, notes and tick (formats an axis number). Optionally, dates (a UTC time
10
+ * per point, spacing points in time), min and max (fixing the value axis, NaN when not set) and log (with posLo and
11
+ * posHi, the data's smallest and largest positive values).
12
+ * Returns the SVG so far, the scales X(i) and Y(v), the plot box (px, pw, top, ph), and clip: an attribute that keeps
13
+ * series inside the plot when min or max is set, or ''.
10
14
  */
11
15
  export function xyFrame(o, th, c, x0, y0, w, h) {
12
- var s = '', top = y0, n = c.n, labels = c.labels;
16
+ var s = '', top = y0, n = c.n, labels = c.labels, T = c.dates;
13
17
  if (c.names) {
14
18
  var lg = legend(c.names, c.pal, th, x0, top, w, c.key, c.kw);
15
19
  s += lg.svg;
@@ -18,19 +22,42 @@ export function xyFrame(o, th, c, x0, y0, w, h) {
18
22
  var mn = c.lo, mx = c.hi;
19
23
  if (has(o.goal)) { mn = Math.min(mn, +o.goal); mx = Math.max(mx, +o.goal); }
20
24
  if (c.zero) { mn = Math.min(0, mn); mx = Math.max(0, mx); }
21
- var sc = niceScale(mn, mx, 4);
25
+ // A min or max you set wins over the data, the goal and zero. Both together must leave some room between them.
26
+ var hasMin = isFinite(c.min), hasMax = isFinite(c.max);
27
+ if (hasMin && hasMax && c.min >= c.max) hasMin = hasMax = false;
28
+ var sc = c.log ? logScale(hasMin && c.min > 0 ? c.min : c.posLo, hasMax && c.max > 0 ? c.max : c.posHi)
29
+ : hasMin || hasMax ? fixedScale(hasMin ? c.min : mn, hasMax ? c.max : mx, hasMin, hasMax)
30
+ : niceScale(mn, mx, 4);
22
31
  var tl = sc.ticks.map(c.tick);
23
32
  var axisW = Math.max.apply(null, tl.map(function (t) { return tw(t, 10, true); })) + 10;
24
- var bottom = labels.length ? 20 : 4;
33
+ var bottom = labels.length || T ? 20 : 4;
25
34
  var px = x0 + axisW, pw = w - axisW, ph = h - (top - y0) - bottom;
26
- var X = function (i) { return n === 1 ? px + pw / 2 : px + i * pw / (n - 1); };
27
- var Y = function (v) { return top + ph - (v - sc.lo) / (sc.hi - sc.lo) * ph; };
35
+ var t0 = T ? Math.min.apply(null, T) : 0, span = T ? Math.max.apply(null, T) - t0 : 0;
36
+ // With dates, points sit where they fall in time; otherwise they are evenly spaced.
37
+ var X = T
38
+ ? function (i) { return span ? px + (T[i] - t0) / span * pw : px + pw / 2; }
39
+ : function (i) { return n === 1 ? px + pw / 2 : px + i * pw / (n - 1); };
40
+ var Y = sc.log
41
+ ? function (v) { return top + ph - (Math.log10(v) - sc.a) / (sc.b - sc.a) * ph; }
42
+ : function (v) { return top + ph - (v - sc.lo) / (sc.hi - sc.lo) * ph; };
43
+ var clip = '';
44
+ if (hasMin || hasMax) {
45
+ var cid = newId('c');
46
+ s += '<defs><clipPath id="' + cid + '"><rect x="' + r2(px - 4) + '" y="' + r2(top - 4) + '" width="' + r2(pw + 8) + '" height="' + r2(ph + 8) + '"/></clipPath></defs>';
47
+ clip = ' clip-path="url(#' + cid + ')"';
48
+ }
28
49
  sc.ticks.forEach(function (t, i) {
29
50
  var y = Y(t);
30
51
  s += '<line x1="' + r2(px) + '" x2="' + r2(px + pw) + '" y1="' + r2(y) + '" y2="' + r2(y) + '" stroke="' + th.grid + '"' + (t === 0 ? '' : ' stroke-dasharray="2 4"') + '/>' +
31
52
  text(px - 8, y + 3.5, tl[i], small(th, 10, 'text-anchor="end"'));
32
53
  });
33
- if (labels.length) {
54
+ if (T) {
55
+ // Date ticks pick their own step, and labels near the ends are nudged in so they stay inside the plot.
56
+ dateTicks(t0, t0 + span, pw, o.lang || 'en-GB').forEach(function (d) {
57
+ var half = tw(d.text, 10, true) / 2, x = span ? px + (d.t - t0) / span * pw : px + pw / 2;
58
+ s += text(clamp(x, px + half, px + pw - half), top + ph + 15, d.text, small(th, 10, 'text-anchor="middle"'));
59
+ });
60
+ } else if (labels.length) {
34
61
  var every = Math.max(1, Math.ceil(n * (Math.max.apply(null, labels.map(function (l) { return tw(l, 10, true); })) + 10) / pw));
35
62
  for (var i = 0; i < n; i += every) {
36
63
  if (labels[i] == null) continue;
@@ -44,38 +71,46 @@ export function xyFrame(o, th, c, x0, y0, w, h) {
44
71
  s += '<line x1="' + r2(x) + '" x2="' + r2(x) + '" y1="' + r2(top + 12) + '" y2="' + r2(top + ph) + '" stroke="' + th.fg + '" stroke-opacity=".4" stroke-dasharray="3 3"/>' +
45
72
  text(clamp(x, px + 30, px + pw - 30), top + 8, nt.text, small(th, 10, 'text-anchor="middle"', th.fg));
46
73
  });
47
- return { svg: s, X: X, Y: Y, px: px, pw: pw, top: top, ph: ph };
74
+ return { svg: s, X: X, Y: Y, px: px, pw: pw, top: top, ph: ph, clip: clip, uneven: !!T };
48
75
  }
49
76
 
50
- /** The goal line, then one hover column per point, centred on it. tip(j) gives each column's tooltip. */
51
- export function xyFinish(o, th, f, n, tip) {
52
- var s = has(o.goal) ? goalLine(o, th, f.px, f.px + f.pw, f.Y(+o.goal)) : '';
53
- var step = n > 1 ? f.pw / (n - 1) : f.pw;
54
- for (var j = 0; j < n; j++) s += hit(f.X(j) - step / 2, f.top, step, f.ph, tip(j));
55
- return s;
56
- }
57
-
58
- /** A legend swatch for a line: a short sample with its dash and marker. */
59
- export function lineKey(pal, dashOf, markOf, bg) {
60
- return function (k, x, y) {
61
- var c = pal[k % pal.length], dk = dashOf ? DASHES[dashOf(k)] : '', mk = markOf ? markOf(k) : 'none';
62
- return '<line x1="' + r2(x) + '" x2="' + r2(x + 18) + '" y1="' + r2(y + 6) + '" y2="' + r2(y + 6) + '" stroke="' + c + '" stroke-width="2.5" stroke-linecap="round"' +
63
- (dk ? ' stroke-dasharray="' + dk + '"' : '') + '/>' + (mk !== 'none' ? marker(mk, x + 9, y + 6, 3.5, ' fill="' + c + '" stroke="' + bg + '" stroke-width="1.5"') : '');
64
- };
77
+ /**
78
+ * A scale from lo to hi where min, max or both are fixed. Gridlines fall on the round steps inside the range, and a
79
+ * fixed end gets its own line too unless a round one sits close by.
80
+ */
81
+ function fixedScale(lo, hi, hasMin, hasMax) {
82
+ var nice = niceScale(lo, hi, 4), a = hasMin ? lo : nice.lo, b = hasMax ? hi : nice.hi, step = nice.ticks[1] - nice.ticks[0];
83
+ var ticks = nice.ticks.filter(function (t) { return t >= a - step * 1e-9 && t <= b + step * 1e-9; });
84
+ if (hasMin && (!ticks.length || ticks[0] - a > step * 0.4)) ticks.unshift(a);
85
+ if (hasMax && (!ticks.length || b - ticks[ticks.length - 1] > step * 0.4)) ticks.push(b);
86
+ return { lo: a, hi: b, ticks: ticks };
65
87
  }
66
88
 
67
- /** A series line along path d. The draw-in animation is itself a dash, so dashed lines fade in instead. */
68
- export function seriesLine(o, d, c, k, dk) {
69
- return dk
70
- ? '<path d="' + d + '" fill="none" stroke="' + c + '" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="' + dk + '"' + anim(o, 'bzc-a', k * 0.15) + '/>'
71
- : '<path d="' + d + '" fill="none" stroke="' + c + '" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" pathLength="1" style="stroke-dasharray:1' + (o.animate ? ';animation-delay:' + k * 0.15 + 's' : '') + '"' + (o.animate ? ' class="bzc-d"' : '') + '/>';
89
+ /** A log scale over whole powers of ten from lo to hi, with 2 and 5 between them when it spans two powers or fewer. */
90
+ function logScale(lo, hi) {
91
+ var a = Math.floor(Math.log10(lo)), b = Math.ceil(Math.log10(hi)), ticks = [];
92
+ if (b === a) b++;
93
+ for (var e = a; e <= b; e++) {
94
+ var p = Number('1e' + e);
95
+ ticks.push(p);
96
+ if (b - a <= 2 && e < b) ticks.push(2 * p, 5 * p);
97
+ }
98
+ return { log: true, a: a, b: b, lo: ticks[0], hi: ticks[ticks.length - 1], ticks: ticks };
72
99
  }
73
100
 
74
- /** Markers on a series' points. Long series get one on every few points, and always on the last. */
75
- export function pointMarks(o, mk, ys, X, Y, c, bg, k) {
76
- var s = '', every = Math.ceil(ys.length / 40);
77
- ys.forEach(function (y, i) {
78
- if (i % every === 0 || i === ys.length - 1) s += marker(mk, X(i), Y(y), 3.5, ' fill="' + c + '" stroke="' + bg + '" stroke-width="1.5"' + anim(o, 'bzc-a', 0.6 + k * 0.15));
79
- });
101
+ /**
102
+ * The goal line, then one hover column per point. Evenly spaced points get equal columns centred on them; dated points
103
+ * get columns that reach halfway to each neighbour. tip(j) gives each column's tooltip.
104
+ */
105
+ export function xyFinish(o, th, f, n, tip) {
106
+ var g = has(o.goal) ? f.Y(+o.goal) : NaN;
107
+ var s = isFinite(g) ? goalLine(o, th, f.px, f.px + f.pw, g) : '';
108
+ var step = n > 1 ? f.pw / (n - 1) : f.pw, X = f.X;
109
+ for (var j = 0; j < n; j++) {
110
+ if (!f.uneven) { s += hit(X(j) - step / 2, f.top, step, f.ph, tip(j)); continue; }
111
+ var l = j ? (X(j - 1) + X(j)) / 2 : X(0) - (n > 1 ? (X(1) - X(0)) / 2 : f.pw / 2);
112
+ var r = j < n - 1 ? (X(j) + X(j + 1)) / 2 : X(j) + (n > 1 ? (X(j) - X(j - 1)) / 2 : f.pw / 2);
113
+ s += hit(l, f.top, Math.max(1, r - l), f.ph, tip(j));
114
+ }
80
115
  return s;
81
116
  }
package/src/element.js CHANGED
@@ -7,7 +7,7 @@ export const ATTRIBUTES = [
7
7
  'type', 'values', 'labels', 'series', 'heading', 'prefix', 'suffix', 'decimals', 'compact', 'color', 'theme', 'frame',
8
8
  'width', 'height', 'goal', 'goal-label', 'notes', 'show-values', 'stat', 'delta', 'delta-label', 'max', 'min', 'value',
9
9
  'zones', 'horizontal', 'straight', 'zero', 'start', 'lang', 'corners', 'background', 'center-label', 'highlight',
10
- 'x', 'y', 'x-label', 'y-label', 'trend', 'static', 'no-tooltips', 'stacked', 'angle-labels', 'dash', 'markers', 'patterns', 'bins', 'bin-width', 'mark', 'percent', 'whiskers',
10
+ 'x', 'y', 'x-label', 'y-label', 'trend', 'static', 'no-tooltips', 'stacked', 'angle-labels', 'dash', 'markers', 'patterns', 'bins', 'bin-width', 'mark', 'percent', 'whiskers', 'line', 'line-series', 'line-prefix', 'line-suffix', 'line-decimals', 'line-label', 'line-zero', 'dates', 'log',
11
11
  ];
12
12
 
13
13
  const HOST_CSS =
@@ -0,0 +1,10 @@
1
+ /** <bezel-combo> on its own: import 'bezel-charts/combo', or load dist/charts/combo.js. */
2
+ import combo from '../charts/combo.js';
3
+ import { register } from '../render.js';
4
+ import { define } from '../element.js';
5
+
6
+ register('combo', combo);
7
+ define('combo');
8
+
9
+ export { render, toSVG } from '../render.js';
10
+ export { BezelChart } from '../element.js';
package/src/index.js CHANGED
@@ -8,6 +8,7 @@ import './entries/spark.js';
8
8
  import './entries/line.js';
9
9
  import './entries/area.js';
10
10
  import './entries/bars.js';
11
+ import './entries/combo.js';
11
12
  import './entries/histogram.js';
12
13
  import './entries/boxplot.js';
13
14
  import './entries/scatter.js';
@@ -20,6 +21,6 @@ import { NAMES, render, toSVG } from './render.js';
20
21
  import { THEMES } from './core/themes.js';
21
22
  import { BezelChart } from './element.js';
22
23
 
23
- export const version = '1.6.0';
24
+ export const version = '1.8.0';
24
25
  export const types = NAMES.slice();
25
26
  export { render, toSVG, BezelChart, THEMES as themes };
package/src/render.js CHANGED
@@ -2,7 +2,7 @@
2
2
  import { THEMES } from './core/themes.js';
3
3
  import { bool } from './core/util.js';
4
4
  /** Every chart type, in the order the full bundle loads them. */
5
- export const NAMES = ['spark', 'line', 'area', 'bars', 'histogram', 'boxplot', 'scatter', 'ring', 'donut', 'gauge', 'progress', 'heatmap'];
5
+ export const NAMES = ['spark', 'line', 'area', 'bars', 'combo', 'histogram', 'boxplot', 'scatter', 'ring', 'donut', 'gauge', 'progress', 'heatmap'];
6
6
 
7
7
  /** The renderers loaded so far. Each chart's entry file adds its own, so a page only loads the charts it uses. */
8
8
  export const TYPES = {};
@@ -1,2 +0,0 @@
1
- /*! Bezel Charts v1.6.0 | MIT | github.com/ShazMoghaddam/bezel-charts */
2
- import{a as q,b as B,c as C,g as Y}from"./shared-56ZZ4O3F.js";import{e as p,f as H,h as L,m as S,r as M,s as b,t as K,u as X,y as W}from"./shared-YIDF72XF.js";function Q(e,t,r,i,l,h,u){var a="",n=l,s=r.n,k=r.labels;if(r.names){var c=B(r.names,r.pal,t,i,n,h,r.key,r.kw);a+=c.svg,n+=c.height}var f=r.lo,v=r.hi;L(e.goal)&&(f=Math.min(f,+e.goal),v=Math.max(v,+e.goal)),r.zero&&(f=Math.min(0,f),v=Math.max(0,v));var g=q(f,v,4),F=g.ticks.map(r.tick),A=Math.max.apply(null,F.map(function(o){return S(o,10,!0)}))+10,G=k.length?20:4,m=i+A,d=h-A,y=u-(n-l)-G,z=function(o){return s===1?m+d/2:m+o*d/(s-1)},D=function(o){return n+y-(o-g.lo)/(g.hi-g.lo)*y};if(g.ticks.forEach(function(o,w){var E=D(o);a+='<line x1="'+p(m)+'" x2="'+p(m+d)+'" y1="'+p(E)+'" y2="'+p(E)+'" stroke="'+t.grid+'"'+(o===0?"":' stroke-dasharray="2 4"')+"/>"+M(m-8,E+3.5,F[w],b(t,10,'text-anchor="end"'))}),k.length){for(var I=Math.max(1,Math.ceil(s*(Math.max.apply(null,k.map(function(o){return S(o,10,!0)}))+10)/d)),x=0;x<s;x+=I)if(k[x]!=null){var J=s>1&&x===0?"start":s>1&&x===s-1?"end":"middle";a+=M(z(x),n+y+15,k[x],b(t,10,'text-anchor="'+J+'"'))}}return r.notes.forEach(function(o){if(!(o.i>=s)){var w=z(o.i);a+='<line x1="'+p(w)+'" x2="'+p(w)+'" y1="'+p(n+12)+'" y2="'+p(n+y)+'" stroke="'+t.fg+'" stroke-opacity=".4" stroke-dasharray="3 3"/>'+M(H(w,m+30,m+d-30),n+8,o.text,b(t,10,'text-anchor="middle"',t.fg))}}),{svg:a,X:z,Y:D,px:m,pw:d,top:n,ph:y}}function R(e,t,r,i,l){for(var h=L(e.goal)?W(e,t,r.px,r.px+r.pw,r.Y(+e.goal)):"",u=i>1?r.pw/(i-1):r.pw,a=0;a<i;a++)h+=K(r.X(a)-u/2,r.top,u,r.ph,l(a));return h}function T(e,t,r,i){return function(l,h,u){var a=e[l%e.length],n=t?C[t(l)]:"",s=r?r(l):"none";return'<line x1="'+p(h)+'" x2="'+p(h+18)+'" y1="'+p(u+6)+'" y2="'+p(u+6)+'" stroke="'+a+'" stroke-width="2.5" stroke-linecap="round"'+(n?' stroke-dasharray="'+n+'"':"")+"/>"+(s!=="none"?Y(s,h+9,u+6,3.5,' fill="'+a+'" stroke="'+i+'" stroke-width="1.5"'):"")}}function U(e,t,r,i,l){return l?'<path d="'+t+'" fill="none" stroke="'+r+'" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="'+l+'"'+X(e,"bzc-a",i*.15)+"/>":'<path d="'+t+'" fill="none" stroke="'+r+'" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" pathLength="1" style="stroke-dasharray:1'+(e.animate?";animation-delay:"+i*.15+"s":"")+'"'+(e.animate?' class="bzc-d"':"")+"/>"}function V(e,t,r,i,l,h,u,a){var n="",s=Math.ceil(r.length/40);return r.forEach(function(k,c){(c%s===0||c===r.length-1)&&(n+=Y(t,i(c),l(k),3.5,' fill="'+h+'" stroke="'+u+'" stroke-width="1.5"'+X(e,"bzc-a",.6+a*.15)))}),n}export{Q as a,R as b,T as c,U as d,V as e};