@brad-frost-web/eddie-charts 0.1.0-alpha.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,305 @@
1
+ import { c as v } from "../../../chunks/vendor.lY5z9hnC.js";
2
+ import { i as g, b as h, n as d, r as _, e as C } from "../../../chunks/lit.CdaoESnp.js";
3
+ import { C as m, B as y, a as k, b as w, L as $, p as x, c as E } from "../../../chunks/chart-js.6-aKO5e6.js";
4
+ var B = Object.defineProperty, F = (e, t, r, s) => {
5
+ for (var a = void 0, l = e.length - 1, o; l >= 0; l--)
6
+ (o = e[l]) && (a = o(t, r, a) || a);
7
+ return a && B(t, r, a), a;
8
+ };
9
+ class f extends g {
10
+ /**
11
+ * Abstraction of `classnames` that automatically includes any style modifier
12
+ * as well as any set variants.
13
+ *
14
+ * It is expected that `variant` would be overridden in a subclass with more
15
+ * specific types, `@property() variant?: 'foo' | 'bar'`
16
+ *
17
+ * @param baseClassName
18
+ */
19
+ componentClassNames(t, r = {}) {
20
+ return v(t, r);
21
+ }
22
+ /**
23
+ * Check if a slot is empty
24
+ *
25
+ * @param slotName
26
+ */
27
+ slotEmpty(t) {
28
+ return !this.querySelector(`[slot="${t}"]`);
29
+ }
30
+ /**
31
+ * Check if a slot is not empty
32
+ *
33
+ * @param slotName
34
+ */
35
+ slotNotEmpty(t) {
36
+ if (!this.slotEmpty(t))
37
+ return !this.slotEmpty(t);
38
+ }
39
+ /**
40
+ * Dispatch a custom event.
41
+ */
42
+ dispatch({ e: t, eventName: r, detailObj: s = {}, optionsObj: a = {} }) {
43
+ const l = {
44
+ bubbles: !0,
45
+ composed: !0,
46
+ ...a,
47
+ detail: { ...t && { originalEvent: t }, ...s }
48
+ }, o = new CustomEvent(r, l);
49
+ return this.dispatchEvent(o), o;
50
+ }
51
+ /**
52
+ * Example render, should not be used
53
+ */
54
+ render() {
55
+ return h`<slot></slot>`;
56
+ }
57
+ }
58
+ F([
59
+ d()
60
+ ], f.prototype, "styleModifier");
61
+ function i(e, t, r) {
62
+ return getComputedStyle(e).getPropertyValue(t).trim() || r;
63
+ }
64
+ function J(e) {
65
+ const t = getComputedStyle(e), r = [];
66
+ for (let s = 1; s <= 12; s++) {
67
+ const a = t.getPropertyValue(`--ed-chart-palette-${s}`).trim();
68
+ a && r.push(a);
69
+ }
70
+ if (r.length >= 3) return r;
71
+ r.length = 0;
72
+ for (let s = 1; s <= 8; s++) {
73
+ const a = t.getPropertyValue(`--ed-theme-color-content-accent-${s}`).trim();
74
+ a && r.push(a);
75
+ }
76
+ return r.length >= 3 ? r : [
77
+ "#4a90e2",
78
+ "#e24a90",
79
+ "#50c878",
80
+ "#f5a623",
81
+ "#9013fe",
82
+ "#50e3c2",
83
+ "#d0021b",
84
+ "#7ed321"
85
+ ];
86
+ }
87
+ function p(e) {
88
+ return {
89
+ palette: J(e),
90
+ axisColor: i(
91
+ e,
92
+ "--ed-chart-axis-color",
93
+ i(
94
+ e,
95
+ "--ed-theme-color-border-default",
96
+ i(e, "--ed-theme-color-border-subtle", "#333333")
97
+ )
98
+ ),
99
+ gridlineColor: i(
100
+ e,
101
+ "--ed-chart-gridline-color",
102
+ i(e, "--ed-theme-color-border-subtle", "#dddddd")
103
+ ),
104
+ tickColor: i(
105
+ e,
106
+ "--ed-chart-tick-color",
107
+ i(e, "--ed-theme-color-content-default", "#333333")
108
+ ),
109
+ labelColor: i(
110
+ e,
111
+ "--ed-chart-label-color",
112
+ i(e, "--ed-theme-color-content-default", "#111111")
113
+ ),
114
+ tooltipBg: i(
115
+ e,
116
+ "--ed-chart-tooltip-bg",
117
+ i(e, "--ed-theme-color-background-default", "#ffffff")
118
+ ),
119
+ tooltipFg: i(
120
+ e,
121
+ "--ed-chart-tooltip-fg",
122
+ i(e, "--ed-theme-color-content-default", "#111111")
123
+ ),
124
+ tooltipBorder: i(
125
+ e,
126
+ "--ed-chart-tooltip-border",
127
+ i(e, "--ed-theme-color-border-subtle", "#dddddd")
128
+ ),
129
+ positive: i(e, "--ed-chart-positive", "#2e7d32"),
130
+ negative: i(e, "--ed-chart-negative", "#c62828"),
131
+ neutral: i(e, "--ed-chart-neutral", "#757575")
132
+ };
133
+ }
134
+ function P(e, t) {
135
+ t(p(e));
136
+ const r = new MutationObserver(() => t(p(e)));
137
+ return r.observe(document.documentElement, {
138
+ attributes: !0,
139
+ attributeFilter: ["class", "data-theme"]
140
+ }), () => r.disconnect();
141
+ }
142
+ function z() {
143
+ return typeof window > "u" || !window.matchMedia ? !1 : window.matchMedia("(prefers-reduced-motion: reduce)").matches;
144
+ }
145
+ const L = ":root,:host{--size-base-unit: .5rem}*,::slotted(*),*:before,*:after{box-sizing:border-box}h1,h2,h3,h4,h5,h6{margin:0}.ed-r-bar-chart{display:block;container-type:inline-size;color:var(--ed-theme-color-content-default)}.ed-r-bar-chart__canvas{position:relative;width:100%;height:20rem;margin-block-end:1.5rem}.ed-r-bar-chart__canvas-el{display:block}.ed-r-bar-chart__details{margin-block-end:1rem;border:var(--ed-theme-border-width-sm) solid var(--ed-theme-color-border-subtle);border-radius:var(--ed-theme-border-radius-sm);background:var(--ed-theme-color-background-subtle)}.ed-r-bar-chart__summary{padding:.5rem 1rem;cursor:pointer;font-weight:700;color:var(--ed-theme-color-content-default)}.ed-r-bar-chart__table{width:100%;border-collapse:collapse;color:var(--ed-theme-color-content-default)}.ed-r-bar-chart__caption{padding:.5rem 1rem;text-align:start;font-weight:700;caption-side:top}.ed-r-bar-chart__th,.ed-r-bar-chart__td{padding:.5rem 1rem;border-block-end:var(--ed-theme-border-width-sm) solid var(--ed-theme-color-border-subtle);text-align:start}.ed-r-bar-chart__th{background:var(--ed-theme-color-background-subtle);font-weight:700}";
146
+ var O = Object.defineProperty, n = (e, t, r, s) => {
147
+ for (var a = void 0, l = e.length - 1, o; l >= 0; l--)
148
+ (o = e[l]) && (a = o(t, r, a) || a);
149
+ return a && O(t, r, a), a;
150
+ };
151
+ m.register(y, k, w, $, x, E);
152
+ class c extends f {
153
+ constructor() {
154
+ super(...arguments), this.data = { labels: [], datasets: [] }, this.orientation = "vertical", this.variant = "grouped", this.chartLabel = "", this.dataTable = !0, this.yFormat = (t) => t.toLocaleString(), this.theme = null;
155
+ }
156
+ static get styles() {
157
+ return _(L);
158
+ }
159
+ disconnectedCallback() {
160
+ super.disconnectedCallback(), this.unobserve?.(), this.chart?.destroy(), this.chart = void 0;
161
+ }
162
+ firstUpdated() {
163
+ this.canvas && (this.unobserve = P(this, (t) => {
164
+ this.theme = t, this.chart ? (this.chart.options = this.toChartJsOptions(), this.chart.data = this.toChartJsData(), this.chart.update()) : this.createChart();
165
+ }));
166
+ }
167
+ updated(t) {
168
+ if (!this.chart || !this.theme) return;
169
+ t.has("orientation") || t.has("variant") ? (this.chart.destroy(), this.chart = void 0, this.createChart()) : (t.has("data") || t.has("yFormat")) && (this.chart.data = this.toChartJsData(), this.chart.options = this.toChartJsOptions(), this.chart.update());
170
+ }
171
+ createChart() {
172
+ !this.canvas || !this.theme || (this.chart = new m(this.canvas, {
173
+ type: "bar",
174
+ data: this.toChartJsData(),
175
+ options: this.toChartJsOptions()
176
+ }));
177
+ }
178
+ toChartJsData() {
179
+ const { palette: t, positive: r, negative: s, neutral: a } = this.theme, l = (o) => {
180
+ if (o === "positive") return r;
181
+ if (o === "negative") return s;
182
+ if (o === "neutral") return a;
183
+ };
184
+ return {
185
+ labels: this.data.labels,
186
+ datasets: this.data.datasets.map((o, b) => {
187
+ const u = l(o.semantic) ?? t[b % t.length];
188
+ return {
189
+ label: o.label,
190
+ data: o.values,
191
+ backgroundColor: u,
192
+ borderColor: u,
193
+ borderWidth: 1
194
+ };
195
+ })
196
+ };
197
+ }
198
+ toChartJsOptions() {
199
+ const t = this.theme, r = this.yFormat, s = this.variant === "stacked", a = this.orientation === "horizontal", l = (o) => r(Number(o));
200
+ return {
201
+ indexAxis: a ? "y" : "x",
202
+ responsive: !0,
203
+ maintainAspectRatio: !1,
204
+ animation: z() ? !1 : { duration: 400 },
205
+ plugins: {
206
+ legend: {
207
+ display: this.data.datasets.length > 1,
208
+ labels: { color: t.labelColor }
209
+ },
210
+ tooltip: {
211
+ backgroundColor: t.tooltipBg,
212
+ titleColor: t.tooltipFg,
213
+ bodyColor: t.tooltipFg,
214
+ borderColor: t.tooltipBorder,
215
+ borderWidth: 1,
216
+ callbacks: {
217
+ label: (o) => {
218
+ const b = a ? o.parsed.x : o.parsed.y;
219
+ return `${o.dataset.label}: ${r(Number(b))}`;
220
+ }
221
+ }
222
+ }
223
+ },
224
+ scales: {
225
+ x: {
226
+ stacked: s,
227
+ ticks: a ? { color: t.tickColor, callback: l } : { color: t.tickColor },
228
+ grid: { color: t.gridlineColor },
229
+ border: { color: t.axisColor }
230
+ },
231
+ y: {
232
+ stacked: s,
233
+ ticks: a ? { color: t.tickColor } : { color: t.tickColor, callback: l },
234
+ grid: { color: t.gridlineColor },
235
+ border: { color: t.axisColor }
236
+ }
237
+ }
238
+ };
239
+ }
240
+ render() {
241
+ const t = this.componentClassNames("ed-r-bar-chart", {
242
+ "ed-r-bar-chart--horizontal": this.orientation === "horizontal",
243
+ "ed-r-bar-chart--stacked": this.variant === "stacked"
244
+ });
245
+ return h`
246
+ <div class="${t}">
247
+ <div class="ed-r-bar-chart__canvas" role="img" aria-label=${this.chartLabel}>
248
+ <canvas class="ed-r-bar-chart__canvas-el"></canvas>
249
+ </div>
250
+ <slot></slot>
251
+ ${this.dataTable ? this.renderTable() : null}
252
+ </div>
253
+ `;
254
+ }
255
+ renderTable() {
256
+ const { labels: t, datasets: r } = this.data;
257
+ return h`
258
+ <details class="ed-r-bar-chart__details">
259
+ <summary class="ed-r-bar-chart__summary">View data table</summary>
260
+ <table class="ed-r-bar-chart__table">
261
+ <caption class="ed-r-bar-chart__caption">${this.chartLabel}</caption>
262
+ <thead class="ed-r-bar-chart__thead">
263
+ <tr class="ed-r-bar-chart__row">
264
+ <th scope="col" class="ed-r-bar-chart__th">Category</th>
265
+ ${r.map((s) => h`<th scope="col" class="ed-r-bar-chart__th">${s.label}</th>`)}
266
+ </tr>
267
+ </thead>
268
+ <tbody class="ed-r-bar-chart__tbody">
269
+ ${t.map((s, a) => h`
270
+ <tr class="ed-r-bar-chart__row">
271
+ <th scope="row" class="ed-r-bar-chart__th">${s}</th>
272
+ ${r.map((l) => h`<td class="ed-r-bar-chart__td">${this.yFormat(l.values[a])}</td>`)}
273
+ </tr>
274
+ `)}
275
+ </tbody>
276
+ </table>
277
+ </details>
278
+ `;
279
+ }
280
+ }
281
+ n([
282
+ d({ attribute: !1 })
283
+ ], c.prototype, "data");
284
+ n([
285
+ d({ reflect: !0 })
286
+ ], c.prototype, "orientation");
287
+ n([
288
+ d({ reflect: !0 })
289
+ ], c.prototype, "variant");
290
+ n([
291
+ d({ attribute: "chart-label" })
292
+ ], c.prototype, "chartLabel");
293
+ n([
294
+ d({ type: Boolean, attribute: "data-table" })
295
+ ], c.prototype, "dataTable");
296
+ n([
297
+ d({ attribute: !1 })
298
+ ], c.prototype, "yFormat");
299
+ n([
300
+ C("canvas")
301
+ ], c.prototype, "canvas");
302
+ customElements.get("ed-r-bar-chart") === void 0 && customElements.define("ed-r-bar-chart", c);
303
+ export {
304
+ c as EdRBarChart
305
+ };