@caqophony/ui 0.6.3 → 0.6.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/dist/charts.d.mts CHANGED
@@ -8,7 +8,7 @@ export { Chart as ChartJS } from 'chart.js';
8
8
  */
9
9
  interface ChartDataPoint {
10
10
  label: string;
11
- value: number;
11
+ value: number | null;
12
12
  }
13
13
  interface TimeSeriesDataPoint {
14
14
  date: string | Date;
package/dist/charts.js CHANGED
@@ -181,38 +181,81 @@ function LineChart({
181
181
  }
182
182
 
183
183
  // src/charts/bar-chart.tsx
184
- import { useMemo as useMemo2 } from "react";
184
+ import { useEffect, useMemo as useMemo2, useRef, useState } from "react";
185
185
  import { Bar } from "react-chartjs-2";
186
186
  import { jsx as jsx2 } from "react/jsx-runtime";
187
+ var BAR_TOKENS = {
188
+ bar: "--primary",
189
+ text: "--muted-foreground",
190
+ grid: "--border"
191
+ };
192
+ function paletteOf(el) {
193
+ const style = getComputedStyle(el);
194
+ const read = (name) => {
195
+ const value = style.getPropertyValue(name).trim();
196
+ if (value === "")
197
+ throw new Error(
198
+ `BarChart: the token ${name} is not set on the page: the chart draws in the kit's tokens, so the page loads @caqophony/ui/styles.css`
199
+ );
200
+ return `hsl(${value})`;
201
+ };
202
+ return {
203
+ bar: read(BAR_TOKENS.bar),
204
+ text: read(BAR_TOKENS.text),
205
+ grid: read(BAR_TOKENS.grid)
206
+ };
207
+ }
208
+ var valueTick = (tickValue) => formatNumber(
209
+ typeof tickValue === "number" ? tickValue : parseFloat(String(tickValue))
210
+ );
187
211
  function BarChart({
188
212
  data,
189
213
  label = "Value",
190
- color = CHART_COLORS.primary,
214
+ color,
191
215
  horizontal = false,
192
216
  showLegend = false,
193
217
  height = 256,
194
218
  emptyMessage = "No data available",
195
219
  className
196
220
  }) {
221
+ const box = useRef(null);
222
+ const [palette, setPalette] = useState(null);
223
+ const empty = data.length === 0;
224
+ useEffect(() => {
225
+ if (!empty && box.current !== null) setPalette(paletteOf(box.current));
226
+ }, [empty]);
197
227
  const chartData = useMemo2(() => {
198
- const labels = data.map((d) => d.label);
199
- const colors = data.length > 1 ? generateColors(data.length) : [color];
228
+ const fill = color ?? palette?.bar;
200
229
  return {
201
- labels,
230
+ labels: data.map((d) => d.label),
202
231
  datasets: [
203
232
  {
204
233
  label,
205
234
  data: data.map((d) => d.value),
206
- backgroundColor: colors,
207
- borderColor: colors.map((c) => c.replace("0.8", "1")),
208
- borderWidth: 1
235
+ backgroundColor: fill,
236
+ borderWidth: 0,
237
+ borderRadius: 4
209
238
  }
210
239
  ]
211
240
  };
212
- }, [data, label, color]);
213
- const options = useMemo2(
214
- () => ({
241
+ }, [data, label, color, palette]);
242
+ const options = useMemo2(() => {
243
+ const valueAxis = {
244
+ beginAtZero: true,
245
+ ticks: { callback: valueTick, color: palette?.text },
246
+ grid: { color: palette?.grid },
247
+ border: { display: false }
248
+ };
249
+ const categoryAxis = {
250
+ ticks: { color: palette?.text },
251
+ grid: { display: false },
252
+ border: { color: palette?.grid }
253
+ };
254
+ return {
215
255
  ...BASE_CHART_OPTIONS,
256
+ // #6326: the bars are drawn at their values at once: a reader (or a
257
+ // capture of the page) never sees a bar part-way to its value
258
+ animation: false,
216
259
  indexAxis: horizontal ? "y" : "x",
217
260
  plugins: {
218
261
  legend: {
@@ -221,48 +264,26 @@ function BarChart({
221
264
  tooltip: {
222
265
  callbacks: {
223
266
  label: (context) => {
224
- const val = horizontal ? context.parsed.x ?? 0 : context.parsed.y ?? 0;
225
- return `${context.dataset.label}: ${formatNumber(val)}`;
267
+ const val = horizontal ? context.parsed.x : context.parsed.y;
268
+ return val === null ? `${context.dataset.label}: no value` : `${context.dataset.label}: ${formatNumber(val)}`;
226
269
  }
227
270
  }
228
271
  }
229
272
  },
230
- scales: {
231
- x: {
232
- beginAtZero: true,
233
- ticks: horizontal ? {
234
- callback: (tickValue) => {
235
- if (tickValue === null) return "";
236
- const value = typeof tickValue === "number" ? tickValue : parseFloat(String(tickValue));
237
- return formatNumber(value);
238
- }
239
- } : void 0
240
- },
241
- y: {
242
- beginAtZero: true,
243
- ticks: !horizontal ? {
244
- callback: (tickValue) => {
245
- if (tickValue === null) return "";
246
- const value = typeof tickValue === "number" ? tickValue : parseFloat(String(tickValue));
247
- return formatNumber(value);
248
- }
249
- } : void 0
250
- }
251
- }
252
- }),
253
- [horizontal, showLegend]
254
- );
273
+ scales: horizontal ? { x: valueAxis, y: categoryAxis } : { x: categoryAxis, y: valueAxis }
274
+ };
275
+ }, [horizontal, showLegend, palette]);
255
276
  if (data.length === 0) {
256
277
  return /* @__PURE__ */ jsx2(
257
278
  "div",
258
279
  {
259
- className: `flex items-center justify-center text-gray-500 ${className ?? ""}`,
280
+ className: `flex items-center justify-center text-muted-foreground ${className ?? ""}`,
260
281
  style: { height },
261
282
  children: emptyMessage
262
283
  }
263
284
  );
264
285
  }
265
- return /* @__PURE__ */ jsx2("div", { className, style: { height }, children: /* @__PURE__ */ jsx2(Bar, { data: chartData, options }) });
286
+ return /* @__PURE__ */ jsx2("div", { ref: box, className, style: { height }, children: palette !== null ? /* @__PURE__ */ jsx2(Bar, { data: chartData, options }) : null });
266
287
  }
267
288
 
268
289
  // src/charts/doughnut-chart.tsx
@@ -279,7 +300,8 @@ function DoughnutChart({
279
300
  className
280
301
  }) {
281
302
  const total = useMemo3(
282
- () => data.reduce((sum, d) => sum + d.value, 0),
303
+ // #6326: the total of the values the data holds; a null point is not 0
304
+ () => data.reduce((sum, d) => d.value === null ? sum : sum + d.value, 0),
283
305
  [data]
284
306
  );
285
307
  const chartData = useMemo3(() => {
@@ -309,6 +331,7 @@ function DoughnutChart({
309
331
  callbacks: {
310
332
  label: (context) => {
311
333
  const val = context.parsed;
334
+ if (val === null) return `${context.label}: no value`;
312
335
  const percentage = total > 0 ? Math.round(val / total * 100) : 0;
313
336
  if (showPercentage) {
314
337
  return `${context.label}: ${formatNumber(val)} (${percentage}%)`;
@@ -325,7 +348,7 @@ function DoughnutChart({
325
348
  return /* @__PURE__ */ jsx3(
326
349
  "div",
327
350
  {
328
- className: `flex items-center justify-center text-gray-500 ${className ?? ""}`,
351
+ className: `flex items-center justify-center text-muted-foreground ${className ?? ""}`,
329
352
  style: { height },
330
353
  children: emptyMessage
331
354
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caqophony/ui",
3
- "version": "0.6.3",
3
+ "version": "0.6.4",
4
4
  "description": "Generic, optional React UI reuse library (Gossamer tokens). Centrally maintained by Caqophony.",
5
5
  "license": "MIT",
6
6
  "author": "Caqophony",