@funnest/charts-react 0.6.20 → 0.6.22

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.js CHANGED
@@ -1129,7 +1129,75 @@ var mix = (a, b, t) => {
1129
1129
  return `#${c.map((v) => v.toString(16).padStart(2, "0")).join("")}`;
1130
1130
  };
1131
1131
  var defaultChange = (c) => `${c > 0 ? "+" : ""}${c.toFixed(2)}%`;
1132
- var textWidth2 = (t, size) => [...t].reduce((w, ch) => w + (ch.charCodeAt(0) > 11904 ? size : size * 0.6), 0);
1132
+ var estimate = (t, size) => [...t].reduce((w, ch) => w + (ch.charCodeAt(0) > 11904 ? size : size * 0.62), 0);
1133
+ var canvasCtx;
1134
+ function measurer(family) {
1135
+ if (canvasCtx === void 0) {
1136
+ try {
1137
+ canvasCtx = typeof document !== "undefined" ? document.createElement("canvas").getContext("2d") : null;
1138
+ } catch {
1139
+ canvasCtx = null;
1140
+ }
1141
+ }
1142
+ const ctx = canvasCtx;
1143
+ if (!ctx || !family) return (t, size) => estimate(t, size);
1144
+ const cache = /* @__PURE__ */ new Map();
1145
+ return (t, size, weight) => {
1146
+ const key = `${weight}|${t}`;
1147
+ let w1 = cache.get(key);
1148
+ if (w1 === void 0) {
1149
+ ctx.font = `${weight} 100px ${family}`;
1150
+ w1 = ctx.measureText(t).width / 100;
1151
+ cache.set(key, w1);
1152
+ }
1153
+ return w1 * size;
1154
+ };
1155
+ }
1156
+ var MIN_FONT = 7;
1157
+ var isHangul = (ch) => /[\u3131-\u318e\uac00-\ud7a3]/.test(ch);
1158
+ function splits(label) {
1159
+ const chars = [...label];
1160
+ const out = [];
1161
+ for (let i = 1; i < chars.length; i++) {
1162
+ const a = chars[i - 1];
1163
+ const b = chars[i];
1164
+ if (b === " ") out.push([chars.slice(0, i).join(""), chars.slice(i + 1).join("")]);
1165
+ else if (a !== " " && (isHangul(a) || isHangul(b))) out.push([chars.slice(0, i).join(""), chars.slice(i).join("")]);
1166
+ }
1167
+ return out.filter(([x, y]) => x.trim() && y.trim());
1168
+ }
1169
+ function fitLabel(label, sub, width, height, measure) {
1170
+ const w = width - 8;
1171
+ const h = height - 6;
1172
+ if (w < MIN_FONT * 1.5 || h < MIN_FONT * 1.1) return null;
1173
+ const base = Math.min(22, Math.sqrt(width * height) / 6);
1174
+ const sizeFor = (lines) => {
1175
+ const widest = Math.max(...lines.map((l) => measure(l, 1, 700)));
1176
+ return Math.min(base, w / Math.max(widest, 0.01), h / (lines.length * 1.2));
1177
+ };
1178
+ let best = null;
1179
+ const one = sizeFor([label]);
1180
+ if (one >= Math.min(base, 9)) best = { lines: [label], size: one };
1181
+ else {
1182
+ const candidates = [[label], ...splits(label)].map((lines) => ({ lines, size: sizeFor(lines) }));
1183
+ for (const c of candidates) if (c.size >= MIN_FONT && (!best || c.size > best.size + 0.5)) best = c;
1184
+ }
1185
+ if (!best) {
1186
+ const size = Math.max(MIN_FONT, Math.min(base, h / 1.2));
1187
+ const chars = [...label];
1188
+ let n = chars.length - 1;
1189
+ while (n >= 2 && measure(`${chars.slice(0, n).join("")}\u2026`, size, 700) > w) n--;
1190
+ if (n < 2 || size > h / 1.1) return null;
1191
+ return { lines: [`${chars.slice(0, n).join("")}\u2026`], size };
1192
+ }
1193
+ const out = { lines: best.lines, size: best.size };
1194
+ if (sub) {
1195
+ const size = Math.min(best.size * 0.78, w / Math.max(measure(sub, 1, 600), 0.01));
1196
+ const used = best.lines.length * best.size * 1.2;
1197
+ if (size >= MIN_FONT && used + size * 1.3 <= h) out.sub = { text: sub, size };
1198
+ }
1199
+ return out;
1200
+ }
1133
1201
  function TreemapChart({
1134
1202
  data,
1135
1203
  width: fixedWidth,
@@ -1154,6 +1222,12 @@ function TreemapChart({
1154
1222
  const [measured, setMeasured] = useState6(fixedWidth ?? 0);
1155
1223
  const [active, setActive] = useState6(null);
1156
1224
  const [focused, setFocused] = useState6(false);
1225
+ const [family, setFamily] = useState6(null);
1226
+ useEffect5(() => {
1227
+ const el = wrapRef.current;
1228
+ if (el) setFamily(getComputedStyle(el).fontFamily || null);
1229
+ }, []);
1230
+ const measure = useMemo3(() => measurer(family), [family]);
1157
1231
  useEffect5(() => {
1158
1232
  if (fixedWidth !== void 0) return setMeasured(fixedWidth);
1159
1233
  const el = wrapRef.current;
@@ -1199,7 +1273,7 @@ function TreemapChart({
1199
1273
  if (enableTooltip && hit && hitItem) {
1200
1274
  const rows = [[hitItem.label, valueFormatter(hitItem.value)]];
1201
1275
  if (hitItem.change !== void 0) rows.push(["", changeFormatter(hitItem.change)]);
1202
- const tw = Math.max(...rows.map(([a, b]) => textWidth2(a, 12) + textWidth2(b, 12) + 28)) + 8;
1276
+ const tw = Math.max(...rows.map(([a, b]) => measure(a, 12, 400) + measure(b, 12, 700) + 28)) + 8;
1203
1277
  const th = rows.length * 18 + 14;
1204
1278
  const cx = hit.x + hit.width / 2;
1205
1279
  let tx = cx - tw / 2;
@@ -1228,11 +1302,10 @@ function TreemapChart({
1228
1302
  rects.map((r) => {
1229
1303
  const { d, i } = byId.get(r.id);
1230
1304
  const on = active === r.id;
1231
- const fs = Math.max(9, Math.min(22, Math.sqrt(r.width * r.height) / 6, (r.width - 10) / Math.max(1, textWidth2(d.label, 1))));
1232
- const sub = d.change !== void 0 ? changeFormatter(d.change) : d.sublabel;
1233
- const fitsLabel = r.width >= textWidth2(d.label, fs) + 10 && r.height >= fs + 8;
1234
- const fitsSub = !!sub && fitsLabel && r.height >= fs * 2 + 14 && r.width >= textWidth2(sub, fs * 0.8) + 10;
1235
- const cy = r.y + r.height / 2;
1305
+ const fit = fitLabel(d.label, d.change !== void 0 ? changeFormatter(d.change) : d.sublabel, r.width, r.height, measure);
1306
+ const lineH = fit ? fit.size * 1.2 : 0;
1307
+ const block = fit ? fit.lines.length * lineH + (fit.sub ? fit.sub.size * 1.3 : 0) : 0;
1308
+ const top = r.y + r.height / 2 - block / 2;
1236
1309
  return /* @__PURE__ */ jsxs7("g", { onPointerEnter: () => setActive(r.id), onClick: onItemClick ? () => onItemClick(d) : void 0, style: { cursor: onItemClick ? "pointer" : "default" }, children: [
1237
1310
  /* @__PURE__ */ jsx10(
1238
1311
  "rect",
@@ -1259,34 +1332,21 @@ function TreemapChart({
1259
1332
  style: { stroke: "#fff" }
1260
1333
  }
1261
1334
  ),
1262
- fitsLabel && /* @__PURE__ */ jsx10(
1263
- "text",
1264
- {
1265
- x: r.x + r.width / 2,
1266
- y: fitsSub ? cy - fs * 0.45 : cy,
1267
- textAnchor: "middle",
1268
- dominantBaseline: "central",
1269
- fontSize: fs,
1270
- fontWeight: 700,
1271
- pointerEvents: "none",
1272
- style: { fill: "#fff" },
1273
- children: d.label
1274
- }
1275
- ),
1276
- fitsSub && /* @__PURE__ */ jsx10(
1277
- "text",
1278
- {
1279
- x: r.x + r.width / 2,
1280
- y: cy + fs * 0.65,
1281
- textAnchor: "middle",
1282
- dominantBaseline: "central",
1283
- fontSize: fs * 0.78,
1284
- fontWeight: 600,
1285
- pointerEvents: "none",
1286
- style: { fill: "rgba(255,255,255,0.88)", fontVariantNumeric: "tabular-nums" },
1287
- children: sub
1288
- }
1289
- )
1335
+ fit && /* @__PURE__ */ jsxs7("text", { textAnchor: "middle", pointerEvents: "none", fontWeight: 700, fontSize: fit.size, style: { fill: "#fff" }, children: [
1336
+ fit.lines.map((line, k) => /* @__PURE__ */ jsx10("tspan", { x: r.x + r.width / 2, y: top + lineH * (k + 0.5), dominantBaseline: "central", children: line }, k)),
1337
+ fit.sub && /* @__PURE__ */ jsx10(
1338
+ "tspan",
1339
+ {
1340
+ x: r.x + r.width / 2,
1341
+ y: top + fit.lines.length * lineH + fit.sub.size * 0.65,
1342
+ dominantBaseline: "central",
1343
+ fontSize: fit.sub.size,
1344
+ fontWeight: 600,
1345
+ style: { fill: "rgba(255,255,255,0.88)", fontVariantNumeric: "tabular-nums" },
1346
+ children: fit.sub.text
1347
+ }
1348
+ )
1349
+ ] })
1290
1350
  ] }, r.id);
1291
1351
  }),
1292
1352
  tip && /* @__PURE__ */ jsxs7("g", { transform: `translate(${tip.x},${tip.y})`, pointerEvents: "none", children: [
@@ -1489,7 +1549,7 @@ import { jsx as jsx12, jsxs as jsxs9 } from "react/jsx-runtime";
1489
1549
  var TIP_FONT = 12;
1490
1550
  var ROW = 18;
1491
1551
  var TIP_PAD = 7;
1492
- var textWidth3 = (t) => [...t].reduce((w, ch) => w + (ch.charCodeAt(0) > 11904 ? TIP_FONT : TIP_FONT * 0.6), 0);
1552
+ var textWidth2 = (t) => [...t].reduce((w, ch) => w + (ch.charCodeAt(0) > 11904 ? TIP_FONT : TIP_FONT * 0.6), 0);
1493
1553
  function Scrubber({
1494
1554
  label,
1495
1555
  seriesIds,
@@ -1542,7 +1602,7 @@ function Scrubber({
1542
1602
  const text2 = lo !== null && lo !== void 0 && lo !== v ? `${valueFormatter(lo, s.id)} \u2013 ${valueFormatter(v, s.id)}` : valueFormatter(v, s.id);
1543
1603
  return [{ s, text: text2 }];
1544
1604
  }) : [];
1545
- const tipW = Math.max(0, ...rows.map((r) => textWidth3(r.text) + (rows.length > 1 || r.s.label ? textWidth3(r.s.label ?? "") + 26 : 18))) + 16;
1605
+ const tipW = Math.max(0, ...rows.map((r) => textWidth2(r.text) + (rows.length > 1 || r.s.label ? textWidth2(r.s.label ?? "") + 26 : 18))) + 16;
1546
1606
  const tipH = rows.length * ROW + TIP_PAD * 2;
1547
1607
  const tipRight = x + 12 + tipW <= plot.right;
1548
1608
  const tipX = tipRight ? x + 12 : x - 12 - tipW;
@@ -1556,7 +1616,7 @@ function Scrubber({
1556
1616
  const v = s.data[i];
1557
1617
  const color = colorOf(s, v);
1558
1618
  const text2 = beaconLabels ? typeof beaconLabels === "function" ? beaconLabels(v, s.id) : valueFormatter(v, s.id) : "";
1559
- const right = x + 12 + textWidth3(text2) <= plot.right;
1619
+ const right = x + 12 + textWidth2(text2) <= plot.right;
1560
1620
  return /* @__PURE__ */ jsxs9("g", { children: [
1561
1621
  /* @__PURE__ */ jsx12("circle", { cx: x, cy: y, r: 7, fillOpacity: 0.18, style: { fill: color } }),
1562
1622
  /* @__PURE__ */ jsx12("circle", { cx: x, cy: y, r: 4, strokeWidth: 2, style: { fill: color, stroke: VAR.bg } }),
@@ -41,8 +41,26 @@ export interface TreemapChartProps {
41
41
  style?: CSSProperties;
42
42
  'aria-label'?: string;
43
43
  }
44
+ type Measure = (text: string, size: number, weight: number) => number;
45
+ export interface TileLabel {
46
+ lines: string[];
47
+ size: number;
48
+ /** The second line (change or sublabel) and its size, when it fits too. */
49
+ sub?: {
50
+ text: string;
51
+ size: number;
52
+ };
53
+ }
54
+ /**
55
+ * How a tile's label fits: one line as big as the tile allows (up to 22px); if that's under 7px,
56
+ * the label on two lines (a narrow, tall tile); still not, cut short with "…". The change goes
57
+ * under it when there's room. Null when not even two letters fit.
58
+ */
59
+ export declare function fitLabel(label: string, sub: string | undefined, width: number, height: number, measure: Measure): TileLabel | null;
44
60
  /**
45
61
  * Tiles sized by value and colored by change: market maps, sector heatmaps, holdings by weight.
46
- * Labels shrink with the tile and hide when they don't fit.
62
+ * Labels shrink with the tile, wrap onto two lines in narrow ones and are cut short with "…" in
63
+ * small ones; only tiles too small for two letters go without.
47
64
  */
48
65
  export declare function TreemapChart({ data, width: fixedWidth, height, gap, radius, upColor, downColor, flatColor, maxChange, getColor, changeFormatter, valueFormatter, enableTooltip, onItemClick, palette, className, style, 'aria-label': ariaLabel, }: TreemapChartProps): import("react").JSX.Element;
66
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funnest/charts-react",
3
- "version": "0.6.20",
3
+ "version": "0.6.22",
4
4
  "description": "React bindings for Funnest Charts: the trading widget plus SVG line, area and bar charts",
5
5
  "keywords": [
6
6
  "react",
@@ -46,9 +46,9 @@
46
46
  "NOTICE"
47
47
  ],
48
48
  "dependencies": {
49
- "@funnest/charts": "0.6.20",
50
- "@funnest/charts-ui": "0.6.20",
51
- "@funnest/charts-viz": "0.6.20"
49
+ "@funnest/charts-ui": "0.6.22",
50
+ "@funnest/charts": "0.6.22",
51
+ "@funnest/charts-viz": "0.6.22"
52
52
  },
53
53
  "peerDependencies": {
54
54
  "react": ">=18",