@rtemis/draw 0.1.1 → 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 (51) hide show
  1. package/README.md +12 -0
  2. package/dist/charts/bar/config.d.ts +64 -0
  3. package/dist/charts/bar/config.d.ts.map +1 -0
  4. package/dist/charts/bar/config.js +172 -0
  5. package/dist/charts/bar/config.js.map +1 -0
  6. package/dist/charts/bar/index.d.ts +3 -0
  7. package/dist/charts/bar/index.d.ts.map +1 -0
  8. package/dist/charts/bar/index.js +2 -0
  9. package/dist/charts/bar/index.js.map +1 -0
  10. package/dist/charts/line/config.d.ts +104 -0
  11. package/dist/charts/line/config.d.ts.map +1 -0
  12. package/dist/charts/line/config.js +372 -0
  13. package/dist/charts/line/config.js.map +1 -0
  14. package/dist/charts/line/index.d.ts +3 -0
  15. package/dist/charts/line/index.d.ts.map +1 -0
  16. package/dist/charts/line/index.js +2 -0
  17. package/dist/charts/line/index.js.map +1 -0
  18. package/dist/index.d.ts +27 -0
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +19 -0
  21. package/dist/index.js.map +1 -1
  22. package/dist/rtemis-draw-builders.cjs +832 -2
  23. package/dist/shared/format.d.ts +14 -0
  24. package/dist/shared/format.d.ts.map +1 -0
  25. package/dist/shared/format.js +27 -0
  26. package/dist/shared/format.js.map +1 -0
  27. package/dist/shared/labels.d.ts +21 -0
  28. package/dist/shared/labels.d.ts.map +1 -0
  29. package/dist/shared/labels.js +31 -0
  30. package/dist/shared/labels.js.map +1 -0
  31. package/dist/shared/layout.d.ts +29 -0
  32. package/dist/shared/layout.d.ts.map +1 -0
  33. package/dist/shared/layout.js +59 -0
  34. package/dist/shared/layout.js.map +1 -0
  35. package/dist/shared/legend.d.ts +14 -0
  36. package/dist/shared/legend.d.ts.map +1 -0
  37. package/dist/shared/legend.js +2 -0
  38. package/dist/shared/legend.js.map +1 -0
  39. package/dist/shared/limits.d.ts +32 -0
  40. package/dist/shared/limits.d.ts.map +1 -0
  41. package/dist/shared/limits.js +62 -0
  42. package/dist/shared/limits.js.map +1 -0
  43. package/dist/shared/source.d.ts +57 -0
  44. package/dist/shared/source.d.ts.map +1 -0
  45. package/dist/shared/source.js +71 -0
  46. package/dist/shared/source.js.map +1 -0
  47. package/dist/theme/tokens.d.ts +76 -0
  48. package/dist/theme/tokens.d.ts.map +1 -0
  49. package/dist/theme/tokens.js +207 -0
  50. package/dist/theme/tokens.js.map +1 -0
  51. package/package.json +1 -1
