bezel-charts 1.1.1 → 1.3.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,10 @@
1
+ /** <bezel-gauge> on its own: import 'bezel-charts/gauge', or load dist/charts/gauge.js. */
2
+ import gauge from '../charts/gauge.js';
3
+ import { register } from '../render.js';
4
+ import { define } from '../element.js';
5
+
6
+ register('gauge', gauge);
7
+ define('gauge');
8
+
9
+ export { render, toSVG } from '../render.js';
10
+ export { BezelChart } from '../element.js';
@@ -0,0 +1,10 @@
1
+ /** <bezel-heatmap> on its own: import 'bezel-charts/heatmap', or load dist/charts/heatmap.js. */
2
+ import heatmap from '../charts/heatmap.js';
3
+ import { register } from '../render.js';
4
+ import { define } from '../element.js';
5
+
6
+ register('heatmap', heatmap);
7
+ define('heatmap');
8
+
9
+ export { render, toSVG } from '../render.js';
10
+ export { BezelChart } from '../element.js';
@@ -0,0 +1,10 @@
1
+ /** <bezel-line> on its own: import 'bezel-charts/line', or load dist/charts/line.js. */
2
+ import line from '../charts/line.js';
3
+ import { register } from '../render.js';
4
+ import { define } from '../element.js';
5
+
6
+ register('line', line);
7
+ define('line');
8
+
9
+ export { render, toSVG } from '../render.js';
10
+ export { BezelChart } from '../element.js';
@@ -0,0 +1,10 @@
1
+ /** <bezel-progress> on its own: import 'bezel-charts/progress', or load dist/charts/progress.js. */
2
+ import progress from '../charts/progress.js';
3
+ import { register } from '../render.js';
4
+ import { define } from '../element.js';
5
+
6
+ register('progress', progress);
7
+ define('progress');
8
+
9
+ export { render, toSVG } from '../render.js';
10
+ export { BezelChart } from '../element.js';
@@ -0,0 +1,10 @@
1
+ /** <bezel-ring> on its own: import 'bezel-charts/ring', or load dist/charts/ring.js. */
2
+ import ring from '../charts/ring.js';
3
+ import { register } from '../render.js';
4
+ import { define } from '../element.js';
5
+
6
+ register('ring', ring);
7
+ define('ring');
8
+
9
+ export { render, toSVG } from '../render.js';
10
+ export { BezelChart } from '../element.js';
@@ -0,0 +1,10 @@
1
+ /** <bezel-scatter> on its own: import 'bezel-charts/scatter', or load dist/charts/scatter.js. */
2
+ import scatter from '../charts/scatter.js';
3
+ import { register } from '../render.js';
4
+ import { define } from '../element.js';
5
+
6
+ register('scatter', scatter);
7
+ define('scatter');
8
+
9
+ export { render, toSVG } from '../render.js';
10
+ export { BezelChart } from '../element.js';
@@ -0,0 +1,10 @@
1
+ /** <bezel-spark> on its own: import 'bezel-charts/spark', or load dist/charts/spark.js. */
2
+ import spark from '../charts/spark.js';
3
+ import { register } from '../render.js';
4
+ import { define } from '../element.js';
5
+
6
+ register('spark', spark);
7
+ define('spark');
8
+
9
+ export { render, toSVG } from '../render.js';
10
+ export { BezelChart } from '../element.js';
package/src/index.js CHANGED
@@ -3,24 +3,20 @@
3
3
  * Small, polished charts as web components. Zero dependencies, pure SVG.
4
4
  * Created by Shaz Moghaddam, London · https://shazmoghaddam.github.io
5
5
  */
6
- import { TYPES, render, toSVG } from './render.js';
6
+ // Each entry registers its chart and defines its tag. Pages that only need a few charts can import those entries instead.
7
+ import './entries/spark.js';
8
+ import './entries/line.js';
9
+ import './entries/bars.js';
10
+ import './entries/scatter.js';
11
+ import './entries/ring.js';
12
+ import './entries/donut.js';
13
+ import './entries/gauge.js';
14
+ import './entries/progress.js';
15
+ import './entries/heatmap.js';
16
+ import { NAMES, render, toSVG } from './render.js';
7
17
  import { THEMES } from './core/themes.js';
