@rtemis/draw 0.2.0 → 0.3.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.
Files changed (38) hide show
  1. package/README.md +7 -2
  2. package/dist/charts/bar/config.d.ts +15 -10
  3. package/dist/charts/bar/config.d.ts.map +1 -1
  4. package/dist/charts/bar/config.js +47 -26
  5. package/dist/charts/bar/config.js.map +1 -1
  6. package/dist/charts/line/config.d.ts +104 -0
  7. package/dist/charts/line/config.d.ts.map +1 -0
  8. package/dist/charts/line/config.js +372 -0
  9. package/dist/charts/line/config.js.map +1 -0
  10. package/dist/charts/line/index.d.ts +3 -0
  11. package/dist/charts/line/index.d.ts.map +1 -0
  12. package/dist/charts/line/index.js +2 -0
  13. package/dist/charts/line/index.js.map +1 -0
  14. package/dist/index.d.ts +18 -1
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +14 -0
  17. package/dist/index.js.map +1 -1
  18. package/dist/rtemis-draw-builders.cjs +472 -22
  19. package/dist/shared/format.d.ts +14 -0
  20. package/dist/shared/format.d.ts.map +1 -0
  21. package/dist/shared/format.js +27 -0
  22. package/dist/shared/format.js.map +1 -0
  23. package/dist/shared/labels.d.ts +21 -0
  24. package/dist/shared/labels.d.ts.map +1 -0
  25. package/dist/shared/labels.js +31 -0
  26. package/dist/shared/labels.js.map +1 -0
  27. package/dist/shared/limits.d.ts +32 -0
  28. package/dist/shared/limits.d.ts.map +1 -0
  29. package/dist/shared/limits.js +62 -0
  30. package/dist/shared/limits.js.map +1 -0
  31. package/dist/shared/source.d.ts +25 -0
  32. package/dist/shared/source.d.ts.map +1 -1
  33. package/dist/shared/source.js +39 -0
  34. package/dist/shared/source.js.map +1 -1
  35. package/dist/theme/tokens.d.ts.map +1 -1
  36. package/dist/theme/tokens.js +3 -1
  37. package/dist/theme/tokens.js.map +1 -1
  38. package/package.json +1 -1
@@ -25,18 +25,30 @@ var RtemisDraw = (() => {
25
25
  BAR_CHART_CONFIG_MEMBERS: () => BAR_CHART_CONFIG_MEMBERS,
26
26
  DEFAULT_FONT_FAMILY: () => DEFAULT_FONT_FAMILY,
27
27
  DEFAULT_MARGIN: () => DEFAULT_MARGIN,
28
+ DEFAULT_PAD: () => DEFAULT_PAD,
29
+ LINE_CHART_CONFIG_MEMBERS: () => LINE_CHART_CONFIG_MEMBERS,
28
30
  RTEMIS_PALETTE: () => RTEMIS_PALETTE,
31
+ SIGNIFICANT_DIGITS: () => SIGNIFICANT_DIGITS,
29
32
  SIGN_COLORS: () => SIGN_COLORS,
30
33
  ZOOM_SLIDER_ID: () => ZOOM_SLIDER_ID,
31
34
  a3SurfaceHints: () => a3SurfaceHints,
35
+ aspectHints: () => aspectHints,
32
36
  buildA3Option: () => buildA3Option,
33
37
  buildBarOption: () => buildBarOption,
38
+ buildLineOption: () => buildLineOption,
34
39
  builders: () => builders,
40
+ calcLimits: () => calcLimits,
41
+ calcLimitsFromZero: () => calcLimitsFromZero,
35
42
  darkThemeTokens: () => darkThemeTokens,
36
43
  fontStack: () => fontStack,
44
+ formatNumber: () => formatNumber,
37
45
  lightThemeTokens: () => lightThemeTokens,
46
+ lineAspect: () => lineAspect,
38
47
  themeFontSizes: () => themeFontSizes,
39
- themeOption: () => themeOption
48
+ themeOption: () => themeOption,
49
+ timeLabel: () => timeLabel,
50
+ timeMs: () => timeMs,
51
+ valueLabel: () => valueLabel
40
52
  });
41
53
 
42
54
  // src/charts/a3/layout.ts
