@x-cmd/draw 0.0.0-stage → 0.2.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/LICENSE +228 -0
- package/README.cn.md +235 -0
- package/README.md +235 -2
- package/dist/lib.d.ts +25 -0
- package/dist/lib.js +50 -0
- package/dist/node/cli.d.ts +2 -0
- package/dist/node/cli.js +79 -0
- package/dist/render/area.d.ts +8 -0
- package/dist/render/area.js +414 -0
- package/dist/render/bar-grid.d.ts +3 -0
- package/dist/render/bar-grid.js +52 -0
- package/dist/render/bar-grouped.d.ts +3 -0
- package/dist/render/bar-grouped.js +131 -0
- package/dist/render/bar-stacked.d.ts +3 -0
- package/dist/render/bar-stacked.js +142 -0
- package/dist/render/bar.d.ts +3 -0
- package/dist/render/bar.js +74 -0
- package/dist/render/combo.d.ts +3 -0
- package/dist/render/combo.js +256 -0
- package/dist/render/dispatch.d.ts +3 -0
- package/dist/render/dispatch.js +69 -0
- package/dist/render/gauge.d.ts +3 -0
- package/dist/render/gauge.js +216 -0
- package/dist/render/hbar-spec.d.ts +7 -0
- package/dist/render/hbar-spec.js +265 -0
- package/dist/render/hbar.d.ts +3 -0
- package/dist/render/hbar.js +55 -0
- package/dist/render/line-multi.d.ts +3 -0
- package/dist/render/line-multi.js +89 -0
- package/dist/render/line.d.ts +11 -0
- package/dist/render/line.js +123 -0
- package/dist/render/measure.d.ts +60 -0
- package/dist/render/measure.js +75 -0
- package/dist/render/pie.d.ts +5 -0
- package/dist/render/pie.js +439 -0
- package/dist/render/qr.d.ts +8 -0
- package/dist/render/qr.js +52 -0
- package/dist/render/query.d.ts +14 -0
- package/dist/render/query.js +220 -0
- package/dist/render/radar.d.ts +5 -0
- package/dist/render/radar.js +221 -0
- package/dist/render/sankey.d.ts +3 -0
- package/dist/render/sankey.js +280 -0
- package/dist/render/scatter.d.ts +3 -0
- package/dist/render/scatter.js +98 -0
- package/dist/render/svg.d.ts +93 -0
- package/dist/render/svg.js +471 -0
- package/dist/specs/base.d.ts +34 -0
- package/dist/specs/base.js +159 -0
- package/dist/specs/combo.d.ts +15 -0
- package/dist/specs/combo.js +135 -0
- package/dist/specs/csv.d.ts +1 -0
- package/dist/specs/csv.js +40 -0
- package/dist/specs/gauge.d.ts +15 -0
- package/dist/specs/gauge.js +108 -0
- package/dist/specs/index.d.ts +8 -0
- package/dist/specs/index.js +9 -0
- package/dist/specs/input.d.ts +11 -0
- package/dist/specs/input.js +33 -0
- package/dist/specs/multi.d.ts +12 -0
- package/dist/specs/multi.js +112 -0
- package/dist/specs/sankey.d.ts +13 -0
- package/dist/specs/sankey.js +112 -0
- package/dist/specs/scatter.d.ts +20 -0
- package/dist/specs/scatter.js +121 -0
- package/dist/store.d.ts +15 -0
- package/dist/store.js +121 -0
- package/dist/styles/area.d.ts +2 -0
- package/dist/styles/area.js +106 -0
- package/dist/styles/bar.d.ts +2 -0
- package/dist/styles/bar.js +52 -0
- package/dist/styles/combo.d.ts +2 -0
- package/dist/styles/combo.js +37 -0
- package/dist/styles/common.d.ts +34 -0
- package/dist/styles/common.js +259 -0
- package/dist/styles/gauge.d.ts +2 -0
- package/dist/styles/gauge.js +51 -0
- package/dist/styles/handlee.d.ts +1 -0
- package/dist/styles/handlee.js +2 -0
- package/dist/styles/hbar.d.ts +2 -0
- package/dist/styles/hbar.js +66 -0
- package/dist/styles/index.d.ts +7 -0
- package/dist/styles/index.js +79 -0
- package/dist/styles/inter.d.ts +3 -0
- package/dist/styles/inter.js +10 -0
- package/dist/styles/line.d.ts +2 -0
- package/dist/styles/line.js +97 -0
- package/dist/styles/pie.d.ts +2 -0
- package/dist/styles/pie.js +163 -0
- package/dist/styles/qr.d.ts +2 -0
- package/dist/styles/qr.js +12 -0
- package/dist/styles/radar.d.ts +2 -0
- package/dist/styles/radar.js +50 -0
- package/dist/styles/sankey.d.ts +2 -0
- package/dist/styles/sankey.js +29 -0
- package/dist/styles/scatter.d.ts +2 -0
- package/dist/styles/scatter.js +21 -0
- package/dist/styles/types.d.ts +105 -0
- package/dist/styles/types.js +70 -0
- package/dist/theme.d.ts +3 -0
- package/dist/theme.js +11 -0
- package/dist/types.d.ts +17 -0
- package/dist/types.js +1 -0
- package/dist/version.d.ts +6 -0
- package/dist/version.js +16 -0
- package/package.json +53 -4
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
// 面积图:行为由皮肤规则驱动(规范 面积图 1-3)。
|
|
2
|
+
import { ApiError } from '../specs/index.js';
|
|
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';
|
|
5
|
+
import { xOverhang, yTickInset } from './measure.js';
|
|
6
|
+
import { HANDEE_WOFF2 } from '../styles/handlee.js';
|
|
7
|
+
const SKETCH_FONT = "Handlee,'Comic Sans MS','Chalkboard SE','Comic Neue','Segoe Print',cursive";
|
|
8
|
+
// 规范 area-1 实测几何(960×520);留白 = max(规范比例, 实测)
|
|
9
|
+
function areaGeo(w, h, hasTitle, m = {}) {
|
|
10
|
+
const halfX = xOverhang(m);
|
|
11
|
+
return {
|
|
12
|
+
left: Math.round(Math.max(w * 0.0667, halfX, yTickInset(m))),
|
|
13
|
+
right: w - Math.round(Math.max(w * 0.0625, halfX)),
|
|
14
|
+
top: Math.round(h * (hasTitle ? 0.2 : 0.173)),
|
|
15
|
+
bottom: Math.round(h * 0.8077),
|
|
16
|
+
xLabelY: Math.round(h * 0.896),
|
|
17
|
+
legendY: Math.round(h * 0.092),
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
// 规范 area-3 实测几何(766×500)
|
|
21
|
+
function opendataGeo(w, h, m = {}) {
|
|
22
|
+
const halfX = xOverhang(m);
|
|
23
|
+
return {
|
|
24
|
+
left: Math.round(Math.max(w * 0.064, halfX, yTickInset(m))),
|
|
25
|
+
right: w - Math.round(Math.max(w * 0.034, halfX)),
|
|
26
|
+
top: Math.round(h * 0.268),
|
|
27
|
+
bottom: Math.round(h * 0.8096),
|
|
28
|
+
xLabelY: Math.round(h * 0.852),
|
|
29
|
+
legendY: Math.round(h * 0.092),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
// 单/多系列共用布局
|
|
33
|
+
function areaLayout(spec, o, m = {}) {
|
|
34
|
+
const { width, height, skin } = o;
|
|
35
|
+
const rules = skin.rules;
|
|
36
|
+
const pad = 24;
|
|
37
|
+
let headSvg = '';
|
|
38
|
+
let headH = 0;
|
|
39
|
+
if (rules.title === 'editorial' && spec.title) {
|
|
40
|
+
const t = editorialTitle(spec.title, width, pad);
|
|
41
|
+
// 编辑式副标题(规范 area-3:16px 灰,随末行标题 +29)
|
|
42
|
+
headSvg = t.svg + (spec.subtitle
|
|
43
|
+
? `<text x="${pad}" y="${r2(pad + t.height + 18.5)}" font-size="16" fill="var(--d-muted)">${esc(spec.subtitle)}</text>`
|
|
44
|
+
: '');
|
|
45
|
+
headH = t.height + 6 + (spec.subtitle ? 26 : 0);
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
headSvg = titleBlock(spec.title, pad);
|
|
49
|
+
if (spec.title)
|
|
50
|
+
headH = 24;
|
|
51
|
+
}
|
|
52
|
+
const hasLegendRow = rules.legend.startsWith('top');
|
|
53
|
+
// plain 画布用规范几何,card 变体用内边距布局
|
|
54
|
+
const geo = rules.canvas === 'plain' && !rules.sketch
|
|
55
|
+
? (rules.title === 'editorial' ? opendataGeo(width, height, m) : areaGeo(width, height, !!spec.title, m))
|
|
56
|
+
: null;
|
|
57
|
+
const left = geo ? geo.left : pad + (rules.axisInline ? 42 : 48);
|
|
58
|
+
return {
|
|
59
|
+
headSvg,
|
|
60
|
+
contentTop: geo ? Math.max(geo.top, pad + headH + (hasLegendRow ? 26 : 14)) : pad + headH + (hasLegendRow ? 36 : 20),
|
|
61
|
+
left,
|
|
62
|
+
right: geo ? geo.right : width - pad,
|
|
63
|
+
bottom: geo ? geo.bottom : height - pad - 30,
|
|
64
|
+
xLabelY: geo ? geo.xLabelY : height - pad - 30 + (rules.baseline === 'dots' ? 34 : 20),
|
|
65
|
+
legendY: geo ? geo.legendY : pad + 16,
|
|
66
|
+
// area-3:数据点自 left 内缩(时间轴留白)
|
|
67
|
+
xFirst: rules.title === 'editorial' && geo ? left + Math.round(width * 0.0574) : left,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
export function renderArea(spec, o) {
|
|
71
|
+
const { width, height, version, skin } = o;
|
|
72
|
+
const rules = skin.rules;
|
|
73
|
+
if (rules.sketch)
|
|
74
|
+
return renderSketchArea(spec, o);
|
|
75
|
+
const values = spec.data.map((d) => d.value);
|
|
76
|
+
const max = Math.max(...values);
|
|
77
|
+
let peakIdx = 0;
|
|
78
|
+
values.forEach((v, i) => {
|
|
79
|
+
if (v > values[peakIdx])
|
|
80
|
+
peakIdx = i;
|
|
81
|
+
});
|
|
82
|
+
const vmin = Math.min(...values);
|
|
83
|
+
// Y 轴区间:默认从 0;area-3 用数据区间外扩一格
|
|
84
|
+
let ticks;
|
|
85
|
+
if (rules.yFromZero) {
|
|
86
|
+
ticks = niceTicks(max, 4);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
const step = niceStep((max - vmin) / 6);
|
|
90
|
+
if (!(step > 0)) {
|
|
91
|
+
// 常量数据回退 0 基准,避免 NaN
|
|
92
|
+
ticks = niceTicks(max, 4);
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
const lo = Math.floor(vmin / step) * step;
|
|
96
|
+
// area-3:顶刻度取数据下方最近格点,峰值允许微出上格
|
|
97
|
+
let hi = Math.floor(max / step) * step;
|
|
98
|
+
if (hi < lo + step)
|
|
99
|
+
hi = lo + step * 6;
|
|
100
|
+
ticks = [];
|
|
101
|
+
for (let v = lo; v <= hi + 1e-9; v += step)
|
|
102
|
+
ticks.push(snap(v, step));
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const { headSvg, contentTop, left, right, bottom, xLabelY, xFirst } = areaLayout(spec, o, { xLabels: spec.data.map((d) => d.label), xFs: rules.tickFont, ...(rules.axisInline ? {} : { ticks, tickFs: rules.tickFont }) });
|
|
106
|
+
const yMin = ticks[0];
|
|
107
|
+
const yMax = ticks[ticks.length - 1];
|
|
108
|
+
const y = (v) => bottom - ((v - yMin) / (yMax - yMin)) * (bottom - contentTop);
|
|
109
|
+
const n = spec.data.length;
|
|
110
|
+
const xs = (i) => (n === 1 ? (left + right) / 2 : xFirst + ((right - xFirst) * i) / (n - 1));
|
|
111
|
+
const pts = spec.data.map((d, i) => [xs(i), y(d.value)]);
|
|
112
|
+
const path = rules.smooth ? smoothPath(pts) : `M${pts.map((p) => `${r2(p[0])} ${r2(p[1])}`).join('L')}`;
|
|
113
|
+
const parts = [card(width, height, rules.canvas === 'plain')];
|
|
114
|
+
parts.push(`<defs><linearGradient id="dg-area" x1="0" y1="0" x2="0" y2="1">` +
|
|
115
|
+
`<stop offset="0" stop-color="var(--d-s1)" stop-opacity="${rules.areaTop}"/>` +
|
|
116
|
+
`<stop offset="1" stop-color="var(--d-s1)" stop-opacity="${rules.areaBottom}"/></linearGradient></defs>`);
|
|
117
|
+
yAxisGrid(parts, ticks, y, left, right, bottom, rules.grid, rules.gridDash, rules.axisInline, rules.baseline, pts.map((p) => p[0]), rules.tickFont);
|
|
118
|
+
if (rules.axisTitle)
|
|
119
|
+
parts.push(axisTitleY(spec.unit, (contentTop + bottom) / 2, left - (rules.axisInline ? 17 : 26)));
|
|
120
|
+
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
|
+
const dash = rules.lineDash ? ` stroke-dasharray="${rules.lineDash}"` : '';
|
|
122
|
+
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);
|
|
124
|
+
const skip = n > 14 ? Math.ceil(n / 7) : 1;
|
|
125
|
+
spec.data.forEach((d, i) => {
|
|
126
|
+
if (i % skip !== 0 && i !== n - 1)
|
|
127
|
+
return;
|
|
128
|
+
parts.push(`<text x="${r2(xs(i))}" y="${r2(xLabelY)}" text-anchor="middle" font-size="${rules.tickFont}" fill="var(--d-muted)">${esc(trunc(d.label, 12))}</text>`);
|
|
129
|
+
});
|
|
130
|
+
// 数值标签:全有或全无;规范变体默认不标
|
|
131
|
+
const num = o.valueLabels ?? rules.num;
|
|
132
|
+
if (num !== 'none') {
|
|
133
|
+
const items = pts
|
|
134
|
+
.map((p, i) => ({ px: p[0], py: p[1], text: fmtNum(spec.data[i].value), isPeak: i === peakIdx }))
|
|
135
|
+
.filter((_, i) => num === 'all' || i === peakIdx);
|
|
136
|
+
const placer = new LabelPlacer();
|
|
137
|
+
// 预留刻度带/标签带
|
|
138
|
+
reserveBands(placer, left, right, contentTop, bottom, xLabelY, [], 0, 0);
|
|
139
|
+
pushLineLabelsAllOrNothing(parts, placer, items, toSegments(pts));
|
|
140
|
+
}
|
|
141
|
+
// area-3 编辑元素:均值参考线 + 末点标注 + 来源脚注
|
|
142
|
+
if (rules.refAvg) {
|
|
143
|
+
const mean = values.reduce((a, b) => a + b, 0) / values.length;
|
|
144
|
+
const my = y(mean);
|
|
145
|
+
parts.push(`<line x1="${left}" y1="${r2(my)}" x2="${right}" y2="${r2(my)}" stroke="${sVar(2)}" stroke-width="1" stroke-dasharray="4 3"/>`);
|
|
146
|
+
parts.push(`<text x="${r2(right)}" y="${r2(my - 6)}" text-anchor="end" font-size="11" fill="${sVar(2)}" ` +
|
|
147
|
+
`paint-order="stroke" stroke="var(--d-halo)" stroke-width="3" stroke-linejoin="round">Avg: ${fmtNum(mean)}</text>`);
|
|
148
|
+
}
|
|
149
|
+
if (rules.endValue) {
|
|
150
|
+
const [ex, ey] = pts[n - 1];
|
|
151
|
+
parts.push(`<circle cx="${r2(ex)}" cy="${r2(ey)}" r="4" fill="var(--d-halo)" stroke="var(--d-faint)" stroke-width="1.5"/>`);
|
|
152
|
+
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
|
+
`paint-order="stroke" stroke="var(--d-halo)" stroke-width="3" stroke-linejoin="round" style="${TAB}">${fmtFull(spec.data[n - 1].value)}</text>`);
|
|
154
|
+
}
|
|
155
|
+
if (spec.source) {
|
|
156
|
+
parts.push(`<text x="24" y="${r2(Math.round(height * 0.904))}" font-size="13" fill="var(--d-muted)">${esc(spec.source)}</text>`);
|
|
157
|
+
}
|
|
158
|
+
return svgDoc(width, height, 'area', version, spec.title ?? 'area chart', spec, skin, headSvg + parts.join(''));
|
|
159
|
+
}
|
|
160
|
+
// 多系列面积(area-1):三遍绘制(面积 → 虚线描边 → 点)
|
|
161
|
+
export function renderAreaMulti(spec, o) {
|
|
162
|
+
const { width, height, version, skin } = o;
|
|
163
|
+
const rules = skin.rules;
|
|
164
|
+
const n = spec.labels.length;
|
|
165
|
+
const gMax = Math.max(...spec.series.flatMap((s) => s.values));
|
|
166
|
+
const [peakS, peakP] = findPeak(spec.series);
|
|
167
|
+
const ticks = niceTicks(gMax, 4);
|
|
168
|
+
const { headSvg, contentTop, left, right, bottom, xLabelY, legendY, xFirst } = areaLayout(spec, o, { xLabels: spec.labels, xFs: rules.tickFont, ...(rules.axisInline ? {} : { ticks, tickFs: rules.tickFont }) });
|
|
169
|
+
const yMax = ticks[ticks.length - 1];
|
|
170
|
+
const y = (v) => bottom - (v / yMax) * (bottom - contentTop);
|
|
171
|
+
const xs = (i) => (n === 1 ? (left + right) / 2 : xFirst + ((right - xFirst) * i) / (n - 1));
|
|
172
|
+
const colors = spec.series.map((_, i) => sVar(i % skin.series.length));
|
|
173
|
+
const allPts = spec.series.map((s) => s.values.map((v, i) => [xs(i), y(v)]));
|
|
174
|
+
const pathOf = (pts) => rules.smooth ? smoothPath(pts) : `M${pts.map((p) => `${r2(p[0])} ${r2(p[1])}`).join('L')}`;
|
|
175
|
+
const parts = [card(width, height, rules.canvas === 'plain')];
|
|
176
|
+
parts.push(`<defs>${spec.series.map((_, i) => `<linearGradient id="dg-area-${i}" x1="0" y1="0" x2="0" y2="1">` +
|
|
177
|
+
`<stop offset="0" stop-color="${colors[i]}" stop-opacity="${rules.areaTop}"/>` +
|
|
178
|
+
`<stop offset="1" stop-color="${colors[i]}" stop-opacity="${rules.areaBottom}"/></linearGradient>`).join('')}</defs>`);
|
|
179
|
+
if (rules.legend === 'top-left') {
|
|
180
|
+
parts.push(topLegend(spec.series.map((s, i) => ({ name: s.name, color: colors[i] })), left, legendY, 'left', rules.legendShape));
|
|
181
|
+
}
|
|
182
|
+
else if (rules.legend === 'top-right') {
|
|
183
|
+
parts.push(topLegend(spec.series.map((s, i) => ({ name: s.name, color: colors[i] })), right, legendY, 'right', rules.legendShape));
|
|
184
|
+
}
|
|
185
|
+
yAxisGrid(parts, ticks, y, left, right, bottom, rules.grid, rules.gridDash, rules.axisInline, rules.baseline, spec.labels.map((_, i) => xs(i)), rules.tickFont);
|
|
186
|
+
if (rules.axisTitle)
|
|
187
|
+
parts.push(axisTitleY(spec.unit, (contentTop + bottom) / 2, left - (rules.axisInline ? 17 : 26)));
|
|
188
|
+
allPts.forEach((pts, i) => {
|
|
189
|
+
parts.push(`<path d="${pathOf(pts)}L${r2(pts[n - 1][0])} ${r2(bottom)}L${r2(pts[0][0])} ${r2(bottom)}Z" fill="url(#dg-area-${i})"/>`);
|
|
190
|
+
});
|
|
191
|
+
const dash = rules.lineDash ? ` stroke-dasharray="${rules.lineDash}"` : '';
|
|
192
|
+
allPts.forEach((pts, i) => {
|
|
193
|
+
parts.push(`<path d="${pathOf(pts)}" fill="none" stroke="${colors[i]}" stroke-width="${rules.lineWidth}" stroke-linecap="round" stroke-linejoin="round"${dash}/>`);
|
|
194
|
+
});
|
|
195
|
+
allPts.forEach((pts, i) => pushPoints(parts, pts, colors[i], rules));
|
|
196
|
+
const skip = n > 14 ? Math.ceil(n / 7) : 1;
|
|
197
|
+
spec.labels.forEach((l, i) => {
|
|
198
|
+
if (i % skip !== 0 && i !== n - 1)
|
|
199
|
+
return;
|
|
200
|
+
parts.push(`<text x="${r2(xs(i))}" y="${r2(xLabelY)}" text-anchor="middle" font-size="${rules.tickFont}" fill="var(--d-muted)">${esc(trunc(l, 12))}</text>`);
|
|
201
|
+
});
|
|
202
|
+
const num = o.valueLabels ?? rules.num;
|
|
203
|
+
if (num !== 'none') {
|
|
204
|
+
const items = [];
|
|
205
|
+
spec.series.forEach((s, si) => {
|
|
206
|
+
s.values.forEach((v, pi) => {
|
|
207
|
+
const isPeak = si === peakS && pi === peakP;
|
|
208
|
+
if (num === 'peak' && !isPeak)
|
|
209
|
+
return;
|
|
210
|
+
items.push({ px: allPts[si][pi][0], py: allPts[si][pi][1], text: fmtNum(v), isPeak });
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
// 数值标签:全有或无;预留刻度带
|
|
214
|
+
if (items.length > 0) {
|
|
215
|
+
const placer = new LabelPlacer();
|
|
216
|
+
reserveBands(placer, left, right, contentTop, bottom, xLabelY, [], 0, 0);
|
|
217
|
+
pushLineLabelsAllOrNothing(parts, placer, items, allPts.flatMap((p) => toSegments(p)));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return svgDoc(width, height, 'area', version, spec.title ?? 'area chart', spec, skin, headSvg + parts.join(''));
|
|
221
|
+
}
|
|
222
|
+
// 手绘风面积图(area-2):xkcd 滤镜 + Handlee 手写字体 + 端点标注
|
|
223
|
+
function renderSketchArea(spec, o) {
|
|
224
|
+
const { width, height, version, skin } = o;
|
|
225
|
+
const values = spec.data.map((d) => d.value);
|
|
226
|
+
const max = Math.max(...values);
|
|
227
|
+
// 几何取自规范 area-2(800x533)实测,按画布等比
|
|
228
|
+
const left = Math.round(width * 0.1125);
|
|
229
|
+
const right = width - Math.round(width * 0.044);
|
|
230
|
+
const bottom = Math.round(height * 0.868);
|
|
231
|
+
const top = Math.round(height * (spec.title ? 0.16 : 0.15));
|
|
232
|
+
const xLabelY = Math.round(height * 0.915);
|
|
233
|
+
const step = niceStep(max / 4);
|
|
234
|
+
const yMax = Math.ceil(max / step) * step;
|
|
235
|
+
const y = (v) => bottom - (v / yMax) * (bottom - top);
|
|
236
|
+
const n = spec.data.length;
|
|
237
|
+
const xs = (i) => (n === 1 ? (left + right) / 2 : left + ((right - left) * i) / (n - 1));
|
|
238
|
+
const pts = spec.data.map((d, i) => [xs(i), y(d.value)]);
|
|
239
|
+
const path = smoothPath(pts);
|
|
240
|
+
const F = 'filter="url(#xkcd)"';
|
|
241
|
+
const parts = [
|
|
242
|
+
card(width, height, false),
|
|
243
|
+
`<defs>` +
|
|
244
|
+
`<style>@font-face{font-family:"Handlee";src:url(${HANDEE_WOFF2}) format("woff2")}</style>` +
|
|
245
|
+
`<filter id="xkcd" width="100%" height="100%" x="-5%" y="-5%" filterUnits="userSpaceOnUse">` +
|
|
246
|
+
`<feTurbulence baseFrequency=".05" result="noise" type="fractalNoise"/>` +
|
|
247
|
+
`<feDisplacementMap in="SourceGraphic" in2="noise" scale="3" xChannelSelector="R" yChannelSelector="G"/></filter>` +
|
|
248
|
+
`<linearGradient id="dg-area" x1="0" y1="0" x2="0" y2="1">` +
|
|
249
|
+
`<stop offset="0%" stop-color="var(--d-s1)" stop-opacity="0.22"/>` +
|
|
250
|
+
`<stop offset="100%" stop-color="var(--d-s1)" stop-opacity="0.02"/></linearGradient></defs>`,
|
|
251
|
+
`<style>text{font-family:${SKETCH_FONT.replace(/"/g, '"')}}</style>`,
|
|
252
|
+
];
|
|
253
|
+
if (spec.title) {
|
|
254
|
+
parts.push(`<text x="${width / 2}" y="${r2(Math.round(height * 0.056))}" text-anchor="middle" font-size="20" font-weight="bold" fill="var(--d-ink)" ${F}>${esc(spec.title)}</text>`);
|
|
255
|
+
}
|
|
256
|
+
for (let t = step; t <= yMax; t += step) {
|
|
257
|
+
parts.push(`<line x1="${left}" y1="${r2(y(t))}" x2="${right}" y2="${r2(y(t))}" stroke="var(--d-grid)" stroke-width="1" ${F}/>`);
|
|
258
|
+
parts.push(`<text x="${left - 8}" y="${r2(y(t) + 5)}" text-anchor="end" font-size="16" font-weight="bold" fill="var(--d-ink)">${fmtNum(t)}</text>`);
|
|
259
|
+
}
|
|
260
|
+
parts.push(`<text x="0" y="${r2((top + bottom) / 2)}" text-anchor="middle" font-size="17" font-weight="bold" fill="var(--d-ink)" transform="rotate(-90, 16, ${r2((top + bottom) / 2)})">${esc(spec.unit ?? '')}</text>`);
|
|
261
|
+
// 黑色粗坐标轴(L 形,手绘抖动)
|
|
262
|
+
parts.push(`<line x1="${left}" y1="${top - 6}" x2="${left}" y2="${bottom}" stroke="var(--d-baseline)" stroke-width="2.5" ${F}/>`);
|
|
263
|
+
parts.push(`<line x1="${left}" y1="${bottom}" x2="${right}" y2="${bottom}" stroke="var(--d-baseline)" stroke-width="2.5" ${F}/>`);
|
|
264
|
+
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)"/>`);
|
|
265
|
+
parts.push(`<path d="${path}" fill="none" stroke="var(--d-s1)" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" ${F}/>`);
|
|
266
|
+
const [ex, ey] = pts[n - 1];
|
|
267
|
+
parts.push(`<circle cx="${r2(ex)}" cy="${r2(ey)}" r="5" fill="var(--d-s1)" stroke="var(--d-halo)" stroke-width="2" ${F}/>`);
|
|
268
|
+
parts.push(`<text x="${r2(ex - 8)}" y="${r2(ey - Math.round(height * 0.0225))}" text-anchor="end" font-size="14" font-weight="bold" fill="var(--d-s1)">${fmtNum(values[n - 1])}</text>`);
|
|
269
|
+
// X 刻度(抽样避免拥挤)
|
|
270
|
+
const skip = Math.ceil(n / 8);
|
|
271
|
+
spec.data.forEach((d, i) => {
|
|
272
|
+
if (i % skip !== 0 && i !== n - 1)
|
|
273
|
+
return;
|
|
274
|
+
parts.push(`<text x="${r2(xs(i))}" y="${r2(xLabelY)}" text-anchor="middle" font-size="16" font-weight="bold" fill="var(--d-ink)">${esc(trunc(d.label, 10))}</text>`);
|
|
275
|
+
});
|
|
276
|
+
return svgDoc(width, height, 'area', version, spec.title ?? 'star history', spec, skin, parts.join(''));
|
|
277
|
+
}
|
|
278
|
+
// 像素 GitHub Star(area-3,900×470):100×46 格背景 + 双层发光轨迹 + 手写标注
|
|
279
|
+
function renderPixelArea(spec, o) {
|
|
280
|
+
const { width, height, version, skin } = o;
|
|
281
|
+
const COLS = 100;
|
|
282
|
+
const ROWS = 46;
|
|
283
|
+
const dark = skin.scheme === 'dark';
|
|
284
|
+
const anchors = spec.data.map((d) => d.value);
|
|
285
|
+
const max = Math.max(...anchors);
|
|
286
|
+
// 锚点 → 每列线性插值(规范同法)
|
|
287
|
+
const cols = [];
|
|
288
|
+
for (let i = 0; i < COLS; i++) {
|
|
289
|
+
const p = (i / (COLS - 1)) * (anchors.length - 1);
|
|
290
|
+
const a = Math.floor(p);
|
|
291
|
+
const t = p - a;
|
|
292
|
+
cols.push(anchors[a] + (anchors[Math.min(a + 1, anchors.length - 1)] - anchors[a]) * t);
|
|
293
|
+
}
|
|
294
|
+
const sx = width / 900;
|
|
295
|
+
const sy = height / 470;
|
|
296
|
+
const x0 = 43 * sx;
|
|
297
|
+
const y0 = 66 * sy;
|
|
298
|
+
const pitchX = 8.15 * sx;
|
|
299
|
+
const pitchY = 7.65 * sy;
|
|
300
|
+
const cell = 5.9 * Math.min(sx, sy);
|
|
301
|
+
// 轨迹像素中心(列 → 行,自底向上)
|
|
302
|
+
const centers = [];
|
|
303
|
+
for (let c = 0; c < COLS; c++) {
|
|
304
|
+
const row = Math.max(0, Math.min(ROWS - 1, ROWS - 1 - Math.round((cols[c] / max) * (ROWS - 1))));
|
|
305
|
+
centers.push([x0 + c * pitchX + pitchX / 2, y0 + row * pitchY + pitchY / 2]);
|
|
306
|
+
}
|
|
307
|
+
const parts = [
|
|
308
|
+
`<defs>` +
|
|
309
|
+
`<linearGradient id="pxbg" x1="0" y1="0" x2="1" y2="1">` +
|
|
310
|
+
`<stop stop-color="${dark ? '#050605' : '#FFFFFF'}"/><stop offset=".58" stop-color="${dark ? '#071008' : '#FFFFFF'}"/><stop offset="1" stop-color="${dark ? '#0A1008' : '#FFFFFF'}"/></linearGradient>` +
|
|
311
|
+
`<filter id="px-wide" x="-30%" y="-40%" width="160%" height="180%"><feGaussianBlur stdDeviation="9"/></filter>` +
|
|
312
|
+
`<filter id="px-glow" x="-80%" y="-80%" width="260%" height="260%"><feGaussianBlur stdDeviation="2" result="b"/><feMerge><feMergeNode in="b"/><feMergeNode in="SourceGraphic"/></feMerge></filter>` +
|
|
313
|
+
`</defs>`,
|
|
314
|
+
`<rect width="${r2(width)}" height="${r2(height)}" fill="url(#pxbg)"/>`,
|
|
315
|
+
// 底层宽光晕
|
|
316
|
+
`<polyline points="${centers.map((p) => `${r2(p[0])},${r2(p[1])}`).join(' ')}" fill="none" stroke="#64D51D" stroke-opacity="${dark ? 0.3 : 0.08}" stroke-width="20" stroke-linecap="round" stroke-linejoin="round" filter="url(#px-wide)"/>`,
|
|
317
|
+
];
|
|
318
|
+
for (let c = 0; c < COLS; c++) {
|
|
319
|
+
for (let rw = 0; rw < ROWS; rw++) {
|
|
320
|
+
parts.push(`<rect x="${r2(x0 + c * pitchX + (pitchX - cell) / 2)}" y="${r2(y0 + rw * pitchY + (pitchY - cell) / 2)}" width="${r2(cell)}" height="${r2(cell)}" rx="${r2(cell * 0.042)}" fill="${dark ? '#071009' : '#FFFFFF'}" stroke="var(--d-grid)" stroke-width="0.7"/>`);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
parts.push(`<g filter="url(#px-glow)">${cols.map((_, c) => {
|
|
324
|
+
const [px, py] = centers[c];
|
|
325
|
+
const s = 4.8 * Math.min(sx, sy);
|
|
326
|
+
return `<rect x="${r2(px - s / 2)}" y="${r2(py - s / 2)}" width="${r2(s)}" height="${r2(s)}" rx="${r2(s * 0.083)}" fill="#8FEA2A" stroke="#B4FF43" stroke-width="0.3"/>`;
|
|
327
|
+
}).join('')}</g>`);
|
|
328
|
+
// 手写标题(总量)与日期刻度
|
|
329
|
+
const ink = dark ? '#D8DEE9' : '#3F3F46';
|
|
330
|
+
const last = spec.data[spec.data.length - 1];
|
|
331
|
+
parts.push(`<text x="${r2(width - 44 * sx)}" y="${r2(44 * sy)}" text-anchor="end" fill="${ink}" font-family="${SKETCH_FONT}" font-size="${r2(23 * sy)}">${fmtFull(last.value)} ${esc(trunc(spec.unit ?? '', 16))}</text>`);
|
|
332
|
+
for (let i = 0; i <= 6; i++) {
|
|
333
|
+
const frac = i / 6;
|
|
334
|
+
const x = x0 + (COLS - 1) * pitchX * frac;
|
|
335
|
+
const anchor = i === 0 ? 'start' : i === 6 ? 'end' : 'middle';
|
|
336
|
+
parts.push(`<line x1="${r2(x)}" y1="${r2(418 * sy)}" x2="${r2(x)}" y2="${r2(424 * sy)}" stroke="${ink}" stroke-opacity="0.35"/>`);
|
|
337
|
+
const label = spec.data[Math.round(frac * (spec.data.length - 1))]?.label ?? '';
|
|
338
|
+
parts.push(`<text x="${r2(x)}" y="${r2(447 * sy)}" text-anchor="${anchor}" fill="${ink}" font-family="${SKETCH_FONT}" font-size="${r2(12 * sy)}">${esc(trunc(label, 12))}</text>`);
|
|
339
|
+
}
|
|
340
|
+
return svgDoc(width, height, 'area', version, spec.title ?? 'pixel star history', spec, skin, parts.join(''));
|
|
341
|
+
}
|
|
342
|
+
// 波点 GitHub Star(area-4,900×470):阶梯面积 + 9×9 圆点 pattern,双系列对比
|
|
343
|
+
function renderPolkaArea(spec, o) {
|
|
344
|
+
const { width, height, version, skin } = o;
|
|
345
|
+
const k = spec.series.length;
|
|
346
|
+
const gmax = Math.max(...spec.series.flatMap((s) => s.values));
|
|
347
|
+
const step = niceStep(gmax / 3.2);
|
|
348
|
+
const ticksY = [];
|
|
349
|
+
for (let v = 0; v <= Math.ceil(gmax / step) * step + 1e-9; v += step)
|
|
350
|
+
ticksY.push(v);
|
|
351
|
+
const domain = Math.max(gmax * 1.1, ticksY[ticksY.length - 1]);
|
|
352
|
+
const sx = width / 900;
|
|
353
|
+
const sy = height / 470;
|
|
354
|
+
const x0 = 48 * sx;
|
|
355
|
+
const y0 = 414 * sy;
|
|
356
|
+
const w = 804 * sx;
|
|
357
|
+
const h = 238 * sy;
|
|
358
|
+
const n = spec.labels.length;
|
|
359
|
+
const px = (i) => x0 + (w * i) / Math.max(n - 1, 1);
|
|
360
|
+
const py = (v) => y0 - (h * v) / domain;
|
|
361
|
+
const stepPath = (values) => {
|
|
362
|
+
let d = `M${r2(px(0))} ${r2(py(values[0]))}`;
|
|
363
|
+
for (let i = 1; i < values.length; i++) {
|
|
364
|
+
d += ` H${r2(px(i))} V${r2(py(values[i]))}`;
|
|
365
|
+
}
|
|
366
|
+
return d;
|
|
367
|
+
};
|
|
368
|
+
const parts = [
|
|
369
|
+
card(width, height, true),
|
|
370
|
+
`<defs>${spec.series.map((_, i) => `<pattern id="pk-${i}" width="9" height="9" patternUnits="userSpaceOnUse"><circle cx="2" cy="2" r="1.5" fill="${sVar(i % skin.series.length)}"/></pattern>`).join('')}</defs>`,
|
|
371
|
+
`<line x1="${r2(x0)}" y1="${r2(y0)}" x2="${r2(x0 + w)}" y2="${r2(y0)}" stroke="var(--d-grid)"/>`,
|
|
372
|
+
];
|
|
373
|
+
for (const t of ticksY) {
|
|
374
|
+
const gy = py(t);
|
|
375
|
+
parts.push(`<line x1="${r2(x0)}" y1="${r2(gy)}" x2="${r2(x0 + w)}" y2="${r2(gy)}" stroke="var(--d-grid)" stroke-dasharray="3 6"/>`);
|
|
376
|
+
parts.push(`<text x="${r2(39 * sx)}" y="${r2(gy + 4)}" text-anchor="end" font-size="10" fill="var(--d-muted)" style="${TAB}">${fmtNum(t)}</text>`);
|
|
377
|
+
}
|
|
378
|
+
// 系列:后序先画,首个在最上(规范 a 覆盖 b)
|
|
379
|
+
for (let i = k - 1; i >= 0; i--) {
|
|
380
|
+
const s = spec.series[i];
|
|
381
|
+
const color = sVar(i % skin.series.length);
|
|
382
|
+
const sp = stepPath(s.values);
|
|
383
|
+
parts.push(`<path d="${sp} V${r2(y0)} H${r2(px(0))} Z" fill="url(#pk-${i})" fill-opacity="${i === 0 ? 0.38 : 0.42}"/>`);
|
|
384
|
+
parts.push(`<path d="${sp}" fill="none" stroke="${color}" stroke-width="2"/>`);
|
|
385
|
+
}
|
|
386
|
+
// 图例:圆圈 + 仓库名 + 大数值(两列排布)
|
|
387
|
+
spec.series.forEach((s, i) => {
|
|
388
|
+
const cx = x0 + 22 * sx + (i % 2) * (w * 0.502);
|
|
389
|
+
const rowY = (62 + Math.floor(i / 2) * 56) * sy;
|
|
390
|
+
const color = sVar(i % skin.series.length);
|
|
391
|
+
parts.push(`<circle cx="${r2(cx)}" cy="${r2(rowY)}" r="7" fill="none" stroke="${color}" stroke-width="2"/>`);
|
|
392
|
+
parts.push(`<text x="${r2(cx + 15)}" y="${r2(rowY + 5)}" font-size="14" fill="var(--d-muted)">${esc(trunc(s.name, 16))}</text>`);
|
|
393
|
+
parts.push(`<text x="${r2(cx - 2)}" y="${r2(rowY + 40 * sy)}" font-size="${r2(31 * sy)}" font-weight="700" fill="var(--d-ink)" style="${TAB}">${fmtFull(s.values[s.values.length - 1])}</text>`);
|
|
394
|
+
});
|
|
395
|
+
parts.push(`<text x="${r2(x0)}" y="${r2(446 * sy)}" font-size="11" fill="var(--d-muted)">${esc(trunc(spec.labels[0], 12))}</text>`);
|
|
396
|
+
parts.push(`<text x="${r2(x0 + w)}" y="${r2(446 * sy)}" text-anchor="end" font-size="11" fill="var(--d-muted)">${esc(trunc(spec.labels[n - 1], 12))}</text>`);
|
|
397
|
+
return svgDoc(width, height, 'area', version, spec.title ?? 'star history', spec, skin, parts.join(''));
|
|
398
|
+
}
|
|
399
|
+
// 面积模式分发:auto=常规;pixel=单系列像素;polka=阶梯波点(单/多系列)
|
|
400
|
+
export function renderAreaByMode(input, o) {
|
|
401
|
+
const mode = o.skin.rules.areaMode;
|
|
402
|
+
if (mode === 'pixel') {
|
|
403
|
+
if (input.multi)
|
|
404
|
+
throw new ApiError(400, 't=pixel takes a single cumulative series');
|
|
405
|
+
return renderPixelArea(input.spec, o);
|
|
406
|
+
}
|
|
407
|
+
if (mode === 'polka') {
|
|
408
|
+
if (input.multi)
|
|
409
|
+
return renderPolkaArea(input.spec, o);
|
|
410
|
+
const s = input.spec;
|
|
411
|
+
return renderPolkaArea({ title: s.title, unit: s.unit, labels: s.data.map((d) => d.label), series: [{ name: s.title ?? 'stars', values: s.data.map((d) => d.value) }] }, o);
|
|
412
|
+
}
|
|
413
|
+
return input.multi ? renderAreaMulti(input.spec, o) : renderArea(input.spec, o);
|
|
414
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { card } from '../theme.js';
|
|
2
|
+
import { esc, fmtFull, r2, trunc, svgDoc, sVar } from './svg.js';
|
|
3
|
+
const MONO = "ui-monospace,SFMono-Regular,Menlo,Consolas,monospace";
|
|
4
|
+
export function renderBarGrid(spec, o) {
|
|
5
|
+
const { width, height, version, skin } = o;
|
|
6
|
+
const plain = skin.rules.canvas === 'plain';
|
|
7
|
+
const n = spec.data.length;
|
|
8
|
+
const levels = spec.data.map((d) => Math.max(0, Math.round(d.value)));
|
|
9
|
+
const rows = Math.max(1, ...levels);
|
|
10
|
+
const total = spec.data.reduce((s, d) => s + d.value, 0);
|
|
11
|
+
let peakIdx = 0;
|
|
12
|
+
spec.data.forEach((d, i) => {
|
|
13
|
+
if (d.value > spec.data[peakIdx].value)
|
|
14
|
+
peakIdx = i;
|
|
15
|
+
});
|
|
16
|
+
// 几何(规范 960×520 实测):起点 78/214,格 14×12,横距 18 纵距 6
|
|
17
|
+
const sx = Math.round(width * 0.0604);
|
|
18
|
+
const startX = Math.round(width * 0.0813);
|
|
19
|
+
const startY = Math.round(height * 0.4115);
|
|
20
|
+
const pitch = Math.min(32, (width - startX * 2) / Math.max(n, 1));
|
|
21
|
+
const cellW = pitch * (14 / 32);
|
|
22
|
+
const vs = height / 520;
|
|
23
|
+
const cellH = 12 * vs;
|
|
24
|
+
const gapY = 6 * vs;
|
|
25
|
+
const headR = Math.round(width * 0.9396);
|
|
26
|
+
const parts = [card(width, height, plain)];
|
|
27
|
+
// 表头(等宽):[Σ] Total | [◆] Peak | 右侧注释行
|
|
28
|
+
const mono = (x, y, fs, text, cls = 'muted', anchor = 'start') => `<text x="${r2(x)}" y="${r2(y)}" text-anchor="${anchor}" font-family="${MONO}" font-size="${r2(fs)}" fill="var(--d-${cls === 'muted' ? 'muted' : 'text'})"${cls === 'value' ? ' style="font-variant-numeric:tabular-nums"' : ''}>${esc(text)}</text>`;
|
|
29
|
+
parts.push(mono(sx, Math.round(72 * vs), 15 * vs, '[Σ] Total'));
|
|
30
|
+
parts.push(mono(sx, Math.round(122 * vs), 36 * vs, fmtFull(total), 'value'));
|
|
31
|
+
parts.push(`<line x1="${r2(Math.round(width * 0.2125))}" y1="${r2(Math.round(58 * vs))}" x2="${r2(Math.round(width * 0.2125))}" y2="${r2(Math.round(124 * vs))}" stroke="var(--d-grid)" stroke-dasharray="4 5"/>`);
|
|
32
|
+
parts.push(mono(Math.round(width * 0.2375), Math.round(72 * vs), 15 * vs, '[◆] Peak'));
|
|
33
|
+
parts.push(mono(Math.round(width * 0.2375), Math.round(122 * vs), 36 * vs, trunc(spec.data[peakIdx].label, 12), 'value'));
|
|
34
|
+
parts.push(mono(headR, Math.round(74 * vs), 12, '// CELL: 1:1', 'muted', 'end'));
|
|
35
|
+
parts.push(mono(headR, Math.round(98 * vs), 12, '// TYPE: GRID', 'muted', 'end'));
|
|
36
|
+
parts.push(`<line x1="${r2(sx)}" y1="${r2(Math.round(154 * vs))}" x2="${r2(headR)}" y2="${r2(Math.round(154 * vs))}" stroke="var(--d-grid)" stroke-dasharray="4 5"/>`);
|
|
37
|
+
// 单元格:行 0 在顶,active = 自底向上 levels[col] 格
|
|
38
|
+
for (let col = 0; col < n; col++) {
|
|
39
|
+
for (let row = 0; row < rows; row++) {
|
|
40
|
+
const active = row >= rows - levels[col];
|
|
41
|
+
parts.push(`<rect x="${r2(startX + col * pitch)}" y="${r2(startY + row * (cellH + gapY))}" width="${r2(cellW)}" height="${r2(cellH)}" rx="1" fill="${active ? sVar(0) : 'var(--d-faint)'}"/>`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// 时间刻度:约每 n/12 列一个(规范 24 列 → 每隔 1 列)
|
|
45
|
+
const tickSkip = Math.max(1, Math.ceil(n / 12));
|
|
46
|
+
spec.data.forEach((d, i) => {
|
|
47
|
+
if (i % tickSkip !== 0)
|
|
48
|
+
return;
|
|
49
|
+
parts.push(`<text x="${r2(startX + i * pitch + cellW / 2)}" y="${r2(Math.round(height * 0.9))}" text-anchor="middle" font-family="${MONO}" font-size="12" fill="var(--d-muted)" style="font-variant-numeric:tabular-nums">${esc(trunc(d.label, 12))}</text>`);
|
|
50
|
+
});
|
|
51
|
+
return svgDoc(width, height, 'bar', version, spec.title ?? 'grid bar', spec, skin, parts.join(''));
|
|
52
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { card } from '../theme.js';
|
|
2
|
+
import { esc, fmtNum, niceTicks, r2, textW, topLegend, trunc, svgDoc, titleBlock, yAxisGrid, findPeak, onVar, sVar, pushBarLabelsAllOrNothing, labelStride, LabelPlacer, reserveBands } from './svg.js';
|
|
3
|
+
import { yTickInset } from './measure.js';
|
|
4
|
+
// 规范 bar-1 实测几何(960×520):left 76、右留 50、顶 94(0.181h)、底 448(0.862h)、X 标签 480
|
|
5
|
+
// 留白 = max(规范比例, 实测刻度列):长刻度/窄画布时左缘撑大,刻度不裁字
|
|
6
|
+
function barGeo(w, h, hasTitle, m = {}) {
|
|
7
|
+
return {
|
|
8
|
+
left: Math.round(Math.max(w * 0.0792, yTickInset(m))),
|
|
9
|
+
right: w - Math.round(w * 0.0521),
|
|
10
|
+
top: Math.round(h * (hasTitle ? 0.2 : 0.181)),
|
|
11
|
+
bottom: Math.round(h * 0.8615),
|
|
12
|
+
xLabelY: Math.round(h * 0.9231),
|
|
13
|
+
legendY: Math.round(h * 0.0923),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export function renderBarMulti(spec, o) {
|
|
17
|
+
const height = o.height;
|
|
18
|
+
const { version, skin } = o;
|
|
19
|
+
const rules = skin.rules;
|
|
20
|
+
const k = spec.series.length;
|
|
21
|
+
const colors = spec.series.map((_, i) => sVar(i % skin.series.length));
|
|
22
|
+
const n = spec.labels.length;
|
|
23
|
+
const gMax = Math.max(...spec.series.flatMap((s) => s.values));
|
|
24
|
+
const [peakS, peakP] = findPeak(spec.series);
|
|
25
|
+
// 自动加宽:目标是"数值标签能放进柱内"。按与下面 barW 完全一致的公式反推所需槽宽,
|
|
26
|
+
// 不够宽则放大画布(封顶 2.5×),把回退到柱顶的数字收回柱内
|
|
27
|
+
let width = o.width;
|
|
28
|
+
if (o.autoWidth && n > 0) {
|
|
29
|
+
const maxLabelW = Math.max(...spec.series.flatMap((ser) => ser.values.map((v) => textW(fmtNum(v), 7))));
|
|
30
|
+
const corner = rules.barCorner > 0;
|
|
31
|
+
const gapAB = corner ? 5 : 0;
|
|
32
|
+
const cap = corner ? 22 : 40;
|
|
33
|
+
const factor = 0.72;
|
|
34
|
+
const plain = rules.canvas === 'plain';
|
|
35
|
+
const margins = plain ? 0 : 92;
|
|
36
|
+
const ratio = plain ? 0.8687 : 1;
|
|
37
|
+
// barW = min(cap, (step*factor - gap*(k-1))/k) → 解出 step;柱内标签还需纵向放得下(略)
|
|
38
|
+
// 只有"扩了真能装进柱"才扩:扩到位(封顶 1.5×)后柱宽仍 < 标签宽 → 不扩,
|
|
39
|
+
// 数字按"只进柱"整层丢弃——白扩不加宽(corner 变体柱宽封顶常触发此情形)
|
|
40
|
+
const needBarW = Math.min(maxLabelW + 6, cap);
|
|
41
|
+
const needStep = (needBarW * k + gapAB * (k - 1)) / factor;
|
|
42
|
+
const curStep = ((width - margins) * ratio) / n;
|
|
43
|
+
if (curStep < needStep) {
|
|
44
|
+
const w2 = Math.round(Math.min(width * 1.5, (needStep * n) / ratio + margins));
|
|
45
|
+
const step2 = ((w2 - margins) * ratio) / n;
|
|
46
|
+
const barW2 = Math.min(cap, (step2 * factor - gapAB * (k - 1)) / k);
|
|
47
|
+
if (barW2 >= maxLabelW + 4)
|
|
48
|
+
width = w2;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const pad = 24;
|
|
52
|
+
const plain = rules.canvas === 'plain';
|
|
53
|
+
const ticks = niceTicks(gMax, plain ? 5 : 4);
|
|
54
|
+
// 组数少(<5)时宽度贴内容收缩(设计密度 5 组:card 槽 93.6、plain 槽 139;下限 320)
|
|
55
|
+
if (o.autoWidth && n > 0 && n < 5) {
|
|
56
|
+
const slot = plain ? 139 : 93.6;
|
|
57
|
+
const insets = plain ? width * 0.0792 + width * 0.0521 : pad + 44 + pad;
|
|
58
|
+
const need = insets + n * slot;
|
|
59
|
+
if (need < width)
|
|
60
|
+
width = Math.round(Math.max(320, need));
|
|
61
|
+
}
|
|
62
|
+
const geo = plain ? barGeo(width, height, !!spec.title, { ticks }) : null;
|
|
63
|
+
const contentTop = geo
|
|
64
|
+
? Math.max(geo.top, pad + (spec.title ? 32 : 0) + (spec.unit ? 18 : 0) + 24)
|
|
65
|
+
: pad + (spec.title ? 32 : 0) + (spec.unit ? 18 : 0) + 24;
|
|
66
|
+
const left = geo ? geo.left : Math.max(pad + 44, yTickInset({ ticks }));
|
|
67
|
+
const right = geo ? geo.right : width - pad;
|
|
68
|
+
const bottom = geo ? geo.bottom : height - pad - 30;
|
|
69
|
+
const xLabelY = geo ? geo.xLabelY : bottom + 20;
|
|
70
|
+
const legendY = geo ? geo.legendY : pad + 16;
|
|
71
|
+
const yMax = ticks[ticks.length - 1];
|
|
72
|
+
const y = (v) => bottom - (v / yMax) * (bottom - contentTop);
|
|
73
|
+
const parts = [
|
|
74
|
+
card(width, height, plain),
|
|
75
|
+
`<defs><linearGradient id="dg-bar" x1="0" y1="0" x2="0" y2="1">` +
|
|
76
|
+
`<stop offset="0" stop-color="var(--d-ramp-a)"/><stop offset="1" stop-color="var(--d-ramp-b)"/></linearGradient></defs>`,
|
|
77
|
+
];
|
|
78
|
+
if (rules.legend === 'top-left') {
|
|
79
|
+
parts.push(topLegend(spec.series.map((s, i) => ({ name: s.name, color: colors[i] })), left, legendY, 'left', rules.legendShape));
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
parts.push(topLegend(spec.series.map((s, i) => ({ name: s.name, color: colors[i] })), plain ? right : width - pad, legendY, 'right', rules.legendShape));
|
|
83
|
+
}
|
|
84
|
+
yAxisGrid(parts, ticks, y, left, right, bottom, rules.grid, rules.gridDash, false, rules.baseline, [], rules.tickFont, geo ? 18 : 8);
|
|
85
|
+
const step = (right - left) / n;
|
|
86
|
+
// 规范 bar-1:柱宽 22、系列间距 5,四角圆角 rx4;通用形态按槽宽自适应
|
|
87
|
+
const gap = rules.barCorner > 0 ? 5 : 0;
|
|
88
|
+
const barW = rules.barCorner > 0
|
|
89
|
+
? Math.max(4, Math.min(22, (step * 0.72 - (k - 1) * gap) / k))
|
|
90
|
+
: Math.max(4, Math.min(40, (step * 0.72) / k));
|
|
91
|
+
const num = o.valueLabels ?? rules.num;
|
|
92
|
+
const numItems = [];
|
|
93
|
+
// 回退位:组中线 + 组内最高柱顶(同组标签在组上方单列堆叠)
|
|
94
|
+
const groupTop = spec.labels.map((_, pi) => Math.min(...spec.series.map((s) => y(s.values[pi]))));
|
|
95
|
+
spec.series.forEach((s, si) => {
|
|
96
|
+
s.values.forEach((v, pi) => {
|
|
97
|
+
if (v <= 0)
|
|
98
|
+
return;
|
|
99
|
+
const groupX = left + step * pi + (step - (barW * k + (k - 1) * gap)) / 2;
|
|
100
|
+
const x = groupX + si * (barW + gap);
|
|
101
|
+
const yt = y(v);
|
|
102
|
+
if (rules.barCorner > 0) {
|
|
103
|
+
parts.push(`<rect x="${r2(x)}" y="${r2(yt)}" width="${r2(barW)}" height="${r2(bottom - yt)}" rx="${rules.barCorner}" fill="${colors[si]}"/>`);
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
const m = Math.min(3, barW / 2, bottom - yt);
|
|
107
|
+
const path = `M${r2(x)} ${r2(bottom)}V${r2(yt + m)}Q${r2(x)} ${r2(yt)} ${r2(x + m)} ${r2(yt)}` +
|
|
108
|
+
`H${r2(x + barW - m)}Q${r2(x + barW)} ${r2(yt)} ${r2(x + barW)} ${r2(yt + m)}V${r2(bottom)}Z`;
|
|
109
|
+
parts.push(`<path d="${path}" fill="${colors[si]}"/>`);
|
|
110
|
+
}
|
|
111
|
+
const isPeak = si === peakS && pi === peakP;
|
|
112
|
+
// 零值/占位不标:零高柱上飘"0"是噪音(标注克制原则)
|
|
113
|
+
if (v > 0 && (num === 'all' || (num === 'peak' && isPeak))) {
|
|
114
|
+
// 数值只进柱:柱内放不下(缩到 7px 也不行)→ 整层不标。
|
|
115
|
+
// 不做组顶飘浮回退——密集图上一群数字浮在柱群上方比没有数字难看(标注克制)
|
|
116
|
+
const text = fmtNum(v);
|
|
117
|
+
numItems.push({ x, yt, w: barW, hAvail: bottom - yt, text, fs: isPeak ? 12 : 10.5, peak: isPeak, fill: onVar(si % skin.series.length), row: si, fx: left + step * pi + step / 2, fyt: groupTop[pi], haloColor: colors[si], insideOnly: true });
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
const placer = new LabelPlacer();
|
|
122
|
+
reserveBands(placer, left, right, contentTop, bottom, xLabelY, spec.series, width - pad, legendY);
|
|
123
|
+
pushBarLabelsAllOrNothing(parts, numItems, placer);
|
|
124
|
+
const xStride = labelStride(spec.labels, step);
|
|
125
|
+
spec.labels.forEach((l, pi) => {
|
|
126
|
+
const cx = left + step * pi + step / 2;
|
|
127
|
+
if (pi % xStride === 0)
|
|
128
|
+
parts.push(`<text x="${r2(cx)}" y="${r2(xLabelY)}" text-anchor="middle" font-size="12" fill="var(--d-muted)">${esc(trunc(l, 12))}</text>`);
|
|
129
|
+
});
|
|
130
|
+
return svgDoc(width, height, 'bar', version, spec.title ?? 'bar chart', spec, skin, titleBlock(spec.title) + parts.join(''));
|
|
131
|
+
}
|