8
18
  import { BezelChart } from './element.js';
9
19
 
10
- export const version = '1.1.1';
11
- export const types = Object.keys(TYPES);
20
+ export const version = '1.3.0';
21
+ export const types = NAMES.slice();
12
22
  export { render, toSVG, BezelChart, THEMES as themes };
13
-
14
- if (typeof customElements !== 'undefined' && typeof HTMLElement !== 'undefined') {
15
- for (const type of types) {
16
- const tag = `bezel-${type}`;
17
- if (!customElements.get(tag)) {
18
- customElements.define(tag, class extends BezelChart {
19
- get chartType() {
20
- return type;
21
- }
22
- });
23
- }
24
- }
25
- if (!customElements.get('bezel-chart')) customElements.define('bezel-chart', class extends BezelChart {});
26
- }
package/src/render.js CHANGED
@@ -1,17 +1,16 @@
1
1
  /** Pure rendering: options in, SVG string out. No DOM needed, so it runs in browsers, Node and build scripts. */
2
2
  import { THEMES } from './core/themes.js';
3
3
  import { bool } from './core/util.js';
4
- import spark from './charts/spark.js';
5
- import line from './charts/line.js';
6
- import bars from './charts/bars.js';
7
- import ring from './charts/ring.js';
8
- import donut from './charts/donut.js';
9
- import gauge from './charts/gauge.js';
10
- import progress from './charts/progress.js';
11
- import heatmap from './charts/heatmap.js';
12
- import scatter from './charts/scatter.js';
13
-
14
- export const TYPES = { spark, line, bars, scatter, ring, donut, gauge, progress, heatmap };
4
+ /** Every chart type, in the order the full bundle loads them. */
5
+ export const NAMES = ['spark', 'line', 'bars', 'scatter', 'ring', 'donut', 'gauge', 'progress', 'heatmap'];
6
+
7
+ /** The renderers loaded so far. Each chart's entry file adds its own, so a page only loads the charts it uses. */
8
+ export const TYPES = {};
9
+
10
+ /** Makes a chart type available to render(), toSVG() and the elements. */
11
+ export function register(type, fn) {
12
+ TYPES[type] = fn;
13
+ }
15
14
 
16
15
  const camel = (k) => k.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
17
16
 
@@ -20,18 +19,33 @@ const camel = (k) => k.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
20
19
  * @param {string} type one of TYPES
21
20
  * @param {object} [options] the same names as the HTML attributes, in kebab-case or camelCase
22
21
  */
23
- export function render(type, options = {}) {
22
+ function prepare(type, options) {
24
23
  if (!TYPES[type]) {
25
- throw new Error(`Bezel Charts: unknown chart type "${type}". Choose one of: ${Object.keys(TYPES).join(', ')}.`);
24
+ throw new Error(NAMES.includes(type)
25
+ ? `Bezel Charts: the ${type} chart isn't loaded. Add import 'bezel-charts/${type}', or load dist/charts/${type}.js.`
26
+ : `Bezel Charts: unknown chart type "${type}". Choose one of: ${NAMES.join(', ')}.`);
26
27
  }
27
28
  const o = {};
28
29
  for (const [k, v] of Object.entries(options)) o[camel(k)] = v;
30
+ return o;
31
+ }
32
+
33
+ export function render(type, options = {}) {
34
+ const o = prepare(type, options);
29
35
  o.animate = bool(o.animate);
30
36
  o.frame = o.frame === 'none' ? 'none' : 'bezel';
31
37
  const mode = o.theme === 'light' || o.theme === 'dark' ? o.theme : o.autoTheme || 'dark';
32
38
  return TYPES[type](o, THEMES[mode]);
33
39
  }
34
40
 
41
+ /**
42
+ * The chart's data as a table, for screen readers and anyone who wants the exact numbers:
43
+ * { head: [column names], rows: [[cells], ...] }, with numbers formatted the same way as the chart.
44
+ */
45
+ export function table(type, options = {}) {
46
+ return TYPES[type].table(prepare(type, options));
47
+ }
48
+
35
49
  /** Strips hover targets and tooltip data so the SVG is a clean static image. */
36
50
  export function toStatic(svg) {
37
51
  return svg