@@ -942,6 +954,19 @@ var RtemisDraw = (() => {
942
954
  return value ? { [key]: value } : {};
943
955
  }
944
956
 
957
+ // src/shared/format.ts
958
+ var SIGNIFICANT_DIGITS = 3;
959
+ function formatNumber(value, digits = SIGNIFICANT_DIGITS) {
960
+ if (typeof value !== "number" || !Number.isFinite(value)) return value;
961
+ if (Number.isInteger(value)) return String(value);
962
+ const magnitude = Math.abs(value);
963
+ if (magnitude < 1e-3 || magnitude >= 1e15) return value.toExponential(digits - 1);
964
+ if (Math.floor(Math.log10(magnitude)) + 1 >= digits) return value.toFixed(0);
965
+ const rounded = Number(value.toPrecision(digits));
966
+ const decimals = Math.max(0, digits - 1 - Math.floor(Math.log10(Math.abs(rounded))));
967
+ return rounded.toFixed(decimals);
968
+ }
969
+
945
970
  // src/shared/layout.ts
946
971
  var DEFAULT_MARGIN = 36;
947
972
  var SIDES = ["top", "right", "bottom", "left"];
@@ -982,6 +1007,40 @@ var RtemisDraw = (() => {
982
1007
  }
983
1008
 
984
1009
  // src/shared/source.ts
1010
+ function isTime(data, name) {
1011
+ return data.types?.[name] === "time";
1012
+ }
1013
+ var ISO_TIME = /^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2})(?:\.(\d+))?)?)?/;
1014
+ function timeMs(value) {
1015
+ if (typeof value === "number") return Number.isFinite(value) ? value : null;
1016
+ if (typeof value !== "string") return null;
1017
+ const m = ISO_TIME.exec(value);
1018
+ if (!m) return null;
1019
+ const [, y, mo, d, h, mi, s, frac] = m;
1020
+ const ms = frac ? Math.round(Number(`0.${frac}`) * 1e3) : 0;
1021
+ return Date.UTC(
1022
+ Number(y),
1023
+ Number(mo) - 1,
1024
+ Number(d),
1025
+ Number(h ?? 0),
1026
+ Number(mi ?? 0),
1027
+ Number(s ?? 0),
1028
+ ms
1029
+ );
1030
+ }
1031
+ function timeLabel(ms) {
1032
+ const iso = new Date(ms).toISOString();
1033
+ if (ms % 864e5 === 0) return iso.slice(0, 10);
1034
+ return `${iso.slice(0, 10)} ${iso.slice(11, ms % 1e3 === 0 ? 19 : 23)}`;
1035
+ }
1036
+ function textColumn(data, name, member) {
1037
+ const values = column(data.columns, name, member);
1038
+ if (!isTime(data, name)) return values.map((v) => String(v));
1039
+ return values.map((v) => {
1040
+ const ms = timeMs(v);
1041
+ return ms === null ? String(v) : timeLabel(ms);
1042
+ });
1043
+ }
985
1044
  function distinct(values, levels) {
986
1045
  const seen = new Set(values);
987
1046
  const ordered = (levels ?? []).filter((level) => seen.has(level));
@@ -1031,35 +1090,45 @@ var RtemisDraw = (() => {
1031
1090
  function buildBarOption(data, config, appearance = {}) {
1032
1091
  const source = data.columns;
1033
1092
  const yColumns = asArray(config.y);
1034
- if (!config.x || yColumns.length === 0) {
1035
- throw new Error("A bar chart needs both `x` and `y` set to draw.");
1093
+ if (!config.x) {
1094
+ throw new Error("A bar chart needs `x` set to draw.");
1036
1095
  }
1037
- const xs = column(source, config.x, "x").map((v) => String(v));
1096
+ const xs = textColumn(data, config.x, "x");
1038
1097
  const aggregate = config.aggregate ?? "none";
1098
+ const counting = yColumns.length === 0;
1099
+ if (counting && aggregate !== "none") {
1100
+ throw new Error(
1101
+ "`aggregate` combines the values of `y`, and `y` is unset: each bar counts the rows of its category."
1102
+ );
1103
+ }
1104
+ const frequency = data.frequency ? column(source, data.frequency, "frequency") : null;
1105
+ const valuesOf = (y) => y === null ? xs.map((_, row) => frequency ? numberOrNull(frequency[row]) : 1) : column(source, y, "y").map(numberOrNull);
1106
+ const measures = counting ? [null] : [...yColumns];
1107
+ const groupValues = config.group ? textColumn(data, config.group, "group") : null;
1108
+ if (!counting) requireOneRowPer(xs, groupValues ?? void 0);
1039
1109
  let categories;
1040
1110
  let series;
1041
- if (config.group) {
1042
- const groupValues = column(source, config.group, "group").map((v) => String(v));
1043
- requireOneRowPer(xs, groupValues);
1111
+ if (groupValues || counting) {
1044
1112
  categories = distinct(xs, data.levels?.[config.x]);
1045
- const groups = distinct(groupValues, data.levels?.[config.group]);
1113
+ const groups = groupValues ? distinct(groupValues, data.levels?.[config.group]) : [null];
1046
1114
  const at = new Map(categories.map((c, i) => [c, i]));
1047
1115
  series = groups.flatMap(
1048
- (g) => yColumns.map((y) => {
1049
- const values = column(source, y, "y");
1050
- const data2 = categories.map(() => null);
1116
+ (g) => measures.map((y) => {
1117
+ const values = valuesOf(y);
1118
+ const cells = categories.map(() => null);
1051
1119
  xs.forEach((x, row) => {
1052
1120
  const i = at.get(x);
1053
- if (groupValues[row] === g && i !== void 0) data2[i] = numberOrNull(values[row]);
1121
+ if (i === void 0 || groupValues && groupValues[row] !== g) return;
1122
+ const value2 = values[row] ?? null;
1123
+ cells[i] = counting ? (cells[i] ?? 0) + (value2 ?? 0) : value2;
1054
1124
  });
1055
- const name = groups.length > 1 ? yColumns.length > 1 ? `${y} / ${g}` : g : y;
1056
- return { name, data: data2 };
1125
+ const name = g !== null && groups.length > 1 ? measures.length > 1 ? `${y} / ${g}` : g : y ?? "Count";
1126
+ return { name, data: cells };
1057
1127
  })
1058
1128
  );
1059
1129
  } else {
1060
- if (aggregate !== "none") requireOneRowPer(xs);
1061
1130
  categories = xs;
1062
- series = yColumns.map((y) => ({ name: y, data: column(source, y, "y").map(numberOrNull) }));
1131
+ series = measures.map((y) => ({ name: y, data: valuesOf(y) }));
1063
1132
  }
1064
1133
  const permutation = order(categories, series, config.order ?? "input");
1065
1134
  categories = permutation.map((i) => categories[i]);
@@ -1088,11 +1157,10 @@ var RtemisDraw = (() => {
1088
1157
  type: "category",
1089
1158
  data: categories,
1090
1159
  axisPointer: { type: "none" },
1091
- axisTick: { show: false }
1160
+ ...horizontal ? { inverse: true } : {}
1092
1161
  };
1093
1162
  const value = {
1094
1163
  type: "value",
1095
- axisTick: { show: false },
1096
1164
  ...config.ylim ? { min: config.ylim[0], max: config.ylim[1] } : {}
1097
1165
  };
1098
1166
  const xAxis = horizontal ? value : category;
@@ -1103,7 +1171,7 @@ var RtemisDraw = (() => {
1103
1171
  const dataZoom = config.zoom ? axisZoom(horizontal ? "y" : "x", grid) : void 0;
1104
1172
  return {
1105
1173
  ...config.title ? { title: { text: config.title } } : {},
1106
- tooltip: { trigger: "axis" },
1174
+ tooltip: { trigger: "axis", valueFormatter: formatNumber },
1107
1175
  ...series.length > 1 ? { legend: { data: series.map((s) => s.name) } } : {},
1108
1176
  xAxis,
1109
1177
  yAxis,
@@ -1127,11 +1195,387 @@ var RtemisDraw = (() => {
1127
1195
  if (seen.has(key)) {
1128
1196
  const where = groups ? `category '${x}' in group '${groups[row]}'` : `category '${x}'`;
1129
1197
  throw new Error(
1130
- `Several rows hold ${where}. Combine them before building, as the \`aggregate\` member says (sum, mean, median, or count).`
1198
+ `Several rows hold ${where}. Combine them before building, as the \`aggregate\` member says (sum, mean, or median).`
1199
+ );
1200
+ }
1201
+ seen.add(key);
1202
+ });
1203
+ }
1204
+
1205
+ // src/shared/limits.ts
1206
+ var DEFAULT_PAD = 0.04;
1207
+ function calcLimits(values, pad = DEFAULT_PAD) {
1208
+ let min = Number.POSITIVE_INFINITY;
1209
+ let max = Number.NEGATIVE_INFINITY;
1210
+ for (const v of values) {
1211
+ if (typeof v !== "number" || !Number.isFinite(v)) continue;
1212
+ if (v < min) min = v;
1213
+ if (v > max) max = v;
1214
+ }
1215
+ if (min > max) return null;
1216
+ const span = max - min === 0 ? Math.abs(min) * 0.1 : max - min;
1217
+ return [min - pad * span, max + pad * span];
1218
+ }
1219
+ function calcLimitsFromZero(values, pad = DEFAULT_PAD) {
1220
+ const range2 = calcLimits(values, 0);
1221
+ if (!range2) return null;
1222
+ const min = Math.min(range2[0], 0);
1223
+ const max = Math.max(range2[1], 0);
1224
+ const span = max - min;
1225
+ return [min === 0 ? 0 : min - pad * span, max === 0 ? 0 : max + pad * span];
1226
+ }
1227
+ function equalAxisLimits(x, y, xlim, ylim, pad, square, equalAxes) {
1228
+ if (!square || !equalAxes) return { xlim, ylim };
1229
+ if (xlim && ylim) {
1230
+ const same = xlim.every(
1231
+ (v, i) => Math.abs(v - ylim[i]) <= 15e-9 * Math.max(1, Math.abs(v))
1232
+ );
1233
+ if (!same) {
1234
+ throw new Error(
1235
+ `\`square\` and \`equal_axes\` together require both axes to span the same interval, but \`xlim\` is [${xlim.join(", ")}] and \`ylim\` is [${ylim.join(", ")}]. Give them the same limits, or unset one of the two.`
1236
+ );
1237
+ }
1238
+ return { xlim, ylim };
1239
+ }
1240
+ const common = xlim ?? ylim ?? calcLimits([...x ?? [], ...y], pad);
1241
+ return common ? { xlim: common, ylim: common } : { xlim, ylim };
1242
+ }
1243
+
1244
+ // src/charts/line/config.ts
1245
+ var LINE_CHART_CONFIG_MEMBERS = Object.freeze([
1246
+ "type",
1247
+ "x",
1248
+ "y",
1249
+ "group",
1250
+ "aggregate",
1251
+ "blocks",
1252
+ "smooth",
1253
+ "area",
1254
+ "points",
1255
+ "stack",
1256
+ "zoom",
1257
+ "palette",
1258
+ "line_style",
1259
+ "block_color",
1260
+ "block_opacity",
1261
+ "square",
1262
+ "equal_axes",
1263
+ "pad",
1264
+ "xlim",
1265
+ "ylim",
1266
+ "xlab",
1267
+ "ylab",
1268
+ "title",
1269
+ "margin_top",
1270
+ "margin_right",
1271
+ "margin_bottom",
1272
+ "margin_left",
1273
+ "legend_position",
1274
+ "legend_placement",
1275
+ "legend_title"
1276
+ ]);
1277
+ function buildLineOption(data, config, appearance = {}) {
1278
+ const yColumns = asArray(config.y);
1279
+ if (!config.x || yColumns.length === 0) {
1280
+ throw new Error("A line chart needs both `x` and `y` set to draw.");
1281
+ }
1282
+ const xType = xAxisType(data, config.x);
1283
+ if (config.group && config.blocks) {
1284
+ throw new Error(
1285
+ "`blocks` shades runs along `x`, and with `group` each x repeats once per group: bind one of the two."
1286
+ );
1287
+ }
1288
+ if (config.equal_axes && xType !== "value") {
1289
+ throw new Error(
1290
+ `\`equal_axes\` compares the two axes' scales, which needs a numeric x. This chart's x is ${xType === "time" ? "a date or time" : "categorical"}.`
1291
+ );
1292
+ }
1293
+ if (config.xlim && xType === "category") {
1294
+ throw new Error("`xlim` only applies when `x` is numeric or a date.");
1295
+ }
1296
+ if (config.stack && config.equal_axes) {
1297
+ throw new Error(
1298
+ "A stacked y axis shows sums, so `equal_axes` cannot apply: unset one of the two."
1299
+ );
1300
+ }
1301
+ if (config.stack && !config.area) {
1302
+ throw new Error(
1303
+ "`stack` draws each line on the sum of those below it, which only filled bands show: set `area` too."
1304
+ );
1305
+ }
1306
+ const rawX = column(data.columns, config.x, "x");
1307
+ const xNum = xType === "category" ? null : rawX.map(xType === "time" ? timeMs : numberOrNull);
1308
+ const xText = xType === "category" ? textColumn(data, config.x, "x") : null;
1309
+ const categories = xText ? distinct(xText, data.levels?.[config.x]) : null;
1310
+ const xKey = (row) => (xNum ? xNum[row] : xText?.[row]) ?? null;
1311
+ const series = lineSeries(data, config, yColumns, xKey);
1312
+ for (const s of series) requireOneRowPerX(s, xKey, xText !== null);
1313
+ if (config.stack) stackOnKnownSums(series, xKey);
1314
+ const pad = config.pad ?? DEFAULT_PAD;
1315
+ const yValues = series.flatMap((s) => s.y);
1316
+ const limits = equalAxisLimits(
1317
+ xNum,
1318
+ yValues,
1319
+ config.xlim ?? null,
1320
+ config.ylim ?? null,
1321
+ pad,
1322
+ config.square ?? false,
1323
+ config.equal_axes ?? false
1324
+ );
1325
+ const xLim = xNum ? limits.xlim ?? calcLimits(xNum, pad) : null;
1326
+ const yDerive = config.area ? calcLimitsFromZero : calcLimits;
1327
+ const yLim = limits.ylim ?? yDerive(config.stack ? stackedValues(series, xKey) : yValues, pad);
1328
+ const palette = asArray(config.palette);
1329
+ const dashes = asArray(config.line_style);
1330
+ const at = categories ? new Map(categories.map((c, i) => [c, i])) : null;
1331
+ const inOrder = xText !== null && categories !== null && !config.group && xText.length === categories.length && xText.every((x, i) => x === categories[i]);
1332
+ const builtSeries = series.map((s, i) => {
1333
+ const points = s.rows.map((row, j) => {
1334
+ const y = s.y[j] ?? null;
1335
+ if (inOrder) return y;
1336
+ if (at && xText) return [at.get(xText[row]) ?? null, y];
1337
+ return [xNum?.[row] ?? null, y];
1338
+ });
1339
+ const isolated = config.points ? [] : isolatedPoints(s.y);
1340
+ const marked = isolated.length > 0;
1341
+ const values = marked ? points.map((value, j) => isolated.includes(j) ? value : { value, symbol: "none" }) : points;
1342
+ const color = palette.length > 0 ? palette[i % palette.length] : void 0;
1343
+ const dash = dashes.length > 0 ? dashes[i % dashes.length] : void 0;
1344
+ return {
1345
+ type: "line",
1346
+ ...series.length === 1 ? {} : { name: s.name },
1347
+ data: values,
1348
+ ...config.smooth ? { smooth: true } : {},
1349
+ ...config.points || marked ? {} : { showSymbol: false },
1350
+ ...config.stack ? { stack: "total" } : {},
1351
+ ...config.area ? { areaStyle: {} } : {},
1352
+ ...color === void 0 ? {} : { itemStyle: { color } },
1353
+ ...dash === void 0 ? {} : { lineStyle: { type: dash } }
1354
+ };
1355
+ });
1356
+ if (config.blocks) {
1357
+ const markArea = blockMarkArea(data, config, config.blocks, xNum, xText);
1358
+ if (markArea && builtSeries[0]) builtSeries[0].markArea = markArea;
1359
+ }
1360
+ const xAxis = {
1361
+ type: xType,
1362
+ ...config.xlab ? { name: config.xlab, nameLocation: "middle" } : {},
1363
+ ...categories ? { data: categories } : {},
1364
+ ...xType === "value" ? { scale: true } : {},
1365
+ ...xLim ? { min: xLim[0], max: xLim[1] } : {},
1366
+ ...xType === "category" ? {} : { splitLine: NO_CORNER_LINES },
1367
+ ...xType === "time" ? { axisLabel: { showMinLabel: false, showMaxLabel: false } } : {},
1368
+ ...yLim ? { axisLine: zeroAxisLine(yLim) } : {}
1369
+ };
1370
+ const yAxis = {
1371
+ type: "value",
1372
+ ...config.ylab ? { name: config.ylab, nameLocation: "middle" } : {},
1373
+ scale: true,
1374
+ ...yLim ? { min: yLim[0], max: yLim[1] } : {},
1375
+ splitLine: NO_CORNER_LINES,
1376
+ ...xLim ? { axisLine: zeroAxisLine(xLim) } : {}
1377
+ };
1378
+ const grid = cartesianGrid(config, Boolean(config.xlab || config.ylab));
1379
+ const dataZoom = config.zoom ? axisZoom("x", grid) : void 0;
1380
+ return {
1381
+ ...config.title ? { title: { text: config.title } } : {},
1382
+ tooltip: { trigger: "axis", valueFormatter: formatNumber },
1383
+ ...series.length > 1 ? { legend: { data: series.map((s) => s.name) } } : {},
1384
+ xAxis,
1385
+ yAxis,
1386
+ grid,
1387
+ ...dataZoom ? { dataZoom } : {},
1388
+ ...xType === "time" ? { useUTC: true } : {},
1389
+ series: builtSeries,
1390
+ ...appearance.animation === void 0 ? {} : { animation: appearance.animation }
1391
+ };
1392
+ }
1393
+ var NO_CORNER_LINES = Object.freeze({ showMinLine: false, showMaxLine: false });
1394
+ function zeroAxisLine(otherLimits) {
1395
+ return { show: otherLimits[0] <= 0 && otherLimits[1] >= 0, onZero: true };
1396
+ }
1397
+ function xAxisType(data, name) {
1398
+ if (isTime(data, name)) return "time";
1399
+ const values = column(data.columns, name, "x");
1400
+ if (!Array.isArray(data.columns[name])) return "value";
1401
+ const present2 = values.filter((v) => v !== null && v !== void 0);
1402
+ return present2.length > 0 && present2.every((v) => typeof v === "number") ? "value" : "category";
1403
+ }
1404
+ function lineSeries(data, config, yColumns, xKey) {
1405
+ const ys = new Map(yColumns.map((y) => [y, column(data.columns, y, "y").map(numberOrNull)]));
1406
+ const allRows = (ys.get(yColumns[0]) ?? []).map((_, i) => i).filter((row) => xKey(row) !== null);
1407
+ const select = (y, rows) => {
1408
+ const values = ys.get(y) ?? [];
1409
+ return rows.map((row) => values[row] ?? null);
1410
+ };
1411
+ if (!config.group) {
1412
+ return yColumns.map((y) => ({ name: y, rows: allRows, y: select(y, allRows) }));
1413
+ }
1414
+ const raw = column(data.columns, config.group, "group");
1415
+ const labels = textColumn(data, config.group, "group");
1416
+ const missing = (v) => v === null || v === void 0 || Number.isNaN(v);
1417
+ const present2 = labels.filter((_, row) => !missing(raw[row]));
1418
+ const groups = distinct(present2, data.levels?.[config.group]);
1419
+ const multiY = yColumns.length > 1;
1420
+ return groups.flatMap((g) => {
1421
+ const rows = allRows.filter((row) => !missing(raw[row]) && labels[row] === g);
1422
+ return yColumns.map((y) => ({
1423
+ name: groups.length > 1 ? multiY ? `${y} / ${g}` : g : y,
1424
+ rows,
1425
+ y: select(y, rows)
1426
+ }));
1427
+ });
1428
+ }
1429
+ function requireOneRowPerX(line, xKey, category) {
1430
+ const seen = /* @__PURE__ */ new Set();
1431
+ for (const row of line.rows) {
1432
+ const key = xKey(row);
1433
+ if (seen.has(key)) {
1434
+ const shown = category ? `'${String(key)}'` : String(key);
1435
+ throw new Error(
1436
+ `Several rows hold x = ${shown} in line '${line.name}'. Combine them before building, as the \`aggregate\` member says (sum, mean, or median).`
1131
1437
  );
1132
1438
  }
1133
1439
  seen.add(key);
1440
+ }
1441
+ }
1442
+ function stackOnKnownSums(series, xKey) {
1443
+ const known = [];
1444
+ series.forEach((s, i) => {
1445
+ const below = known.slice(0, i);
1446
+ const y = s.y.map(
1447
+ (value, j) => value !== null && below.every((keys) => keys.has(xKey(s.rows[j]))) ? value : null
1448
+ );
1449
+ series[i] = { ...s, y };
1450
+ known.push(new Set(s.rows.filter((_, j) => y[j] !== null).map((row) => xKey(row))));
1451
+ });
1452
+ }
1453
+ function stackedValues(series, xKey) {
1454
+ const tops = { positive: /* @__PURE__ */ new Map(), negative: /* @__PURE__ */ new Map() };
1455
+ const out = [];
1456
+ for (const s of series) {
1457
+ s.rows.forEach((row, j) => {
1458
+ const y = s.y[j];
1459
+ if (y === null || y === void 0) return;
1460
+ const key = xKey(row);
1461
+ const side = y >= 0 ? "positive" : "negative";
1462
+ const value = (tops[side].get(key) ?? 0) + y;
1463
+ tops[side].set(key, value);
1464
+ out.push(value);
1465
+ });
1466
+ }
1467
+ return out;
1468
+ }
1469
+ function isolatedPoints(y) {
1470
+ const present2 = (j) => y[j] !== null && y[j] !== void 0;
1471
+ return y.flatMap((_, j) => present2(j) && !present2(j - 1) && !present2(j + 1) ? [j] : []);
1472
+ }
1473
+ function blockMarkArea(data, config, blocks, xNum, xText) {
1474
+ const colors = asArray(config.block_color);
1475
+ if (colors.length === 0) {
1476
+ throw new Error("`block_color` must be set when `blocks` is.");
1477
+ }
1478
+ const raw = column(data.columns, blocks, "blocks");
1479
+ const missing = (v) => v === null || v === void 0 || Number.isNaN(v);
1480
+ const keys = raw.map((v) => missing(v) ? null : String(v));
1481
+ const present2 = new Set(keys.filter((k) => k !== null));
1482
+ const declared = data.levels?.[blocks];
1483
+ const levels = declared ? declared.filter((level) => present2.has(level)) : sortedLevels(raw.filter((v) => !missing(v)));
1484
+ if (colors.length !== levels.length) {
1485
+ throw new Error(
1486
+ `\`block_color\` must have length ${levels.length} (one per level of \`blocks\`); it has ${colors.length}.`
1487
+ );
1488
+ }
1489
+ const colorOf = new Map(levels.map((level, i) => [level, colors[i] ?? null]));
1490
+ const drawn = (key) => {
1491
+ if (key === null) return false;
1492
+ const color = colorOf.get(key);
1493
+ return typeof color === "string" && color !== "transparent";
1494
+ };
1495
+ const position = (row) => (xNum ? xNum[row] : xText?.[row]) ?? null;
1496
+ const runs = [];
1497
+ keys.forEach((key, row) => {
1498
+ const last = runs[runs.length - 1];
1499
+ if (last && last.key === key) last.end = row;
1500
+ else runs.push({ key, start: row, end: row });
1134
1501
  });
1502
+ const pairs = runs.flatMap((run, i) => {
1503
+ if (!drawn(run.key)) return [];
1504
+ const next = runs[i + 1];
1505
+ const end = xNum && next && drawn(next.key) ? next.start : run.end;
1506
+ return [
1507
+ [
1508
+ {
1509
+ xAxis: position(run.start),
1510
+ name: run.key,
1511
+ itemStyle: {
1512
+ color: colorOf.get(run.key),
1513
+ opacity: config.block_opacity ?? 0.2
1514
+ }
1515
+ },
1516
+ { xAxis: position(end) }
1517
+ ]
1518
+ ];
1519
+ });
1520
+ return pairs.length > 0 ? { data: pairs, silent: true } : null;
1521
+ }
1522
+ function sortedLevels(values) {
1523
+ const unique = [...new Set(values)];
1524
+ if (unique.every((v) => typeof v === "number")) {
1525
+ return unique.sort((a, b) => a - b).map(String);
1526
+ }
1527
+ return unique.map(String).sort((a, b) => a.localeCompare(b));
1528
+ }
1529
+ function lineAspect(option, config) {
1530
+ const square = config.square ?? false;
1531
+ const equal = config.equal_axes ?? false;
1532
+ if (!square && !equal) return null;
1533
+ const request = square ? "square" : "equal_axes";
1534
+ const span = (axis, which) => {
1535
+ const a = axis ?? {};
1536
+ if (typeof a.min !== "number" || typeof a.max !== "number") {
1537
+ throw new Error(
1538
+ `\`${request}\` needs a numeric ${which} axis with known limits, and this chart's is ${a.type === "category" ? "categorical" : "unbounded"}. Plot numeric values, or set \`${which}lim\`.`
1539
+ );
1540
+ }
1541
+ return [a.min, a.max];
1542
+ };
1543
+ const x = span(option.xAxis, "x");
1544
+ const y = span(option.yAxis, "y");
1545
+ const grid = option.grid ?? {};
1546
+ const px = (side) => {
1547
+ const value = grid[side];
1548
+ if (typeof value !== "number" || !Number.isFinite(value)) {
1549
+ throw new Error(
1550
+ `\`square\` and \`equal_axes\` need all four margins as pixel numbers, and \`${side}\` is ${value === void 0 ? "unset" : `'${String(value)}'`}. Set the four \`margin_*\` members.`
1551
+ );
1552
+ }
1553
+ return value;
1554
+ };
1555
+ return {
1556
+ ratio: equal ? (y[1] - y[0]) / (x[1] - x[0]) : 1,
1557
+ leftPx: px("left"),
1558
+ rightPx: px("right"),
1559
+ topPx: px("top"),
1560
+ botPx: px("bottom")
1561
+ };
1562
+ }
1563
+
1564
+ // src/shared/labels.ts
1565
+ function valueLabel(config, name = (column2) => column2) {
1566
+ const ys = asArray(config.y);
1567
+ if (ys.length === 0) return "Count";
1568
+ const only = ys.length === 1 ? name(ys[0]) : null;
1569
+ switch (config.aggregate ?? "none") {
1570
+ case "mean":
1571
+ return only === null ? "Mean" : `Mean ${only}`;
1572
+ case "median":
1573
+ return only === null ? "Median" : `Median ${only}`;
1574
+ case "sum":
1575
+ return only === null ? "Sum" : `Sum of ${only}`;
1576
+ default:
1577
+ return only;
1578
+ }
1135
1579
  }
1136
1580
 
1137
1581
  // src/theme/tokens.ts
@@ -1252,9 +1696,11 @@ var RtemisDraw = (() => {
1252
1696
  const withText = (style) => ({ ...textStyle, ...style });
1253
1697
  const axisLabel = { fontSize: sizes.axisLabel, color: fg };
1254
1698
  const axisLine = tokens.axis_color ? { lineStyle: { color: tokens.axis_color } } : void 0;
1699
+ const axisTick = { show: false };
1255
1700
  const valueAxis = present({
1256
1701
  axisLabel,
1257
1702
  axisLine,
1703
+ axisTick,
1258
1704
  splitLine: tokens.grid_color ? { lineStyle: { color: tokens.grid_color } } : void 0
1259
1705
  });
1260
1706
  return {
@@ -1302,7 +1748,7 @@ var RtemisDraw = (() => {
1302
1748
  }
1303
1749
  },
1304
1750
  line: { symbol: "circle" },
1305
- categoryAxis: present({ axisLabel, axisLine, splitLine: { show: false } }),
1751
+ categoryAxis: present({ axisLabel, axisLine, axisTick, splitLine: { show: false } }),
1306
1752
  valueAxis,
1307
1753
  logAxis: valueAxis,
1308
1754
  timeAxis: valueAxis
@@ -1312,7 +1758,11 @@ var RtemisDraw = (() => {
1312
1758
  // src/index.ts
1313
1759
  var builders = Object.freeze({
1314
1760
  a3: buildA3Option,
1315
- bar: buildBarOption
1761
+ bar: buildBarOption,
1762
+ line: buildLineOption
1763
+ });
1764
+ var aspectHints = Object.freeze({
1765
+ line: lineAspect
1316
1766
  });
1317
1767
  return __toCommonJS(index_exports);
1318
1768
  })();
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Number formatting shared by the builders' tooltips and labels.
3
+ * spec: draw/shared-builders#numbers
4
+ */
5
+ /** Significant digits a non-integer is shown with. */
6
+ export declare const SIGNIFICANT_DIGITS = 3;
7
+ /**
8
+ * A number as text with `digits` significant digits: an integer as it is; a
9
+ * non-integer rounded to `digits` significant digits, keeping every digit of
10
+ * its integer part, in exponent form below 1e-3 or from 1e15; anything that
11
+ * is not a finite number as it is.
12
+ */
13
+ export declare function formatNumber(value: unknown, digits?: number): unknown;
14
+ //# sourceMappingURL=format.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../../src/shared/format.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,sDAAsD;AACtD,eAAO,MAAM,kBAAkB,IAAI,CAAC;AAEpC;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,SAAqB,GAAG,OAAO,CASjF"}
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Number formatting shared by the builders' tooltips and labels.
3
+ * spec: draw/shared-builders#numbers
4
+ */
5
+ /** Significant digits a non-integer is shown with. */
6
+ export const SIGNIFICANT_DIGITS = 3;
7
+ /**
8
+ * A number as text with `digits` significant digits: an integer as it is; a
9
+ * non-integer rounded to `digits` significant digits, keeping every digit of
10
+ * its integer part, in exponent form below 1e-3 or from 1e15; anything that
11
+ * is not a finite number as it is.
12
+ */
13
+ export function formatNumber(value, digits = SIGNIFICANT_DIGITS) {
14
+ if (typeof value !== "number" || !Number.isFinite(value))
15
+ return value;
16
+ if (Number.isInteger(value))
17
+ return String(value);
18
+ const magnitude = Math.abs(value);
19
+ if (magnitude < 1e-3 || magnitude >= 1e15)
20
+ return value.toExponential(digits - 1);
21
+ if (Math.floor(Math.log10(magnitude)) + 1 >= digits)
22
+ return value.toFixed(0);
23
+ const rounded = Number(value.toPrecision(digits));
24
+ const decimals = Math.max(0, digits - 1 - Math.floor(Math.log10(Math.abs(rounded))));
25
+ return rounded.toFixed(decimals);
26
+ }
27
+ //# sourceMappingURL=format.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"format.js","sourceRoot":"","sources":["../../src/shared/format.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,sDAAsD;AACtD,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,CAAC;AAEpC;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,KAAc,EAAE,MAAM,GAAG,kBAAkB;IACtE,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IACvE,IAAI,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC;QAAE,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;IAClD,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAClC,IAAI,SAAS,GAAG,IAAI,IAAI,SAAS,IAAI,IAAI;QAAE,OAAO,KAAK,CAAC,aAAa,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IAClF,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,IAAI,MAAM;QAAE,OAAO,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;IAC7E,MAAM,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC;IAClD,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,GAAG,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;IACrF,OAAO,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;AACnC,CAAC"}
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Axis names a host derives from a config when the author sets none.
3
+ * spec: draw/shared-builders#bar
4
+ */
5
+ import { type ColumnNames } from "./source.ts";
6
+ /** The value-axis members a measure is named from. */
7
+ export interface MeasureMembers {
8
+ readonly y?: ColumnNames | null;
9
+ readonly aggregate?: "none" | "sum" | "mean" | "median";
10
+ }
11
+ /**
12
+ * The name of a value axis: the measure the chart shows. With `y` unset,
13
+ * "Count" (a bar chart counting rows); one `y` column gives its name, prefixed
14
+ * by the aggregate ("Mean X", "Median X", "Sum of X"); several give the
15
+ * aggregate alone, or `null` without one, since the legend names each
16
+ * series. `name` maps a column name to its display text. Hosts set it as the
17
+ * value axis's name unless the author set one. rtemis.draw's `resolve()`
18
+ * derives the same text.
19
+ */
20
+ export declare function valueLabel(config: MeasureMembers, name?: (column: string) => string): string | null;
21
+ //# sourceMappingURL=labels.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"labels.d.ts","sourceRoot":"","sources":["../../src/shared/labels.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAW,KAAK,WAAW,EAAE,MAAM,aAAa,CAAC;AAExD,sDAAsD;AACtD,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,CAAC,CAAC,EAAE,WAAW,GAAG,IAAI,CAAC;IAChC,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG,QAAQ,CAAC;CACzD;AAED;;;;;;;;GAQG;AACH,wBAAgB,UAAU,CACxB,MAAM,EAAE,cAAc,EACtB,IAAI,GAAE,CAAC,MAAM,EAAE,MAAM,KAAK,MAA2B,GACpD,MAAM,GAAG,IAAI,CAcf"}