@@ -22,9 +22,33 @@ var RtemisDraw = (() => {
22
22
  var index_exports = {};
23
23
  __export(index_exports, {
24
24
  A3_CHART_CONFIG_MEMBERS: () => A3_CHART_CONFIG_MEMBERS,
25
+ BAR_CHART_CONFIG_MEMBERS: () => BAR_CHART_CONFIG_MEMBERS,
26
+ DEFAULT_FONT_FAMILY: () => DEFAULT_FONT_FAMILY,
27
+ DEFAULT_MARGIN: () => DEFAULT_MARGIN,
28
+ DEFAULT_PAD: () => DEFAULT_PAD,
29
+ LINE_CHART_CONFIG_MEMBERS: () => LINE_CHART_CONFIG_MEMBERS,
30
+ RTEMIS_PALETTE: () => RTEMIS_PALETTE,
31
+ SIGNIFICANT_DIGITS: () => SIGNIFICANT_DIGITS,
32
+ SIGN_COLORS: () => SIGN_COLORS,
33
+ ZOOM_SLIDER_ID: () => ZOOM_SLIDER_ID,
25
34
  a3SurfaceHints: () => a3SurfaceHints,
35
+ aspectHints: () => aspectHints,
26
36
  buildA3Option: () => buildA3Option,
27
- builders: () => builders
37
+ buildBarOption: () => buildBarOption,
38
+ buildLineOption: () => buildLineOption,
39
+ builders: () => builders,
40
+ calcLimits: () => calcLimits,
41
+ calcLimitsFromZero: () => calcLimitsFromZero,
42
+ darkThemeTokens: () => darkThemeTokens,
43
+ fontStack: () => fontStack,
44
+ formatNumber: () => formatNumber,
45
+ lightThemeTokens: () => lightThemeTokens,
46
+ lineAspect: () => lineAspect,
47
+ themeFontSizes: () => themeFontSizes,
48
+ themeOption: () => themeOption,
49
+ timeLabel: () => timeLabel,
50
+ timeMs: () => timeMs,
51
+ valueLabel: () => valueLabel
28
52
  });
29
53
 
30
54
  // src/charts/a3/layout.ts
@@ -930,9 +954,815 @@ var RtemisDraw = (() => {
930
954
  return value ? { [key]: value } : {};
931
955
  }
932
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
+
970
+ // src/shared/layout.ts
971
+ var DEFAULT_MARGIN = 36;
972
+ var SIDES = ["top", "right", "bottom", "left"];
973
+ function cartesianGrid(config, axisNames) {
974
+ const set = SIDES.filter((side) => typeof config[`margin_${side}`] === "number");
975
+ const grid = {};
976
+ for (const side of SIDES) {
977
+ const value = config[`margin_${side}`];
978
+ if (set.length === 0) grid[side] = DEFAULT_MARGIN;
979
+ else if (typeof value === "number") grid[side] = value;
980
+ }
981
+ grid.outerBoundsMode = "same";
982
+ grid.outerBoundsContain = axisNames ? "all" : "axisLabel";
983
+ return grid;
984
+ }
985
+ var ZOOM_SLIDER_ID = "rtemis-zoom-slider";
986
+ var SLIDER_SIZE = 30;
987
+ var SLIDER_HANDLE = 7;
988
+ var SLIDER_GAP = 8;
989
+ function axisZoom(axis, grid) {
990
+ const side = axis === "x" ? "bottom" : "right";
991
+ const current = grid[side];
992
+ const margin = typeof current === "number" ? current : axis === "x" ? 80 : 15;
993
+ const index = axis === "x" ? { xAxisIndex: 0 } : { yAxisIndex: 0 };
994
+ grid[side] = margin + SLIDER_HANDLE + SLIDER_SIZE + SLIDER_GAP;
995
+ return [
996
+ {
997
+ type: "slider",
998
+ id: ZOOM_SLIDER_ID,
999
+ start: 0,
1000
+ end: 100,
1001
+ ...index,
1002
+ [side]: margin + SLIDER_HANDLE,
1003
+ ...axis === "x" ? { height: SLIDER_SIZE } : { width: SLIDER_SIZE }
1004
+ },
1005
+ { type: "inside", ...index, zoomOnMouseWheel: "shift", moveOnMouseMove: true }
1006
+ ];
1007
+ }
1008
+
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
+ }
1044
+ function distinct(values, levels) {
1045
+ const seen = new Set(values);
1046
+ const ordered = (levels ?? []).filter((level) => seen.has(level));
1047
+ const known = new Set(ordered);
1048
+ return [...ordered, ...[...seen].filter((v) => !known.has(v))];
1049
+ }
1050
+ function column(source, name, member) {
1051
+ const values = source[name];
1052
+ if (values === void 0) {
1053
+ throw new Error(`Column '${name}' (config member '${member}') is not in the data.`);
1054
+ }
1055
+ return Array.isArray(values) ? values : Array.from(values);
1056
+ }
1057
+ function asArray(value) {
1058
+ if (value === null || value === void 0) return [];
1059
+ return Array.isArray(value) ? value : [value];
1060
+ }
1061
+ function numberOrNull(value) {
1062
+ return typeof value === "number" && Number.isFinite(value) ? value : null;
1063
+ }
1064
+
1065
+ // src/charts/bar/config.ts
1066
+ var BAR_CHART_CONFIG_MEMBERS = Object.freeze([
1067
+ "type",
1068
+ "x",
1069
+ "y",
1070
+ "group",
1071
+ "aggregate",
1072
+ "order",
1073
+ "stack",
1074
+ "horizontal",
1075
+ "zoom",
1076
+ "bar_width",
1077
+ "palette",
1078
+ "ylim",
1079
+ "xlab",
1080
+ "ylab",
1081
+ "title",
1082
+ "margin_top",
1083
+ "margin_right",
1084
+ "margin_bottom",
1085
+ "margin_left",
1086
+ "legend_position",
1087
+ "legend_placement",
1088
+ "legend_title"
1089
+ ]);
1090
+ function buildBarOption(data, config, appearance = {}) {
1091
+ const source = data.columns;
1092
+ const yColumns = asArray(config.y);
1093
+ if (!config.x) {
1094
+ throw new Error("A bar chart needs `x` set to draw.");
1095
+ }
1096
+ const xs = textColumn(data, config.x, "x");
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);
1109
+ let categories;
1110
+ let series;
1111
+ if (groupValues || counting) {
1112
+ categories = distinct(xs, data.levels?.[config.x]);
1113
+ const groups = groupValues ? distinct(groupValues, data.levels?.[config.group]) : [null];
1114
+ const at = new Map(categories.map((c, i) => [c, i]));
1115
+ series = groups.flatMap(
1116
+ (g) => measures.map((y) => {
1117
+ const values = valuesOf(y);
1118
+ const cells = categories.map(() => null);
1119
+ xs.forEach((x, row) => {
1120
+ const i = at.get(x);
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;
1124
+ });
1125
+ const name = g !== null && groups.length > 1 ? measures.length > 1 ? `${y} / ${g}` : g : y ?? "Count";
1126
+ return { name, data: cells };
1127
+ })
1128
+ );
1129
+ } else {
1130
+ categories = xs;
1131
+ series = measures.map((y) => ({ name: y, data: valuesOf(y) }));
1132
+ }
1133
+ const permutation = order(categories, series, config.order ?? "input");
1134
+ categories = permutation.map((i) => categories[i]);
1135
+ series = series.map((s) => ({ ...s, data: permutation.map((i) => s.data[i] ?? null) }));
1136
+ const palette = asArray(config.palette);
1137
+ const stack = config.stack ? "total" : void 0;
1138
+ const barWidth = typeof config.bar_width === "number" ? config.bar_width : void 0;
1139
+ const builtSeries = series.map((s, i) => {
1140
+ const single = series.length === 1;
1141
+ const perBar = single && palette.length > 1;
1142
+ const color = perBar ? void 0 : palette.length > 0 ? palette[i % palette.length] : void 0;
1143
+ return {
1144
+ type: "bar",
1145
+ ...single ? {} : { name: s.name },
1146
+ data: perBar ? s.data.map((value2, j) => ({
1147
+ value: value2,
1148
+ itemStyle: { color: palette[(permutation[j] ?? j) % palette.length] }
1149
+ })) : s.data,
1150
+ ...stack ? { stack } : {},
1151
+ ...barWidth === void 0 ? {} : { barWidth },
1152
+ ...color === void 0 ? {} : { itemStyle: { color } }
1153
+ };
1154
+ });
1155
+ const horizontal = config.horizontal ?? false;
1156
+ const category = {
1157
+ type: "category",
1158
+ data: categories,
1159
+ axisPointer: { type: "none" },
1160
+ ...horizontal ? { inverse: true } : {}
1161
+ };
1162
+ const value = {
1163
+ type: "value",
1164
+ ...config.ylim ? { min: config.ylim[0], max: config.ylim[1] } : {}
1165
+ };
1166
+ const xAxis = horizontal ? value : category;
1167
+ const yAxis = horizontal ? category : value;
1168
+ if (config.xlab) Object.assign(xAxis, { name: config.xlab, nameLocation: "middle" });
1169
+ if (config.ylab) Object.assign(yAxis, { name: config.ylab, nameLocation: "middle" });
1170
+ const grid = cartesianGrid(config, Boolean(config.xlab || config.ylab));
1171
+ const dataZoom = config.zoom ? axisZoom(horizontal ? "y" : "x", grid) : void 0;
1172
+ return {
1173
+ ...config.title ? { title: { text: config.title } } : {},
1174
+ tooltip: { trigger: "axis", valueFormatter: formatNumber },
1175
+ ...series.length > 1 ? { legend: { data: series.map((s) => s.name) } } : {},
1176
+ xAxis,
1177
+ yAxis,
1178
+ grid,
1179
+ ...dataZoom ? { dataZoom } : {},
1180
+ series: builtSeries,
1181
+ ...appearance.animation === void 0 ? {} : { animation: appearance.animation }
1182
+ };
1183
+ }
1184
+ function order(categories, series, how) {
1185
+ const index = categories.map((_, i) => i);
1186
+ if (how === "input") return index;
1187
+ const total = (i) => series.reduce((sum, s) => sum + (s.data[i] ?? 0), 0);
1188
+ const sign = how === "increasing" ? 1 : -1;
1189
+ return index.sort((a, b) => sign * (total(a) - total(b)) || a - b);
1190
+ }
1191
+ function requireOneRowPer(xs, groups) {
1192
+ const seen = /* @__PURE__ */ new Set();
1193
+ xs.forEach((x, row) => {
1194
+ const key = groups ? `${x}\0${groups[row]}` : x;
1195
+ if (seen.has(key)) {
1196
+ const where = groups ? `category '${x}' in group '${groups[row]}'` : `category '${x}'`;
1197
+ throw new Error(
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).`
1437
+ );
1438
+ }
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 });
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
+ }
1579
+ }
1580
+
1581
+ // src/theme/tokens.ts
1582
+ var RTEMIS_PALETTE = Object.freeze([
1583
+ "#6CA3A0",
1584
+ "#F08904",
1585
+ "#BE2E5F",
1586
+ "#B3CFE8",
1587
+ "#0F6A66",
1588
+ "#FDB808",
1589
+ "#EA384A",
1590
+ "#466D96",
1591
+ "#526551",
1592
+ "#F384FF",
1593
+ "#7D0830",
1594
+ "#375D86",
1595
+ "#B1A7B3",
1596
+ "#7364F2",
1597
+ "#895140",
1598
+ "#C5A267"
1599
+ ]);
1600
+ var SIGN_COLORS = Object.freeze({
1601
+ negative: "#2D83D3",
1602
+ neutral: "#808080",
1603
+ positive: "#C55E28"
1604
+ });
1605
+ function lightThemeTokens(overrides = {}) {
1606
+ return Object.freeze({
1607
+ scheme: "light",
1608
+ base_font_size: 12,
1609
+ palette: RTEMIS_PALETTE,
1610
+ bg_color: "#ffffff",
1611
+ fg_color: "rgba(0, 0, 0, 0.7)",
1612
+ negative_color: SIGN_COLORS.negative,
1613
+ neutral_color: SIGN_COLORS.neutral,
1614
+ positive_color: SIGN_COLORS.positive,
1615
+ ...overrides
1616
+ });
1617
+ }
1618
+ function darkThemeTokens(overrides = {}) {
1619
+ return Object.freeze({
1620
+ scheme: "dark",
1621
+ base_font_size: 12,
1622
+ palette: RTEMIS_PALETTE,
1623
+ bg_color: "#181818",
1624
+ fg_color: "rgba(255, 255, 255, 0.7)",
1625
+ title_color: "rgba(255, 255, 255, 0.9)",
1626
+ subtitle_color: "rgba(255, 255, 255, 0.5)",
1627
+ axis_color: "rgba(255, 255, 255, 0.3)",
1628
+ grid_color: "rgba(255, 255, 255, 0.1)",
1629
+ tooltip_bg: "rgba(20, 20, 20, 0.9)",
1630
+ tooltip_border_color: "rgba(255, 255, 255, 0.1)",
1631
+ negative_color: SIGN_COLORS.negative,
1632
+ neutral_color: SIGN_COLORS.neutral,
1633
+ positive_color: SIGN_COLORS.positive,
1634
+ ...overrides
1635
+ });
1636
+ }
1637
+ var DEFAULT_FONT_FAMILY = "system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif";
1638
+ var GENERIC_FONT_FAMILIES = /* @__PURE__ */ new Set([
1639
+ "serif",
1640
+ "sans-serif",
1641
+ "monospace",
1642
+ "cursive",
1643
+ "fantasy",
1644
+ "math",
1645
+ "emoji",
1646
+ "fangsong",
1647
+ "system-ui",
1648
+ "ui-serif",
1649
+ "ui-sans-serif",
1650
+ "ui-monospace",
1651
+ "ui-rounded",
1652
+ "-apple-system",
1653
+ "blinkmacsystemfont"
1654
+ ]);
1655
+ function fontStack(fontFamily) {
1656
+ if (fontFamily === void 0 || fontFamily === null) return DEFAULT_FONT_FAMILY;
1657
+ const entries = fontFamily.split(",").map((e) => e.trim()).filter((e) => e.length > 0);
1658
+ if (entries.length === 0) throw new Error("`font_family` must be a nonempty font family.");
1659
+ const ident = /^-?[A-Za-z_][A-Za-z0-9_-]*( +-?[A-Za-z_][A-Za-z0-9_-]*)*$/;
1660
+ const quoted = /^(['"]).*\1$/;
1661
+ const out = entries.map((e) => {
1662
+ const generic = GENERIC_FONT_FAMILIES.has(e.toLowerCase());
1663
+ return quoted.test(e) || generic || ident.test(e) ? e : `'${e.replaceAll("'", "\\'")}'`;
1664
+ });
1665
+ const last = entries[entries.length - 1] ?? "";
1666
+ if (!GENERIC_FONT_FAMILIES.has(last.toLowerCase())) out.push(DEFAULT_FONT_FAMILY);
1667
+ return out.join(", ");
1668
+ }
1669
+ function roundHalfEven(x) {
1670
+ const floor = Math.floor(x);
1671
+ const diff = x - floor;
1672
+ if (Math.abs(diff - 0.5) > 1e-9) return Math.round(x);
1673
+ return floor % 2 === 0 ? floor : floor + 1;
1674
+ }
1675
+ function themeFontSizes(tokens) {
1676
+ const base = tokens.base_font_size;
1677
+ return {
1678
+ base,
1679
+ title: tokens.title_font_size ?? roundHalfEven(base * 1.2),
1680
+ subtitle: tokens.subtitle_font_size ?? base,
1681
+ axisLabel: tokens.axis_label_font_size ?? base,
1682
+ legend: tokens.legend_font_size ?? base,
1683
+ tooltip: tokens.tooltip_font_size ?? base
1684
+ };
1685
+ }
1686
+ function present(entries) {
1687
+ return Object.fromEntries(
1688
+ Object.entries(entries).filter(([, v]) => v !== void 0 && v !== null)
1689
+ );
1690
+ }
1691
+ function themeOption(tokens) {
1692
+ const sizes = themeFontSizes(tokens);
1693
+ const dark = tokens.scheme === "dark";
1694
+ const fg = tokens.fg_color;
1695
+ const textStyle = { color: fg, fontFamily: fontStack(tokens.font_family), fontSize: sizes.base };
1696
+ const withText = (style) => ({ ...textStyle, ...style });
1697
+ const axisLabel = { fontSize: sizes.axisLabel, color: fg };
1698
+ const axisLine = tokens.axis_color ? { lineStyle: { color: tokens.axis_color } } : void 0;
1699
+ const axisTick = { show: false };
1700
+ const valueAxis = present({
1701
+ axisLabel,
1702
+ axisLine,
1703
+ axisTick,
1704
+ splitLine: tokens.grid_color ? { lineStyle: { color: tokens.grid_color } } : void 0
1705
+ });
1706
+ return {
1707
+ color: [...tokens.palette],
1708
+ backgroundColor: tokens.bg_color,
1709
+ textStyle,
1710
+ title: {
1711
+ textStyle: withText(present({ color: tokens.title_color, fontSize: sizes.title })),
1712
+ subtextStyle: withText(present({ color: tokens.subtitle_color, fontSize: sizes.subtitle }))
1713
+ },
1714
+ legend: {
1715
+ textStyle: withText({ color: tokens.legend_color ?? fg, fontSize: sizes.legend })
1716
+ },
1717
+ tooltip: present({
1718
+ backgroundColor: tokens.tooltip_bg,
1719
+ borderColor: tokens.tooltip_border_color,
1720
+ textStyle: withText({ color: tokens.tooltip_color ?? fg, fontSize: sizes.tooltip })
1721
+ }),
1722
+ visualMap: { textStyle },
1723
+ // Neutral controls leave the palette to encode data; every accent-bearing
1724
+ // slider state is set so ECharts' blue defaults cannot show.
1725
+ // spec: draw/visual-qa-docs#neutral-data-zoom-sliders
1726
+ dataZoom: {
1727
+ backgroundColor: "rgba(128,128,128,0.06)",
1728
+ borderColor: dark ? "#606060" : "#b0b0b0",
1729
+ fillerColor: "rgba(128,128,128,0.18)",
1730
+ dataBackground: {
1731
+ lineStyle: { color: "#808080" },
1732
+ areaStyle: { color: "#808080", opacity: 0.12 }
1733
+ },
1734
+ selectedDataBackground: {
1735
+ lineStyle: { color: dark ? "#b0b0b0" : "#707070" },
1736
+ areaStyle: { color: "#808080", opacity: 0.25 }
1737
+ },
1738
+ handleStyle: {
1739
+ color: dark ? "#303030" : "#ffffff",
1740
+ borderColor: dark ? "#909090" : "#808080"
1741
+ },
1742
+ moveHandleStyle: { color: "#909090", opacity: 0.6 },
1743
+ brushStyle: { color: "#808080", opacity: 0.3 },
1744
+ textStyle: axisLabel,
1745
+ emphasis: {
1746
+ handleStyle: { borderColor: dark ? "#cccccc" : "#505050" },
1747
+ moveHandleStyle: { color: dark ? "#b0b0b0" : "#707070" }
1748
+ }
1749
+ },
1750
+ line: { symbol: "circle" },
1751
+ categoryAxis: present({ axisLabel, axisLine, axisTick, splitLine: { show: false } }),
1752
+ valueAxis,
1753
+ logAxis: valueAxis,
1754
+ timeAxis: valueAxis
1755
+ };
1756
+ }
1757
+
933
1758
  // src/index.ts
934
1759
  var builders = Object.freeze({
935
- a3: buildA3Option
1760
+ a3: buildA3Option,
1761
+ bar: buildBarOption,
1762
+ line: buildLineOption
1763
+ });
1764
+ var aspectHints = Object.freeze({
1765
+ line: lineAspect
936
1766
  });
937
1767
  return __toCommonJS(index_exports);
938
1768
  })();