@x-cmd/draw 0.2.4 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/render/bar-grouped.js +1 -1
- package/dist/render/bar-stacked.js +1 -1
- package/dist/render/combo.js +5 -5
- package/dist/render/hbar-spec.js +18 -13
- package/dist/render/hbar.js +3 -2
- package/dist/render/pie.js +7 -7
- package/dist/render/svg.d.ts +3 -1
- package/dist/render/svg.js +7 -6
- package/dist/specs/base.d.ts +2 -0
- package/dist/specs/base.js +14 -2
- package/package.json +1 -1
|
@@ -118,7 +118,7 @@ export function renderBarMulti(spec, o) {
|
|
|
118
118
|
// 数值只进柱:柱内放不下(缩到 7px 也不行)→ 整层不标。
|
|
119
119
|
// 不做组顶飘浮回退——密集图上一群数字浮在柱群上方比没有数字难看(标注克制)
|
|
120
120
|
const text = fmtNum(v);
|
|
121
|
-
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 });
|
|
121
|
+
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, series: si });
|
|
122
122
|
}
|
|
123
123
|
});
|
|
124
124
|
});
|
|
@@ -127,7 +127,7 @@ export function renderBarStacked(spec, o) {
|
|
|
127
127
|
const fs = isPeakSeg ? 12 : 11;
|
|
128
128
|
const minFs = 7;
|
|
129
129
|
if (barW >= textW(text, minFs) + 4 && segH >= minFs + 4) {
|
|
130
|
-
numItems.push({ x, yt: yTop, w: barW, hAvail: segH, text, fs, peak: isPeakSeg, fill: onVar(si % skin.series.length), center: true, haloColor: colors[si] });
|
|
130
|
+
numItems.push({ x, yt: yTop, w: barW, hAvail: segH, text, fs, peak: isPeakSeg, fill: onVar(si % skin.series.length), center: true, haloColor: colors[si], series: si });
|
|
131
131
|
}
|
|
132
132
|
}
|
|
133
133
|
cursor = yTop;
|
package/dist/render/combo.js
CHANGED
|
@@ -77,7 +77,7 @@ export function renderCombo(spec, o) {
|
|
|
77
77
|
lineSvg.push(`<circle cx="${r2(lx)}" cy="${r2(ly)}" r="2.5" fill="${colors[gIdx]}" stroke="#ffffff" stroke-width="1.5"${tip}/>`);
|
|
78
78
|
const isPeak = gIdx === peakS && pi === peakP;
|
|
79
79
|
if (num === 'all' || (num === 'peak' && isPeak)) {
|
|
80
|
-
lineLabels.push({ px: lx, py: ly, text: fmtNum(s.values[pi]), isPeak });
|
|
80
|
+
lineLabels.push({ px: lx, py: ly, text: fmtNum(s.values[pi]), isPeak, series: gIdx });
|
|
81
81
|
}
|
|
82
82
|
});
|
|
83
83
|
});
|
|
@@ -115,7 +115,7 @@ export function renderCombo(spec, o) {
|
|
|
115
115
|
if (num === 'all' || (num === 'peak' && isPeak)) {
|
|
116
116
|
const text = fmtNum(v);
|
|
117
117
|
if (barW >= textW(text, 7) + 4 && segH >= 7 + 4) {
|
|
118
|
-
withinStack.push({ x, yt: yTop, w: barW, hAvail: segH, text, fs: isPeak ? 12 : 11, peak: isPeak, fill: onVar(gIdx % skin.series.length), center: true, haloColor: colors[gIdx] });
|
|
118
|
+
withinStack.push({ x, yt: yTop, w: barW, hAvail: segH, text, fs: isPeak ? 12 : 11, peak: isPeak, fill: onVar(gIdx % skin.series.length), center: true, haloColor: colors[gIdx], series: gIdx });
|
|
119
119
|
}
|
|
120
120
|
}
|
|
121
121
|
}
|
|
@@ -128,7 +128,7 @@ export function renderCombo(spec, o) {
|
|
|
128
128
|
`H${r2(x + barW - m)}Q${r2(x + barW)} ${r2(yt)} ${r2(x + barW)} ${r2(yt + m)}V${r2(bottom)}Z" fill="${colors[gIdx]}"${tip}/>`);
|
|
129
129
|
// 零值不标(标注克制原则:零高柱上飘"0"是噪音)
|
|
130
130
|
if (v > 0 && (num === 'all' || (num === 'peak' && isPeak))) {
|
|
131
|
-
barLabels.push({ x, yt, v, color: colors[gIdx], isPeak, barW, row: bi, fx: left + step * pi + step / 2, fyt: groupTop[pi] });
|
|
131
|
+
barLabels.push({ x, yt, v, color: colors[gIdx], isPeak, barW, row: bi, fx: left + step * pi + step / 2, fyt: groupTop[pi], series: gIdx });
|
|
132
132
|
}
|
|
133
133
|
}
|
|
134
134
|
});
|
|
@@ -157,7 +157,7 @@ export function renderCombo(spec, o) {
|
|
|
157
157
|
let allPlaced = true;
|
|
158
158
|
if (barLabels.length > 0) {
|
|
159
159
|
const barOut = [];
|
|
160
|
-
const ok = pushBarLabelsAllOrNothing(barOut, barLabels.map((l) => ({ x: l.x, yt: l.yt, w: l.barW, hAvail: bottom - l.yt, text: fmtNum(l.v), fs: l.isPeak ? 12 : 10.5, peak: l.isPeak, fill: onVar(0), row: l.row, fx: l.fx, fyt: l.fyt, insideOnly: true })), // 只进柱:放不下整层不标(同 bar-grouped)
|
|
160
|
+
const ok = pushBarLabelsAllOrNothing(barOut, barLabels.map((l) => ({ x: l.x, yt: l.yt, w: l.barW, hAvail: bottom - l.yt, text: fmtNum(l.v), fs: l.isPeak ? 12 : 10.5, peak: l.isPeak, fill: onVar(0), row: l.row, fx: l.fx, fyt: l.fyt, insideOnly: true, series: l.series })), // 只进柱:放不下整层不标(同 bar-grouped)
|
|
161
161
|
placer, lineSegs);
|
|
162
162
|
if (!ok)
|
|
163
163
|
allPlaced = false;
|
|
@@ -168,7 +168,7 @@ export function renderCombo(spec, o) {
|
|
|
168
168
|
const sorted = [...lineLabels].sort((a, b) => Number(b.isPeak) - Number(a.isPeak));
|
|
169
169
|
for (const l of sorted) {
|
|
170
170
|
const probe = [];
|
|
171
|
-
pushLineLabel(probe, placer, l.px, l.py, l.text, l.isPeak, lineSegs);
|
|
171
|
+
pushLineLabel(probe, placer, l.px, l.py, l.text, l.isPeak, lineSegs, l.series);
|
|
172
172
|
if (probe.length === 0) {
|
|
173
173
|
allPlaced = false;
|
|
174
174
|
break;
|
package/dist/render/hbar-spec.js
CHANGED
|
@@ -5,6 +5,7 @@ import { TAB, card } from '../theme.js';
|
|
|
5
5
|
import { esc, fmtFull, fmtNum, niceTicks, r2, sVar, textW, trunc, svgDoc } from './svg.js';
|
|
6
6
|
import { fitCut } from './measure.js';
|
|
7
7
|
import { renderHBar } from './hbar.js';
|
|
8
|
+
const tipOf = (o, text) => (o.interactive ? ` data-tip="${esc(text)}"` : '');
|
|
8
9
|
/**
|
|
9
10
|
* Horizontal bars, dispatched by skin family (base / opendata / xcmd / butterfly / rank).
|
|
10
11
|
*/
|
|
@@ -93,7 +94,7 @@ export function renderHbarOpendata(spec, o) {
|
|
|
93
94
|
const y = top + i * pitch;
|
|
94
95
|
const w = (plotW * d.value) / yMax;
|
|
95
96
|
parts.push(`<text x="${r2(labelEnd)}" y="${r2(y + pitch * 0.36)}" text-anchor="end" font-size="14" fill="var(--d-muted)">${esc(trunc(d.label, lblCut))}</text>`);
|
|
96
|
-
parts.push(`<rect x="${x}" y="${r2(y)}" width="${r2(w)}" height="${r2(barH)}" rx="3" fill="${sVar(i % skin.series.length)}"/>`);
|
|
97
|
+
parts.push(`<rect x="${x}" y="${r2(y)}" width="${r2(w)}" height="${r2(barH)}" rx="3" fill="${sVar(i % skin.series.length)}"${tipOf(o, `${d.label} · ${fmtFull(d.value)}${spec.unit ? ' ' + spec.unit : ''}`)}/>`);
|
|
97
98
|
// 数值内标白字;放不下则外置墨色
|
|
98
99
|
const text = fmtFull(d.value);
|
|
99
100
|
if (textW(text, 11) + 14 <= w) {
|
|
@@ -145,10 +146,11 @@ export function renderHbarCells(spec, o) {
|
|
|
145
146
|
const exact = (d.value / 100) * CELLS; // 值为百分比
|
|
146
147
|
for (let c = 0; c < CELLS; c++) {
|
|
147
148
|
const cx = startX + c * cellPitch;
|
|
148
|
-
|
|
149
|
+
const cellTip = tipOf(o, `${d.label} · ${Math.round(d.value * 10) / 10}%`);
|
|
150
|
+
parts.push(`<rect x="${r2(cx)}" y="${r2(y)}" width="${r2(cw)}" height="${r2(cellH)}" rx="1" fill="var(--d-faint)"${cellTip}/>`);
|
|
149
151
|
const cover = Math.max(0, Math.min(1, exact - c));
|
|
150
152
|
if (cover > 0) {
|
|
151
|
-
parts.push(`<rect x="${r2(cx)}" y="${r2(y)}" width="${r2(cw * cover)}" height="${r2(cellH)}" rx="1" fill="${sVar(0)}"/>`);
|
|
153
|
+
parts.push(`<rect x="${r2(cx)}" y="${r2(y)}" width="${r2(cw * cover)}" height="${r2(cellH)}" rx="1" fill="${sVar(0)}"${cellTip}/>`);
|
|
152
154
|
}
|
|
153
155
|
}
|
|
154
156
|
parts.push(`<text x="${r2(valueEndX)}" y="${r2(rowY + height * 0.06)}" text-anchor="end" font-size="18" font-weight="700" fill="var(--d-muted)" style="${TAB}">${Math.round(d.value * 10) / 10}%</text>`);
|
|
@@ -184,21 +186,23 @@ export function renderHbarDiverging(spec, o) {
|
|
|
184
186
|
const ly = shrunk ? 30 : height * 0.0811;
|
|
185
187
|
const lx1 = width * 0.4756;
|
|
186
188
|
const lx2 = width * 0.5756;
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
parts.push(`<circle cx="${r2(
|
|
190
|
-
parts.push(`<text x="${r2(
|
|
189
|
+
const lg0 = o.interactive ? ' data-legend="0"' : '';
|
|
190
|
+
const lg1 = o.interactive ? ' data-legend="1"' : '';
|
|
191
|
+
parts.push(`<circle cx="${r2(lx1)}" cy="${r2(shrunk ? 20 : height * 0.0541)}" r="5" fill="var(--d-s1)"${lg0}/>`);
|
|
192
|
+
parts.push(`<text x="${r2(lx1 + 16)}" y="${r2(ly)}" font-size="12" fill="var(--d-muted)"${lg0}>${esc(trunc(left.name, 12))}</text>`);
|
|
193
|
+
parts.push(`<circle cx="${r2(lx2)}" cy="${r2(shrunk ? 20 : height * 0.0541)}" r="5" fill="var(--d-s2)"${lg1}/>`);
|
|
194
|
+
parts.push(`<text x="${r2(lx2 + 16)}" y="${r2(ly)}" font-size="12" fill="var(--d-muted)"${lg1}>${esc(trunc(right.name, 12))}</text>`);
|
|
191
195
|
spec.labels.forEach((l, i) => {
|
|
192
196
|
const y = top + i * pitch;
|
|
193
197
|
const lw = (half * left.values[i]) / max;
|
|
194
198
|
const rw = (half * right.values[i]) / max;
|
|
195
199
|
parts.push(`<text x="${r2(width * 0.0707)}" y="${r2(y + barH * 0.77)}" font-size="16" fill="var(--d-muted)">${esc(trunc(l, lblCut))}</text>`);
|
|
196
|
-
parts.push(`<text x="${r2(width * 0.2707)}" y="${r2(y + barH * 0.77)}" text-anchor="end" font-size="15" font-weight="700" fill="var(--d-text)" style="${TAB}">${fmtFull(left.values[i])}</text>`);
|
|
200
|
+
parts.push(`<text x="${r2(width * 0.2707)}" y="${r2(y + barH * 0.77)}" text-anchor="end" font-size="15" font-weight="700" fill="var(--d-text)" style="${TAB}"${o.interactive ? ' data-series="0"' : ''}>${fmtFull(left.values[i])}</text>`);
|
|
197
201
|
parts.push(`<rect x="${r2(width * 0.2878)}" y="${r2(y)}" width="${r2(half)}" height="${r2(barH)}" rx="4" fill="var(--d-faint)"/>`);
|
|
198
|
-
parts.push(`<rect x="${r2(center - lw - gap)}" y="${r2(y)}" width="${r2(lw)}" height="${r2(barH)}" rx="4" fill="var(--d-s1)"/>`);
|
|
202
|
+
parts.push(`<rect x="${r2(center - lw - gap)}" y="${r2(y)}" width="${r2(lw)}" height="${r2(barH)}" rx="4" fill="var(--d-s1)"${o.interactive ? ` data-series="0" data-tip="${esc(`${l} · ${left.name}: ${fmtFull(left.values[i])}${spec.unit ? ' ' + spec.unit : ''}`)}"` : ''}/>`);
|
|
199
203
|
parts.push(`<rect x="${r2(center + gap)}" y="${r2(y)}" width="${r2(half)}" height="${r2(barH)}" rx="4" fill="var(--d-faint)"/>`);
|
|
200
|
-
parts.push(`<rect x="${r2(center + gap)}" y="${r2(y)}" width="${r2(rw)}" height="${r2(barH)}" rx="4" fill="var(--d-s2)"/>`);
|
|
201
|
-
parts.push(`<text x="${r2(width * 0.9268)}" y="${r2(y + barH * 0.77)}" font-size="15" font-weight="700" fill="var(--d-text)" style="${TAB}">${fmtFull(right.values[i])}</text>`);
|
|
204
|
+
parts.push(`<rect x="${r2(center + gap)}" y="${r2(y)}" width="${r2(rw)}" height="${r2(barH)}" rx="4" fill="var(--d-s2)"${o.interactive ? ` data-series="1" data-tip="${esc(`${l} · ${right.name}: ${fmtFull(right.values[i])}${spec.unit ? ' ' + spec.unit : ''}`)}"` : ''}/>`);
|
|
205
|
+
parts.push(`<text x="${r2(width * 0.9268)}" y="${r2(y + barH * 0.77)}" font-size="15" font-weight="700" fill="var(--d-text)" style="${TAB}"${o.interactive ? ' data-series="1"' : ''}>${fmtFull(right.values[i])}</text>`);
|
|
202
206
|
});
|
|
203
207
|
return svgDoc(width, height, 'hbar', version, spec.title ?? 'issues', spec, skin, parts.join(''));
|
|
204
208
|
}
|
|
@@ -252,9 +256,10 @@ export function renderHbarLift(spec, o) {
|
|
|
252
256
|
parts.push(`<text x="${r2(width * 0.0292)}" y="${r2(y + barH * 0.73)}" font-size="12" fill="var(--d-muted)" style="${TAB}">${i + 1}</text>`);
|
|
253
257
|
parts.push(`<rect x="${r2(width * 0.049)}" y="${r2(y + barH * 0.09)}" width="3" height="${r2(barH * 0.82)}" fill="${i === 0 ? '#E5A719' : 'var(--d-muted)'}"/>`);
|
|
254
258
|
parts.push(`<text x="${r2(width * 0.075)}" y="${r2(y + barH * 0.77)}" font-size="14" fill="var(--d-text)">${esc(trunc(l, nameCut))}</text>`);
|
|
255
|
-
|
|
259
|
+
const liftTip = tipOf(o, `${l} · ${fmtFull(total)} +${fmtFull(lift.values[i])}${spec.unit ? ' ' + spec.unit : ''}`);
|
|
260
|
+
parts.push(`<rect x="${r2(x0)}" y="${r2(y)}" width="${r2(bw)}" height="${r2(barH)}" rx="1" fill="${baseColor}"${liftTip}/>`);
|
|
256
261
|
if (tw > bw) {
|
|
257
|
-
parts.push(`<rect x="${r2(x0 + bw)}" y="${r2(y)}" width="${r2(tw - bw)}" height="${r2(barH)}" rx="1" fill="url(#dgl-hatch)" stroke="${baseColor}"/>`);
|
|
262
|
+
parts.push(`<rect x="${r2(x0 + bw)}" y="${r2(y)}" width="${r2(tw - bw)}" height="${r2(barH)}" rx="1" fill="url(#dgl-hatch)" stroke="${baseColor}"${liftTip}/>`);
|
|
258
263
|
}
|
|
259
264
|
parts.push(`<text x="${r2(x0 + tw + 8)}" y="${r2(y + barH * 0.73)}" font-size="12" fill="var(--d-muted)" style="${TAB}">${fmtFull(total)} +${fmtFull(lift.values[i])}</text>`);
|
|
260
265
|
});
|
package/dist/render/hbar.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TAB, card } from '../theme.js';
|
|
2
|
-
import { esc, fmtNum, niceTicks, r2, svgDoc, textW, titleBlock, trunc } from './svg.js';
|
|
2
|
+
import { esc, fmtFull, fmtNum, niceTicks, r2, svgDoc, textW, titleBlock, trunc } from './svg.js';
|
|
3
3
|
export function renderHBar(spec, o) {
|
|
4
4
|
const { width, version, skin } = o;
|
|
5
5
|
let height = o.height;
|
|
@@ -43,7 +43,8 @@ export function renderHBar(spec, o) {
|
|
|
43
43
|
// 仅右端(leading端)圆角
|
|
44
44
|
const path = `M${r2(x0)} ${r2(y)}H${r2(x1 - m)}Q${r2(x1)} ${r2(y)} ${r2(x1)} ${r2(y + m)}` +
|
|
45
45
|
`V${r2(y + barH - m)}Q${r2(x1)} ${r2(y + barH)} ${r2(x1 - m)} ${r2(y + barH)}H${r2(x0)}Z`;
|
|
46
|
-
|
|
46
|
+
const tip = o.interactive ? ` data-tip="${esc(`${d.label} · ${fmtFull(d.value)}${spec.unit ? ' ' + spec.unit : ''}`)}"` : '';
|
|
47
|
+
parts.push(`<path d="${path}" fill="url(#dg-bar)"${tip}/>`);
|
|
47
48
|
parts.push(`<text x="${r2(pad + labelW)}" y="${r2(y + barH / 2 + 4)}" text-anchor="end" font-size="12.5" fill="var(--d-text)">${esc(trunc(d.label, 16))}</text>`);
|
|
48
49
|
if (d.value > 0 && (num === 'all' || (num === 'peak' && isPeak))) { // 零值不标(标注克制)
|
|
49
50
|
parts.push(`<text x="${r2(x1 + 8)}" y="${r2(y + barH / 2 + 4)}" font-size="${isPeak ? 13 : 11}" ${isPeak ? 'font-weight="600" fill="' + 'var(--d-ink)' + '"' : 'fill="' + 'var(--d-text)' + '"'} style="${TAB}">${fmtNum(d.value)}</text>`);
|
package/dist/render/pie.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { card, TAB } from '../theme.js';
|
|
2
|
-
import { esc, fmtFull, fmtNum, r2, textW, trunc, svgDoc, editorialTitle, titleBlock, sVar,
|
|
2
|
+
import { esc, fmtFull, fmtNum, r2, textW, trunc, svgDoc, editorialTitle, titleBlock, sVar, LabelPlacer, } from './svg.js';
|
|
3
3
|
import { radialFit } from './measure.js';
|
|
4
4
|
const MAX_SLICES = 9; // 8 个分类 + "Other"
|
|
5
5
|
function buildRows(data, seriesCount) {
|
|
@@ -19,7 +19,7 @@ function buildRows(data, seriesCount) {
|
|
|
19
19
|
return rows;
|
|
20
20
|
}
|
|
21
21
|
const rowColor = (i) => (i === -1 ? 'var(--d-faint)' : sVar(i));
|
|
22
|
-
const
|
|
22
|
+
const fillOf = (row) => row.color ?? rowColor(row.colorIdx);
|
|
23
23
|
// ECharts 极坐标(0=3 点钟,正角逆时针);圆角扇区恒 a0→a1 逆时针,
|
|
24
24
|
// 外弧两端切线 fillet(Evil 系签名),角半径受跨距/环宽约束。
|
|
25
25
|
export function pol(cx, cy, rad, a) {
|
|
@@ -143,7 +143,7 @@ export function renderPie(spec, o) {
|
|
|
143
143
|
}).join('');
|
|
144
144
|
parts.push(`<defs>${defs}</defs>`);
|
|
145
145
|
}
|
|
146
|
-
const sliceFill = (i) => (rules.pieGradient ? `url(#dgs-${i})` :
|
|
146
|
+
const sliceFill = (i) => (rules.pieGradient && !rows[i].color ? `url(#dgs-${i})` : fillOf(rows[i]));
|
|
147
147
|
// Interactive embedders: preformatted tooltip text (value + share) on each slice
|
|
148
148
|
const tipAttr = (row) => o.interactive
|
|
149
149
|
? ` data-tip="${esc(`${row.label} · ${fmtFull(row.value)}${spec.unit ? ' ' + spec.unit : ''} (${((row.value / total) * 100).toFixed(1)}%)`)}"`
|
|
@@ -176,7 +176,7 @@ export function renderPie(spec, o) {
|
|
|
176
176
|
cum += frac;
|
|
177
177
|
const d = segPath(cx, cy, R, r, lo, hi, rules.pieCorner);
|
|
178
178
|
if (d) {
|
|
179
|
-
const fill = rules.pieGradient ? `url(#dgs-${rows.indexOf(row)})` :
|
|
179
|
+
const fill = rules.pieGradient && !row.color ? `url(#dgs-${rows.indexOf(row)})` : fillOf(row);
|
|
180
180
|
const stroke = gap > 0 ? ` stroke="var(--d-halo)" stroke-width="${gap}" stroke-linejoin="round" stroke-linecap="round"` : '';
|
|
181
181
|
parts.push(`<path d="${d}" fill="${fill}"${stroke}${tipAttr(row)}/>`);
|
|
182
182
|
}
|
|
@@ -324,7 +324,7 @@ export function renderPie(spec, o) {
|
|
|
324
324
|
const x = pad + (i % cols) * colW;
|
|
325
325
|
const y = legendTop + Math.floor(i / cols) * 26;
|
|
326
326
|
const pct = Math.round((row.value / total) * 100);
|
|
327
|
-
parts.push(`<rect x="${r2(x)}" y="${r2(y - 8)}" width="9" height="9" rx="2" fill="${
|
|
327
|
+
parts.push(`<rect x="${r2(x)}" y="${r2(y - 8)}" width="9" height="9" rx="2" fill="${fillOf(row)}"/>`);
|
|
328
328
|
parts.push(`<text x="${r2(x + 15)}" y="${r2(y)}" font-size="12" font-weight="500" fill="var(--d-ink)">${esc(trunc(row.label, 14))}</text>`);
|
|
329
329
|
if (rules.legendValue) {
|
|
330
330
|
parts.push(`<text x="${r2(x + colW - 8)}" y="${r2(y)}" text-anchor="end" font-size="12" style="${TAB}">` +
|
|
@@ -347,7 +347,7 @@ export function renderPie(spec, o) {
|
|
|
347
347
|
x = pad;
|
|
348
348
|
y += 24;
|
|
349
349
|
}
|
|
350
|
-
parts.push(`<rect x="${r2(x)}" y="${r2(y - 8)}" width="9" height="9" rx="2" fill="${
|
|
350
|
+
parts.push(`<rect x="${r2(x)}" y="${r2(y - 8)}" width="9" height="9" rx="2" fill="${fillOf(it.row)}"/>`);
|
|
351
351
|
parts.push(`<text x="${r2(x + 15)}" y="${r2(y)}" font-size="12" font-weight="500" fill="var(--d-muted)">${esc(it.name)}</text>`);
|
|
352
352
|
x += it.w;
|
|
353
353
|
}
|
|
@@ -361,7 +361,7 @@ export function renderPie(spec, o) {
|
|
|
361
361
|
const firstY = legendTop + (legendAvail - rowStep * (rows.length - 1)) / 2;
|
|
362
362
|
rows.forEach((row, i) => {
|
|
363
363
|
const y = firstY + i * rowStep;
|
|
364
|
-
parts.push(`<rect x="${r2(lx)}" y="${r2(y - 8)}" width="10" height="10" rx="2" fill="${
|
|
364
|
+
parts.push(`<rect x="${r2(lx)}" y="${r2(y - 8)}" width="10" height="10" rx="2" fill="${fillOf(row)}"/>`);
|
|
365
365
|
parts.push(`<text x="${r2(lx + 16)}" y="${r2(y)}" font-size="12" font-weight="500" fill="var(--d-muted)">${esc(trunc(row.label, 14))}</text>`);
|
|
366
366
|
if (rules.legendValue) {
|
|
367
367
|
parts.push(`<text x="${r2(width - pad)}" y="${r2(y)}" text-anchor="end" font-size="12" font-weight="500" fill="var(--d-ink)" style="${TAB}">${fmtFull(row.value)}</text>`);
|
package/dist/render/svg.d.ts
CHANGED
|
@@ -84,16 +84,18 @@ export declare function pushBarLabelsAllOrNothing(parts: string[], items: readon
|
|
|
84
84
|
fyt?: number;
|
|
85
85
|
haloColor?: string;
|
|
86
86
|
insideOnly?: boolean;
|
|
87
|
+
series?: number;
|
|
87
88
|
}[], sharedPlacer?: LabelPlacer, segments?: readonly LineSeg[]): boolean;
|
|
88
89
|
export declare function pushLineLabelsAllOrNothing(parts: string[], placer: LabelPlacer, items: readonly {
|
|
89
90
|
px: number;
|
|
90
91
|
py: number;
|
|
91
92
|
text: string;
|
|
92
93
|
isPeak: boolean;
|
|
94
|
+
series?: number;
|
|
93
95
|
}[], segments?: readonly LineSeg[]): boolean;
|
|
94
96
|
export declare function segRectIntersect(x1: number, y1: number, x2: number, y2: number, rx: number, ry: number, rw: number, rh: number): boolean;
|
|
95
97
|
/** 折线障碍段:文字不得压线(线横穿数字会看不清) */
|
|
96
98
|
export type LineSeg = readonly [number, number, number, number];
|
|
97
99
|
/** 由点序列构造折线障碍段 */
|
|
98
100
|
export declare function toSegments(pts: readonly (readonly [number, number])[]): LineSeg[];
|
|
99
|
-
export declare function pushLineLabel(parts: string[], placer: LabelPlacer, px: number, py: number, text: string, isPeak: boolean, segments?: readonly LineSeg[]): void;
|
|
101
|
+
export declare function pushLineLabel(parts: string[], placer: LabelPlacer, px: number, py: number, text: string, isPeak: boolean, segments?: readonly LineSeg[], series?: number): void;
|
package/dist/render/svg.js
CHANGED
|
@@ -322,6 +322,7 @@ export function pushPoints(parts, pts, color, rules) {
|
|
|
322
322
|
// 全有或全无的柱内数值标注:入柱(字号可缩到 7px)→ 回退柱顶/柱右侧 → 整层不标
|
|
323
323
|
export function pushBarLabelsAllOrNothing(parts, items, sharedPlacer, segments = []) {
|
|
324
324
|
const out = [];
|
|
325
|
+
const ds = (s) => (s === undefined ? '' : ` data-series="${s}"`);
|
|
325
326
|
const placer = sharedPlacer ?? new LabelPlacer();
|
|
326
327
|
for (const l of items) {
|
|
327
328
|
// 优先入柱:宽度/高度不够先自动缩字号(下限 7px)
|
|
@@ -352,7 +353,7 @@ export function pushBarLabelsAllOrNothing(parts, items, sharedPlacer, segments =
|
|
|
352
353
|
if (!placer.fits(bx, ty - fs, tw + 2, fs + 2))
|
|
353
354
|
continue;
|
|
354
355
|
const halo = l.haloColor ? `paint-order="stroke" stroke="${l.haloColor}" stroke-width="3" stroke-linejoin="round" ` : '';
|
|
355
|
-
out.push(`<text x="${r2(cx)}" y="${r2(ty)}" text-anchor="middle" font-size="${r2(fs)}" ${l.peak ? 'font-weight="600" ' : ''}fill="${l.fill}" ${halo}style="${TAB}">${l.text}</text>`);
|
|
356
|
+
out.push(`<text x="${r2(cx)}" y="${r2(ty)}" text-anchor="middle" font-size="${r2(fs)}" ${l.peak ? 'font-weight="600" ' : ''}fill="${l.fill}" ${halo}style="${TAB}"${ds(l.series)}>${l.text}</text>`);
|
|
356
357
|
placed = true;
|
|
357
358
|
break;
|
|
358
359
|
}
|
|
@@ -379,7 +380,7 @@ export function pushBarLabelsAllOrNothing(parts, items, sharedPlacer, segments =
|
|
|
379
380
|
continue;
|
|
380
381
|
if (!placer.fits(bx, ty - l.fs, fTw + 2, l.fs + 2))
|
|
381
382
|
continue;
|
|
382
|
-
out.push(`<text x="${r2(bx)}" y="${r2(ty)}" text-anchor="start" font-size="${l.fs}" ${l.peak ? 'font-weight="600" ' : ''}fill="${l.fallFill ?? 'var(--d-ink)'}" ${HALO} style="${TAB}">${l.text}</text>`);
|
|
383
|
+
out.push(`<text x="${r2(bx)}" y="${r2(ty)}" text-anchor="start" font-size="${l.fs}" ${l.peak ? 'font-weight="600" ' : ''}fill="${l.fallFill ?? 'var(--d-ink)'}" ${HALO} style="${TAB}"${ds(l.series)}>${l.text}</text>`);
|
|
383
384
|
ok = true;
|
|
384
385
|
break;
|
|
385
386
|
}
|
|
@@ -393,7 +394,7 @@ export function pushBarLabelsAllOrNothing(parts, items, sharedPlacer, segments =
|
|
|
393
394
|
const bx = cx - fTw / 2 - 1;
|
|
394
395
|
if (!placer.fits(bx, ty - l.fs, fTw + 2, l.fs + 2))
|
|
395
396
|
return false;
|
|
396
|
-
out.push(`<text x="${r2(cx)}" y="${r2(ty)}" text-anchor="middle" font-size="${l.fs}" ${l.peak ? 'font-weight="600" ' : ''}fill="var(--d-ink)" paint-order="stroke" stroke="var(--d-halo)" stroke-width="3" stroke-linejoin="round" style="${TAB}">${l.text}</text>`);
|
|
397
|
+
out.push(`<text x="${r2(cx)}" y="${r2(ty)}" text-anchor="middle" font-size="${l.fs}" ${l.peak ? 'font-weight="600" ' : ''}fill="var(--d-ink)" paint-order="stroke" stroke="var(--d-halo)" stroke-width="3" stroke-linejoin="round" style="${TAB}"${ds(l.series)}>${l.text}</text>`);
|
|
397
398
|
}
|
|
398
399
|
}
|
|
399
400
|
parts.push(...out);
|
|
@@ -404,7 +405,7 @@ export function pushLineLabelsAllOrNothing(parts, placer, items, segments = [])
|
|
|
404
405
|
const out = [];
|
|
405
406
|
for (const l of items) {
|
|
406
407
|
const probe = [];
|
|
407
|
-
pushLineLabel(probe, placer, l.px, l.py, l.text, l.isPeak, segments);
|
|
408
|
+
pushLineLabel(probe, placer, l.px, l.py, l.text, l.isPeak, segments, l.series);
|
|
408
409
|
if (probe.length === 0)
|
|
409
410
|
return false;
|
|
410
411
|
out.push(...probe);
|
|
@@ -451,7 +452,7 @@ export function toSegments(pts) {
|
|
|
451
452
|
return out;
|
|
452
453
|
}
|
|
453
454
|
// 线点数值标签:近位(上下左右)→ 远位,共 8 个候选;还需避开折线本身
|
|
454
|
-
export function pushLineLabel(parts, placer, px, py, text, isPeak, segments = []) {
|
|
455
|
+
export function pushLineLabel(parts, placer, px, py, text, isPeak, segments = [], series) {
|
|
455
456
|
const fs = isPeak ? 13 : 11;
|
|
456
457
|
const w = textW(text, fs);
|
|
457
458
|
const candidates = [
|
|
@@ -473,7 +474,7 @@ export function pushLineLabel(parts, placer, px, py, text, isPeak, segments = []
|
|
|
473
474
|
if (relevant.some(([sx1, sy1, sx2, sy2]) => segRectIntersect(sx1, sy1, sx2, sy2, bx - 1, by - 1, w + 2, fs + 2)))
|
|
474
475
|
continue;
|
|
475
476
|
if (placer.fits(bx, by, w, fs)) {
|
|
476
|
-
parts.push(`<text x="${r2(c.x)}" y="${r2(c.y)}" text-anchor="${c.anchor}" font-size="${fs}" ${isPeak ? 'font-weight="600" ' : ''}fill="${isPeak ? 'var(--d-ink)' : 'var(--d-text)'}" ${HALO} style="${TAB}">${text}</text>`);
|
|
477
|
+
parts.push(`<text x="${r2(c.x)}" y="${r2(c.y)}" text-anchor="${c.anchor}" font-size="${fs}" ${isPeak ? 'font-weight="600" ' : ''}fill="${isPeak ? 'var(--d-ink)' : 'var(--d-text)'}" ${HALO} style="${TAB}"${series === undefined ? '' : ` data-series="${series}"`}>${text}</text>`);
|
|
477
478
|
return;
|
|
478
479
|
}
|
|
479
480
|
}
|
package/dist/specs/base.d.ts
CHANGED
|
@@ -12,6 +12,8 @@ export declare class ApiError extends Error {
|
|
|
12
12
|
export interface Datum {
|
|
13
13
|
label: string;
|
|
14
14
|
value: number;
|
|
15
|
+
/** Optional explicit color (#rrggbb); renderers that color per datum (pie) honor it. */
|
|
16
|
+
color?: string;
|
|
15
17
|
}
|
|
16
18
|
/**
|
|
17
19
|
* Single-series chart data (pie / bar / hbar / line / area / radar).
|
package/dist/specs/base.js
CHANGED
|
@@ -114,7 +114,14 @@ export function parseChartSpec(raw, kind) {
|
|
|
114
114
|
if (d.value < 0) {
|
|
115
115
|
throw new ApiError(400, `data[${i}].value must be >= 0, got ${d.value}`);
|
|
116
116
|
}
|
|
117
|
-
|
|
117
|
+
let color;
|
|
118
|
+
if (d.color !== undefined) {
|
|
119
|
+
if (typeof d.color !== 'string' || !/^#[0-9a-f]{6}$/i.test(d.color)) {
|
|
120
|
+
throw new ApiError(400, `data[${i}].color must be a hex color like "#6366f1", got ${JSON.stringify(d.color)}`);
|
|
121
|
+
}
|
|
122
|
+
color = d.color.toLowerCase();
|
|
123
|
+
}
|
|
124
|
+
return color ? { label, value: d.value, color } : { label, value: d.value };
|
|
118
125
|
});
|
|
119
126
|
const total = data.reduce((s, d) => s + d.value, 0);
|
|
120
127
|
if (!(total > 0)) {
|
|
@@ -148,8 +155,13 @@ export function specFromParams(params, kind) {
|
|
|
148
155
|
throw new ApiError(400, `values[${i}] is not a number: ${JSON.stringify(v)}`);
|
|
149
156
|
return n;
|
|
150
157
|
});
|
|
158
|
+
// ?colors=%23aa00ff,...:parallel to labels, per-datum explicit colors (pie etc.)
|
|
159
|
+
const colors = flatList(params, 'colors');
|
|
151
160
|
const obj = {
|
|
152
|
-
data: labels.map((label, i) =>
|
|
161
|
+
data: labels.map((label, i) => {
|
|
162
|
+
const c = colors[i];
|
|
163
|
+
return c && /^#[0-9a-f]{6}$/i.test(c) ? { label, value: nums[i], color: c.toLowerCase() } : { label, value: nums[i] };
|
|
164
|
+
}),
|
|
153
165
|
};
|
|
154
166
|
const title = params.get('title');
|
|
155
167
|
if (title)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x-cmd/draw",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.5",
|
|
4
4
|
"description": "SVG chart rendering as a service, a library, and a CLI — pie/bar/hbar/line/area/radar/gauge/scatter/combo/sankey/qr, same params everywhere",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|