@officedk/pptxdk 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 +21 -0
- package/dist/build-zrZuX4fK.d.ts +3 -0
- package/dist/build.d.ts +35 -0
- package/dist/build.js +128 -0
- package/dist/capabilities/dist/all.js +34 -0
- package/dist/capabilities/dist/box/box-codegen.js +84 -0
- package/dist/capabilities/dist/box/box-declaration.js +32 -0
- package/dist/capabilities/dist/box/box-elements.js +17 -0
- package/dist/capabilities/dist/box/box.js +142 -0
- package/dist/capabilities/dist/chart/chart-codegen.js +79 -0
- package/dist/capabilities/dist/chart/chart-declaration.js +7 -0
- package/dist/capabilities/dist/chart/chart-elements.js +12 -0
- package/dist/capabilities/dist/chart/chart-import.js +2403 -0
- package/dist/capabilities/dist/chart/chart-labels.js +86 -0
- package/dist/capabilities/dist/chart/chart-model.js +9 -0
- package/dist/capabilities/dist/chart/chart-schemas.js +1088 -0
- package/dist/capabilities/dist/chart/chart-xml.js +788 -0
- package/dist/capabilities/dist/chart/chart.js +1119 -0
- package/dist/capabilities/dist/chart/preview/axes-and-grid.js +311 -0
- package/dist/capabilities/dist/chart/preview/bar-column.js +143 -0
- package/dist/capabilities/dist/chart/preview/data-labels.js +334 -0
- package/dist/capabilities/dist/chart/preview/error-bars.js +213 -0
- package/dist/capabilities/dist/chart/preview/index.js +192 -0
- package/dist/capabilities/dist/chart/preview/layout.js +592 -0
- package/dist/capabilities/dist/chart/preview/legend.js +273 -0
- package/dist/capabilities/dist/chart/preview/line-area.js +404 -0
- package/dist/capabilities/dist/chart/preview/markers.js +100 -0
- package/dist/capabilities/dist/chart/preview/number-format.js +247 -0
- package/dist/capabilities/dist/chart/preview/pie-doughnut.js +159 -0
- package/dist/capabilities/dist/chart/preview/scales.js +365 -0
- package/dist/capabilities/dist/chart/preview/scatter.js +131 -0
- package/dist/capabilities/dist/chart/preview/style.js +60 -0
- package/dist/capabilities/dist/chart/preview/text-render.js +116 -0
- package/dist/capabilities/dist/chart/preview/trendlines.js +537 -0
- package/dist/capabilities/dist/chart/preview/types.js +5 -0
- package/dist/capabilities/dist/document.js +168 -0
- package/dist/capabilities/dist/font/font-embedding.js +119 -0
- package/dist/capabilities/dist/font/font-engine.js +834 -0
- package/dist/capabilities/dist/font/font-import.js +766 -0
- package/dist/capabilities/dist/font/font-subset.js +55 -0
- package/dist/capabilities/dist/font/font.js +72 -0
- package/dist/capabilities/dist/group/group-codegen.js +74 -0
- package/dist/capabilities/dist/group/group-declaration.js +41 -0
- package/dist/capabilities/dist/group/group-elements.js +12 -0
- package/dist/capabilities/dist/group/group-import.js +649 -0
- package/dist/capabilities/dist/group/group-xml.js +8 -0
- package/dist/capabilities/dist/group/group.js +260 -0
- package/dist/capabilities/dist/image/gif-decoder.js +244 -0
- package/dist/capabilities/dist/image/image-codegen.js +164 -0
- package/dist/capabilities/dist/image/image-declaration.js +8 -0
- package/dist/capabilities/dist/image/image-decode.js +244 -0
- package/dist/capabilities/dist/image/image-elements.js +12 -0
- package/dist/capabilities/dist/image/image-import.js +984 -0
- package/dist/capabilities/dist/image/image-schemas.js +147 -0
- package/dist/capabilities/dist/image/image-xml.js +12 -0
- package/dist/capabilities/dist/image/image.js +552 -0
- package/dist/capabilities/dist/line/line-codegen.js +89 -0
- package/dist/capabilities/dist/line/line-declaration.js +72 -0
- package/dist/capabilities/dist/line/line-elements.js +12 -0
- package/dist/capabilities/dist/line/line-import.js +504 -0
- package/dist/capabilities/dist/line/line-xml.js +9 -0
- package/dist/capabilities/dist/line/line.js +331 -0
- package/dist/capabilities/dist/masters/footer-placeholder.js +683 -0
- package/dist/capabilities/dist/masters/masters-codegen.js +69 -0
- package/dist/capabilities/dist/masters/masters.js +250 -0
- package/dist/capabilities/dist/motion/motion-api.js +757 -0
- package/dist/capabilities/dist/motion/motion-codegen.js +406 -0
- package/dist/capabilities/dist/motion/motion-elements.js +43 -0
- package/dist/capabilities/dist/motion/motion-import.js +1568 -0
- package/dist/capabilities/dist/motion/motion-plan.js +2340 -0
- package/dist/capabilities/dist/motion/motion-query.js +423 -0
- package/dist/capabilities/dist/motion/motion-schemas.js +339 -0
- package/dist/capabilities/dist/motion/motion.js +1617 -0
- package/dist/capabilities/dist/notes/notes-codegen.js +19 -0
- package/dist/capabilities/dist/notes/notes-declaration.js +8 -0
- package/dist/capabilities/dist/notes/notes-elements.js +12 -0
- package/dist/capabilities/dist/notes/notes-import.js +292 -0
- package/dist/capabilities/dist/notes/notes-schemas.js +38 -0
- package/dist/capabilities/dist/notes/notes-xml.js +38 -0
- package/dist/capabilities/dist/notes/notes.js +194 -0
- package/dist/capabilities/dist/placeholder/placeholder-codegen.js +54 -0
- package/dist/capabilities/dist/placeholder/placeholder-elements.js +13 -0
- package/dist/capabilities/dist/placeholder/placeholder-schemas.js +33 -0
- package/dist/capabilities/dist/placeholder/placeholder.js +451 -0
- package/dist/capabilities/dist/shape/shape-codegen.js +236 -0
- package/dist/capabilities/dist/shape/shape-declaration.js +45 -0
- package/dist/capabilities/dist/shape/shape-elements.js +12 -0
- package/dist/capabilities/dist/shape/shape-import.js +628 -0
- package/dist/capabilities/dist/shape/shape-xml.js +6 -0
- package/dist/capabilities/dist/shape/shape.js +320 -0
- package/dist/capabilities/dist/shared/geometry.js +1681 -0
- package/dist/capabilities/dist/shared/identity.js +205 -0
- package/dist/capabilities/dist/shared/morph-api.js +11 -0
- package/dist/capabilities/dist/shared/placeholder-import.js +238 -0
- package/dist/capabilities/dist/shared/preset-definitions.js +22423 -0
- package/dist/capabilities/dist/shared/style-inheritance.js +161 -0
- package/dist/capabilities/dist/shared/text-schemas.js +241 -0
- package/dist/capabilities/dist/shared/text-service.js +2252 -0
- package/dist/capabilities/dist/shared/visual.js +1197 -0
- package/dist/capabilities/dist/slide-background/slide-background-codegen.js +44 -0
- package/dist/capabilities/dist/slide-background/slide-background-import.js +166 -0
- package/dist/capabilities/dist/slide-background/slide-background-xml.js +34 -0
- package/dist/capabilities/dist/slide-background/slide-background.js +238 -0
- package/dist/capabilities/dist/table/table-codegen.js +125 -0
- package/dist/capabilities/dist/table/table-declaration.js +15 -0
- package/dist/capabilities/dist/table/table-elements.js +27 -0
- package/dist/capabilities/dist/table/table-import.js +606 -0
- package/dist/capabilities/dist/table/table-schemas.js +334 -0
- package/dist/capabilities/dist/table/table-xml.js +30 -0
- package/dist/capabilities/dist/table/table.js +739 -0
- package/dist/capabilities/dist/text/text-codegen.js +198 -0
- package/dist/capabilities/dist/text/text-declaration.js +27 -0
- package/dist/capabilities/dist/text/text-elements.js +54 -0
- package/dist/capabilities/dist/text/text-import.js +1224 -0
- package/dist/capabilities/dist/text/text-xml.js +9 -0
- package/dist/capabilities/dist/text/text.js +320 -0
- package/dist/capabilities/dist/transition/transition-api.js +136 -0
- package/dist/capabilities/dist/transition/transition-codegen.js +45 -0
- package/dist/capabilities/dist/transition/transition-import.js +477 -0
- package/dist/capabilities/dist/transition/transition.js +260 -0
- package/dist/capability-BbKW9w4O.d.ts +2131 -0
- package/dist/cli.js +20 -0
- package/dist/codegen/dist/assets.js +79 -0
- package/dist/codegen/dist/codegen.js +691 -0
- package/dist/codegen/dist/identifiers.js +203 -0
- package/dist/codegen/dist/index.js +3 -0
- package/dist/codegen/dist/printer.js +143 -0
- package/dist/codegen/dist/types.js +77 -0
- package/dist/core/dist/author-ids.js +51 -0
- package/dist/core/dist/author-schemas.js +330 -0
- package/dist/core/dist/block-flex-layout.js +801 -0
- package/dist/core/dist/build.js +4 -0
- package/dist/core/dist/capability.js +15 -0
- package/dist/core/dist/declaration.js +16 -0
- package/dist/core/dist/diagnostics.js +353 -0
- package/dist/core/dist/document-internals.js +26 -0
- package/dist/core/dist/document.js +417 -0
- package/dist/core/dist/font-seal.js +204 -0
- package/dist/core/dist/geometry-schemas.js +256 -0
- package/dist/core/dist/grid-layout.js +449 -0
- package/dist/core/dist/host.js +40 -0
- package/dist/core/dist/index.js +15 -0
- package/dist/core/dist/input-values.js +61 -0
- package/dist/core/dist/layout-protocol.js +61 -0
- package/dist/core/dist/layout-schemas.js +201 -0
- package/dist/core/dist/layout.js +340 -0
- package/dist/core/dist/masters.js +194 -0
- package/dist/core/dist/morph.js +30 -0
- package/dist/core/dist/placeholders.js +275 -0
- package/dist/core/dist/query.js +190 -0
- package/dist/core/dist/resolution.js +12 -0
- package/dist/core/dist/resolve.js +2246 -0
- package/dist/core/dist/stage-options.js +300 -0
- package/dist/core/dist/theme.js +1023 -0
- package/dist/core/dist/transform-geometry.js +72 -0
- package/dist/document-41spD7bq.d.ts +1350 -0
- package/dist/emit/dist/emit.js +957 -0
- package/dist/emit/dist/index.js +5 -0
- package/dist/emit/dist/ooxml.js +661 -0
- package/dist/emit/dist/shape-tree.js +12 -0
- package/dist/import/dist/alternate-content.js +54 -0
- package/dist/import/dist/diagnostics.js +157 -0
- package/dist/import/dist/fonts.js +309 -0
- package/dist/import/dist/import.js +513 -0
- package/dist/import/dist/index.js +16 -0
- package/dist/import/dist/masters.js +1293 -0
- package/dist/import/dist/mechanism.js +10 -0
- package/dist/import/dist/namespaces.js +61 -0
- package/dist/import/dist/notes-masters.js +300 -0
- package/dist/import/dist/opc.js +233 -0
- package/dist/import/dist/resources.js +166 -0
- package/dist/import/dist/sections.js +139 -0
- package/dist/import/dist/themes.js +777 -0
- package/dist/import/dist/visual.js +821 -0
- package/dist/import/dist/xml-fragments.js +277 -0
- package/dist/import/dist/xml-reader.js +267 -0
- package/dist/index-1ag7U9Jt.d.ts +63 -0
- package/dist/index-BEwhk7c8.d.ts +72 -0
- package/dist/index-cK2KtukZ.d.ts +161 -0
- package/dist/index-v77CTJev.d.ts +55 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +82 -0
- package/dist/node.d.ts +53 -0
- package/dist/node.js +225 -0
- package/dist/pptxdk/src/cli-commands.js +907 -0
- package/dist/pptxdk/src/compose.js +53 -0
- package/dist/pptxdk/src/slide.js +9 -0
- package/dist/preview/dist/index.js +342 -0
- package/dist/preview/dist/options.js +40 -0
- package/dist/preview/dist/svg-guard.js +1089 -0
- package/dist/preview.d.ts +12 -0
- package/dist/preview.js +29 -0
- package/dist/react/dist/append.js +66 -0
- package/dist/react/dist/document-adapter.js +404 -0
- package/dist/react/dist/elements.js +82 -0
- package/dist/react/dist/from-vanilla.js +57 -0
- package/dist/react/dist/index.js +89 -0
- package/dist/react/dist/mount.js +34 -0
- package/dist/react/dist/reconciler.js +290 -0
- package/dist/react.d.ts +235 -0
- package/dist/react.js +31 -0
- package/dist/transition-6n8GZxSg.d.ts +9 -0
- package/dist/vanilla.d.ts +4 -0
- package/dist/vanilla.js +15 -0
- package/package.json +79 -3
- package/README.md +0 -3
|
@@ -0,0 +1,537 @@
|
|
|
1
|
+
import { CapabilityError } from "../../../../core/dist/diagnostics.js";
|
|
2
|
+
import "../../../../core/dist/capability.js";
|
|
3
|
+
import { chartAccentSlot, resolveChartLineStroke, strokeAttrs } from "./style.js";
|
|
4
|
+
import { measureChartText, mergeChartTextStyle, renderChartTextSvg, resolveChartFontSizePixels } from "./text-render.js";
|
|
5
|
+
import { assertNever } from "./types.js";
|
|
6
|
+
// ../capabilities/dist/chart/preview/trendlines.js
|
|
7
|
+
function solveGaussianPartialPivoting(matrix, rhs) {
|
|
8
|
+
const n = rhs.length;
|
|
9
|
+
const a = matrix.map((row) => [...row]);
|
|
10
|
+
const b = [...rhs];
|
|
11
|
+
for (let col = 0; col < n; col += 1) {
|
|
12
|
+
let pivotRow = col;
|
|
13
|
+
let pivotAbs = Math.abs(a[col][col]);
|
|
14
|
+
for (let row = col + 1; row < n; row += 1) {
|
|
15
|
+
const candidate = Math.abs(a[row][col]);
|
|
16
|
+
if (candidate > pivotAbs) {
|
|
17
|
+
pivotAbs = candidate;
|
|
18
|
+
pivotRow = row;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
if (!Number.isFinite(pivotAbs) || pivotAbs < 1e-12) return;
|
|
22
|
+
if (pivotRow !== col) {
|
|
23
|
+
const tmpRow = a[col];
|
|
24
|
+
a[col] = a[pivotRow];
|
|
25
|
+
a[pivotRow] = tmpRow;
|
|
26
|
+
const tmpB = b[col];
|
|
27
|
+
b[col] = b[pivotRow];
|
|
28
|
+
b[pivotRow] = tmpB;
|
|
29
|
+
}
|
|
30
|
+
const diag = a[col][col];
|
|
31
|
+
for (let row = col + 1; row < n; row += 1) {
|
|
32
|
+
const factor = a[row][col] / diag;
|
|
33
|
+
if (factor === 0) continue;
|
|
34
|
+
for (let k = col; k < n; k += 1) a[row][k] = a[row][k] - factor * a[col][k];
|
|
35
|
+
b[row] = b[row] - factor * b[col];
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const sol = Array.from({ length: n }, () => 0);
|
|
39
|
+
for (let row = n - 1; row >= 0; row -= 1) {
|
|
40
|
+
let sum = b[row];
|
|
41
|
+
for (let col = row + 1; col < n; col += 1) sum -= a[row][col] * sol[col];
|
|
42
|
+
const val = sum / a[row][row];
|
|
43
|
+
if (!Number.isFinite(val)) return void 0;
|
|
44
|
+
sol[row] = val;
|
|
45
|
+
}
|
|
46
|
+
return sol;
|
|
47
|
+
}
|
|
48
|
+
function fitLinearOls(points, fixedIntercept) {
|
|
49
|
+
const n = points.length;
|
|
50
|
+
if (fixedIntercept !== void 0) {
|
|
51
|
+
if (n < 1) return void 0;
|
|
52
|
+
let sumXX = 0;
|
|
53
|
+
let sumXY = 0;
|
|
54
|
+
for (const pt of points) {
|
|
55
|
+
sumXX += pt.x * pt.x;
|
|
56
|
+
sumXY += pt.x * (pt.y - fixedIntercept);
|
|
57
|
+
}
|
|
58
|
+
if (sumXX <= 1e-15) return void 0;
|
|
59
|
+
return {
|
|
60
|
+
slope: sumXY / sumXX,
|
|
61
|
+
intercept: fixedIntercept
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
if (n < 2) return void 0;
|
|
65
|
+
let sumX = 0;
|
|
66
|
+
let sumY = 0;
|
|
67
|
+
for (const pt of points) {
|
|
68
|
+
sumX += pt.x;
|
|
69
|
+
sumY += pt.y;
|
|
70
|
+
}
|
|
71
|
+
const meanX = sumX / n;
|
|
72
|
+
const meanY = sumY / n;
|
|
73
|
+
let sxx = 0;
|
|
74
|
+
let sxy = 0;
|
|
75
|
+
for (const pt of points) {
|
|
76
|
+
const dx = pt.x - meanX;
|
|
77
|
+
sxx += dx * dx;
|
|
78
|
+
sxy += dx * (pt.y - meanY);
|
|
79
|
+
}
|
|
80
|
+
if (sxx <= 1e-15) return void 0;
|
|
81
|
+
const slope = sxy / sxx;
|
|
82
|
+
return {
|
|
83
|
+
slope,
|
|
84
|
+
intercept: meanY - slope * meanX
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
function computeRSquared(points, evalY) {
|
|
88
|
+
const n = points.length;
|
|
89
|
+
if (n === 0) return 1;
|
|
90
|
+
let sumY = 0;
|
|
91
|
+
for (const pt of points) sumY += pt.y;
|
|
92
|
+
const meanY = sumY / n;
|
|
93
|
+
let ssTot = 0;
|
|
94
|
+
let ssRes = 0;
|
|
95
|
+
for (const pt of points) {
|
|
96
|
+
const dy = pt.y - meanY;
|
|
97
|
+
ssTot += dy * dy;
|
|
98
|
+
const err = pt.y - evalY(pt.x);
|
|
99
|
+
ssRes += err * err;
|
|
100
|
+
}
|
|
101
|
+
if (ssTot <= 1e-15) return 1;
|
|
102
|
+
return Math.max(0, 1 - ssRes / ssTot);
|
|
103
|
+
}
|
|
104
|
+
function formatNum(value) {
|
|
105
|
+
const clean = Math.abs(value) < 1e-10 ? 0 : Number(value.toPrecision(4));
|
|
106
|
+
return String(clean);
|
|
107
|
+
}
|
|
108
|
+
function formatLinearTerm(slope, variable) {
|
|
109
|
+
return `${formatNum(slope)}${variable}`;
|
|
110
|
+
}
|
|
111
|
+
function formatSignedOffset(value) {
|
|
112
|
+
const clean = Math.abs(value) < 1e-10 ? 0 : value;
|
|
113
|
+
if (clean === 0) return "";
|
|
114
|
+
return clean > 0 ? ` + ${formatNum(clean)}` : ` - ${formatNum(-clean)}`;
|
|
115
|
+
}
|
|
116
|
+
function formatPolynomialEquation(coeffsX) {
|
|
117
|
+
const terms = [];
|
|
118
|
+
for (let p = coeffsX.length - 1; p >= 0; p -= 1) {
|
|
119
|
+
const c = coeffsX[p];
|
|
120
|
+
if (Math.abs(c) < 1e-10 && p > 0) continue;
|
|
121
|
+
if (p === 0 && Math.abs(c) < 1e-10 && terms.length > 0) continue;
|
|
122
|
+
const absStr = formatNum(Math.abs(c));
|
|
123
|
+
const varPart = p === 0 ? "" : p === 1 ? "x" : `x^${p}`;
|
|
124
|
+
if (terms.length === 0) terms.push(`${c < 0 ? "-" : ""}${absStr}${varPart}`);
|
|
125
|
+
else terms.push(`${c < 0 ? " - " : " + "}${absStr}${varPart}`);
|
|
126
|
+
}
|
|
127
|
+
return `y = ${terms.length > 0 ? terms.join("") : "0"}`;
|
|
128
|
+
}
|
|
129
|
+
function collectCategoryFitPoints(values) {
|
|
130
|
+
const pts = [];
|
|
131
|
+
for (let i = 0; i < values.length; i += 1) {
|
|
132
|
+
const y = values[i];
|
|
133
|
+
if (y !== null && y !== void 0 && Number.isFinite(y)) pts.push({
|
|
134
|
+
x: i + 1,
|
|
135
|
+
y
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
return pts;
|
|
139
|
+
}
|
|
140
|
+
function collectScatterFitPoints(xs, ys) {
|
|
141
|
+
const pts = [];
|
|
142
|
+
const len = Math.min(xs.length, ys.length);
|
|
143
|
+
for (let i = 0; i < len; i += 1) {
|
|
144
|
+
const x = xs[i];
|
|
145
|
+
const y = ys[i];
|
|
146
|
+
if (x !== null && x !== void 0 && Number.isFinite(x) && y !== null && y !== void 0 && Number.isFinite(y)) pts.push({
|
|
147
|
+
x,
|
|
148
|
+
y
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
pts.sort((a, b) => a.x - b.x);
|
|
152
|
+
return pts;
|
|
153
|
+
}
|
|
154
|
+
function fitTrendline(points, trendline) {
|
|
155
|
+
if (points.length === 0) return void 0;
|
|
156
|
+
if (trendline.type === "movingAverage") {
|
|
157
|
+
const p = trendline.period;
|
|
158
|
+
if (points.length < p || p < 2) return void 0;
|
|
159
|
+
const sorted = [...points].sort((a, b) => a.x - b.x);
|
|
160
|
+
const out = [];
|
|
161
|
+
let windowSum = 0;
|
|
162
|
+
for (let i = 0; i < sorted.length; i += 1) {
|
|
163
|
+
windowSum += sorted[i].y;
|
|
164
|
+
if (i >= p) windowSum -= sorted[i - p].y;
|
|
165
|
+
if (i >= p - 1) out.push({
|
|
166
|
+
x: sorted[i].x,
|
|
167
|
+
y: windowSum / p
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
return {
|
|
171
|
+
kind: "movingAverage",
|
|
172
|
+
points: out
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
let rawMinX = points[0].x;
|
|
176
|
+
let rawMaxX = points[0].x;
|
|
177
|
+
for (const pt of points) {
|
|
178
|
+
if (pt.x < rawMinX) rawMinX = pt.x;
|
|
179
|
+
if (pt.x > rawMaxX) rawMaxX = pt.x;
|
|
180
|
+
}
|
|
181
|
+
let xMin = rawMinX - (trendline.backward ?? 0);
|
|
182
|
+
const xMax = rawMaxX + (trendline.forward ?? 0);
|
|
183
|
+
switch (trendline.type) {
|
|
184
|
+
case "linear": {
|
|
185
|
+
const fit = fitLinearOls(points, trendline.intercept);
|
|
186
|
+
if (fit === void 0) return void 0;
|
|
187
|
+
const evalY = (x) => fit.slope * x + fit.intercept;
|
|
188
|
+
return {
|
|
189
|
+
kind: "curve",
|
|
190
|
+
xMin,
|
|
191
|
+
xMax,
|
|
192
|
+
evalY,
|
|
193
|
+
rSquared: computeRSquared(points, evalY),
|
|
194
|
+
equationText: `y = ${formatLinearTerm(fit.slope, "x")}${formatSignedOffset(fit.intercept)}`
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
case "exponential": {
|
|
198
|
+
if (points.some((pt) => pt.y <= 0)) return void 0;
|
|
199
|
+
if (trendline.intercept !== void 0 && trendline.intercept <= 0) return;
|
|
200
|
+
const fit = fitLinearOls(points.map((pt) => ({
|
|
201
|
+
x: pt.x,
|
|
202
|
+
y: Math.log(pt.y)
|
|
203
|
+
})), trendline.intercept !== void 0 ? Math.log(trendline.intercept) : void 0);
|
|
204
|
+
if (fit === void 0) return void 0;
|
|
205
|
+
const a = Math.exp(fit.intercept);
|
|
206
|
+
const b = fit.slope;
|
|
207
|
+
if (!Number.isFinite(a) || !Number.isFinite(b)) return void 0;
|
|
208
|
+
const evalY = (x) => a * Math.exp(b * x);
|
|
209
|
+
return {
|
|
210
|
+
kind: "curve",
|
|
211
|
+
xMin,
|
|
212
|
+
xMax,
|
|
213
|
+
evalY,
|
|
214
|
+
rSquared: computeRSquared(points, evalY),
|
|
215
|
+
equationText: `y = ${formatNum(a)}e^(${formatNum(b)}x)`
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
case "logarithmic": {
|
|
219
|
+
if (points.some((pt) => pt.x <= 0)) return void 0;
|
|
220
|
+
const fit = fitLinearOls(points.map((pt) => ({
|
|
221
|
+
x: Math.log(pt.x),
|
|
222
|
+
y: pt.y
|
|
223
|
+
})));
|
|
224
|
+
if (fit === void 0) return void 0;
|
|
225
|
+
xMin = Math.max(1e-6, xMin);
|
|
226
|
+
if (xMax <= xMin) return void 0;
|
|
227
|
+
const evalY = (x) => fit.slope * Math.log(Math.max(1e-12, x)) + fit.intercept;
|
|
228
|
+
return {
|
|
229
|
+
kind: "curve",
|
|
230
|
+
xMin,
|
|
231
|
+
xMax,
|
|
232
|
+
evalY,
|
|
233
|
+
rSquared: computeRSquared(points, evalY),
|
|
234
|
+
equationText: `y = ${formatNum(fit.slope)}ln(x)${formatSignedOffset(fit.intercept)}`
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
case "power": {
|
|
238
|
+
if (points.some((pt) => pt.x <= 0 || pt.y <= 0)) return void 0;
|
|
239
|
+
const fit = fitLinearOls(points.map((pt) => ({
|
|
240
|
+
x: Math.log(pt.x),
|
|
241
|
+
y: Math.log(pt.y)
|
|
242
|
+
})));
|
|
243
|
+
if (fit === void 0) return void 0;
|
|
244
|
+
const a = Math.exp(fit.intercept);
|
|
245
|
+
const b = fit.slope;
|
|
246
|
+
if (!Number.isFinite(a) || !Number.isFinite(b)) return void 0;
|
|
247
|
+
xMin = Math.max(1e-6, xMin);
|
|
248
|
+
if (xMax <= xMin) return void 0;
|
|
249
|
+
const evalY = (x) => a * Math.max(1e-12, x) ** b;
|
|
250
|
+
return {
|
|
251
|
+
kind: "curve",
|
|
252
|
+
xMin,
|
|
253
|
+
xMax,
|
|
254
|
+
evalY,
|
|
255
|
+
rSquared: computeRSquared(points, evalY),
|
|
256
|
+
equationText: `y = ${formatNum(a)}x^${formatNum(b)}`
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
case "polynomial": {
|
|
260
|
+
const k = trendline.order;
|
|
261
|
+
if (trendline.intercept !== void 0) {
|
|
262
|
+
const c0 = trendline.intercept;
|
|
263
|
+
if (points.length < k) return void 0;
|
|
264
|
+
let maxAbsX = 1;
|
|
265
|
+
for (const pt of points) maxAbsX = Math.max(maxAbsX, Math.abs(pt.x));
|
|
266
|
+
const mat = Array.from({ length: k }, () => Array.from({ length: k }, () => 0));
|
|
267
|
+
const rhs = Array.from({ length: k }, () => 0);
|
|
268
|
+
for (const pt of points) {
|
|
269
|
+
const u = pt.x / maxAbsX;
|
|
270
|
+
const resY = pt.y - c0;
|
|
271
|
+
const uPows = Array.from({ length: 2 * k + 1 }, () => 1);
|
|
272
|
+
for (let p = 1; p <= 2 * k; p += 1) uPows[p] = uPows[p - 1] * u;
|
|
273
|
+
for (let r = 0; r < k; r += 1) {
|
|
274
|
+
rhs[r] = rhs[r] + resY * uPows[r + 1];
|
|
275
|
+
for (let c = 0; c < k; c += 1) mat[r][c] = mat[r][c] + uPows[r + c + 2];
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
const sol = solveGaussianPartialPivoting(mat, rhs);
|
|
279
|
+
if (sol === void 0) return void 0;
|
|
280
|
+
const coeffsX = [c0];
|
|
281
|
+
for (let p = 1; p <= k; p += 1) coeffsX.push(sol[p - 1] / maxAbsX ** p);
|
|
282
|
+
const evalY = (x) => {
|
|
283
|
+
const u = x / maxAbsX;
|
|
284
|
+
let sum = c0;
|
|
285
|
+
let uPow = u;
|
|
286
|
+
for (let p = 0; p < k; p += 1) {
|
|
287
|
+
sum += sol[p] * uPow;
|
|
288
|
+
uPow *= u;
|
|
289
|
+
}
|
|
290
|
+
return sum;
|
|
291
|
+
};
|
|
292
|
+
return {
|
|
293
|
+
kind: "curve",
|
|
294
|
+
xMin,
|
|
295
|
+
xMax,
|
|
296
|
+
evalY,
|
|
297
|
+
rSquared: computeRSquared(points, evalY),
|
|
298
|
+
equationText: formatPolynomialEquation(coeffsX)
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
if (points.length < k + 1) return void 0;
|
|
302
|
+
let sumX = 0;
|
|
303
|
+
for (const pt of points) sumX += pt.x;
|
|
304
|
+
const meanX = sumX / points.length;
|
|
305
|
+
let scaleX = 1;
|
|
306
|
+
for (const pt of points) scaleX = Math.max(scaleX, Math.abs(pt.x - meanX));
|
|
307
|
+
const dim = k + 1;
|
|
308
|
+
const mat = Array.from({ length: dim }, () => Array.from({ length: dim }, () => 0));
|
|
309
|
+
const rhs = Array.from({ length: dim }, () => 0);
|
|
310
|
+
for (const pt of points) {
|
|
311
|
+
const u = (pt.x - meanX) / scaleX;
|
|
312
|
+
const uPows = Array.from({ length: 2 * k + 1 }, () => 1);
|
|
313
|
+
for (let p = 1; p <= 2 * k; p += 1) uPows[p] = uPows[p - 1] * u;
|
|
314
|
+
for (let r = 0; r < dim; r += 1) {
|
|
315
|
+
rhs[r] = rhs[r] + pt.y * uPows[r];
|
|
316
|
+
for (let c = 0; c < dim; c += 1) mat[r][c] = mat[r][c] + uPows[r + c];
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
const sol = solveGaussianPartialPivoting(mat, rhs);
|
|
320
|
+
if (sol === void 0) return void 0;
|
|
321
|
+
const coeffsX = Array.from({ length: dim }, () => 0);
|
|
322
|
+
for (let p = 0; p <= k; p += 1) {
|
|
323
|
+
const factor = sol[p] / scaleX ** p;
|
|
324
|
+
let binom = 1;
|
|
325
|
+
for (let j = 0; j <= p; j += 1) {
|
|
326
|
+
coeffsX[j] = coeffsX[j] + factor * binom * (-meanX) ** (p - j);
|
|
327
|
+
binom = binom * (p - j) / (j + 1);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
const evalY = (x) => {
|
|
331
|
+
const u = (x - meanX) / scaleX;
|
|
332
|
+
let sum = 0;
|
|
333
|
+
let uPow = 1;
|
|
334
|
+
for (let p = 0; p <= k; p += 1) {
|
|
335
|
+
sum += sol[p] * uPow;
|
|
336
|
+
uPow *= u;
|
|
337
|
+
}
|
|
338
|
+
return sum;
|
|
339
|
+
};
|
|
340
|
+
return {
|
|
341
|
+
kind: "curve",
|
|
342
|
+
xMin,
|
|
343
|
+
xMax,
|
|
344
|
+
evalY,
|
|
345
|
+
rSquared: computeRSquared(points, evalY),
|
|
346
|
+
equationText: formatPolynomialEquation(coeffsX)
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
default: return assertNever(trendline);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
function categoryFitPositionToNormalized(catScale, oneBasedPos) {
|
|
353
|
+
const index = oneBasedPos - 1;
|
|
354
|
+
const rawT = catScale.crossBetween === "midCat" && catScale.count > 1 ? index / (catScale.count - 1) : (index + .5) / catScale.count;
|
|
355
|
+
return catScale.reversed ? 1 - rawT : rawT;
|
|
356
|
+
}
|
|
357
|
+
function renderSafeTrendlineText(options) {
|
|
358
|
+
for (const text of options.candidates) {
|
|
359
|
+
if (text === "") continue;
|
|
360
|
+
try {
|
|
361
|
+
return renderChartTextSvg({
|
|
362
|
+
text,
|
|
363
|
+
box: options.box,
|
|
364
|
+
style: options.style,
|
|
365
|
+
theme: options.theme,
|
|
366
|
+
slideNumber: options.slideNumber,
|
|
367
|
+
labelPath: options.labelPath,
|
|
368
|
+
align: "right",
|
|
369
|
+
verticalAlign: "middle",
|
|
370
|
+
wrap: false,
|
|
371
|
+
clipBox: options.clipBox,
|
|
372
|
+
clipAllocator: options.clipAllocator
|
|
373
|
+
});
|
|
374
|
+
} catch (error) {
|
|
375
|
+
if (error instanceof CapabilityError && error.code === "preview-missing-glyph") continue;
|
|
376
|
+
throw error;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return "";
|
|
380
|
+
}
|
|
381
|
+
function safeMeasureCandidate(options) {
|
|
382
|
+
for (const text of options.candidates) {
|
|
383
|
+
if (text === "") continue;
|
|
384
|
+
try {
|
|
385
|
+
const m = measureChartText({
|
|
386
|
+
text,
|
|
387
|
+
style: options.style,
|
|
388
|
+
theme: options.theme,
|
|
389
|
+
slideNumber: options.slideNumber,
|
|
390
|
+
labelPath: options.labelPath
|
|
391
|
+
});
|
|
392
|
+
return {
|
|
393
|
+
text,
|
|
394
|
+
width: m.width,
|
|
395
|
+
height: m.height
|
|
396
|
+
};
|
|
397
|
+
} catch (error) {
|
|
398
|
+
if (error instanceof CapabilityError && error.code === "preview-missing-glyph") continue;
|
|
399
|
+
throw error;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
function renderTrendlinesSvg(options) {
|
|
404
|
+
const { parsed, scales, plotRect, chartBox, baseChartTextStyle, theme, slideNumber, clipAllocator } = options;
|
|
405
|
+
const lineParts = [];
|
|
406
|
+
const labelParts = [];
|
|
407
|
+
for (let groupIndex = 0; groupIndex < parsed.groups.length; groupIndex += 1) {
|
|
408
|
+
const group = parsed.groups[groupIndex];
|
|
409
|
+
if (group.kind === "pie" || group.kind === "doughnut") continue;
|
|
410
|
+
const seriesTargets = [];
|
|
411
|
+
if (group.kind === "scatter") {
|
|
412
|
+
const xScale = scales.get(group.axes.x.name);
|
|
413
|
+
const yScale = scales.get(group.axes.y.name);
|
|
414
|
+
if (xScale?.kind !== "value" || yScale?.kind !== "value") continue;
|
|
415
|
+
for (const series of group.series) {
|
|
416
|
+
if (series.trendlines === void 0 || series.trendlines.length === 0) continue;
|
|
417
|
+
seriesTargets.push({
|
|
418
|
+
seriesIndex: series.seriesIndex,
|
|
419
|
+
seriesColor: series.color,
|
|
420
|
+
trendlines: series.trendlines,
|
|
421
|
+
fitPoints: collectScatterFitPoints(series.x, series.y),
|
|
422
|
+
isLinearCartesian: false,
|
|
423
|
+
mapDataToScreen: (xVal, yVal) => {
|
|
424
|
+
if (!Number.isFinite(xVal) || !Number.isFinite(yVal) || xScale.logBase !== void 0 && xVal <= 0 || yScale.logBase !== void 0 && yVal <= 0) return;
|
|
425
|
+
return {
|
|
426
|
+
x: xScale.valueToScreen(xVal, plotRect),
|
|
427
|
+
y: yScale.valueToScreen(yVal, plotRect)
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
} else {
|
|
433
|
+
const catScale = scales.get(group.axes.category.name);
|
|
434
|
+
const valScale = scales.get(group.axes.value.name);
|
|
435
|
+
if (catScale?.kind !== "category" || valScale?.kind !== "value") continue;
|
|
436
|
+
const isHorizontalBar = group.kind === "bar";
|
|
437
|
+
const isLinearCartesian = valScale.logBase === void 0;
|
|
438
|
+
for (const series of group.series) {
|
|
439
|
+
if (series.trendlines === void 0 || series.trendlines.length === 0) continue;
|
|
440
|
+
seriesTargets.push({
|
|
441
|
+
seriesIndex: series.seriesIndex,
|
|
442
|
+
seriesColor: series.color,
|
|
443
|
+
trendlines: series.trendlines,
|
|
444
|
+
fitPoints: collectCategoryFitPoints(series.values),
|
|
445
|
+
isLinearCartesian,
|
|
446
|
+
mapDataToScreen: (xVal, yVal) => {
|
|
447
|
+
if (!Number.isFinite(xVal) || !Number.isFinite(yVal) || valScale.logBase !== void 0 && yVal <= 0) return;
|
|
448
|
+
const catScreen = catScale.mapScreen(categoryFitPositionToNormalized(catScale, xVal), plotRect);
|
|
449
|
+
const valScreen = valScale.valueToScreen(yVal, plotRect);
|
|
450
|
+
return isHorizontalBar ? {
|
|
451
|
+
x: valScreen,
|
|
452
|
+
y: catScreen
|
|
453
|
+
} : {
|
|
454
|
+
x: catScreen,
|
|
455
|
+
y: valScreen
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
for (const target of seriesTargets) for (const trendline of target.trendlines) {
|
|
462
|
+
const model = fitTrendline(target.fitPoints, trendline);
|
|
463
|
+
if (model === void 0) continue;
|
|
464
|
+
const stroke = resolveChartLineStroke(trendline.line, theme, {
|
|
465
|
+
color: target.seriesColor ?? chartAccentSlot(target.seriesIndex),
|
|
466
|
+
width: 1.5
|
|
467
|
+
});
|
|
468
|
+
if (stroke === void 0) continue;
|
|
469
|
+
const screenPts = [];
|
|
470
|
+
if (model.kind === "movingAverage") for (const pt of model.points) {
|
|
471
|
+
const mapped = target.mapDataToScreen(pt.x, pt.y);
|
|
472
|
+
if (mapped !== void 0) screenPts.push(mapped);
|
|
473
|
+
}
|
|
474
|
+
else {
|
|
475
|
+
const sampleCount = trendline.type === "linear" && target.isLinearCartesian ? 1 : 64;
|
|
476
|
+
for (let k = 0; k <= sampleCount; k += 1) {
|
|
477
|
+
const xVal = model.xMin + (model.xMax - model.xMin) * k / sampleCount;
|
|
478
|
+
const yVal = model.evalY(xVal);
|
|
479
|
+
const mapped = target.mapDataToScreen(xVal, yVal);
|
|
480
|
+
if (mapped !== void 0) screenPts.push(mapped);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
if (screenPts.length >= 2) {
|
|
484
|
+
const d = `M${screenPts[0].x} ${screenPts[0].y}` + screenPts.slice(1).map((p) => `L${p.x} ${p.y}`).join("");
|
|
485
|
+
lineParts.push(`<path d="${d}" fill="none" ${strokeAttrs(stroke)} stroke-linecap="round" stroke-linejoin="round"/>`);
|
|
486
|
+
}
|
|
487
|
+
if (model.kind === "curve" && (trendline.displayEquation === true || trendline.displayRSquared === true)) {
|
|
488
|
+
const anchorPt = screenPts[screenPts.length - 1];
|
|
489
|
+
if (anchorPt === void 0) continue;
|
|
490
|
+
const labelStyle = mergeChartTextStyle(baseChartTextStyle, { fontSize: Math.max(9, resolveChartFontSizePixels(baseChartTextStyle?.fontSize) * .85) });
|
|
491
|
+
const labelPath = `Chart.groups[${groupIndex}].trendline`;
|
|
492
|
+
const r2Val = formatNum(model.rSquared);
|
|
493
|
+
const lines = [];
|
|
494
|
+
if (trendline.displayEquation === true) lines.push({ candidates: [model.equationText] });
|
|
495
|
+
if (trendline.displayRSquared === true) lines.push({ candidates: [`R\u00B2 = ${r2Val}`, `R^2 = ${r2Val}`] });
|
|
496
|
+
const lineSpacing = resolveChartFontSizePixels(labelStyle?.fontSize) * 1.25;
|
|
497
|
+
let cursorY = Math.max(plotRect.y + 2, Math.min(plotRect.y + plotRect.height - lines.length * lineSpacing - 2, anchorPt.y - lines.length * lineSpacing - 4));
|
|
498
|
+
for (const lineSpec of lines) {
|
|
499
|
+
const measured = safeMeasureCandidate({
|
|
500
|
+
candidates: lineSpec.candidates,
|
|
501
|
+
style: labelStyle,
|
|
502
|
+
theme,
|
|
503
|
+
slideNumber,
|
|
504
|
+
labelPath
|
|
505
|
+
});
|
|
506
|
+
if (measured === void 0) continue;
|
|
507
|
+
const boxW = Math.max(20, measured.width + 4);
|
|
508
|
+
const boxX = Math.max(plotRect.x + 2, Math.min(plotRect.x + plotRect.width - boxW - 2, anchorPt.x - boxW));
|
|
509
|
+
const rendered = renderSafeTrendlineText({
|
|
510
|
+
candidates: [measured.text],
|
|
511
|
+
box: {
|
|
512
|
+
x: boxX,
|
|
513
|
+
y: cursorY,
|
|
514
|
+
width: boxW,
|
|
515
|
+
height: Math.max(measured.height, lineSpacing)
|
|
516
|
+
},
|
|
517
|
+
style: labelStyle,
|
|
518
|
+
theme,
|
|
519
|
+
slideNumber,
|
|
520
|
+
labelPath,
|
|
521
|
+
clipBox: chartBox,
|
|
522
|
+
clipAllocator
|
|
523
|
+
});
|
|
524
|
+
if (rendered !== "") {
|
|
525
|
+
labelParts.push(rendered);
|
|
526
|
+
cursorY += lineSpacing;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
return {
|
|
533
|
+
linesSvg: lineParts.join(""),
|
|
534
|
+
labelsSvg: labelParts.join("")
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
export { collectCategoryFitPoints, collectScatterFitPoints, fitTrendline, renderTrendlinesSvg };
|