@teachinglab/omd 0.6.1 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +257 -251
- package/README.old.md +137 -137
- package/canvas/core/canvasConfig.js +202 -202
- package/canvas/drawing/segment.js +167 -167
- package/canvas/drawing/stroke.js +385 -385
- package/canvas/events/eventManager.js +444 -444
- package/canvas/events/pointerEventHandler.js +262 -262
- package/canvas/index.js +48 -48
- package/canvas/tools/PointerTool.js +71 -71
- package/canvas/tools/tool.js +222 -222
- package/canvas/utils/boundingBox.js +377 -377
- package/canvas/utils/mathUtils.js +258 -258
- package/docs/api/configuration-options.md +198 -198
- package/docs/api/eventManager.md +82 -82
- package/docs/api/focusFrameManager.md +144 -144
- package/docs/api/index.md +105 -105
- package/docs/api/main.md +62 -62
- package/docs/api/omdBinaryExpressionNode.md +86 -86
- package/docs/api/omdCanvas.md +83 -83
- package/docs/api/omdConfigManager.md +112 -112
- package/docs/api/omdConstantNode.md +52 -52
- package/docs/api/omdDisplay.md +87 -87
- package/docs/api/omdEquationNode.md +174 -174
- package/docs/api/omdEquationSequenceNode.md +258 -258
- package/docs/api/omdEquationStack.md +192 -192
- package/docs/api/omdFunctionNode.md +82 -82
- package/docs/api/omdGroupNode.md +78 -78
- package/docs/api/omdHelpers.md +87 -87
- package/docs/api/omdLeafNode.md +85 -85
- package/docs/api/omdNode.md +201 -201
- package/docs/api/omdOperationDisplayNode.md +117 -117
- package/docs/api/omdOperatorNode.md +91 -91
- package/docs/api/omdParenthesisNode.md +133 -133
- package/docs/api/omdPopup.md +191 -191
- package/docs/api/omdPowerNode.md +131 -131
- package/docs/api/omdRationalNode.md +144 -144
- package/docs/api/omdSequenceNode.md +128 -128
- package/docs/api/omdSimplification.md +78 -78
- package/docs/api/omdSqrtNode.md +144 -144
- package/docs/api/omdStepVisualizer.md +146 -146
- package/docs/api/omdStepVisualizerHighlighting.md +65 -65
- package/docs/api/omdStepVisualizerInteractiveSteps.md +108 -108
- package/docs/api/omdStepVisualizerLayout.md +70 -70
- package/docs/api/omdStepVisualizerNodeUtils.md +140 -140
- package/docs/api/omdStepVisualizerTextBoxes.md +76 -76
- package/docs/api/omdToolbar.md +130 -130
- package/docs/api/omdTranscriptionService.md +95 -95
- package/docs/api/omdTreeDiff.md +169 -169
- package/docs/api/omdUnaryExpressionNode.md +137 -137
- package/docs/api/omdUtilities.md +82 -82
- package/docs/api/omdVariableNode.md +123 -123
- package/docs/api/selectTool.md +74 -74
- package/docs/api/simplificationEngine.md +97 -97
- package/docs/api/simplificationRules.md +76 -76
- package/docs/api/simplificationUtils.md +64 -64
- package/docs/api/transcribe.md +43 -43
- package/docs/api-reference.md +85 -85
- package/docs/index.html +453 -453
- package/docs/index.md +38 -38
- package/docs/omd-objects.md +258 -258
- package/index.js +79 -79
- package/jsvg/index.js +3 -0
- package/jsvg/jsvg.js +898 -898
- package/jsvg/jsvgComponents.js +357 -358
- package/npm-docs/DOCUMENTATION_SUMMARY.md +220 -220
- package/npm-docs/README.md +251 -251
- package/npm-docs/api/api-reference.md +85 -85
- package/npm-docs/api/configuration-options.md +198 -198
- package/npm-docs/api/eventManager.md +82 -82
- package/npm-docs/api/expression-nodes.md +561 -561
- package/npm-docs/api/focusFrameManager.md +144 -144
- package/npm-docs/api/index.md +105 -105
- package/npm-docs/api/main.md +62 -62
- package/npm-docs/api/omdBinaryExpressionNode.md +86 -86
- package/npm-docs/api/omdCanvas.md +83 -83
- package/npm-docs/api/omdConfigManager.md +112 -112
- package/npm-docs/api/omdConstantNode.md +52 -52
- package/npm-docs/api/omdDisplay.md +87 -87
- package/npm-docs/api/omdEquationNode.md +174 -174
- package/npm-docs/api/omdEquationSequenceNode.md +258 -258
- package/npm-docs/api/omdEquationStack.md +192 -192
- package/npm-docs/api/omdFunctionNode.md +82 -82
- package/npm-docs/api/omdGroupNode.md +78 -78
- package/npm-docs/api/omdHelpers.md +87 -87
- package/npm-docs/api/omdLeafNode.md +85 -85
- package/npm-docs/api/omdNode.md +201 -201
- package/npm-docs/api/omdOperationDisplayNode.md +117 -117
- package/npm-docs/api/omdOperatorNode.md +91 -91
- package/npm-docs/api/omdParenthesisNode.md +133 -133
- package/npm-docs/api/omdPopup.md +191 -191
- package/npm-docs/api/omdPowerNode.md +131 -131
- package/npm-docs/api/omdRationalNode.md +144 -144
- package/npm-docs/api/omdSequenceNode.md +128 -128
- package/npm-docs/api/omdSimplification.md +78 -78
- package/npm-docs/api/omdSqrtNode.md +144 -144
- package/npm-docs/api/omdStepVisualizer.md +146 -146
- package/npm-docs/api/omdStepVisualizerHighlighting.md +65 -65
- package/npm-docs/api/omdStepVisualizerInteractiveSteps.md +108 -108
- package/npm-docs/api/omdStepVisualizerLayout.md +70 -70
- package/npm-docs/api/omdStepVisualizerNodeUtils.md +140 -140
- package/npm-docs/api/omdStepVisualizerTextBoxes.md +76 -76
- package/npm-docs/api/omdToolbar.md +130 -130
- package/npm-docs/api/omdTranscriptionService.md +95 -95
- package/npm-docs/api/omdTreeDiff.md +169 -169
- package/npm-docs/api/omdUnaryExpressionNode.md +137 -137
- package/npm-docs/api/omdUtilities.md +82 -82
- package/npm-docs/api/omdVariableNode.md +123 -123
- package/npm-docs/api/selectTool.md +74 -74
- package/npm-docs/api/simplificationEngine.md +97 -97
- package/npm-docs/api/simplificationRules.md +76 -76
- package/npm-docs/api/simplificationUtils.md +64 -64
- package/npm-docs/api/transcribe.md +43 -43
- package/npm-docs/guides/equations.md +854 -854
- package/npm-docs/guides/factory-functions.md +354 -354
- package/npm-docs/guides/getting-started.md +318 -318
- package/npm-docs/guides/quick-examples.md +525 -525
- package/npm-docs/guides/visualizations.md +682 -682
- package/npm-docs/index.html +12 -0
- package/npm-docs/json-schemas.md +826 -826
- package/omd/config/omdConfigManager.js +279 -267
- package/omd/core/index.js +158 -158
- package/omd/core/omdEquationStack.js +546 -546
- package/omd/core/omdUtilities.js +113 -113
- package/omd/display/omdDisplay.js +969 -962
- package/omd/display/omdToolbar.js +501 -501
- package/omd/nodes/omdBinaryExpressionNode.js +459 -459
- package/omd/nodes/omdConstantNode.js +141 -141
- package/omd/nodes/omdEquationNode.js +1327 -1327
- package/omd/nodes/omdFunctionNode.js +351 -351
- package/omd/nodes/omdGroupNode.js +67 -67
- package/omd/nodes/omdLeafNode.js +76 -76
- package/omd/nodes/omdNode.js +556 -556
- package/omd/nodes/omdOperationDisplayNode.js +321 -321
- package/omd/nodes/omdOperatorNode.js +108 -108
- package/omd/nodes/omdParenthesisNode.js +292 -292
- package/omd/nodes/omdPowerNode.js +235 -235
- package/omd/nodes/omdRationalNode.js +295 -295
- package/omd/nodes/omdSqrtNode.js +307 -307
- package/omd/nodes/omdUnaryExpressionNode.js +227 -227
- package/omd/nodes/omdVariableNode.js +122 -122
- package/omd/simplification/omdSimplification.js +140 -140
- package/omd/simplification/omdSimplificationEngine.js +887 -887
- package/omd/simplification/package.json +5 -5
- package/omd/simplification/rules/binaryRules.js +1037 -1037
- package/omd/simplification/rules/functionRules.js +111 -111
- package/omd/simplification/rules/index.js +48 -48
- package/omd/simplification/rules/parenthesisRules.js +19 -19
- package/omd/simplification/rules/powerRules.js +143 -143
- package/omd/simplification/rules/rationalRules.js +725 -725
- package/omd/simplification/rules/sqrtRules.js +48 -48
- package/omd/simplification/rules/unaryRules.js +37 -37
- package/omd/simplification/simplificationRules.js +31 -31
- package/omd/simplification/simplificationUtils.js +1055 -1055
- package/omd/step-visualizer/omdStepVisualizer.js +947 -947
- package/omd/step-visualizer/omdStepVisualizerHighlighting.js +246 -246
- package/omd/step-visualizer/omdStepVisualizerLayout.js +892 -892
- package/omd/step-visualizer/omdStepVisualizerTextBoxes.js +200 -200
- package/omd/utils/aiNextEquationStep.js +106 -106
- package/omd/utils/omdNodeOverlay.js +638 -638
- package/omd/utils/omdPopup.js +1203 -1203
- package/omd/utils/omdStepVisualizerInteractiveSteps.js +684 -684
- package/omd/utils/omdStepVisualizerNodeUtils.js +267 -267
- package/omd/utils/omdTranscriptionService.js +123 -123
- package/omd/utils/omdTreeDiff.js +733 -733
- package/package.json +59 -57
- package/readme.html +184 -120
- package/src/index.js +74 -74
- package/src/json-schemas.md +576 -576
- package/src/omd-json-samples.js +147 -147
- package/src/omdApp.js +391 -391
- package/src/omdAppCanvas.js +335 -335
- package/src/omdBalanceHanger.js +199 -199
- package/src/omdColor.js +13 -13
- package/src/omdCoordinatePlane.js +541 -541
- package/src/omdExpression.js +115 -115
- package/src/omdFactory.js +150 -150
- package/src/omdFunction.js +114 -114
- package/src/omdMetaExpression.js +290 -290
- package/src/omdNaturalExpression.js +563 -563
- package/src/omdNode.js +383 -383
- package/src/omdNumber.js +52 -52
- package/src/omdNumberLine.js +114 -112
- package/src/omdNumberTile.js +118 -118
- package/src/omdOperator.js +72 -72
- package/src/omdPowerExpression.js +91 -91
- package/src/omdProblem.js +259 -259
- package/src/omdRatioChart.js +251 -251
- package/src/omdRationalExpression.js +114 -114
- package/src/omdSampleData.js +215 -215
- package/src/omdShapes.js +512 -512
- package/src/omdSpinner.js +151 -151
- package/src/omdString.js +49 -49
- package/src/omdTable.js +498 -498
- package/src/omdTapeDiagram.js +244 -244
- package/src/omdTerm.js +91 -91
- package/src/omdTileEquation.js +349 -349
- package/src/omdUtils.js +84 -84
- package/src/omdVariable.js +51 -51
|
@@ -1,542 +1,542 @@
|
|
|
1
|
-
import { omdColor } from "./omdColor.js";
|
|
2
|
-
import { jsvgGroup, jsvgRect, jsvgClipMask, jsvgLine, jsvgEllipse, jsvgTextBox, jsvgTextLine, jsvgPath } from "@teachinglab/jsvg";
|
|
3
|
-
import {
|
|
4
|
-
omdRightTriangle,
|
|
5
|
-
omdIsoscelesTriangle,
|
|
6
|
-
omdRectangle,
|
|
7
|
-
omdEllipse,
|
|
8
|
-
omdCircle,
|
|
9
|
-
omdRegularPolygon
|
|
10
|
-
} from "./omdShapes.js";
|
|
11
|
-
|
|
12
|
-
export class omdCoordinatePlane extends jsvgGroup {
|
|
13
|
-
constructor() {
|
|
14
|
-
super();
|
|
15
|
-
this.graphEquations = [];
|
|
16
|
-
this.lineSegments = [];
|
|
17
|
-
this.dotValues = [];
|
|
18
|
-
this.shapeSet = [];
|
|
19
|
-
|
|
20
|
-
this.xMin = -5;
|
|
21
|
-
this.xMax = 5;
|
|
22
|
-
this.yMin = -5;
|
|
23
|
-
this.yMax = 5;
|
|
24
|
-
|
|
25
|
-
this.xLabel = "";
|
|
26
|
-
this.yLabel = "";
|
|
27
|
-
this.axisLabelOffsetPx = 20; //offset from axes
|
|
28
|
-
|
|
29
|
-
this.size = "medium";
|
|
30
|
-
|
|
31
|
-
this.tickInterval = 1; //interval between ticks
|
|
32
|
-
this.forceAllTickLabels = true; // show all tick labels despite potential overlap
|
|
33
|
-
this.tickLabelOffsetPx = 5; //offset from axes
|
|
34
|
-
this.showTickLabels = true; // show numeric tick labels
|
|
35
|
-
|
|
36
|
-
// Background customization properties
|
|
37
|
-
this.backgroundColor = omdColor.lightGray;
|
|
38
|
-
this.backgroundCornerRadius = 15;
|
|
39
|
-
this.backgroundOpacity = 1.0;
|
|
40
|
-
this.showBackground = true;
|
|
41
|
-
|
|
42
|
-
this.calculatePadding();
|
|
43
|
-
this.updateLayout();
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
loadFromJSON(data) {
|
|
49
|
-
// Assume valid JSON shape
|
|
50
|
-
this.graphEquations = data.graphEquations || [];
|
|
51
|
-
this.lineSegments = data.lineSegments || [];
|
|
52
|
-
this.dotValues = data.dotValues || [];
|
|
53
|
-
this.shapeSet = data.shapeSet || [];
|
|
54
|
-
|
|
55
|
-
this.xMin = (data.xMin !== undefined) ? data.xMin : this.xMin;
|
|
56
|
-
this.xMax = (data.xMax !== undefined) ? data.xMax : this.xMax;
|
|
57
|
-
this.yMin = (data.yMin !== undefined) ? data.yMin : this.yMin;
|
|
58
|
-
this.yMax = (data.yMax !== undefined) ? data.yMax : this.yMax;
|
|
59
|
-
|
|
60
|
-
this.xLabel = (data.xLabel !== undefined) ? data.xLabel : this.xLabel;
|
|
61
|
-
this.yLabel = (data.yLabel !== undefined) ? data.yLabel : this.yLabel;
|
|
62
|
-
this.axisLabelOffsetPx = (data.axisLabelOffsetPx !== undefined) ? data.axisLabelOffsetPx : this.axisLabelOffsetPx;
|
|
63
|
-
|
|
64
|
-
this.size = (data.size !== undefined) ? data.size : this.size;
|
|
65
|
-
|
|
66
|
-
this.tickInterval = (data.tickInterval !== undefined) ? data.tickInterval : this.tickInterval;
|
|
67
|
-
this.tickLabelOffsetPx = (data.tickLabelOffsetPx !== undefined) ? data.tickLabelOffsetPx : this.tickLabelOffsetPx;
|
|
68
|
-
this.forceAllTickLabels = (data.forceAllTickLabels !== undefined) ? data.forceAllTickLabels : this.forceAllTickLabels;
|
|
69
|
-
this.showTickLabels = (data.showTickLabels !== undefined) ? data.showTickLabels : this.showTickLabels;
|
|
70
|
-
|
|
71
|
-
// Load background customization properties
|
|
72
|
-
this.backgroundColor = data.backgroundColor || this.backgroundColor;
|
|
73
|
-
this.backgroundCornerRadius = (data.backgroundCornerRadius !== undefined) ? data.backgroundCornerRadius : this.backgroundCornerRadius;
|
|
74
|
-
this.backgroundOpacity = (data.backgroundOpacity !== undefined) ? data.backgroundOpacity : this.backgroundOpacity;
|
|
75
|
-
this.showBackground = (data.showBackground !== undefined) ? data.showBackground : this.showBackground;
|
|
76
|
-
|
|
77
|
-
this.calculatePadding();
|
|
78
|
-
this.updateLayout();
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
updateLayout() {
|
|
84
|
-
this.removeAllChildren();
|
|
85
|
-
this.calculateGraphDimensions();
|
|
86
|
-
|
|
87
|
-
const outerWidth = this.width;
|
|
88
|
-
const outerHeight = this.height;
|
|
89
|
-
|
|
90
|
-
const clipMask = new jsvgClipMask(outerWidth, outerHeight, 15);
|
|
91
|
-
this.addChild(clipMask);
|
|
92
|
-
|
|
93
|
-
const whiteRect = new jsvgRect();
|
|
94
|
-
whiteRect.setWidthAndHeight(outerWidth, outerHeight);
|
|
95
|
-
|
|
96
|
-
// Apply customizable background properties
|
|
97
|
-
if (this.showBackground) {
|
|
98
|
-
whiteRect.setFillColor(this.backgroundColor);
|
|
99
|
-
if (this.backgroundOpacity < 1.0) {
|
|
100
|
-
whiteRect.setOpacity(this.backgroundOpacity);
|
|
101
|
-
}
|
|
102
|
-
} else {
|
|
103
|
-
whiteRect.setFillColor("transparent");
|
|
104
|
-
}
|
|
105
|
-
whiteRect.setStrokeWidth(0);
|
|
106
|
-
|
|
107
|
-
if (whiteRect.setCornerRadius && this.backgroundCornerRadius > 0) {
|
|
108
|
-
whiteRect.setCornerRadius(this.backgroundCornerRadius);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
clipMask.addChild(whiteRect);
|
|
112
|
-
|
|
113
|
-
const contentGroup = new jsvgGroup();
|
|
114
|
-
contentGroup.setPosition(0, 0);
|
|
115
|
-
clipMask.addChild(contentGroup);
|
|
116
|
-
|
|
117
|
-
this.setWidthAndHeight(outerWidth, outerHeight);
|
|
118
|
-
this.svgObject.setAttribute("viewBox", `0 0 ${outerWidth} ${outerHeight}`);
|
|
119
|
-
|
|
120
|
-
const gridHolder = new jsvgGroup();
|
|
121
|
-
contentGroup.addChild(gridHolder);
|
|
122
|
-
this.makeGrid(gridHolder);
|
|
123
|
-
|
|
124
|
-
const graphClipMask = new jsvgClipMask(this.graphWidth, this.graphHeight, 0);
|
|
125
|
-
graphClipMask.setPosition(this.paddingLeft, this.paddingTop);
|
|
126
|
-
contentGroup.addChild(graphClipMask);
|
|
127
|
-
|
|
128
|
-
// Traditional explicit calls instead of dynamic invocation
|
|
129
|
-
const functionsHolder = new jsvgGroup();
|
|
130
|
-
graphClipMask.addChild(functionsHolder);
|
|
131
|
-
this.graphMultipleFunctions(functionsHolder);
|
|
132
|
-
|
|
133
|
-
const segmentsHolder = new jsvgGroup();
|
|
134
|
-
graphClipMask.addChild(segmentsHolder);
|
|
135
|
-
this.drawLineSegments(segmentsHolder);
|
|
136
|
-
|
|
137
|
-
const dotsHolder = new jsvgGroup();
|
|
138
|
-
graphClipMask.addChild(dotsHolder);
|
|
139
|
-
this.plotDots(dotsHolder);
|
|
140
|
-
|
|
141
|
-
const shapesHolder = new jsvgGroup();
|
|
142
|
-
graphClipMask.addChild(shapesHolder);
|
|
143
|
-
this.addShapes(shapesHolder);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
createAxisGrid(gridHolder, isXAxis) {
|
|
147
|
-
const interval = this.tickInterval || 1;
|
|
148
|
-
const min = isXAxis ? this.xMin : this.yMin;
|
|
149
|
-
const max = isXAxis ? this.xMax : this.yMax;
|
|
150
|
-
|
|
151
|
-
const firstTick = Math.ceil(min);
|
|
152
|
-
const lastTick = Math.floor(max);
|
|
153
|
-
const minLabelSpacing = 10;
|
|
154
|
-
const labelWidthEstimate = 20; // matches createNumericLabel width
|
|
155
|
-
const requiredLabelSpacing = minLabelSpacing + labelWidthEstimate;
|
|
156
|
-
|
|
157
|
-
// Compute label interval for label display only
|
|
158
|
-
let labelInterval = interval;
|
|
159
|
-
if (!this.forceAllTickLabels) {
|
|
160
|
-
const span = isXAxis ? this.xMax - this.xMin : this.yMax - this.yMin;
|
|
161
|
-
let floored = Math.floor(span / 10) * 10;
|
|
162
|
-
let raw = floored / 6;
|
|
163
|
-
let rounded = Math.round(raw / 5) * 5;
|
|
164
|
-
if (rounded === 0) rounded = 1;
|
|
165
|
-
labelInterval = rounded;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
let lastLabelPos = -Infinity;
|
|
169
|
-
let zeroLineDrawn = false;
|
|
170
|
-
|
|
171
|
-
for (let value = firstTick; value <= lastTick; value += interval) {
|
|
172
|
-
const pos = this.computeAxisPos(isXAxis, value);
|
|
173
|
-
|
|
174
|
-
const line = new jsvgLine();
|
|
175
|
-
this.setAxisLineEndpoints(isXAxis, line, pos);
|
|
176
|
-
line.setStrokeColor("black");
|
|
177
|
-
line.setOpacity(value === 0 ? 1.0 : 0.15);
|
|
178
|
-
gridHolder.addChild(line);
|
|
179
|
-
|
|
180
|
-
if (value === 0) {
|
|
181
|
-
zeroLineDrawn = true;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
let shouldShowLabel = false;
|
|
185
|
-
if (this.showTickLabels) {
|
|
186
|
-
if (this.forceAllTickLabels) {
|
|
187
|
-
shouldShowLabel = true;
|
|
188
|
-
} else {
|
|
189
|
-
// Only show label if value is a multiple of labelInterval
|
|
190
|
-
const eps = 1e-9;
|
|
191
|
-
const isMultiple = (v, candidate) => Math.abs(v / candidate - Math.round(v / candidate)) < eps;
|
|
192
|
-
shouldShowLabel = isMultiple(value, labelInterval);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
if (shouldShowLabel) {
|
|
197
|
-
this.addTickLabel(gridHolder, isXAxis, pos, value);
|
|
198
|
-
// store the actual clamped center used by addTickLabel
|
|
199
|
-
if (isXAxis) {
|
|
200
|
-
const center = Math.max(20, Math.min(pos, this.width - 20));
|
|
201
|
-
lastLabelPos = center;
|
|
202
|
-
} else {
|
|
203
|
-
const center = Math.max(15, Math.min(pos, this.height - 7.5));
|
|
204
|
-
lastLabelPos = center;
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
if (0 >= min && 0 <= max && !zeroLineDrawn) {
|
|
210
|
-
const zeroPos = this.computeAxisPos(isXAxis, 0);
|
|
211
|
-
const zeroLine = new jsvgLine();
|
|
212
|
-
this.setAxisLineEndpoints(isXAxis, zeroLine, zeroPos);
|
|
213
|
-
zeroLine.setStrokeColor("black");
|
|
214
|
-
zeroLine.setOpacity(1.0);
|
|
215
|
-
gridHolder.addChild(zeroLine);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
createAxisLabels(gridHolder) {
|
|
220
|
-
if (this.xLabel) {
|
|
221
|
-
const w = Math.max(60, this.xLabel.length * 10);
|
|
222
|
-
const h = 20;
|
|
223
|
-
const xAxisLabel = new jsvgTextBox();
|
|
224
|
-
|
|
225
|
-
xAxisLabel.setWidthAndHeight(w, h);
|
|
226
|
-
xAxisLabel.setText(this.xLabel);
|
|
227
|
-
xAxisLabel.setFontSize(14);
|
|
228
|
-
xAxisLabel.setFontColor("black");
|
|
229
|
-
xAxisLabel.setAlignment("center");
|
|
230
|
-
xAxisLabel.setVerticalCentering();
|
|
231
|
-
xAxisLabel.setFontWeight(600);
|
|
232
|
-
|
|
233
|
-
const idealX = this.paddingLeft + this.graphWidth / 2 - w / 2;
|
|
234
|
-
const lx = Math.max(5, Math.min(idealX, this.width - w - 5));
|
|
235
|
-
|
|
236
|
-
xAxisLabel.setPosition(lx, this.height - this.paddingBottom + this.axisLabelOffsetPx);
|
|
237
|
-
gridHolder.addChild(xAxisLabel);
|
|
238
|
-
}
|
|
239
|
-
if (this.yLabel) {
|
|
240
|
-
const fontSize = 14;
|
|
241
|
-
const finalX = Math.max(15, this.paddingLeft - (this.axisLabelOffsetPx + 10));
|
|
242
|
-
const finalY = this.paddingTop + this.graphHeight / 2;
|
|
243
|
-
const labelGroup = new jsvgGroup();
|
|
244
|
-
const yAxisLabel = new jsvgTextLine();
|
|
245
|
-
|
|
246
|
-
yAxisLabel.setText(this.yLabel);
|
|
247
|
-
yAxisLabel.setFontSize(fontSize);
|
|
248
|
-
yAxisLabel.setFontColor("black");
|
|
249
|
-
yAxisLabel.setPosition(0, 0);
|
|
250
|
-
yAxisLabel.svgObject.setAttribute("text-anchor", "middle");
|
|
251
|
-
yAxisLabel.svgObject.setAttribute("dominant-baseline", "middle");
|
|
252
|
-
yAxisLabel.setFontWeight(600);
|
|
253
|
-
|
|
254
|
-
labelGroup.addChild(yAxisLabel);
|
|
255
|
-
labelGroup.svgObject.setAttribute("transform", `translate(${finalX}, ${finalY}) rotate(-90)`);
|
|
256
|
-
|
|
257
|
-
gridHolder.addChild(labelGroup);
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
makeGrid(gridHolder) {
|
|
262
|
-
this.createAxisGrid(gridHolder, true);
|
|
263
|
-
this.createAxisGrid(gridHolder, false);
|
|
264
|
-
this.createAxisLabels(gridHolder);
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
// ===== Helper functions =====
|
|
268
|
-
|
|
269
|
-
calculatePadding() {
|
|
270
|
-
this.paddingLeft = this.yLabel ? 50 : 30;
|
|
271
|
-
this.paddingBottom = this.xLabel ? 50 : 30;
|
|
272
|
-
this.paddingTop = 25;
|
|
273
|
-
this.paddingRight = 25;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
computeAxisPos(isXAxis, value) {
|
|
277
|
-
return isXAxis
|
|
278
|
-
? this.paddingLeft + (value - this.xMin) * this.xSpacer
|
|
279
|
-
: this.height - this.paddingBottom - (value - this.yMin) * this.ySpacer;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
setAxisLineEndpoints(isXAxis, line, pos) {
|
|
283
|
-
if (isXAxis) {
|
|
284
|
-
line.setEndpoints(pos, this.paddingTop, pos, this.height - this.paddingBottom);
|
|
285
|
-
} else {
|
|
286
|
-
line.setEndpoints(this.paddingLeft, pos, this.width - this.paddingRight, pos);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
addTickLabel(gridHolder, isXAxis, pos, value) {
|
|
291
|
-
const label = this.createNumericLabel(value);
|
|
292
|
-
if (isXAxis) {
|
|
293
|
-
const lx = Math.max(10, Math.min(pos - 10, this.width - 30));
|
|
294
|
-
label.setPosition(lx, this.height - this.paddingBottom + this.tickLabelOffsetPx);
|
|
295
|
-
} else {
|
|
296
|
-
const lx = Math.max(5, this.paddingLeft - (this.tickLabelOffsetPx + 20));
|
|
297
|
-
const ly = Math.max(7.5, Math.min(pos - 7.5, this.height - 15));
|
|
298
|
-
label.setPosition(lx, ly);
|
|
299
|
-
}
|
|
300
|
-
gridHolder.addChild(label);
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
toGraphPixelX(x) {
|
|
304
|
-
return (x - this.xMin) * this.xSpacer;
|
|
305
|
-
}
|
|
306
|
-
toGraphPixelY(y) {
|
|
307
|
-
return this.graphHeight - (y - this.yMin) * this.ySpacer;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
createNumericLabel(text) {
|
|
311
|
-
const label = new jsvgTextBox();
|
|
312
|
-
label.setWidthAndHeight(20, 15);
|
|
313
|
-
label.setText(String(text));
|
|
314
|
-
label.setFontSize(9);
|
|
315
|
-
label.setFontColor(this.getAllowedColor("black"));
|
|
316
|
-
label.setAlignment("center");
|
|
317
|
-
label.setVerticalCentering();
|
|
318
|
-
return label;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
getAllowedColor(inputColor) {
|
|
322
|
-
if (!inputColor) return "black";
|
|
323
|
-
const k = String(inputColor).trim();
|
|
324
|
-
return omdColor[k.toLowerCase()] || k || "black"; //first try omdColor, then try string, then black
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
assignFromData(data, mappings) {
|
|
328
|
-
for (const [key, type] of Object.entries(mappings)) {
|
|
329
|
-
if (typeof data[key] === type) this[key] = data[key];
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
calculateGraphDimensions() {
|
|
334
|
-
let baseSize = 200;
|
|
335
|
-
let dotSize = 8;
|
|
336
|
-
|
|
337
|
-
if (this.size === "large") {
|
|
338
|
-
baseSize = 300;
|
|
339
|
-
dotSize = 12;
|
|
340
|
-
} else if (this.size === "small") {
|
|
341
|
-
baseSize = 100;
|
|
342
|
-
dotSize = 6;
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
this.dotSize = dotSize;
|
|
346
|
-
this.xSpan = this.xMax - this.xMin;
|
|
347
|
-
this.ySpan = this.yMax - this.yMin;
|
|
348
|
-
|
|
349
|
-
const unit = Math.min(baseSize / this.xSpan, baseSize / this.ySpan);
|
|
350
|
-
|
|
351
|
-
this.graphWidth = unit * this.xSpan;
|
|
352
|
-
this.graphHeight = unit * this.ySpan;
|
|
353
|
-
|
|
354
|
-
this.width = this.graphWidth + this.paddingLeft + this.paddingRight;
|
|
355
|
-
this.height = this.graphHeight + this.paddingTop + this.paddingBottom;
|
|
356
|
-
|
|
357
|
-
this.xSpacer = unit;
|
|
358
|
-
this.ySpacer = unit;
|
|
359
|
-
|
|
360
|
-
this.originX = this.paddingLeft - this.xMin * this.xSpacer;
|
|
361
|
-
this.originY = this.height - this.paddingBottom + this.yMin * this.ySpacer;
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
graphMultipleFunctions(holder) {
|
|
365
|
-
for (const functionConfig of this.graphEquations) {
|
|
366
|
-
const path = new jsvgPath();
|
|
367
|
-
path.setStrokeColor(this.getAllowedColor(functionConfig.color));
|
|
368
|
-
path.setStrokeWidth(functionConfig.strokeWidth);
|
|
369
|
-
this.graphFunctionWithDomain(path, functionConfig.equation, functionConfig.domain);
|
|
370
|
-
holder.addChild(path);
|
|
371
|
-
|
|
372
|
-
if (functionConfig.label) {
|
|
373
|
-
this.addFunctionLabel(holder, functionConfig);
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
graphFunctionWithDomain(pathObject, functionString, domain) {
|
|
379
|
-
pathObject.clearPoints();
|
|
380
|
-
|
|
381
|
-
let expression = functionString;
|
|
382
|
-
if (expression.toLowerCase().startsWith("y=")) {
|
|
383
|
-
expression = expression.substring(2).trim();
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
const compiledExpression = math.compile(expression);
|
|
387
|
-
|
|
388
|
-
const leftLimit = (domain && typeof domain.min === 'number') ? domain.min : this.xMin;
|
|
389
|
-
const rightLimit = (domain && typeof domain.max === 'number') ? domain.max : this.xMax;
|
|
390
|
-
|
|
391
|
-
const step = Math.abs(rightLimit - leftLimit) / 1000;
|
|
392
|
-
for (let x = leftLimit; x <= rightLimit; x += step) {
|
|
393
|
-
const y = compiledExpression.evaluate({ x });
|
|
394
|
-
pathObject.addPoint(this.toGraphPixelX(x), this.toGraphPixelY(y));
|
|
395
|
-
}
|
|
396
|
-
pathObject.updatePath();
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
addFunctionLabel(holder, functionConfig) {
|
|
400
|
-
const labelText = String(functionConfig.label);
|
|
401
|
-
const fontSize = 12;
|
|
402
|
-
const padding = 6;
|
|
403
|
-
const estimatedWidth = Math.max(40, labelText.length * (fontSize * 0.6));
|
|
404
|
-
const estimatedHeight = fontSize + padding;
|
|
405
|
-
|
|
406
|
-
const label = new jsvgTextBox();
|
|
407
|
-
label.setWidthAndHeight(estimatedWidth, estimatedHeight);
|
|
408
|
-
label.setText(labelText);
|
|
409
|
-
label.setFontSize(fontSize);
|
|
410
|
-
label.setFontColor(this.getAllowedColor(functionConfig.color || "black"));
|
|
411
|
-
label.setAlignment("center");
|
|
412
|
-
label.setVerticalCentering();
|
|
413
|
-
label.setFontWeight(500);
|
|
414
|
-
|
|
415
|
-
const anchorX = (typeof functionConfig.labelAtX === 'number')
|
|
416
|
-
? functionConfig.labelAtX
|
|
417
|
-
: this.xMin + (this.xMax - this.xMin) * 0.1;
|
|
418
|
-
|
|
419
|
-
let anchorY = this.yMin + (this.yMax - this.yMin) * 0.1;
|
|
420
|
-
let equationBody = functionConfig.equation;
|
|
421
|
-
if (equationBody.toLowerCase().startsWith("y=")) {
|
|
422
|
-
equationBody = equationBody.substring(2).trim();
|
|
423
|
-
}
|
|
424
|
-
const compiled = math.compile(equationBody);
|
|
425
|
-
const yVal = compiled.evaluate({ x: anchorX });
|
|
426
|
-
anchorY = yVal;
|
|
427
|
-
|
|
428
|
-
const xPx = this.toGraphPixelX(anchorX);
|
|
429
|
-
const yPx = this.toGraphPixelY(anchorY);
|
|
430
|
-
|
|
431
|
-
const positionPref = (functionConfig.labelPosition || 'below').toLowerCase();
|
|
432
|
-
const offset = 10;
|
|
433
|
-
let finalX = xPx;
|
|
434
|
-
let finalY = yPx;
|
|
435
|
-
switch (positionPref) {
|
|
436
|
-
case 'above':
|
|
437
|
-
finalY = yPx - offset;
|
|
438
|
-
break;
|
|
439
|
-
case 'below':
|
|
440
|
-
finalY = yPx + offset;
|
|
441
|
-
break;
|
|
442
|
-
case 'left':
|
|
443
|
-
finalX = xPx - offset;
|
|
444
|
-
break;
|
|
445
|
-
case 'right':
|
|
446
|
-
finalX = xPx + offset;
|
|
447
|
-
break;
|
|
448
|
-
default:
|
|
449
|
-
finalY = yPx + offset;
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
label.setPosition(finalX, finalY);
|
|
453
|
-
holder.addChild(label);
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
drawLineSegments(holder) {
|
|
457
|
-
for (const segment of this.lineSegments) {
|
|
458
|
-
const x1 = this.toGraphPixelX(segment.point1[0]);
|
|
459
|
-
const y1 = this.toGraphPixelY(segment.point1[1]);
|
|
460
|
-
const x2 = this.toGraphPixelX(segment.point2[0]);
|
|
461
|
-
const y2 = this.toGraphPixelY(segment.point2[1]);
|
|
462
|
-
|
|
463
|
-
const line = new jsvgLine();
|
|
464
|
-
line.setEndpoints(x1, y1, x2, y2);
|
|
465
|
-
line.setStrokeColor(this.getAllowedColor(segment.color || "blue"));
|
|
466
|
-
line.setStrokeWidth(segment.strokeWidth || 2);
|
|
467
|
-
holder.addChild(line);
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
plotDots(holder) {
|
|
472
|
-
for (const dot of this.dotValues) {
|
|
473
|
-
const pX = this.toGraphPixelX(dot[0]);
|
|
474
|
-
const pY = this.toGraphPixelY(dot[1]);
|
|
475
|
-
|
|
476
|
-
const ellipse = new jsvgEllipse();
|
|
477
|
-
ellipse.setWidthAndHeight(this.dotSize, this.dotSize);
|
|
478
|
-
ellipse.setPosition(pX, pY);
|
|
479
|
-
ellipse.setFillColor(this.getAllowedColor(dot.length >= 3 ? dot[2] : "black"));
|
|
480
|
-
holder.addChild(ellipse);
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
addShapes(holder) {
|
|
485
|
-
holder.setPosition(
|
|
486
|
-
-this.xMin * this.xSpacer,
|
|
487
|
-
this.graphHeight + this.yMin * this.ySpacer
|
|
488
|
-
);
|
|
489
|
-
|
|
490
|
-
const typeToCtor = {
|
|
491
|
-
rightTriangle: omdRightTriangle,
|
|
492
|
-
isoscelesTriangle: omdIsoscelesTriangle,
|
|
493
|
-
rectangle: omdRectangle,
|
|
494
|
-
ellipse: omdEllipse,
|
|
495
|
-
circle: omdCircle,
|
|
496
|
-
regularPolygon: omdRegularPolygon
|
|
497
|
-
};
|
|
498
|
-
|
|
499
|
-
for (const shapeData of this.shapeSet) {
|
|
500
|
-
const Ctor = typeToCtor[shapeData.omdType];
|
|
501
|
-
if (!Ctor) {
|
|
502
|
-
continue;
|
|
503
|
-
}
|
|
504
|
-
shapeData.unitScale = this.xSpacer;
|
|
505
|
-
const shape = new Ctor();
|
|
506
|
-
shape.loadFromJSON(shapeData);
|
|
507
|
-
if (shape.shapePath) {
|
|
508
|
-
shape.shapePath.setFillColor("none");
|
|
509
|
-
}
|
|
510
|
-
holder.addChild(shape);
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
// Background customization methods
|
|
515
|
-
setBackgroundColor(color) {
|
|
516
|
-
this.backgroundColor = color;
|
|
517
|
-
this.updateLayout();
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
setBackgroundCornerRadius(radius) {
|
|
521
|
-
this.backgroundCornerRadius = radius;
|
|
522
|
-
this.updateLayout();
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
setBackgroundOpacity(opacity) {
|
|
526
|
-
this.backgroundOpacity = Math.max(0, Math.min(1, opacity));
|
|
527
|
-
this.updateLayout();
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
setShowBackground(show) {
|
|
531
|
-
this.showBackground = show;
|
|
532
|
-
this.updateLayout();
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
setBackgroundStyle(options = {}) {
|
|
536
|
-
if (options.backgroundColor !== undefined) this.backgroundColor = options.backgroundColor;
|
|
537
|
-
if (options.cornerRadius !== undefined) this.backgroundCornerRadius = options.cornerRadius;
|
|
538
|
-
if (options.opacity !== undefined) this.backgroundOpacity = Math.max(0, Math.min(1, options.opacity));
|
|
539
|
-
if (options.show !== undefined) this.showBackground = options.show;
|
|
540
|
-
this.updateLayout();
|
|
541
|
-
}
|
|
1
|
+
import { omdColor } from "./omdColor.js";
|
|
2
|
+
import { jsvgGroup, jsvgRect, jsvgClipMask, jsvgLine, jsvgEllipse, jsvgTextBox, jsvgTextLine, jsvgPath } from "@teachinglab/jsvg";
|
|
3
|
+
import {
|
|
4
|
+
omdRightTriangle,
|
|
5
|
+
omdIsoscelesTriangle,
|
|
6
|
+
omdRectangle,
|
|
7
|
+
omdEllipse,
|
|
8
|
+
omdCircle,
|
|
9
|
+
omdRegularPolygon
|
|
10
|
+
} from "./omdShapes.js";
|
|
11
|
+
|
|
12
|
+
export class omdCoordinatePlane extends jsvgGroup {
|
|
13
|
+
constructor() {
|
|
14
|
+
super();
|
|
15
|
+
this.graphEquations = [];
|
|
16
|
+
this.lineSegments = [];
|
|
17
|
+
this.dotValues = [];
|
|
18
|
+
this.shapeSet = [];
|
|
19
|
+
|
|
20
|
+
this.xMin = -5;
|
|
21
|
+
this.xMax = 5;
|
|
22
|
+
this.yMin = -5;
|
|
23
|
+
this.yMax = 5;
|
|
24
|
+
|
|
25
|
+
this.xLabel = "";
|
|
26
|
+
this.yLabel = "";
|
|
27
|
+
this.axisLabelOffsetPx = 20; //offset from axes
|
|
28
|
+
|
|
29
|
+
this.size = "medium";
|
|
30
|
+
|
|
31
|
+
this.tickInterval = 1; //interval between ticks
|
|
32
|
+
this.forceAllTickLabels = true; // show all tick labels despite potential overlap
|
|
33
|
+
this.tickLabelOffsetPx = 5; //offset from axes
|
|
34
|
+
this.showTickLabels = true; // show numeric tick labels
|
|
35
|
+
|
|
36
|
+
// Background customization properties
|
|
37
|
+
this.backgroundColor = omdColor.lightGray;
|
|
38
|
+
this.backgroundCornerRadius = 15;
|
|
39
|
+
this.backgroundOpacity = 1.0;
|
|
40
|
+
this.showBackground = true;
|
|
41
|
+
|
|
42
|
+
this.calculatePadding();
|
|
43
|
+
this.updateLayout();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
loadFromJSON(data) {
|
|
49
|
+
// Assume valid JSON shape
|
|
50
|
+
this.graphEquations = data.graphEquations || [];
|
|
51
|
+
this.lineSegments = data.lineSegments || [];
|
|
52
|
+
this.dotValues = data.dotValues || [];
|
|
53
|
+
this.shapeSet = data.shapeSet || [];
|
|
54
|
+
|
|
55
|
+
this.xMin = (data.xMin !== undefined) ? data.xMin : this.xMin;
|
|
56
|
+
this.xMax = (data.xMax !== undefined) ? data.xMax : this.xMax;
|
|
57
|
+
this.yMin = (data.yMin !== undefined) ? data.yMin : this.yMin;
|
|
58
|
+
this.yMax = (data.yMax !== undefined) ? data.yMax : this.yMax;
|
|
59
|
+
|
|
60
|
+
this.xLabel = (data.xLabel !== undefined) ? data.xLabel : this.xLabel;
|
|
61
|
+
this.yLabel = (data.yLabel !== undefined) ? data.yLabel : this.yLabel;
|
|
62
|
+
this.axisLabelOffsetPx = (data.axisLabelOffsetPx !== undefined) ? data.axisLabelOffsetPx : this.axisLabelOffsetPx;
|
|
63
|
+
|
|
64
|
+
this.size = (data.size !== undefined) ? data.size : this.size;
|
|
65
|
+
|
|
66
|
+
this.tickInterval = (data.tickInterval !== undefined) ? data.tickInterval : this.tickInterval;
|
|
67
|
+
this.tickLabelOffsetPx = (data.tickLabelOffsetPx !== undefined) ? data.tickLabelOffsetPx : this.tickLabelOffsetPx;
|
|
68
|
+
this.forceAllTickLabels = (data.forceAllTickLabels !== undefined) ? data.forceAllTickLabels : this.forceAllTickLabels;
|
|
69
|
+
this.showTickLabels = (data.showTickLabels !== undefined) ? data.showTickLabels : this.showTickLabels;
|
|
70
|
+
|
|
71
|
+
// Load background customization properties
|
|
72
|
+
this.backgroundColor = data.backgroundColor || this.backgroundColor;
|
|
73
|
+
this.backgroundCornerRadius = (data.backgroundCornerRadius !== undefined) ? data.backgroundCornerRadius : this.backgroundCornerRadius;
|
|
74
|
+
this.backgroundOpacity = (data.backgroundOpacity !== undefined) ? data.backgroundOpacity : this.backgroundOpacity;
|
|
75
|
+
this.showBackground = (data.showBackground !== undefined) ? data.showBackground : this.showBackground;
|
|
76
|
+
|
|
77
|
+
this.calculatePadding();
|
|
78
|
+
this.updateLayout();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
updateLayout() {
|
|
84
|
+
this.removeAllChildren();
|
|
85
|
+
this.calculateGraphDimensions();
|
|
86
|
+
|
|
87
|
+
const outerWidth = this.width;
|
|
88
|
+
const outerHeight = this.height;
|
|
89
|
+
|
|
90
|
+
const clipMask = new jsvgClipMask(outerWidth, outerHeight, 15);
|
|
91
|
+
this.addChild(clipMask);
|
|
92
|
+
|
|
93
|
+
const whiteRect = new jsvgRect();
|
|
94
|
+
whiteRect.setWidthAndHeight(outerWidth, outerHeight);
|
|
95
|
+
|
|
96
|
+
// Apply customizable background properties
|
|
97
|
+
if (this.showBackground) {
|
|
98
|
+
whiteRect.setFillColor(this.backgroundColor);
|
|
99
|
+
if (this.backgroundOpacity < 1.0) {
|
|
100
|
+
whiteRect.setOpacity(this.backgroundOpacity);
|
|
101
|
+
}
|
|
102
|
+
} else {
|
|
103
|
+
whiteRect.setFillColor("transparent");
|
|
104
|
+
}
|
|
105
|
+
whiteRect.setStrokeWidth(0);
|
|
106
|
+
|
|
107
|
+
if (whiteRect.setCornerRadius && this.backgroundCornerRadius > 0) {
|
|
108
|
+
whiteRect.setCornerRadius(this.backgroundCornerRadius);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
clipMask.addChild(whiteRect);
|
|
112
|
+
|
|
113
|
+
const contentGroup = new jsvgGroup();
|
|
114
|
+
contentGroup.setPosition(0, 0);
|
|
115
|
+
clipMask.addChild(contentGroup);
|
|
116
|
+
|
|
117
|
+
this.setWidthAndHeight(outerWidth, outerHeight);
|
|
118
|
+
this.svgObject.setAttribute("viewBox", `0 0 ${outerWidth} ${outerHeight}`);
|
|
119
|
+
|
|
120
|
+
const gridHolder = new jsvgGroup();
|
|
121
|
+
contentGroup.addChild(gridHolder);
|
|
122
|
+
this.makeGrid(gridHolder);
|
|
123
|
+
|
|
124
|
+
const graphClipMask = new jsvgClipMask(this.graphWidth, this.graphHeight, 0);
|
|
125
|
+
graphClipMask.setPosition(this.paddingLeft, this.paddingTop);
|
|
126
|
+
contentGroup.addChild(graphClipMask);
|
|
127
|
+
|
|
128
|
+
// Traditional explicit calls instead of dynamic invocation
|
|
129
|
+
const functionsHolder = new jsvgGroup();
|
|
130
|
+
graphClipMask.addChild(functionsHolder);
|
|
131
|
+
this.graphMultipleFunctions(functionsHolder);
|
|
132
|
+
|
|
133
|
+
const segmentsHolder = new jsvgGroup();
|
|
134
|
+
graphClipMask.addChild(segmentsHolder);
|
|
135
|
+
this.drawLineSegments(segmentsHolder);
|
|
136
|
+
|
|
137
|
+
const dotsHolder = new jsvgGroup();
|
|
138
|
+
graphClipMask.addChild(dotsHolder);
|
|
139
|
+
this.plotDots(dotsHolder);
|
|
140
|
+
|
|
141
|
+
const shapesHolder = new jsvgGroup();
|
|
142
|
+
graphClipMask.addChild(shapesHolder);
|
|
143
|
+
this.addShapes(shapesHolder);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
createAxisGrid(gridHolder, isXAxis) {
|
|
147
|
+
const interval = this.tickInterval || 1;
|
|
148
|
+
const min = isXAxis ? this.xMin : this.yMin;
|
|
149
|
+
const max = isXAxis ? this.xMax : this.yMax;
|
|
150
|
+
|
|
151
|
+
const firstTick = Math.ceil(min);
|
|
152
|
+
const lastTick = Math.floor(max);
|
|
153
|
+
const minLabelSpacing = 10;
|
|
154
|
+
const labelWidthEstimate = 20; // matches createNumericLabel width
|
|
155
|
+
const requiredLabelSpacing = minLabelSpacing + labelWidthEstimate;
|
|
156
|
+
|
|
157
|
+
// Compute label interval for label display only
|
|
158
|
+
let labelInterval = interval;
|
|
159
|
+
if (!this.forceAllTickLabels) {
|
|
160
|
+
const span = isXAxis ? this.xMax - this.xMin : this.yMax - this.yMin;
|
|
161
|
+
let floored = Math.floor(span / 10) * 10;
|
|
162
|
+
let raw = floored / 6;
|
|
163
|
+
let rounded = Math.round(raw / 5) * 5;
|
|
164
|
+
if (rounded === 0) rounded = 1;
|
|
165
|
+
labelInterval = rounded;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
let lastLabelPos = -Infinity;
|
|
169
|
+
let zeroLineDrawn = false;
|
|
170
|
+
|
|
171
|
+
for (let value = firstTick; value <= lastTick; value += interval) {
|
|
172
|
+
const pos = this.computeAxisPos(isXAxis, value);
|
|
173
|
+
|
|
174
|
+
const line = new jsvgLine();
|
|
175
|
+
this.setAxisLineEndpoints(isXAxis, line, pos);
|
|
176
|
+
line.setStrokeColor("black");
|
|
177
|
+
line.setOpacity(value === 0 ? 1.0 : 0.15);
|
|
178
|
+
gridHolder.addChild(line);
|
|
179
|
+
|
|
180
|
+
if (value === 0) {
|
|
181
|
+
zeroLineDrawn = true;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
let shouldShowLabel = false;
|
|
185
|
+
if (this.showTickLabels) {
|
|
186
|
+
if (this.forceAllTickLabels) {
|
|
187
|
+
shouldShowLabel = true;
|
|
188
|
+
} else {
|
|
189
|
+
// Only show label if value is a multiple of labelInterval
|
|
190
|
+
const eps = 1e-9;
|
|
191
|
+
const isMultiple = (v, candidate) => Math.abs(v / candidate - Math.round(v / candidate)) < eps;
|
|
192
|
+
shouldShowLabel = isMultiple(value, labelInterval);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (shouldShowLabel) {
|
|
197
|
+
this.addTickLabel(gridHolder, isXAxis, pos, value);
|
|
198
|
+
// store the actual clamped center used by addTickLabel
|
|
199
|
+
if (isXAxis) {
|
|
200
|
+
const center = Math.max(20, Math.min(pos, this.width - 20));
|
|
201
|
+
lastLabelPos = center;
|
|
202
|
+
} else {
|
|
203
|
+
const center = Math.max(15, Math.min(pos, this.height - 7.5));
|
|
204
|
+
lastLabelPos = center;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (0 >= min && 0 <= max && !zeroLineDrawn) {
|
|
210
|
+
const zeroPos = this.computeAxisPos(isXAxis, 0);
|
|
211
|
+
const zeroLine = new jsvgLine();
|
|
212
|
+
this.setAxisLineEndpoints(isXAxis, zeroLine, zeroPos);
|
|
213
|
+
zeroLine.setStrokeColor("black");
|
|
214
|
+
zeroLine.setOpacity(1.0);
|
|
215
|
+
gridHolder.addChild(zeroLine);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
createAxisLabels(gridHolder) {
|
|
220
|
+
if (this.xLabel) {
|
|
221
|
+
const w = Math.max(60, this.xLabel.length * 10);
|
|
222
|
+
const h = 20;
|
|
223
|
+
const xAxisLabel = new jsvgTextBox();
|
|
224
|
+
|
|
225
|
+
xAxisLabel.setWidthAndHeight(w, h);
|
|
226
|
+
xAxisLabel.setText(this.xLabel);
|
|
227
|
+
xAxisLabel.setFontSize(14);
|
|
228
|
+
xAxisLabel.setFontColor("black");
|
|
229
|
+
xAxisLabel.setAlignment("center");
|
|
230
|
+
xAxisLabel.setVerticalCentering();
|
|
231
|
+
xAxisLabel.setFontWeight(600);
|
|
232
|
+
|
|
233
|
+
const idealX = this.paddingLeft + this.graphWidth / 2 - w / 2;
|
|
234
|
+
const lx = Math.max(5, Math.min(idealX, this.width - w - 5));
|
|
235
|
+
|
|
236
|
+
xAxisLabel.setPosition(lx, this.height - this.paddingBottom + this.axisLabelOffsetPx);
|
|
237
|
+
gridHolder.addChild(xAxisLabel);
|
|
238
|
+
}
|
|
239
|
+
if (this.yLabel) {
|
|
240
|
+
const fontSize = 14;
|
|
241
|
+
const finalX = Math.max(15, this.paddingLeft - (this.axisLabelOffsetPx + 10));
|
|
242
|
+
const finalY = this.paddingTop + this.graphHeight / 2;
|
|
243
|
+
const labelGroup = new jsvgGroup();
|
|
244
|
+
const yAxisLabel = new jsvgTextLine();
|
|
245
|
+
|
|
246
|
+
yAxisLabel.setText(this.yLabel);
|
|
247
|
+
yAxisLabel.setFontSize(fontSize);
|
|
248
|
+
yAxisLabel.setFontColor("black");
|
|
249
|
+
yAxisLabel.setPosition(0, 0);
|
|
250
|
+
yAxisLabel.svgObject.setAttribute("text-anchor", "middle");
|
|
251
|
+
yAxisLabel.svgObject.setAttribute("dominant-baseline", "middle");
|
|
252
|
+
yAxisLabel.setFontWeight(600);
|
|
253
|
+
|
|
254
|
+
labelGroup.addChild(yAxisLabel);
|
|
255
|
+
labelGroup.svgObject.setAttribute("transform", `translate(${finalX}, ${finalY}) rotate(-90)`);
|
|
256
|
+
|
|
257
|
+
gridHolder.addChild(labelGroup);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
makeGrid(gridHolder) {
|
|
262
|
+
this.createAxisGrid(gridHolder, true);
|
|
263
|
+
this.createAxisGrid(gridHolder, false);
|
|
264
|
+
this.createAxisLabels(gridHolder);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// ===== Helper functions =====
|
|
268
|
+
|
|
269
|
+
calculatePadding() {
|
|
270
|
+
this.paddingLeft = this.yLabel ? 50 : 30;
|
|
271
|
+
this.paddingBottom = this.xLabel ? 50 : 30;
|
|
272
|
+
this.paddingTop = 25;
|
|
273
|
+
this.paddingRight = 25;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
computeAxisPos(isXAxis, value) {
|
|
277
|
+
return isXAxis
|
|
278
|
+
? this.paddingLeft + (value - this.xMin) * this.xSpacer
|
|
279
|
+
: this.height - this.paddingBottom - (value - this.yMin) * this.ySpacer;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
setAxisLineEndpoints(isXAxis, line, pos) {
|
|
283
|
+
if (isXAxis) {
|
|
284
|
+
line.setEndpoints(pos, this.paddingTop, pos, this.height - this.paddingBottom);
|
|
285
|
+
} else {
|
|
286
|
+
line.setEndpoints(this.paddingLeft, pos, this.width - this.paddingRight, pos);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
addTickLabel(gridHolder, isXAxis, pos, value) {
|
|
291
|
+
const label = this.createNumericLabel(value);
|
|
292
|
+
if (isXAxis) {
|
|
293
|
+
const lx = Math.max(10, Math.min(pos - 10, this.width - 30));
|
|
294
|
+
label.setPosition(lx, this.height - this.paddingBottom + this.tickLabelOffsetPx);
|
|
295
|
+
} else {
|
|
296
|
+
const lx = Math.max(5, this.paddingLeft - (this.tickLabelOffsetPx + 20));
|
|
297
|
+
const ly = Math.max(7.5, Math.min(pos - 7.5, this.height - 15));
|
|
298
|
+
label.setPosition(lx, ly);
|
|
299
|
+
}
|
|
300
|
+
gridHolder.addChild(label);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
toGraphPixelX(x) {
|
|
304
|
+
return (x - this.xMin) * this.xSpacer;
|
|
305
|
+
}
|
|
306
|
+
toGraphPixelY(y) {
|
|
307
|
+
return this.graphHeight - (y - this.yMin) * this.ySpacer;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
createNumericLabel(text) {
|
|
311
|
+
const label = new jsvgTextBox();
|
|
312
|
+
label.setWidthAndHeight(20, 15);
|
|
313
|
+
label.setText(String(text));
|
|
314
|
+
label.setFontSize(9);
|
|
315
|
+
label.setFontColor(this.getAllowedColor("black"));
|
|
316
|
+
label.setAlignment("center");
|
|
317
|
+
label.setVerticalCentering();
|
|
318
|
+
return label;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
getAllowedColor(inputColor) {
|
|
322
|
+
if (!inputColor) return "black";
|
|
323
|
+
const k = String(inputColor).trim();
|
|
324
|
+
return omdColor[k.toLowerCase()] || k || "black"; //first try omdColor, then try string, then black
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
assignFromData(data, mappings) {
|
|
328
|
+
for (const [key, type] of Object.entries(mappings)) {
|
|
329
|
+
if (typeof data[key] === type) this[key] = data[key];
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
calculateGraphDimensions() {
|
|
334
|
+
let baseSize = 200;
|
|
335
|
+
let dotSize = 8;
|
|
336
|
+
|
|
337
|
+
if (this.size === "large") {
|
|
338
|
+
baseSize = 300;
|
|
339
|
+
dotSize = 12;
|
|
340
|
+
} else if (this.size === "small") {
|
|
341
|
+
baseSize = 100;
|
|
342
|
+
dotSize = 6;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
this.dotSize = dotSize;
|
|
346
|
+
this.xSpan = this.xMax - this.xMin;
|
|
347
|
+
this.ySpan = this.yMax - this.yMin;
|
|
348
|
+
|
|
349
|
+
const unit = Math.min(baseSize / this.xSpan, baseSize / this.ySpan);
|
|
350
|
+
|
|
351
|
+
this.graphWidth = unit * this.xSpan;
|
|
352
|
+
this.graphHeight = unit * this.ySpan;
|
|
353
|
+
|
|
354
|
+
this.width = this.graphWidth + this.paddingLeft + this.paddingRight;
|
|
355
|
+
this.height = this.graphHeight + this.paddingTop + this.paddingBottom;
|
|
356
|
+
|
|
357
|
+
this.xSpacer = unit;
|
|
358
|
+
this.ySpacer = unit;
|
|
359
|
+
|
|
360
|
+
this.originX = this.paddingLeft - this.xMin * this.xSpacer;
|
|
361
|
+
this.originY = this.height - this.paddingBottom + this.yMin * this.ySpacer;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
graphMultipleFunctions(holder) {
|
|
365
|
+
for (const functionConfig of this.graphEquations) {
|
|
366
|
+
const path = new jsvgPath();
|
|
367
|
+
path.setStrokeColor(this.getAllowedColor(functionConfig.color));
|
|
368
|
+
path.setStrokeWidth(functionConfig.strokeWidth);
|
|
369
|
+
this.graphFunctionWithDomain(path, functionConfig.equation, functionConfig.domain);
|
|
370
|
+
holder.addChild(path);
|
|
371
|
+
|
|
372
|
+
if (functionConfig.label) {
|
|
373
|
+
this.addFunctionLabel(holder, functionConfig);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
graphFunctionWithDomain(pathObject, functionString, domain) {
|
|
379
|
+
pathObject.clearPoints();
|
|
380
|
+
|
|
381
|
+
let expression = functionString;
|
|
382
|
+
if (expression.toLowerCase().startsWith("y=")) {
|
|
383
|
+
expression = expression.substring(2).trim();
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const compiledExpression = math.compile(expression);
|
|
387
|
+
|
|
388
|
+
const leftLimit = (domain && typeof domain.min === 'number') ? domain.min : this.xMin;
|
|
389
|
+
const rightLimit = (domain && typeof domain.max === 'number') ? domain.max : this.xMax;
|
|
390
|
+
|
|
391
|
+
const step = Math.abs(rightLimit - leftLimit) / 1000;
|
|
392
|
+
for (let x = leftLimit; x <= rightLimit; x += step) {
|
|
393
|
+
const y = compiledExpression.evaluate({ x });
|
|
394
|
+
pathObject.addPoint(this.toGraphPixelX(x), this.toGraphPixelY(y));
|
|
395
|
+
}
|
|
396
|
+
pathObject.updatePath();
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
addFunctionLabel(holder, functionConfig) {
|
|
400
|
+
const labelText = String(functionConfig.label);
|
|
401
|
+
const fontSize = 12;
|
|
402
|
+
const padding = 6;
|
|
403
|
+
const estimatedWidth = Math.max(40, labelText.length * (fontSize * 0.6));
|
|
404
|
+
const estimatedHeight = fontSize + padding;
|
|
405
|
+
|
|
406
|
+
const label = new jsvgTextBox();
|
|
407
|
+
label.setWidthAndHeight(estimatedWidth, estimatedHeight);
|
|
408
|
+
label.setText(labelText);
|
|
409
|
+
label.setFontSize(fontSize);
|
|
410
|
+
label.setFontColor(this.getAllowedColor(functionConfig.color || "black"));
|
|
411
|
+
label.setAlignment("center");
|
|
412
|
+
label.setVerticalCentering();
|
|
413
|
+
label.setFontWeight(500);
|
|
414
|
+
|
|
415
|
+
const anchorX = (typeof functionConfig.labelAtX === 'number')
|
|
416
|
+
? functionConfig.labelAtX
|
|
417
|
+
: this.xMin + (this.xMax - this.xMin) * 0.1;
|
|
418
|
+
|
|
419
|
+
let anchorY = this.yMin + (this.yMax - this.yMin) * 0.1;
|
|
420
|
+
let equationBody = functionConfig.equation;
|
|
421
|
+
if (equationBody.toLowerCase().startsWith("y=")) {
|
|
422
|
+
equationBody = equationBody.substring(2).trim();
|
|
423
|
+
}
|
|
424
|
+
const compiled = math.compile(equationBody);
|
|
425
|
+
const yVal = compiled.evaluate({ x: anchorX });
|
|
426
|
+
anchorY = yVal;
|
|
427
|
+
|
|
428
|
+
const xPx = this.toGraphPixelX(anchorX);
|
|
429
|
+
const yPx = this.toGraphPixelY(anchorY);
|
|
430
|
+
|
|
431
|
+
const positionPref = (functionConfig.labelPosition || 'below').toLowerCase();
|
|
432
|
+
const offset = 10;
|
|
433
|
+
let finalX = xPx;
|
|
434
|
+
let finalY = yPx;
|
|
435
|
+
switch (positionPref) {
|
|
436
|
+
case 'above':
|
|
437
|
+
finalY = yPx - offset;
|
|
438
|
+
break;
|
|
439
|
+
case 'below':
|
|
440
|
+
finalY = yPx + offset;
|
|
441
|
+
break;
|
|
442
|
+
case 'left':
|
|
443
|
+
finalX = xPx - offset;
|
|
444
|
+
break;
|
|
445
|
+
case 'right':
|
|
446
|
+
finalX = xPx + offset;
|
|
447
|
+
break;
|
|
448
|
+
default:
|
|
449
|
+
finalY = yPx + offset;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
label.setPosition(finalX, finalY);
|
|
453
|
+
holder.addChild(label);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
drawLineSegments(holder) {
|
|
457
|
+
for (const segment of this.lineSegments) {
|
|
458
|
+
const x1 = this.toGraphPixelX(segment.point1[0]);
|
|
459
|
+
const y1 = this.toGraphPixelY(segment.point1[1]);
|
|
460
|
+
const x2 = this.toGraphPixelX(segment.point2[0]);
|
|
461
|
+
const y2 = this.toGraphPixelY(segment.point2[1]);
|
|
462
|
+
|
|
463
|
+
const line = new jsvgLine();
|
|
464
|
+
line.setEndpoints(x1, y1, x2, y2);
|
|
465
|
+
line.setStrokeColor(this.getAllowedColor(segment.color || "blue"));
|
|
466
|
+
line.setStrokeWidth(segment.strokeWidth || 2);
|
|
467
|
+
holder.addChild(line);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
plotDots(holder) {
|
|
472
|
+
for (const dot of this.dotValues) {
|
|
473
|
+
const pX = this.toGraphPixelX(dot[0]);
|
|
474
|
+
const pY = this.toGraphPixelY(dot[1]);
|
|
475
|
+
|
|
476
|
+
const ellipse = new jsvgEllipse();
|
|
477
|
+
ellipse.setWidthAndHeight(this.dotSize, this.dotSize);
|
|
478
|
+
ellipse.setPosition(pX, pY);
|
|
479
|
+
ellipse.setFillColor(this.getAllowedColor(dot.length >= 3 ? dot[2] : "black"));
|
|
480
|
+
holder.addChild(ellipse);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
addShapes(holder) {
|
|
485
|
+
holder.setPosition(
|
|
486
|
+
-this.xMin * this.xSpacer,
|
|
487
|
+
this.graphHeight + this.yMin * this.ySpacer
|
|
488
|
+
);
|
|
489
|
+
|
|
490
|
+
const typeToCtor = {
|
|
491
|
+
rightTriangle: omdRightTriangle,
|
|
492
|
+
isoscelesTriangle: omdIsoscelesTriangle,
|
|
493
|
+
rectangle: omdRectangle,
|
|
494
|
+
ellipse: omdEllipse,
|
|
495
|
+
circle: omdCircle,
|
|
496
|
+
regularPolygon: omdRegularPolygon
|
|
497
|
+
};
|
|
498
|
+
|
|
499
|
+
for (const shapeData of this.shapeSet) {
|
|
500
|
+
const Ctor = typeToCtor[shapeData.omdType];
|
|
501
|
+
if (!Ctor) {
|
|
502
|
+
continue;
|
|
503
|
+
}
|
|
504
|
+
shapeData.unitScale = this.xSpacer;
|
|
505
|
+
const shape = new Ctor();
|
|
506
|
+
shape.loadFromJSON(shapeData);
|
|
507
|
+
if (shape.shapePath) {
|
|
508
|
+
shape.shapePath.setFillColor("none");
|
|
509
|
+
}
|
|
510
|
+
holder.addChild(shape);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// Background customization methods
|
|
515
|
+
setBackgroundColor(color) {
|
|
516
|
+
this.backgroundColor = color;
|
|
517
|
+
this.updateLayout();
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
setBackgroundCornerRadius(radius) {
|
|
521
|
+
this.backgroundCornerRadius = radius;
|
|
522
|
+
this.updateLayout();
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
setBackgroundOpacity(opacity) {
|
|
526
|
+
this.backgroundOpacity = Math.max(0, Math.min(1, opacity));
|
|
527
|
+
this.updateLayout();
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
setShowBackground(show) {
|
|
531
|
+
this.showBackground = show;
|
|
532
|
+
this.updateLayout();
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
setBackgroundStyle(options = {}) {
|
|
536
|
+
if (options.backgroundColor !== undefined) this.backgroundColor = options.backgroundColor;
|
|
537
|
+
if (options.cornerRadius !== undefined) this.backgroundCornerRadius = options.cornerRadius;
|
|
538
|
+
if (options.opacity !== undefined) this.backgroundOpacity = Math.max(0, Math.min(1, options.opacity));
|
|
539
|
+
if (options.show !== undefined) this.showBackground = options.show;
|
|
540
|
+
this.updateLayout();
|
|
541
|
+
}
|
|
542
542
|
}
|