@x-cmd/draw 0.2.5 → 0.4.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.
- package/README.md +80 -166
- package/dist/lib.d.ts +27 -1
- package/dist/lib.js +39 -1
- package/dist/node/cli.js +109 -11
- package/dist/render/anno.d.ts +50 -0
- package/dist/render/anno.js +589 -0
- package/dist/render/arch.d.ts +106 -0
- package/dist/render/arch.js +1011 -0
- package/dist/render/archmap.d.ts +157 -0
- package/dist/render/archmap.js +683 -0
- package/dist/render/area.js +20 -14
- package/dist/render/bar-grouped.js +8 -5
- package/dist/render/bar-stacked.js +11 -7
- package/dist/render/bar.js +8 -3
- package/dist/render/butterfly.d.ts +3 -0
- package/dist/render/butterfly.js +355 -0
- package/dist/render/calendar.d.ts +6 -0
- package/dist/render/calendar.js +448 -0
- package/dist/render/combo.js +16 -8
- package/dist/render/compass.d.ts +6 -0
- package/dist/render/compass.js +77 -0
- package/dist/render/dispatch.js +26 -1
- package/dist/render/echarts-ssr.d.ts +51 -0
- package/dist/render/echarts-ssr.js +511 -0
- package/dist/render/gantt.d.ts +46 -0
- package/dist/render/gantt.js +415 -0
- package/dist/render/gantt2.d.ts +24 -0
- package/dist/render/gantt2.js +198 -0
- package/dist/render/hbar.js +8 -4
- package/dist/render/icons.d.ts +3 -0
- package/dist/render/icons.js +78 -0
- package/dist/render/kinds.d.ts +11 -0
- package/dist/render/kinds.js +176 -0
- package/dist/render/ladder.d.ts +21 -0
- package/dist/render/ladder.js +230 -0
- package/dist/render/layout.d.ts +46 -0
- package/dist/render/layout.js +57 -0
- package/dist/render/line-multi.js +7 -5
- package/dist/render/line.js +9 -5
- package/dist/render/maps.d.ts +46 -0
- package/dist/render/maps.js +237 -0
- package/dist/render/measure.d.ts +0 -1
- package/dist/render/measure.js +0 -1
- package/dist/render/mermaid.d.ts +30 -0
- package/dist/render/mermaid.js +277 -0
- package/dist/render/mindmap.d.ts +10 -0
- package/dist/render/mindmap.js +388 -0
- package/dist/render/orgicon.d.ts +12 -0
- package/dist/render/orgicon.js +31 -0
- package/dist/render/pie.js +1 -1
- package/dist/render/png.d.ts +21 -0
- package/dist/render/png.js +109 -0
- package/dist/render/quadrant.d.ts +6 -0
- package/dist/render/quadrant.js +201 -0
- package/dist/render/query.d.ts +2 -0
- package/dist/render/query.js +108 -1
- package/dist/render/route.d.ts +86 -0
- package/dist/render/route.js +1077 -0
- package/dist/render/scatter.js +87 -12
- package/dist/render/seq.d.ts +41 -0
- package/dist/render/seq.js +248 -0
- package/dist/render/svg.d.ts +34 -2
- package/dist/render/svg.js +159 -20
- package/dist/render/table.d.ts +30 -0
- package/dist/render/table.js +134 -0
- package/dist/render/tokei.d.ts +18 -0
- package/dist/render/tokei.js +230 -0
- package/dist/render/uc.d.ts +58 -0
- package/dist/render/uc.js +319 -0
- package/dist/render/units.d.ts +32 -0
- package/dist/render/units.js +84 -0
- package/dist/render/vendor/au.js +10 -0
- package/dist/render/vendor/br.js +10 -0
- package/dist/render/vendor/china.js +11 -0
- package/dist/render/vendor/de.js +10 -0
- package/dist/render/vendor/eu.js +10 -0
- package/dist/render/vendor/japan.js +10 -0
- package/dist/render/vendor/kr.js +10 -0
- package/dist/render/vendor/mermaid-flow-parser.js +1164 -0
- package/dist/render/vendor/mermaid-gantt-parser.js +810 -0
- package/dist/render/vendor/us-states.js +11 -0
- package/dist/render/vendor/world-geo.d.ts +22 -0
- package/dist/render/vendor/world-geo.js +9 -0
- package/dist/render/vendor/world.d.ts +13 -0
- package/dist/render/vendor/world.js +12 -0
- package/dist/specs/base.d.ts +16 -0
- package/dist/specs/base.js +34 -1
- package/dist/specs/butterfly.d.ts +46 -0
- package/dist/specs/butterfly.js +241 -0
- package/dist/specs/calendar.d.ts +90 -0
- package/dist/specs/calendar.js +296 -0
- package/dist/specs/compass.d.ts +27 -0
- package/dist/specs/compass.js +111 -0
- package/dist/specs/index.d.ts +5 -0
- package/dist/specs/index.js +5 -0
- package/dist/specs/ladder.d.ts +43 -0
- package/dist/specs/ladder.js +261 -0
- package/dist/specs/quadrant.d.ts +41 -0
- package/dist/specs/quadrant.js +200 -0
- package/dist/specs/scatter.d.ts +14 -8
- package/dist/specs/scatter.js +54 -5
- package/dist/specs/table.d.ts +24 -0
- package/dist/specs/table.js +154 -0
- package/dist/store.d.ts +1 -1
- package/dist/store.js +1 -1
- package/dist/styles/butterfly.d.ts +2 -0
- package/dist/styles/butterfly.js +74 -0
- package/dist/styles/calendar.d.ts +2 -0
- package/dist/styles/calendar.js +51 -0
- package/dist/styles/compass.d.ts +2 -0
- package/dist/styles/compass.js +19 -0
- package/dist/styles/fonts-banner.d.ts +5 -0
- package/dist/styles/fonts-banner.js +9 -0
- package/dist/styles/fonts-cjk.d.ts +2 -0
- package/dist/styles/fonts-cjk.js +6 -0
- package/dist/styles/fonts-ttf.d.ts +2 -0
- package/dist/styles/fonts-ttf.js +6 -0
- package/dist/styles/index.js +13 -0
- package/dist/styles/ladder.d.ts +3 -0
- package/dist/styles/ladder.js +66 -0
- package/dist/styles/quadrant.d.ts +2 -0
- package/dist/styles/quadrant.js +19 -0
- package/dist/styles/table.d.ts +2 -0
- package/dist/styles/table.js +12 -0
- package/dist/styles/types.d.ts +1 -0
- package/dist/styles/types.js +1 -0
- package/dist/theme.d.ts +1 -1
- package/dist/theme.js +3 -4
- package/dist/types/dagre.d.ts +39 -0
- package/dist/types/dagre.js +3 -0
- package/package.json +28 -3
package/dist/render/area.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// 面积图:行为由皮肤规则驱动(规范 面积图 1-3)。
|
|
2
2
|
import { ApiError } from '../specs/index.js';
|
|
3
3
|
import { card, TAB } from '../theme.js';
|
|
4
|
-
import { esc, fmtFull, fmtNum, niceStep, niceTicks, r2, reserveBands, snap, smoothPath, trunc, LabelPlacer, pushLineLabelsAllOrNothing, pushPoints, toSegments, svgDoc, titleBlock, editorialTitle, yAxisGrid, axisTitleY, topLegend, findPeak, sVar, } from './svg.js';
|
|
4
|
+
import { esc, fmtFull, fmtNum, niceStep, niceTicks, r2, reserveBands, snap, smoothPath, roundedPath, trunc, LabelPlacer, pushLineLabelsAllOrNothing, pushPoints, toSegments, svgDoc, titleBlock, editorialTitle, yAxisGrid, axisTitleY, topLegend, findPeak, sVar, markPoints, marksLayer, } from './svg.js';
|
|
5
5
|
import { xOverhang, yTickInset } from './measure.js';
|
|
6
6
|
import { HANDEE_WOFF2 } from '../styles/handlee.js';
|
|
7
7
|
const SKETCH_FONT = "Handlee,'Comic Sans MS','Chalkboard SE','Comic Neue','Segoe Print',cursive";
|
|
@@ -14,7 +14,6 @@ function areaGeo(w, h, hasTitle, m = {}) {
|
|
|
14
14
|
top: Math.round(h * (hasTitle ? 0.2 : 0.173)),
|
|
15
15
|
bottom: Math.round(h * 0.8077),
|
|
16
16
|
xLabelY: Math.round(h * 0.896),
|
|
17
|
-
legendY: Math.round(h * 0.092),
|
|
18
17
|
};
|
|
19
18
|
}
|
|
20
19
|
// 规范 area-3 实测几何(766×500)
|
|
@@ -26,7 +25,6 @@ function opendataGeo(w, h, m = {}) {
|
|
|
26
25
|
top: Math.round(h * 0.268),
|
|
27
26
|
bottom: Math.round(h * 0.8096),
|
|
28
27
|
xLabelY: Math.round(h * 0.852),
|
|
29
|
-
legendY: Math.round(h * 0.092),
|
|
30
28
|
};
|
|
31
29
|
}
|
|
32
30
|
// 单/多系列共用布局
|
|
@@ -62,7 +60,7 @@ function areaLayout(spec, o, m = {}) {
|
|
|
62
60
|
right: geo ? geo.right : width - pad,
|
|
63
61
|
bottom: geo ? geo.bottom : height - pad - 30,
|
|
64
62
|
xLabelY: geo ? geo.xLabelY : height - pad - 30 + (rules.baseline === 'dots' ? 34 : 20),
|
|
65
|
-
legendY:
|
|
63
|
+
legendY: pad + 14, // 图例坐标题基线,与 Edit 角标同排(#27)
|
|
66
64
|
// area-3:数据点自 left 内缩(时间轴留白)
|
|
67
65
|
xFirst: rules.title === 'editorial' && geo ? left + Math.round(width * 0.0574) : left,
|
|
68
66
|
};
|
|
@@ -109,8 +107,9 @@ export function renderArea(spec, o) {
|
|
|
109
107
|
const n = spec.data.length;
|
|
110
108
|
const xs = (i) => (n === 1 ? (left + right) / 2 : xFirst + ((right - xFirst) * i) / (n - 1));
|
|
111
109
|
const pts = spec.data.map((d, i) => [xs(i), y(d.value)]);
|
|
112
|
-
const path = rules.smooth ? smoothPath(pts) :
|
|
113
|
-
const
|
|
110
|
+
const path = rules.smooth ? smoothPath(pts) : roundedPath(pts);
|
|
111
|
+
const bg = card(width, height, rules.canvas === 'plain');
|
|
112
|
+
const parts = [];
|
|
114
113
|
parts.push(`<defs><linearGradient id="dg-area" x1="0" y1="0" x2="0" y2="1">` +
|
|
115
114
|
`<stop offset="0" stop-color="var(--d-s1)" stop-opacity="${rules.areaTop}"/>` +
|
|
116
115
|
`<stop offset="1" stop-color="var(--d-s1)" stop-opacity="${rules.areaBottom}"/></linearGradient></defs>`);
|
|
@@ -120,7 +119,7 @@ export function renderArea(spec, o) {
|
|
|
120
119
|
parts.push(`<path d="${path}L${r2(pts[n - 1][0])} ${r2(bottom)}L${r2(pts[0][0])} ${r2(bottom)}Z" fill="url(#dg-area)"/>`);
|
|
121
120
|
const dash = rules.lineDash ? ` stroke-dasharray="${rules.lineDash}"` : '';
|
|
122
121
|
parts.push(`<path d="${path}" fill="none" stroke="var(--d-s1)" stroke-width="${rules.lineWidth}" stroke-linecap="round" stroke-linejoin="round"${dash}/>`);
|
|
123
|
-
pushPoints(parts, pts, 'var(--d-s1)', rules);
|
|
122
|
+
pushPoints(parts, pts, 'var(--d-s1)', rules, o.interactive ? spec.data.map((d) => `${d.label} · ${fmtFull(d.value)}${spec.unit ? ' ' + spec.unit : ''}`) : undefined, 0);
|
|
124
123
|
const skip = n > 14 ? Math.ceil(n / 7) : 1;
|
|
125
124
|
spec.data.forEach((d, i) => {
|
|
126
125
|
if (i % skip !== 0 && i !== n - 1)
|
|
@@ -144,18 +143,21 @@ export function renderArea(spec, o) {
|
|
|
144
143
|
const my = y(mean);
|
|
145
144
|
parts.push(`<line x1="${left}" y1="${r2(my)}" x2="${right}" y2="${r2(my)}" stroke="${sVar(2)}" stroke-width="1" stroke-dasharray="4 3"/>`);
|
|
146
145
|
parts.push(`<text x="${r2(right)}" y="${r2(my - 6)}" text-anchor="end" font-size="11" fill="${sVar(2)}" ` +
|
|
147
|
-
|
|
146
|
+
`>Avg: ${fmtNum(mean)}</text>`);
|
|
148
147
|
}
|
|
149
148
|
if (rules.endValue) {
|
|
150
149
|
const [ex, ey] = pts[n - 1];
|
|
151
150
|
parts.push(`<circle cx="${r2(ex)}" cy="${r2(ey)}" r="4" fill="var(--d-halo)" stroke="var(--d-faint)" stroke-width="1.5"/>`);
|
|
152
151
|
parts.push(`<text x="${r2(ex - 8)}" y="${r2(ey + 20)}" text-anchor="end" font-size="11" font-weight="500" fill="var(--d-ink)" ` +
|
|
153
|
-
`
|
|
152
|
+
`style="${TAB}">${fmtFull(spec.data[n - 1].value)}</text>`);
|
|
154
153
|
}
|
|
155
154
|
if (spec.source) {
|
|
156
155
|
parts.push(`<text x="24" y="${r2(Math.round(height * 0.904))}" font-size="13" fill="var(--d-muted)">${esc(spec.source)}</text>`);
|
|
157
156
|
}
|
|
158
|
-
|
|
157
|
+
const mpts = markPoints(o.marks, spec.data.map((d) => d.label), xs, y, [contentTop, bottom]);
|
|
158
|
+
if (mpts.length > 0)
|
|
159
|
+
parts.push(marksLayer(mpts, width));
|
|
160
|
+
return svgDoc(width, height, 'area', version, spec.title ?? 'area chart', spec, skin, bg + headSvg + parts.join(''));
|
|
159
161
|
}
|
|
160
162
|
// 多系列面积(area-1):三遍绘制(面积 → 虚线描边 → 点)
|
|
161
163
|
export function renderAreaMulti(spec, o) {
|
|
@@ -171,8 +173,9 @@ export function renderAreaMulti(spec, o) {
|
|
|
171
173
|
const xs = (i) => (n === 1 ? (left + right) / 2 : xFirst + ((right - xFirst) * i) / (n - 1));
|
|
172
174
|
const colors = spec.series.map((_, i) => sVar(i % skin.series.length));
|
|
173
175
|
const allPts = spec.series.map((s) => s.values.map((v, i) => [xs(i), y(v)]));
|
|
174
|
-
const pathOf = (pts) => rules.smooth ? smoothPath(pts) :
|
|
175
|
-
const
|
|
176
|
+
const pathOf = (pts) => rules.smooth ? smoothPath(pts) : roundedPath(pts);
|
|
177
|
+
const bg = card(width, height, rules.canvas === 'plain');
|
|
178
|
+
const parts = [];
|
|
176
179
|
parts.push(`<defs>${spec.series.map((_, i) => `<linearGradient id="dg-area-${i}" x1="0" y1="0" x2="0" y2="1">` +
|
|
177
180
|
`<stop offset="0" stop-color="${colors[i]}" stop-opacity="${rules.areaTop}"/>` +
|
|
178
181
|
`<stop offset="1" stop-color="${colors[i]}" stop-opacity="${rules.areaBottom}"/></linearGradient>`).join('')}</defs>`);
|
|
@@ -192,7 +195,7 @@ export function renderAreaMulti(spec, o) {
|
|
|
192
195
|
allPts.forEach((pts, i) => {
|
|
193
196
|
parts.push(`<path d="${pathOf(pts)}" fill="none" stroke="${colors[i]}" stroke-width="${rules.lineWidth}" stroke-linecap="round" stroke-linejoin="round"${dash}/>`);
|
|
194
197
|
});
|
|
195
|
-
allPts.forEach((pts, i) => pushPoints(parts, pts, colors[i], rules));
|
|
198
|
+
allPts.forEach((pts, i) => pushPoints(parts, pts, colors[i], rules, o.interactive ? spec.series[i].values.map((v, vi) => `${spec.labels[vi]} · ${spec.series[i].name}: ${fmtFull(v)}${spec.unit ? ' ' + spec.unit : ''}`) : undefined, i));
|
|
196
199
|
const skip = n > 14 ? Math.ceil(n / 7) : 1;
|
|
197
200
|
spec.labels.forEach((l, i) => {
|
|
198
201
|
if (i % skip !== 0 && i !== n - 1)
|
|
@@ -217,7 +220,10 @@ export function renderAreaMulti(spec, o) {
|
|
|
217
220
|
pushLineLabelsAllOrNothing(parts, placer, items, allPts.flatMap((p) => toSegments(p)));
|
|
218
221
|
}
|
|
219
222
|
}
|
|
220
|
-
|
|
223
|
+
const mpts = markPoints(o.marks, spec.labels, xs, y, [contentTop, bottom]);
|
|
224
|
+
if (mpts.length > 0)
|
|
225
|
+
parts.push(marksLayer(mpts, width));
|
|
226
|
+
return svgDoc(width, height, 'area', version, spec.title ?? 'area chart', spec, skin, bg + headSvg + parts.join(''));
|
|
221
227
|
}
|
|
222
228
|
// 手绘风面积图(area-2):xkcd 滤镜 + Handlee 手写字体 + 端点标注
|
|
223
229
|
function renderSketchArea(spec, o) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { card } from '../theme.js';
|
|
2
|
-
import { esc, fmtFull, fmtNum, niceTicks, r2, textW, topLegend, trunc, svgDoc, titleBlock, yAxisGrid, findPeak, onVar, sVar, pushBarLabelsAllOrNothing, labelStride, LabelPlacer, reserveBands } from './svg.js';
|
|
2
|
+
import { esc, fmtFull, fmtNum, niceTicks, r2, textW, topLegend, trunc, svgDoc, titleBlock, yAxisGrid, findPeak, onVar, sVar, pushBarLabelsAllOrNothing, labelStride, LabelPlacer, reserveBands, markPoints, marksLayer } from './svg.js';
|
|
3
3
|
import { yTickInset } from './measure.js';
|
|
4
4
|
// 规范 bar-1 实测几何(960×520):left 76、右留 50、顶 94(0.181h)、底 448(0.862h)、X 标签 480
|
|
5
5
|
// 留白 = max(规范比例, 实测刻度列):长刻度/窄画布时左缘撑大,刻度不裁字
|
|
@@ -10,7 +10,6 @@ function barGeo(w, h, hasTitle, m = {}) {
|
|
|
10
10
|
top: Math.round(h * (hasTitle ? 0.2 : 0.181)),
|
|
11
11
|
bottom: Math.round(h * 0.8615),
|
|
12
12
|
xLabelY: Math.round(h * 0.9231),
|
|
13
|
-
legendY: Math.round(h * 0.0923),
|
|
14
13
|
};
|
|
15
14
|
}
|
|
16
15
|
/**
|
|
@@ -70,11 +69,12 @@ export function renderBarMulti(spec, o) {
|
|
|
70
69
|
const right = geo ? geo.right : width - pad;
|
|
71
70
|
const bottom = geo ? geo.bottom : height - pad - 30;
|
|
72
71
|
const xLabelY = geo ? geo.xLabelY : bottom + 20;
|
|
73
|
-
const legendY =
|
|
72
|
+
const legendY = pad + 14; // 图例坐标题基线,与 Edit 角标同排(#27)
|
|
74
73
|
const yMax = ticks[ticks.length - 1];
|
|
75
74
|
const y = (v) => bottom - (v / yMax) * (bottom - contentTop);
|
|
75
|
+
// 卡片垫底(标题压在卡片之上)
|
|
76
|
+
const bg = card(width, height, plain);
|
|
76
77
|
const parts = [
|
|
77
|
-
card(width, height, plain),
|
|
78
78
|
`<defs><linearGradient id="dg-bar" x1="0" y1="0" x2="0" y2="1">` +
|
|
79
79
|
`<stop offset="0" stop-color="var(--d-ramp-a)"/><stop offset="1" stop-color="var(--d-ramp-b)"/></linearGradient></defs>`,
|
|
80
80
|
];
|
|
@@ -131,5 +131,8 @@ export function renderBarMulti(spec, o) {
|
|
|
131
131
|
if (pi % xStride === 0)
|
|
132
132
|
parts.push(`<text x="${r2(cx)}" y="${r2(xLabelY)}" text-anchor="middle" font-size="12" fill="var(--d-muted)">${esc(trunc(l, 12))}</text>`);
|
|
133
133
|
});
|
|
134
|
-
|
|
134
|
+
const mpts = markPoints(o.marks, spec.labels, (i) => left + step * i + step / 2, y, [contentTop, bottom]);
|
|
135
|
+
if (mpts.length > 0)
|
|
136
|
+
parts.push(marksLayer(mpts, width));
|
|
137
|
+
return svgDoc(width, height, 'bar', version, spec.title ?? 'bar chart', spec, skin, bg + titleBlock(spec.title) + parts.join(''));
|
|
135
138
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TAB, card } from '../theme.js';
|
|
2
|
-
import { esc, fmtFull, fmtNum, niceTicks, r2, textW, topLegend, trunc, svgDoc, titleBlock, yAxisGrid, findPeak, onVar, sVar, pushBarLabelsAllOrNothing, labelStride, LabelPlacer, reserveBands } from './svg.js';
|
|
2
|
+
import { esc, fmtFull, fmtNum, niceTicks, r2, textW, topLegend, trunc, svgDoc, titleBlock, yAxisGrid, findPeak, onVar, sVar, pushBarLabelsAllOrNothing, labelStride, LabelPlacer, reserveBands, markPoints, marksLayer } from './svg.js';
|
|
3
3
|
import { yTickInset, tickColW } from './measure.js';
|
|
4
4
|
/**
|
|
5
5
|
* Stacked multi-series bars (auto-switches to 100% stacking on extreme magnitudes).
|
|
@@ -35,7 +35,7 @@ export function renderBarStacked(spec, o) {
|
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
// 百分比模式柱顶要标总量,绘图区需下移到图例之下,否则总量与图例文字重叠
|
|
38
|
-
const contentTop = pctMode ? Math.max(fixedTop, pad +
|
|
38
|
+
const contentTop = pctMode ? Math.max(fixedTop, pad + 14 + 30) : fixedTop;
|
|
39
39
|
// 百分比模式:各组的段按 pct 布局,轴刻度 0-100%
|
|
40
40
|
const tickVals = pctMode ? [0, 20, 40, 60, 80, 100] : niceTicks(gMax, 5);
|
|
41
41
|
// 左内边距 = max(规范 44, 实测刻度列;百分比模式刻度带 "%" 后缀)
|
|
@@ -77,9 +77,10 @@ export function renderBarStacked(spec, o) {
|
|
|
77
77
|
}
|
|
78
78
|
return out;
|
|
79
79
|
});
|
|
80
|
+
// 卡片垫底(标题压在卡片之上)
|
|
81
|
+
const bg = card(width, height);
|
|
80
82
|
const parts = [
|
|
81
|
-
|
|
82
|
-
topLegend(spec.series.map((s, i) => ({ name: s.name, color: colors[i], ...(o.interactive ? { index: i } : {}) })), width - pad, pad + 16),
|
|
83
|
+
topLegend(spec.series.map((s, i) => ({ name: s.name, color: colors[i], ...(o.interactive ? { index: i } : {}) })), width - pad, pad + 14),
|
|
83
84
|
];
|
|
84
85
|
if (pctMode) {
|
|
85
86
|
for (const t of tickVals) {
|
|
@@ -136,11 +137,14 @@ export function renderBarStacked(spec, o) {
|
|
|
136
137
|
parts.push(`<text x="${r2(x + barW / 2)}" y="${r2(bottom + 20)}" text-anchor="middle" font-size="12" fill="var(--d-muted)">${esc(trunc(label, 12))}</text>`);
|
|
137
138
|
// 百分比模式:柱顶标该组总量(补回绝对量)
|
|
138
139
|
if (pctMode) {
|
|
139
|
-
parts.push(`<text x="${r2(x + barW / 2)}" y="${r2(contentTop - 8)}" text-anchor="middle" font-size="11" font-weight="500" fill="var(--d-ink)"
|
|
140
|
+
parts.push(`<text x="${r2(x + barW / 2)}" y="${r2(contentTop - 8)}" text-anchor="middle" font-size="11" font-weight="500" fill="var(--d-ink)" style="${TAB}">${fmtNum(totals[pi])}</text>`);
|
|
140
141
|
}
|
|
141
142
|
});
|
|
142
143
|
const placer = new LabelPlacer();
|
|
143
|
-
reserveBands(placer, left, right, contentTop, bottom, bottom + 20, spec.series, width - pad, pad +
|
|
144
|
+
reserveBands(placer, left, right, contentTop, bottom, bottom + 20, spec.series, width - pad, pad + 14);
|
|
144
145
|
pushBarLabelsAllOrNothing(parts, numItems, placer);
|
|
145
|
-
|
|
146
|
+
const mpts = markPoints(o.marks, spec.labels, (i) => left + step * i + step / 2, y, [contentTop, bottom]);
|
|
147
|
+
if (mpts.length > 0)
|
|
148
|
+
parts.push(marksLayer(mpts, width));
|
|
149
|
+
return svgDoc(width, height, 'bar', version, spec.title ?? 'stacked bar chart', spec, skin, bg + titleBlock(spec.title) + parts.join(''));
|
|
146
150
|
}
|
package/dist/render/bar.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { card } from '../theme.js';
|
|
2
|
-
import { esc, fmtFull, fmtNum, niceTicks, r2, trunc, svgDoc, titleBlock, yAxisGrid, pushBarLabelsAllOrNothing, labelStride, textW, LabelPlacer, reserveBands } from './svg.js';
|
|
2
|
+
import { esc, fmtFull, fmtNum, niceTicks, r2, trunc, svgDoc, titleBlock, yAxisGrid, pushBarLabelsAllOrNothing, labelStride, textW, LabelPlacer, reserveBands, markPoints, marksLayer } from './svg.js';
|
|
3
3
|
import { yTickInset } from './measure.js';
|
|
4
4
|
/**
|
|
5
5
|
* Single-series bar chart.
|
|
@@ -37,8 +37,9 @@ export function renderBar(spec, o) {
|
|
|
37
37
|
const bottom = height - pad - 30;
|
|
38
38
|
const yMax = ticks[ticks.length - 1];
|
|
39
39
|
const y = (v) => bottom - (v / yMax) * (bottom - contentTop);
|
|
40
|
+
// 卡片垫底(标题压在卡片之上)
|
|
41
|
+
const bg = card(width, height);
|
|
40
42
|
const parts = [
|
|
41
|
-
card(width, height),
|
|
42
43
|
// 柱体渐变(顶部亮 → 底部深)
|
|
43
44
|
`<defs><linearGradient id="dg-bar" x1="0" y1="0" x2="0" y2="1">` +
|
|
44
45
|
`<stop offset="0" stop-color="var(--d-ramp-a)"/><stop offset="1" stop-color="var(--d-ramp-b)"/></linearGradient></defs>`,
|
|
@@ -74,5 +75,9 @@ export function renderBar(spec, o) {
|
|
|
74
75
|
// 预留 Y 刻度带与 X 标签带:数值标签不得压刻度
|
|
75
76
|
reserveBands(placer, left, right, contentTop, bottom, bottom + 20, [], 0, 0);
|
|
76
77
|
pushBarLabelsAllOrNothing(parts, numItems, placer);
|
|
77
|
-
|
|
78
|
+
// 标注层(独立 <g id="marks">,markhide 由入口层拦下、这里只管画)
|
|
79
|
+
const mpts = markPoints(o.marks, spec.data.map((d) => d.label), (i) => left + step * i + step / 2, y, [contentTop, bottom]);
|
|
80
|
+
if (mpts.length > 0)
|
|
81
|
+
parts.push(marksLayer(mpts, width));
|
|
82
|
+
return svgDoc(width, height, 'bar', version, spec.title ?? 'bar chart', spec, skin, bg + titleBlock(spec.title) + parts.join(''));
|
|
78
83
|
}
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import { sideSegments, sideTotals } from '../specs/butterfly.js';
|
|
2
|
+
import { TAB, card } from '../theme.js';
|
|
3
|
+
import { esc, fmtFull, niceTicks, r2, sVar, svgDoc, textW, topLegend, trunc, } from './svg.js';
|
|
4
|
+
const PAD = 24;
|
|
5
|
+
const LEGEND_H = 26;
|
|
6
|
+
const VALUE_W = 54; // 外端数值那一列的宽
|
|
7
|
+
const TRACK = 'var(--d-faint)';
|
|
8
|
+
function paintSide(side, fallback, colorIdx, unit) {
|
|
9
|
+
const segs = sideSegments(side);
|
|
10
|
+
return segs.map((sg, i) => ({
|
|
11
|
+
name: sg.name ?? (segs.length === 1 ? side?.name ?? fallback : `${fallback} ${i + 1}`),
|
|
12
|
+
color: sVar((colorIdx + i) % 10),
|
|
13
|
+
values: sg.values,
|
|
14
|
+
}));
|
|
15
|
+
}
|
|
16
|
+
export function renderButterfly(spec, o) {
|
|
17
|
+
const { version, skin } = o;
|
|
18
|
+
const n = spec.labels.length;
|
|
19
|
+
const tb = spec.dir === 'tb';
|
|
20
|
+
// 底纹轨道:空着的底纹容易被当成数据的一部分 → 默认不画(t=track / t=scale 才画)
|
|
21
|
+
const track = skin.rules.butterflyMode === 'track';
|
|
22
|
+
// 刻度线:默认不画(数值就写在柱端,再画一堆没数字的竖线只是噪声)→ grid=none
|
|
23
|
+
const scale = skin.rules.grid !== 'none';
|
|
24
|
+
const unit = spec.unit?.trim();
|
|
25
|
+
const left = paintSide(spec.left, 'Left', 0, unit);
|
|
26
|
+
const right = paintSide(spec.right, 'Right', left.length, unit);
|
|
27
|
+
const line = spec.line
|
|
28
|
+
? { name: spec.line.name ?? 'Line', values: spec.line.values, side: spec.line.side ?? 'right' }
|
|
29
|
+
: undefined;
|
|
30
|
+
const hasBars = left.length > 0 || right.length > 0;
|
|
31
|
+
const leftTot = sideTotals(spec.left, n);
|
|
32
|
+
const rightTot = sideTotals(spec.right, n);
|
|
33
|
+
const barMax = Math.max(...leftTot, ...rightTot, 1);
|
|
34
|
+
const lineMax = line ? Math.max(...line.values.map((v) => Math.abs(v)), 1) : 0;
|
|
35
|
+
let width = o.width;
|
|
36
|
+
let height = o.height;
|
|
37
|
+
const titleH = spec.title ? 34 : 0;
|
|
38
|
+
const legendItems = [];
|
|
39
|
+
// 图例:有段名就列段名(一段时用侧名),否则列侧名 —— 单位跟着名字走
|
|
40
|
+
const withUnit = (s) => (unit ? `${s} (${unit})` : s);
|
|
41
|
+
if (left.length > 1 || right.length > 1) {
|
|
42
|
+
left.forEach((p, i) => legendItems.push({ name: withUnit(p.name), color: p.color, index: i }));
|
|
43
|
+
right.forEach((p, i) => legendItems.push({ name: withUnit(p.name), color: p.color, index: left.length + i }));
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
if (left.length)
|
|
47
|
+
legendItems.push({ name: withUnit(left[0].name), color: left[0].color, index: 0 });
|
|
48
|
+
if (right.length)
|
|
49
|
+
legendItems.push({ name: withUnit(right[0].name), color: right[0].color, index: left.length ? 1 : 0 });
|
|
50
|
+
}
|
|
51
|
+
if (line)
|
|
52
|
+
legendItems.push({ name: withUnit(line.name), color: 'var(--d-s9)', index: legendItems.length });
|
|
53
|
+
const parts = [card(width, height, true)];
|
|
54
|
+
// 图例单独一行(标题下面):双翼图的图例项可能很多,和标题并排会撞在一起
|
|
55
|
+
const contentTop = PAD + titleH + (legendItems.length ? LEGEND_H : 0);
|
|
56
|
+
/* ---- 左右版:类别是行,柱自中轴往两侧长 */
|
|
57
|
+
if (!tb) {
|
|
58
|
+
// 行名列
|
|
59
|
+
// 标签上限可调(labelmax=):名字长的数据,名字本身就是信息,截了等于没说
|
|
60
|
+
const lcap = spec.labelmax ?? 22;
|
|
61
|
+
// 两侧各自带行名时(天梯图:左边 N 卡、右边 A 卡,一行两个型号),
|
|
62
|
+
// 共用那列标签就不画了 —— 各画各的外缘。
|
|
63
|
+
const leftLabels = spec.left?.labels ?? [];
|
|
64
|
+
const rightLabels = spec.right?.labels ?? [];
|
|
65
|
+
const ownLabels = leftLabels.length > 0 || rightLabels.length > 0;
|
|
66
|
+
const labelW = ownLabels
|
|
67
|
+
? 0
|
|
68
|
+
: Math.max(...spec.labels.map((l) => textW(trunc(l, lcap), 13)), 0);
|
|
69
|
+
const labelX = PAD;
|
|
70
|
+
const labelEnd = labelX + labelW;
|
|
71
|
+
const leftNameW = leftLabels.length
|
|
72
|
+
? Math.max(...leftLabels.map((l) => textW(trunc(l, lcap), 13)), 0)
|
|
73
|
+
: 0;
|
|
74
|
+
const rightNameW = rightLabels.length
|
|
75
|
+
? Math.max(...rightLabels.map((l) => textW(trunc(l, lcap), 13)), 0)
|
|
76
|
+
: 0;
|
|
77
|
+
const plotL = labelEnd + leftNameW + VALUE_W + 10;
|
|
78
|
+
const plotR = width - PAD - rightNameW - VALUE_W - 10;
|
|
79
|
+
const centre = (plotL + plotR) / 2;
|
|
80
|
+
const half = (plotR - plotL) / 2;
|
|
81
|
+
// 行高:行多时压缩
|
|
82
|
+
const avail = height - contentTop - PAD - 26;
|
|
83
|
+
const pitch = avail / Math.max(n, 1);
|
|
84
|
+
const barH = Math.min(22, Math.max(6, pitch * 0.56));
|
|
85
|
+
// 网格:中轴 + 两侧等距刻度(scale=each 时两侧各画各的刻度)
|
|
86
|
+
const each = spec.scale === 'each';
|
|
87
|
+
const leftMax = each ? Math.max(...leftTot, 1) : barMax;
|
|
88
|
+
const rightMax = each ? Math.max(...rightTot, 1) : barMax;
|
|
89
|
+
const gridFor = (max, dir) => {
|
|
90
|
+
const ticks = niceTicks(max, 4);
|
|
91
|
+
const tickMax = ticks[ticks.length - 1] || max;
|
|
92
|
+
for (const t of ticks) {
|
|
93
|
+
if (t === 0)
|
|
94
|
+
continue;
|
|
95
|
+
const dx = (t / tickMax) * half;
|
|
96
|
+
const x = dir < 0 ? centre - dx : centre + dx;
|
|
97
|
+
parts.push(`<line x1="${r2(x)}" y1="${r2(contentTop)}" x2="${r2(x)}" y2="${r2(height - PAD - 26)}" stroke="var(--d-grid)"/>`);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
if (scale) {
|
|
101
|
+
if (each) {
|
|
102
|
+
gridFor(leftMax, -1);
|
|
103
|
+
gridFor(rightMax, 1);
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
const ticks = niceTicks(barMax, 4);
|
|
107
|
+
const tickMax = ticks[ticks.length - 1] || barMax;
|
|
108
|
+
for (const t of ticks) {
|
|
109
|
+
if (t === 0)
|
|
110
|
+
continue;
|
|
111
|
+
const dx = (t / tickMax) * half;
|
|
112
|
+
for (const x of [centre - dx, centre + dx]) {
|
|
113
|
+
parts.push(`<line x1="${r2(x)}" y1="${r2(contentTop)}" x2="${r2(x)}" y2="${r2(height - PAD - 26)}" stroke="var(--d-grid)"/>`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
parts.push(`<line x1="${r2(centre)}" y1="${r2(contentTop)}" x2="${r2(centre)}" y2="${r2(height - PAD - 26)}" stroke="var(--d-border)"/>`);
|
|
119
|
+
// 没有折线时,柱端的数值直接画(没有遮挡,不需要垫底)
|
|
120
|
+
// 柱端的数值占的位置 —— 线的标签要避开它们
|
|
121
|
+
const barLabelBoxes = [];
|
|
122
|
+
const barLabels = [];
|
|
123
|
+
spec.labels.forEach((label, i) => {
|
|
124
|
+
const y = contentTop + i * pitch + (pitch - barH) / 2;
|
|
125
|
+
if (!ownLabels) {
|
|
126
|
+
parts.push(`<text x="${r2(labelX)}" y="${r2(y + barH * 0.74)}" font-size="13" fill="var(--d-ink)">${esc(trunc(label, lcap))}</text>`);
|
|
127
|
+
}
|
|
128
|
+
// 两侧自己的名字:画在这一侧的外缘(左边的右对齐,右边的左对齐)
|
|
129
|
+
const ly = leftLabels[i];
|
|
130
|
+
if (ly !== undefined) {
|
|
131
|
+
parts.push(`<text x="${r2(plotL - VALUE_W - 10)}" y="${r2(y + barH * 0.74)}" text-anchor="end" font-size="13" fill="var(--d-ink)">${esc(trunc(ly, lcap))}</text>`);
|
|
132
|
+
}
|
|
133
|
+
const ry = rightLabels[i];
|
|
134
|
+
if (ry !== undefined) {
|
|
135
|
+
parts.push(`<text x="${r2(plotR + VALUE_W + 10)}" y="${r2(y + barH * 0.74)}" font-size="13" fill="var(--d-ink)">${esc(trunc(ry, lcap))}</text>`);
|
|
136
|
+
}
|
|
137
|
+
// 两侧:轨道 + 段(自中轴往外堆叠)
|
|
138
|
+
const drawSide = (segs, totals, dir) => {
|
|
139
|
+
if (!segs.length)
|
|
140
|
+
return;
|
|
141
|
+
const sideMax = dir < 0 ? leftMax : rightMax;
|
|
142
|
+
const totalLen = (totals[i] / sideMax) * half;
|
|
143
|
+
// 轨道:整条到底(bare 风格不画 —— 底纹是空的,读者容易当成数据)
|
|
144
|
+
if (track) {
|
|
145
|
+
parts.push(`<rect x="${r2(dir < 0 ? centre - half : centre)}" y="${r2(y)}" width="${r2(half)}" height="${r2(barH)}" rx="4" fill="${TRACK}" fill-opacity="0.35"/>`);
|
|
146
|
+
}
|
|
147
|
+
let acc = 0;
|
|
148
|
+
for (const seg of segs) {
|
|
149
|
+
const len = (seg.values[i] ?? 0) / sideMax * half;
|
|
150
|
+
if (len <= 0) {
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
const x = dir < 0 ? centre - acc - len : centre + acc;
|
|
154
|
+
parts.push(`<rect x="${r2(x)}" y="${r2(y)}" width="${r2(len)}" height="${r2(barH)}" rx="4" fill="${seg.color}"/>`);
|
|
155
|
+
acc += len;
|
|
156
|
+
}
|
|
157
|
+
// 外端的数值:折线在柱之上,数值得等折线画完再画(否则被线盖住)
|
|
158
|
+
const v = totals[i];
|
|
159
|
+
// 零长度的柱不写数字:那一格什么都没有,写个 "0" 只会把读者的眼睛拉到空处
|
|
160
|
+
if (!(v > 0))
|
|
161
|
+
return;
|
|
162
|
+
const vx = dir < 0 ? centre - totalLen - 8 : centre + totalLen + 8;
|
|
163
|
+
const vw = textW(fmtFull(v), 12);
|
|
164
|
+
barLabelBoxes.push({
|
|
165
|
+
x0: dir < 0 ? vx - vw : vx, x1: dir < 0 ? vx : vx + vw,
|
|
166
|
+
y0: y + barH * 0.74 - 12, y1: y + barH * 0.74 + 3,
|
|
167
|
+
});
|
|
168
|
+
barLabels.push(`<text x="${r2(vx)}" y="${r2(y + barH * 0.74)}" text-anchor="${dir < 0 ? 'end' : 'start'}" font-size="12" font-weight="600" fill="var(--d-text)" style="${TAB}">${esc(fmtFull(v))}</text>`);
|
|
169
|
+
};
|
|
170
|
+
drawSide(left, leftTot, -1);
|
|
171
|
+
drawSide(right, rightTot, 1);
|
|
172
|
+
});
|
|
173
|
+
// 没有折线:柱端的数值直接画(不会被挡)
|
|
174
|
+
if (!line)
|
|
175
|
+
parts.push(...barLabels);
|
|
176
|
+
// 折线:在它那一侧,自己的标尺
|
|
177
|
+
if (line) {
|
|
178
|
+
// 线在自己那一侧从中轴往外长(和柱一样):0 在中轴,最大到那一侧的边
|
|
179
|
+
const dir = line.side === 'left' ? -1 : 1;
|
|
180
|
+
const base = centre;
|
|
181
|
+
const span = dir < 0 ? centre - plotL : plotR - centre;
|
|
182
|
+
const lineTicks = niceTicks(lineMax, 3);
|
|
183
|
+
const lineTop = lineTicks[lineTicks.length - 1] || lineMax;
|
|
184
|
+
const pts = line.values.map((v, i) => {
|
|
185
|
+
const y = contentTop + i * pitch + pitch / 2;
|
|
186
|
+
const d = (Math.abs(v) / lineTop) * span * 0.86;
|
|
187
|
+
return [base + dir * d, y];
|
|
188
|
+
});
|
|
189
|
+
if (pts.length > 1) {
|
|
190
|
+
parts.push(`<path d="${pts.map((p, i) => `${i === 0 ? 'M' : 'L'}${r2(p[0])} ${r2(p[1])}`).join('')}" fill="none" stroke="var(--d-s9)" stroke-width="2" stroke-linejoin="round" stroke-linecap="round"/>`);
|
|
191
|
+
pts.forEach((p) => parts.push(`<circle cx="${r2(p[0])}" cy="${r2(p[1])}" r="3" fill="var(--d-s9)" stroke="var(--d-card)" stroke-width="1.5"/>`));
|
|
192
|
+
}
|
|
193
|
+
// 柱端的数值画在折线之上 —— 数值比线重要,字底下垫一块画布色把线挡住
|
|
194
|
+
for (const t of barLabels) {
|
|
195
|
+
const m = /x="([\d.]+)" y="([\d.]+)"/.exec(t);
|
|
196
|
+
const tx = Number(m[1]);
|
|
197
|
+
const ty = Number(m[2]);
|
|
198
|
+
const anchor = /text-anchor="end"/.test(t) ? 'end' : 'start';
|
|
199
|
+
const tw = textW(t.replace(/<[^>]+>/g, ''), 12);
|
|
200
|
+
const bx = anchor === 'end' ? tx - tw - 4 : tx - 4;
|
|
201
|
+
parts.push(`<rect x="${r2(bx)}" y="${r2(ty - 12)}" width="${r2(tw + 8)}" height="15" rx="4" fill="var(--d-card)" fill-opacity="0.94"/>`, t);
|
|
202
|
+
}
|
|
203
|
+
// 线的数值:在点周围几个位置里挑一个不撞柱端数值的,并垫一块画布色
|
|
204
|
+
// (线和柱会叠,没垫就看不清)
|
|
205
|
+
const cand = [[-16, 0], [-16, -18], [-16, 18], [14, 0], [14, -18], [14, 18]];
|
|
206
|
+
line.values.forEach((v, i) => {
|
|
207
|
+
const y = contentTop + i * pitch + pitch / 2;
|
|
208
|
+
const d = (Math.abs(v) / lineTop) * span * 0.86;
|
|
209
|
+
const x = base + dir * d;
|
|
210
|
+
const text = fmtFull(v);
|
|
211
|
+
const tw = textW(text, 11);
|
|
212
|
+
let bx = x - tw / 2 - 4;
|
|
213
|
+
let by = y - 24;
|
|
214
|
+
for (const [dy, dx] of cand) {
|
|
215
|
+
const cx = x + dx - tw / 2 - 4;
|
|
216
|
+
const cy = y + dy - 11;
|
|
217
|
+
const hit = barLabelBoxes.some((b) => cx < b.x1 && cx + tw + 8 > b.x0 && cy < b.y1 && cy + 14 > b.y0);
|
|
218
|
+
if (!hit) {
|
|
219
|
+
bx = cx;
|
|
220
|
+
by = cy;
|
|
221
|
+
break;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
parts.push(`<rect x="${r2(bx)}" y="${r2(by)}" width="${r2(tw + 8)}" height="14" rx="4" fill="var(--d-card)" fill-opacity="0.92"/>`, `<text x="${r2(x + (bx + 4 + tw / 2) - x)}" y="${r2(by + 11)}" text-anchor="middle" font-size="11" font-weight="600" fill="var(--d-s9)" style="${TAB}">${esc(text)}</text>`);
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
/* ---- 上下版:类别是列,柱自中轴往上/往下长 */
|
|
230
|
+
const labelH = 22;
|
|
231
|
+
// 往下长的柱子,数值写在柱底外侧 → 底下要给数值留一条带,
|
|
232
|
+
// 否则最长的那根柱(数值到头)会和类目标签叠在一起
|
|
233
|
+
const valueBand = 18;
|
|
234
|
+
const plotTop = contentTop + VALUE_W;
|
|
235
|
+
const plotBottom = height - PAD - labelH - valueBand;
|
|
236
|
+
const centreY = (plotTop + plotBottom) / 2;
|
|
237
|
+
const half = (plotBottom - plotTop) / 2;
|
|
238
|
+
const slotW = (width - PAD * 2) / Math.max(n, 1);
|
|
239
|
+
const barW = Math.min(34, Math.max(6, slotW * 0.5));
|
|
240
|
+
const eachTb = spec.scale === 'each';
|
|
241
|
+
const leftMax = eachTb ? Math.max(...leftTot, 1) : barMax;
|
|
242
|
+
const rightMax = eachTb ? Math.max(...rightTot, 1) : barMax;
|
|
243
|
+
const gridTb = (max, dir) => {
|
|
244
|
+
const ticks = niceTicks(max, 4);
|
|
245
|
+
const tickMax = ticks[ticks.length - 1] || max;
|
|
246
|
+
for (const t of ticks) {
|
|
247
|
+
if (t === 0)
|
|
248
|
+
continue;
|
|
249
|
+
const dy = (t / tickMax) * half;
|
|
250
|
+
const y = dir < 0 ? centreY - dy : centreY + dy;
|
|
251
|
+
parts.push(`<line x1="${r2(PAD)}" y1="${r2(y)}" x2="${r2(width - PAD)}" y2="${r2(y)}" stroke="var(--d-grid)"/>`);
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
if (scale) {
|
|
255
|
+
if (eachTb) {
|
|
256
|
+
gridTb(leftMax, -1);
|
|
257
|
+
gridTb(rightMax, 1);
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
const ticks = niceTicks(barMax, 4);
|
|
261
|
+
const tickMax = ticks[ticks.length - 1] || barMax;
|
|
262
|
+
for (const t of ticks) {
|
|
263
|
+
if (t === 0)
|
|
264
|
+
continue;
|
|
265
|
+
const dy = (t / tickMax) * half;
|
|
266
|
+
for (const y of [centreY - dy, centreY + dy]) {
|
|
267
|
+
parts.push(`<line x1="${r2(PAD)}" y1="${r2(y)}" x2="${r2(width - PAD)}" y2="${r2(y)}" stroke="var(--d-grid)"/>`);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
parts.push(`<line x1="${r2(PAD)}" y1="${r2(centreY)}" x2="${r2(width - PAD)}" y2="${r2(centreY)}" stroke="var(--d-border)"/>`);
|
|
273
|
+
const tbLabels = [];
|
|
274
|
+
spec.labels.forEach((label, i) => {
|
|
275
|
+
const cx = PAD + slotW * i + slotW / 2;
|
|
276
|
+
const x = cx - barW / 2;
|
|
277
|
+
parts.push(`<text x="${r2(cx)}" y="${r2(height - PAD - 4)}" text-anchor="middle" font-size="12" fill="var(--d-ink)">${esc(trunc(label, 14))}</text>`);
|
|
278
|
+
const drawSide = (segs, totals, dir) => {
|
|
279
|
+
if (!segs.length)
|
|
280
|
+
return;
|
|
281
|
+
const sideMax = dir < 0 ? leftMax : rightMax;
|
|
282
|
+
const totalLen = (totals[i] / sideMax) * half;
|
|
283
|
+
if (track) {
|
|
284
|
+
parts.push(`<rect x="${r2(x)}" y="${r2(dir < 0 ? centreY - half : centreY)}" width="${r2(barW)}" height="${r2(half)}" rx="4" fill="${TRACK}" fill-opacity="0.35"/>`);
|
|
285
|
+
}
|
|
286
|
+
let acc = 0;
|
|
287
|
+
for (const seg of segs) {
|
|
288
|
+
const len = (seg.values[i] ?? 0) / sideMax * half;
|
|
289
|
+
if (len <= 0)
|
|
290
|
+
continue;
|
|
291
|
+
const y = dir < 0 ? centreY - acc - len : centreY + acc;
|
|
292
|
+
parts.push(`<rect x="${r2(x)}" y="${r2(y)}" width="${r2(barW)}" height="${r2(len)}" rx="4" fill="${seg.color}"/>`);
|
|
293
|
+
acc += len;
|
|
294
|
+
}
|
|
295
|
+
const v = totals[i];
|
|
296
|
+
// 零长度的柱不写数字(同上)
|
|
297
|
+
if (!(v > 0))
|
|
298
|
+
return;
|
|
299
|
+
const vy = dir < 0 ? centreY - totalLen - 6 : centreY + totalLen + 6;
|
|
300
|
+
tbLabels.push(`<text x="${r2(cx)}" y="${r2(vy + (dir < 0 ? 0 : 10))}" text-anchor="middle" font-size="12" font-weight="600" fill="var(--d-text)" style="${TAB}">${esc(fmtFull(v))}</text>`);
|
|
301
|
+
};
|
|
302
|
+
drawSide(left, leftTot, -1);
|
|
303
|
+
drawSide(right, rightTot, 1);
|
|
304
|
+
});
|
|
305
|
+
// 折线:在类别之间画一条曲线(自己的标尺),画在柱之上
|
|
306
|
+
if (line) {
|
|
307
|
+
const lineTop = niceTicks(Math.max(...line.values.map((v) => Math.abs(v)), 1), 3).slice(-1)[0] || 1;
|
|
308
|
+
const dirUp = (line.side ?? 'left') === 'left';
|
|
309
|
+
const pts = line.values.map((v, i) => {
|
|
310
|
+
const cx = PAD + slotW * i + slotW / 2;
|
|
311
|
+
const d = (Math.abs(v) / lineTop) * half * 0.86;
|
|
312
|
+
return [cx, dirUp ? centreY - d : centreY + d];
|
|
313
|
+
});
|
|
314
|
+
if (pts.length > 1) {
|
|
315
|
+
parts.push(`<path d="${pts.map((p, i) => `${i === 0 ? 'M' : 'L'}${r2(p[0])} ${r2(p[1])}`).join('')}" fill="none" stroke="var(--d-s9)" stroke-width="2" stroke-linejoin="round" stroke-linecap="round"/>`);
|
|
316
|
+
pts.forEach((p) => parts.push(`<circle cx="${r2(p[0])}" cy="${r2(p[1])}" r="3" fill="var(--d-s9)" stroke="var(--d-card)" stroke-width="1.5"/>`));
|
|
317
|
+
line.values.forEach((v, i) => {
|
|
318
|
+
const text = fmtFull(v);
|
|
319
|
+
const tw = textW(text, 11);
|
|
320
|
+
parts.push(`<rect x="${r2(pts[i][0] - tw / 2 - 4)}" y="${r2(pts[i][1] - 24)}" width="${r2(tw + 8)}" height="14" rx="4" fill="var(--d-card)" fill-opacity="0.92"/>`, `<text x="${r2(pts[i][0])}" y="${r2(pts[i][1] - 14)}" text-anchor="middle" font-size="11" font-weight="600" fill="var(--d-s9)" style="${TAB}">${esc(text)}</text>`);
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
// 柱端的数值画在折线之上
|
|
324
|
+
for (const t of tbLabels) {
|
|
325
|
+
const m = /x="([\d.]+)" y="([\d.]+)"/.exec(t);
|
|
326
|
+
const tw = textW(t.replace(/<[^>]+>/g, ''), 12);
|
|
327
|
+
parts.push(`<rect x="${r2(Number(m[1]) - tw / 2 - 4)}" y="${r2(Number(m[2]) - 12)}" width="${r2(tw + 8)}" height="15" rx="4" fill="var(--d-card)" fill-opacity="0.94"/>`, t);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
else {
|
|
331
|
+
parts.push(...tbLabels);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
// 标题 + 图例
|
|
335
|
+
const titleSvg = spec.title
|
|
336
|
+
? `<text x="${PAD}" y="${PAD + 14}" font-size="18" font-weight="500" letter-spacing="-0.02em" fill="var(--d-ink)">${esc(spec.title)}</text>`
|
|
337
|
+
: '';
|
|
338
|
+
const legendSvg = legendItems.length
|
|
339
|
+
? topLegend(legendItems, PAD, PAD + titleH + 12, 'left', 'square')
|
|
340
|
+
: '';
|
|
341
|
+
// metadata 带全量 spec —— AI 读它能把图原样重建(角标的 explore 链接也靠它)
|
|
342
|
+
const meta = {
|
|
343
|
+
type: 'butterfly',
|
|
344
|
+
title: spec.title,
|
|
345
|
+
unit: spec.unit,
|
|
346
|
+
dir: spec.dir,
|
|
347
|
+
...(spec.scale ? { scale: spec.scale } : {}),
|
|
348
|
+
labels: spec.labels,
|
|
349
|
+
...(spec.left ? { left: spec.left } : {}),
|
|
350
|
+
...(spec.right ? { right: spec.right } : {}),
|
|
351
|
+
...(spec.line ? { line: spec.line } : {}),
|
|
352
|
+
};
|
|
353
|
+
const svg = svgDoc(width, height, 'butterfly', version, spec.title ?? `butterfly chart, ${n} categories`, meta, skin, titleSvg + legendSvg + parts.join(''));
|
|
354
|
+
return svg;
|
|
355
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { CalendarSpec } from '../specs/calendar.js';
|
|
2
|
+
import { type RenderOpts } from './svg.js';
|
|
3
|
+
/**
|
|
4
|
+
* Render a calendar (month / week / year / bi-month / quarter / course / plan).
|
|
5
|
+
*/
|
|
6
|
+
export declare function renderCalendar(spec: CalendarSpec, o: RenderOpts): string;
|