@ai-matrx/records-ui 0.116.13 → 0.116.14

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/index.d.cts CHANGED
@@ -5360,6 +5360,13 @@ interface ChartBlockProps {
5360
5360
  * ("–"). Absent: the drawn groups added up.
5361
5361
  */
5362
5362
  centerTotal?: number | null | undefined;
5363
+ /**
5364
+ * Value labels on the marks (the chart menu's "Data labels" switch): bars and columns at their end, line points
5365
+ * above, donut slices inside the ring (or outside with a leader for a thin slice). Labels that would collide are
5366
+ * left out, never drawn over each other; the card never changes size. `true`: values on bars and lines, each
5367
+ * slice's share on a donut; "value" / "share" choose for a donut.
5368
+ */
5369
+ dataLabels?: boolean | "value" | "share" | undefined;
5363
5370
  /** `false` draws no legend beside a donut and no value list under bars or a line. Default `true`. */
5364
5371
  legend?: boolean | undefined;
5365
5372
  /**
@@ -5383,7 +5390,7 @@ interface ChartBlockProps {
5383
5390
  token: string;
5384
5391
  } | undefined;
5385
5392
  }
5386
- declare function ChartBlock({ block, subject, tableName, className, centerValue, centerTotal, legend, groupColors, variant, source }: ChartBlockProps): react.JSX.Element;
5393
+ declare function ChartBlock({ block, subject, tableName, className, centerValue, centerTotal, dataLabels, legend, groupColors, variant, source }: ChartBlockProps): react.JSX.Element;
5387
5394
 
5388
5395
  type ChartLayoutKind = "bar" | "line" | "donut";
5389
5396
  /**
package/dist/index.d.ts CHANGED
@@ -5360,6 +5360,13 @@ interface ChartBlockProps {
5360
5360
  * ("–"). Absent: the drawn groups added up.
5361
5361
  */
5362
5362
  centerTotal?: number | null | undefined;
5363
+ /**
5364
+ * Value labels on the marks (the chart menu's "Data labels" switch): bars and columns at their end, line points
5365
+ * above, donut slices inside the ring (or outside with a leader for a thin slice). Labels that would collide are
5366
+ * left out, never drawn over each other; the card never changes size. `true`: values on bars and lines, each
5367
+ * slice's share on a donut; "value" / "share" choose for a donut.
5368
+ */
5369
+ dataLabels?: boolean | "value" | "share" | undefined;
5363
5370
  /** `false` draws no legend beside a donut and no value list under bars or a line. Default `true`. */
5364
5371
  legend?: boolean | undefined;
5365
5372
  /**
@@ -5383,7 +5390,7 @@ interface ChartBlockProps {
5383
5390
  token: string;
5384
5391
  } | undefined;
5385
5392
  }
5386
- declare function ChartBlock({ block, subject, tableName, className, centerValue, centerTotal, legend, groupColors, variant, source }: ChartBlockProps): react.JSX.Element;
5393
+ declare function ChartBlock({ block, subject, tableName, className, centerValue, centerTotal, dataLabels, legend, groupColors, variant, source }: ChartBlockProps): react.JSX.Element;
5387
5394
 
5388
5395
  type ChartLayoutKind = "bar" | "line" | "donut";
5389
5396
  /**
package/dist/index.js CHANGED
@@ -22976,6 +22976,7 @@ import {
22976
22976
  BarChart,
22977
22977
  CartesianGrid,
22978
22978
  Cell,
22979
+ LabelList,
22979
22980
  Legend,
22980
22981
  Line as Line2,
22981
22982
  LineChart,
@@ -23479,7 +23480,7 @@ function colorOfGroup(groups, colors) {
23479
23480
  return colors[raw === null || raw === void 0 ? "" : String(raw)] ?? null;
23480
23481
  }
23481
23482
  var NO_GROUPS = { title: "", kind: "bar", group_by: [], rows: [] };
23482
- function ChartBlock({ block, subject, tableName: tableName2, className, centerValue, centerTotal, legend = true, groupColors, variant = "card", source }) {
23483
+ function ChartBlock({ block, subject, tableName: tableName2, className, centerValue, centerTotal, dataLabels, legend = true, groupColors, variant = "card", source }) {
23483
23484
  const bare = variant === "bare";
23484
23485
  const host = useRecordsUi();
23485
23486
  const kind2 = block.kind;
@@ -23592,7 +23593,8 @@ function ChartBlock({ block, subject, tableName: tableName2, className, centerVa
23592
23593
  config,
23593
23594
  onDrill: drill,
23594
23595
  goal,
23595
- ...centerValue ? { center: centerTotal !== void 0 ? centerTotal : points.reduce((sum, p) => sum + (p.values[primary] ?? 0), 0) } : {},
23596
+ ...centerValue ? { center: centerTotal !== void 0 ? centerTotal : centerOfGroups(points, primary, measures[0].op) } : {},
23597
+ ...dataLabels ? { dataLabels } : {},
23596
23598
  showLegend: legend
23597
23599
  }
23598
23600
  ),
@@ -23608,7 +23610,7 @@ function BigNumber({
23608
23610
  onDrill,
23609
23611
  block
23610
23612
  }) {
23611
- const summed = points.reduce((sum, p) => sum + (p.values[measure] ?? 0), 0);
23613
+ const summed = centerOfGroups(points, measure, String(block.measures?.[0]?.op ?? "count"));
23612
23614
  const total = block.totals && typeof block.totals.current === "number" ? block.totals.current : summed;
23613
23615
  const records = points.reduce((sum, p) => sum + p.n, 0);
23614
23616
  const windows = block.compare ?? null;
@@ -23616,7 +23618,7 @@ function BigNumber({
23616
23618
  const priorTotal = block.totals?.prior;
23617
23619
  const body = /* @__PURE__ */ jsxs72(Fragment34, { children: [
23618
23620
  /* @__PURE__ */ jsxs72("span", { className: "flex items-baseline gap-2", children: [
23619
- /* @__PURE__ */ jsx79("span", { className: "text-2xl font-semibold tabular-nums", children: pretty(total) }),
23621
+ /* @__PURE__ */ jsx79("span", { className: "text-2xl font-semibold tabular-nums", children: total === null ? "\u2013" : pretty(total) }),
23620
23622
  windows ? /* @__PURE__ */ jsx79(Trend, { pct: block.totals?.change_pct, aimingUp: !!target, className: "text-sm" }) : null
23621
23623
  ] }),
23622
23624
  /* @__PURE__ */ jsx79("span", { className: "text-xs text-muted-foreground", children: windows && typeof priorTotal === "number" ? `${windowName(windows.window, windows.period)} \xB7 ${pretty(priorTotal)} in ${windowName(windows.prior_window, windows.period)}` : `from ${formatCount4(records)} ${records === 1 ? "record" : "records"}` })
@@ -23809,8 +23811,10 @@ function Drawing({
23809
23811
  onDrill,
23810
23812
  goal,
23811
23813
  center,
23814
+ dataLabels,
23812
23815
  showLegend = true
23813
23816
  }) {
23817
+ const labelText = (v) => typeof v === "number" ? pretty(v).length > 7 ? compactTick(v) : pretty(v) : "";
23814
23818
  const compared = series.length > 1;
23815
23819
  const drawOrder = compared ? [series[1], series[0]] : series;
23816
23820
  const goalLine = (horizontal2) => goal && typeof goal.value === "number" ? /* @__PURE__ */ jsx79(
@@ -23850,8 +23854,9 @@ function Drawing({
23850
23854
  data,
23851
23855
  dataKey: series[0],
23852
23856
  nameKey: "label",
23853
- innerRadius: 44,
23854
- outerRadius: 66,
23857
+ innerRadius: dataLabels ? DONUT_LABELLED.inner : 44,
23858
+ outerRadius: dataLabels ? DONUT_LABELLED.outer : 66,
23859
+ ...dataLabels ? donutLabelProps(points.map((p) => p.values[series[0]] ?? 0), dataLabels === "value" ? (v) => labelText(v) : null) : {},
23855
23860
  paddingAngle: donutGap(points.length),
23856
23861
  isAnimationActive: false,
23857
23862
  ...click ? { onClick: click, className: "cursor-pointer" } : {},
@@ -23888,7 +23893,7 @@ function Drawing({
23888
23893
  ] });
23889
23894
  }
23890
23895
  if (kind2 === "line") {
23891
- return /* @__PURE__ */ jsx79(ChartFrame, { config, height: HEIGHT, children: (width) => /* @__PURE__ */ jsxs72(LineChart, { width, height: HEIGHT, data, margin: { top: 6, right: 6, bottom: 0, left: 0 }, children: [
23896
+ return /* @__PURE__ */ jsx79(ChartFrame, { config, height: HEIGHT, children: (width) => /* @__PURE__ */ jsxs72(LineChart, { width, height: HEIGHT, data, margin: { top: dataLabels ? 16 : 6, right: dataLabels ? 14 : 6, bottom: 0, left: 0 }, children: [
23892
23897
  /* @__PURE__ */ jsx79(CartesianGrid, { vertical: false }),
23893
23898
  /* @__PURE__ */ jsx79(XAxis, { dataKey: "label", ...axis }),
23894
23899
  /* @__PURE__ */ jsx79(YAxis, { width: 44, ...axis }),
@@ -23905,7 +23910,8 @@ function Drawing({
23905
23910
  ...key !== series[0] ? { strokeDasharray: "4 3" } : {},
23906
23911
  dot: false,
23907
23912
  isAnimationActive: false,
23908
- ...click ? { className: "cursor-pointer" } : {}
23913
+ ...click ? { className: "cursor-pointer" } : {},
23914
+ children: dataLabels && key === series[0] ? /* @__PURE__ */ jsx79(LabelList, { dataKey: key, content: /* @__PURE__ */ jsx79(SpacedLabel, { show: labelsThatFit(points.map((p) => labelText(p.values[key])), width - 50), text: labelText, placement: "above" }) }) : null
23909
23915
  },
23910
23916
  key
23911
23917
  ))
@@ -23919,7 +23925,7 @@ function Drawing({
23919
23925
  height: HEIGHT,
23920
23926
  data,
23921
23927
  layout: horizontal ? "vertical" : "horizontal",
23922
- margin: { top: 6, right: 6, bottom: 0, left: 0 },
23928
+ margin: { top: dataLabels && !horizontal ? 16 : 6, right: dataLabels && horizontal ? 40 : 6, bottom: 0, left: 0 },
23923
23929
  children: [
23924
23930
  /* @__PURE__ */ jsx79(CartesianGrid, { vertical: horizontal, horizontal: !horizontal }),
23925
23931
  horizontal ? /* @__PURE__ */ jsxs72(Fragment34, { children: [
@@ -23932,7 +23938,7 @@ function Drawing({
23932
23938
  tooltip,
23933
23939
  legend,
23934
23940
  goalLine(horizontal),
23935
- drawOrder.map((key, index) => /* @__PURE__ */ jsx79(
23941
+ drawOrder.map((key, index) => /* @__PURE__ */ jsxs72(
23936
23942
  Bar,
23937
23943
  {
23938
23944
  dataKey: key,
@@ -23940,7 +23946,23 @@ function Drawing({
23940
23946
  isAnimationActive: false,
23941
23947
  fill: `var(--color-${key})`,
23942
23948
  ...click ? { onClick: click, className: "cursor-pointer" } : {},
23943
- children: series.length === 1 ? points.map((point, at) => /* @__PURE__ */ jsx79(Cell, { fill: String(config[point.label]?.color ?? SERIES_COLORS[index]) }, `${at}:${point.label}`)) : null
23949
+ children: [
23950
+ series.length === 1 ? points.map((point, at) => /* @__PURE__ */ jsx79(Cell, { fill: String(config[point.label]?.color ?? SERIES_COLORS[index]) }, `${at}:${point.label}`)) : null,
23951
+ dataLabels && key === series[0] ? /* @__PURE__ */ jsx79(
23952
+ LabelList,
23953
+ {
23954
+ dataKey: key,
23955
+ content: /* @__PURE__ */ jsx79(
23956
+ SpacedLabel,
23957
+ {
23958
+ show: horizontal ? labelsThatFit(points.map(() => "0"), HEIGHT - 20, 11, true) : labelsThatFit(points.map((p) => labelText(p.values[key])), width - 50),
23959
+ text: labelText,
23960
+ placement: horizontal ? "end" : "above"
23961
+ }
23962
+ )
23963
+ }
23964
+ ) : null
23965
+ ]
23944
23966
  },
23945
23967
  key
23946
23968
  ))
@@ -23948,6 +23970,79 @@ function Drawing({
23948
23970
  }
23949
23971
  ) });
23950
23972
  }
23973
+ function centerOfGroups(points, key, op) {
23974
+ const vals = points.map((p) => p.values[key]).filter((v) => typeof v === "number" && Number.isFinite(v));
23975
+ if (op === "count" || op === "sum" || op === "filled" || op === "empty") return vals.reduce((a, b) => a + b, 0);
23976
+ if (!vals.length) return null;
23977
+ if (op === "min") return Math.min(...vals);
23978
+ if (op === "max") return Math.max(...vals);
23979
+ return null;
23980
+ }
23981
+ var LABEL_CHAR_PX = 6;
23982
+ function labelsThatFit(texts, span, fixedPx, fixed = false, gap = 4) {
23983
+ const n = texts.length;
23984
+ if (!n) return [];
23985
+ const band = Math.max(1, span / n);
23986
+ const widest = fixed ? fixedPx ?? 11 : Math.max(...texts.map((t) => t.length * LABEL_CHAR_PX)) + gap;
23987
+ const step2 = Math.max(1, Math.ceil(widest / band));
23988
+ return texts.map((_t, i) => i % step2 === 0);
23989
+ }
23990
+ function SpacedLabel(props) {
23991
+ const { show, text, placement, value, index = 0 } = props;
23992
+ const x = Number(props.x ?? 0);
23993
+ const y = Number(props.y ?? 0);
23994
+ const w = Number(props.width ?? 0);
23995
+ const h = Number(props.height ?? 0);
23996
+ if (!show[index]) return null;
23997
+ const words2 = text(value);
23998
+ if (!words2) return null;
23999
+ return placement === "end" ? /* @__PURE__ */ jsx79("text", { x: x + w + 4, y: y + h / 2, dominantBaseline: "central", fontSize: 10, className: "fill-muted-foreground tabular-nums", "data-records-chart-label": "", children: words2 }) : /* @__PURE__ */ jsx79("text", { x: x + w / 2, y: y - 5, textAnchor: "middle", fontSize: 10, className: "fill-muted-foreground tabular-nums", "data-records-chart-label": "", children: words2 });
24000
+ }
24001
+ var DONUT_LABELLED = { inner: 30, outer: 46 };
24002
+ function donutLabelPlan(values, outer = DONUT_LABELLED.outer) {
24003
+ const total = values.reduce((a, b) => a + (b > 0 ? b : 0), 0);
24004
+ if (total <= 0) return values.map(() => "none");
24005
+ let acc = 0;
24006
+ const lastY = { left: null, right: null };
24007
+ return values.map((v) => {
24008
+ const share = v > 0 ? v / total : 0;
24009
+ const mid = (acc + share / 2) * 2 * Math.PI;
24010
+ acc += share;
24011
+ if (share >= 0.09) return "inside";
24012
+ if (share < 0.03) return "none";
24013
+ const y = -Math.sin(mid) * (outer + 10);
24014
+ const side = Math.cos(mid) >= 0 ? "right" : "left";
24015
+ const prev = lastY[side];
24016
+ if (prev !== null && Math.abs(prev - y) < 11) return "none";
24017
+ lastY[side] = y;
24018
+ return "outside";
24019
+ });
24020
+ }
24021
+ function donutLabelProps(values, asValue) {
24022
+ const plan = donutLabelPlan(values);
24023
+ const total = values.reduce((a, b) => a + (b > 0 ? b : 0), 0);
24024
+ const words2 = (i) => asValue ? asValue(values[i] ?? 0) : `${Math.round((values[i] ?? 0) / (total || 1) * 100)}%`;
24025
+ return {
24026
+ labelLine: (p) => {
24027
+ const at = p.index ?? 0;
24028
+ const pts = p.points ?? [];
24029
+ if (plan[at] !== "outside" || pts.length < 2) return /* @__PURE__ */ jsx79("g", {});
24030
+ return /* @__PURE__ */ jsx79("polyline", { points: pts.map((q) => `${q.x},${q.y}`).join(" "), fill: "none", stroke: "currentColor", strokeOpacity: 0.4, strokeWidth: 1, "data-records-chart-leader": "" });
24031
+ },
24032
+ label: (p) => {
24033
+ const at = p.index ?? 0;
24034
+ const where = plan[at];
24035
+ if (!where || where === "none") return /* @__PURE__ */ jsx79("g", {});
24036
+ const rad = Math.PI / 180;
24037
+ const mid = p.midAngle ?? 0;
24038
+ if (where === "inside") {
24039
+ const r = ((p.innerRadius ?? 0) + (p.outerRadius ?? 0)) / 2;
24040
+ return /* @__PURE__ */ jsx79("text", { x: (p.cx ?? 0) + r * Math.cos(-mid * rad), y: (p.cy ?? 0) + r * Math.sin(-mid * rad), textAnchor: "middle", dominantBaseline: "central", fontSize: 9, className: "fill-background font-medium tabular-nums", "data-records-chart-label": "", children: words2(at) });
24041
+ }
24042
+ return /* @__PURE__ */ jsx79("text", { x: p.x ?? 0, y: p.y ?? 0, textAnchor: Math.cos(-mid * rad) >= 0 ? "start" : "end", dominantBaseline: "central", fontSize: 9, className: "fill-muted-foreground tabular-nums", "data-records-chart-label": "", children: words2(at) });
24043
+ }
24044
+ };
24045
+ }
23951
24046
  function onlyWholeNumbers(rows) {
23952
24047
  return rows.every(
23953
24048
  (row) => Object.entries(row).every(([key, value]) => key === "label" || typeof value !== "number" || Number.isInteger(value))
@@ -23991,21 +24086,25 @@ function ChartLayout({ view, fields, filter, onChange, remembered }) {
23991
24086
  let cancelled = false;
23992
24087
  const asked2 = JSON.parse(filterKey);
23993
24088
  const question = JSON.parse(qKey);
23994
- void client.recordAggregate({
24089
+ const whole = question.kind === "donut" ? client.recordAggregate({ table_id: view.subject, groupBy: [], measures: [question.measure], ...Object.keys(asked2).length > 0 ? { filter: asked2 } : {} }) : Promise.resolve(null);
24090
+ void Promise.all([client.recordAggregate({
23995
24091
  table_id: view.subject,
23996
24092
  groupBy: question.groupBy,
23997
24093
  ...question.bucket ? { bucket: question.bucket } : {},
23998
24094
  measures: [question.measure],
23999
24095
  limit: 200,
24000
24096
  ...Object.keys(asked2).length > 0 ? { filter: asked2 } : {}
24001
- }).then((answered) => {
24097
+ }), whole]).then(([answered, overall]) => {
24002
24098
  if (cancelled) return;
24003
24099
  if (!answered.ok) {
24004
24100
  setState({ block: null, error: answered.error });
24005
24101
  return;
24006
24102
  }
24103
+ const wholeRow = overall && overall.ok ? (overall.data ?? [])[0] : void 0;
24104
+ const wholeValue = wholeRow ? wholeRow.measures[measureKey4(question.measure)] : void 0;
24007
24105
  setState({
24008
24106
  error: null,
24107
+ ...question.kind === "donut" ? { total: typeof wholeValue === "number" ? wholeValue : null } : {},
24009
24108
  block: {
24010
24109
  title: view.name,
24011
24110
  kind: question.kind,
@@ -24052,7 +24151,7 @@ function ChartLayout({ view, fields, filter, onChange, remembered }) {
24052
24151
  MEASURE_LABEL2[isChartMeasure(view.chartMeasure) ? view.chartMeasure : "count"],
24053
24152
  " needs a number field \u2014 pick one above. Counting until then."
24054
24153
  ] }) : null,
24055
- state.error ? /* @__PURE__ */ jsx80(RefusalNotice, { error: state.error }) : state.block ? /* @__PURE__ */ jsx80(ChartBlock, { block: state.block, subject: view.subject, variant: "bare", centerValue: true }) : null
24154
+ state.error ? /* @__PURE__ */ jsx80(RefusalNotice, { error: state.error }) : state.block ? /* @__PURE__ */ jsx80(ChartBlock, { block: state.block, subject: view.subject, variant: "bare", centerValue: true, ...state.total !== void 0 ? { centerTotal: state.total } : {} }) : null
24056
24155
  ] });
24057
24156
  }
24058
24157
 
@@ -40761,7 +40860,8 @@ function blockOfAnswer(answer, base, by) {
40761
40860
  for (const [k, v] of Object.entries(r.measures ?? {})) measuresOut[k] = typeof v === "number" ? v : v === null ? null : Number(v);
40762
40861
  return { groups, measures: measuresOut, row_count: Number(r.row_count) };
40763
40862
  });
40764
- return { ...base, rows };
40863
+ const totalValue = answer.data.total?.measures ? Object.values(answer.data.total.measures)[0] : void 0;
40864
+ return { ...base, rows, center_total: typeof totalValue === "number" ? totalValue : totalValue === null || totalValue === void 0 ? null : Number(totalValue) };
40765
40865
  }
40766
40866
  function EntityChartBlock({ source, kind: kind2, by, measure, title, where, organization, centerValue, variant, className }) {
40767
40867
  const client = useRecordsClient91();
@@ -40795,7 +40895,7 @@ function EntityChartBlock({ source, kind: kind2, by, measure, title, where, orga
40795
40895
  block,
40796
40896
  subject: source.token,
40797
40897
  source,
40798
- ...centerValue ? { centerValue } : {},
40898
+ ...centerValue ? { centerValue, ...block.center_total !== void 0 ? { centerTotal: block.center_total } : {} } : {},
40799
40899
  ...variant ? { variant } : {},
40800
40900
  ...className ? { className } : {}
40801
40901
  }