@caqophony/ui 0.9.2 → 0.9.3

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
@@ -40,8 +40,13 @@ interface LineChartProps extends BaseChartProps {
40
40
  /** Date format for labels */
41
41
  dateFormat?: Intl.DateTimeFormatOptions;
42
42
  }
43
+ interface ChartValueUnit {
44
+ kind: 'percent';
45
+ scale: 1 | 100;
46
+ }
43
47
  interface BarChartProps extends BaseChartProps {
44
48
  data: ChartDataPoint[];
49
+ unit?: ChartValueUnit;
45
50
  /** Label for the dataset */
46
51
  label?: string;
47
52
  /** Bar color */
@@ -60,7 +65,7 @@ type NumberFormatMode = 'compact' | 'full' | 'currency';
60
65
 
61
66
  declare function LineChart({ data, label, secondaryLabel, color, secondaryColor, fill, showLegend, height, emptyMessage, dateFormat, className, }: LineChartProps): react_jsx_runtime.JSX.Element;
62
67
 
63
- declare function BarChart({ data, label, color, horizontal, showLegend, height, emptyMessage, className, }: BarChartProps): react_jsx_runtime.JSX.Element;
68
+ declare function BarChart({ data, label, color, horizontal, showLegend, height, emptyMessage, className, unit, }: BarChartProps): react_jsx_runtime.JSX.Element;
64
69
 
65
70
  declare function DoughnutChart({ data, colors, showPercentage, showLegend, height, emptyMessage, className, }: DoughnutChartProps): react_jsx_runtime.JSX.Element;
66
71
 
@@ -92,6 +97,10 @@ declare function ChartSkeleton({ height, className }: ChartSkeletonProps): react
92
97
  * Format a number for display in charts (compact: 1K, 1M, etc.)
93
98
  */
94
99
  declare function formatNumber(value: number, mode?: NumberFormatMode): string;
100
+ /**
101
+ * Format a chart value in its unit: a plain number compact, a percent as `64.6%`
102
+ */
103
+ declare function formatValue(value: number, unit?: ChartValueUnit): string;
95
104
  /**
96
105
  * Format a date for chart labels
97
106
  */
@@ -114,4 +123,4 @@ declare const CHART_COLORS: {
114
123
  readonly gray: "rgb(107, 114, 128)";
115
124
  };
116
125
 
117
- export { BarChart, type BarChartProps, type BaseChartProps, CHART_COLORS, type ChartDataPoint, ChartSkeleton, DoughnutChart, type DoughnutChartProps, LineChart, type LineChartProps, type NumberFormatMode, type TimeSeriesDataPoint, formatDateLabel, formatNumber, generateColors };
126
+ export { BarChart, type BarChartProps, type BaseChartProps, CHART_COLORS, type ChartDataPoint, ChartSkeleton, type ChartValueUnit, DoughnutChart, type DoughnutChartProps, LineChart, type LineChartProps, type NumberFormatMode, type TimeSeriesDataPoint, formatDateLabel, formatNumber, formatValue, generateColors };
package/dist/charts.js CHANGED
@@ -25,6 +25,14 @@ function formatNumber(value, mode = "compact") {
25
25
  }
26
26
  return value.toString();
27
27
  }
28
+ var PERCENT_FRACTION_DIGITS = 1;
29
+ function formatValue(value, unit) {
30
+ if (unit === void 0) return formatNumber(value);
31
+ return new Intl.NumberFormat("en-US", {
32
+ style: "percent",
33
+ maximumFractionDigits: PERCENT_FRACTION_DIGITS
34
+ }).format(unit.scale === 100 ? value / 100 : value);
35
+ }
28
36
  function formatDateLabel(date, options = { month: "short", day: "numeric" }) {
29
37
  const d = typeof date === "string" ? new Date(date) : date;
30
38
  return d.toLocaleDateString("en-US", options);
@@ -205,8 +213,9 @@ function paletteOf(el) {
205
213
  grid: read(BAR_TOKENS.grid)
206
214
  };
207
215
  }
208
- var valueTick = (tickValue) => formatNumber(
209
- typeof tickValue === "number" ? tickValue : parseFloat(String(tickValue))
216
+ var valueTick = (unit) => (tickValue) => formatValue(
217
+ typeof tickValue === "number" ? tickValue : parseFloat(String(tickValue)),
218
+ unit
210
219
  );
211
220
  function BarChart({
212
221
  data,
@@ -216,7 +225,8 @@ function BarChart({
216
225
  showLegend = false,
217
226
  height = 256,
218
227
  emptyMessage = "No data available",
219
- className
228
+ className,
229
+ unit
220
230
  }) {
221
231
  const box = useRef(null);
222
232
  const [palette, setPalette] = useState(null);
@@ -239,10 +249,12 @@ function BarChart({
239
249
  ]
240
250
  };
241
251
  }, [data, label, color, palette]);
252
+ const scale = unit?.scale;
242
253
  const options = useMemo2(() => {
254
+ const valueUnit = scale === void 0 ? void 0 : { kind: "percent", scale };
243
255
  const valueAxis = {
244
256
  beginAtZero: true,
245
- ticks: { callback: valueTick, color: palette?.text },
257
+ ticks: { callback: valueTick(valueUnit), color: palette?.text },
246
258
  grid: { color: palette?.grid },
247
259
  border: { display: false }
248
260
  };
@@ -265,14 +277,14 @@ function BarChart({
265
277
  callbacks: {
266
278
  label: (context) => {
267
279
  const val = horizontal ? context.parsed.x : context.parsed.y;
268
- return val === null ? `${context.dataset.label}: no value` : `${context.dataset.label}: ${formatNumber(val)}`;
280
+ return val === null ? `${context.dataset.label}: no value` : `${context.dataset.label}: ${formatValue(val, valueUnit)}`;
269
281
  }
270
282
  }
271
283
  }
272
284
  },
273
285
  scales: horizontal ? { x: valueAxis, y: categoryAxis } : { x: categoryAxis, y: valueAxis }
274
286
  };
275
- }, [horizontal, showLegend, palette]);
287
+ }, [horizontal, showLegend, palette, scale]);
276
288
  if (data.length === 0) {
277
289
  return /* @__PURE__ */ jsx2(
278
290
  "div",
@@ -386,5 +398,6 @@ export {
386
398
  LineChart,
387
399
  formatDateLabel,
388
400
  formatNumber,
401
+ formatValue,
389
402
  generateColors
390
403
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caqophony/ui",
3
- "version": "0.9.2",
3
+ "version": "0.9.3",
4
4
  "description": "Generic, optional React UI reuse library (Gossamer tokens). Centrally maintained by Caqophony.",
5
5
  "license": "MIT",
6
6
  "author": "Caqophony",