Skip to content

Commit af1a57b

Browse files
committed
still some weirdness on the side of funtionplot
1 parent 4fb0c97 commit af1a57b

4 files changed

Lines changed: 37 additions & 28 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"build:prod": "webpack --env=production",
1111
"build:dev": "webpack",
1212
"build": "npm run build:dev",
13+
"watch": "webpack --watch",
1314
"lint:prod": "npm run eslint -c .eslintrc.prod.json .",
1415
"test": "jest",
1516
"format": "npm run prettier --write .",

src/common/utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ export function toFunctionPlotOptions(
3737
fnType: inputs.fnType,
3838
graphType: inputs.graphType ?? undefined,
3939
fn: inputs.fnType === "linear" ? inputs.fn ?? undefined : undefined,
40-
scope: inputs.scope,
40+
scope: Object.keys(options.constants).length > 0 ? Object.keys(options.constants).reduce((acc, key) => {
41+
acc[key] = options.constants[key].value;
42+
return acc
43+
}, {}) as unknown as {[_: string]: number} : undefined,
4144
vector:
4245
inputs.fnType === "vector" && inputs.vector.x && inputs.vector.y
4346
? [inputs.vector.x, inputs.vector.y]

src/components/Plot/Plot.svelte

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,11 @@
2424
// datum.scope = scope;
2525
// });
2626
// }
27-
$: ((options: PlotInputs) => {
28-
plot.options = options;
29-
})(options);
30-
31-
let target: HTMLDivElement;
32-
$: ((target: HTMLElement) => {
33-
plot.target = target;
34-
console.log("updated target");
35-
})(target);
27+
$: plot.options = options;
3628
</script>
3729

3830
<div>
39-
<div class="fplt-plot" bind:this={target} />
31+
<div class="fplt-plot" bind:this={plot.target} />
4032
<div class="fplt-plot-options">
4133
<div class="fplt-constants">
4234
{#each Object.keys(options.constants) as name}

src/fnplot.ts

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import type { PlotInputs } from "./common/types";
33
import createStylingPlugin from "./plugins/styling";
44
import { toFunctionPlotOptions } from "./common/utils";
55
import type ObsidianFunctionPlot from "./main";
6-
import functionPlot from "function-plot";
6+
import functionPlot, { Chart } from "function-plot";
77

88
export class FunctionPlot {
9-
id?: string;
109
plugin: ObsidianFunctionPlot;
1110
target_: HTMLElement;
1211
options_: PlotInputs;
12+
fnPlotOptions?: FunctionPlotOptions;
13+
chart?: Chart;
1314

1415
constructor(plugin: ObsidianFunctionPlot) {
1516
this.plugin = plugin;
@@ -30,28 +31,40 @@ export class FunctionPlot {
3031
console.log("No target or options set");
3132
return;
3233
}
33-
const stylingPlugin = createStylingPlugin(this.plugin);
3434
try {
35-
const functionPlotOptions = Object.assign(
36-
{},
37-
toFunctionPlotOptions(this.options_, this.target_),
38-
{
39-
plugins: [stylingPlugin],
40-
id: this.id,
41-
// width: 55,
42-
// height: 35,
43-
}
44-
) as FunctionPlotOptions;
45-
console.log(functionPlotOptions);
46-
functionPlot(functionPlotOptions);
47-
this.id = functionPlotOptions.id; // save id to preserve viewport state
35+
if (this.fnPlotOptions !== undefined) {
36+
Object.assign(
37+
this.fnPlotOptions,
38+
toFunctionPlotOptions(this.options_, this.target_)
39+
);
40+
console.log('updated fnPlotOptions: ', JSON.parse(JSON.stringify(this.fnPlotOptions)))
41+
} else {
42+
this.fnPlotOptions = Object.assign(
43+
{},
44+
toFunctionPlotOptions(this.options_, this.target_),
45+
{ plugins: [createStylingPlugin(this.plugin)] }
46+
);
47+
console.log(
48+
"new fnPlotOptions: ",
49+
JSON.parse(JSON.stringify(this.fnPlotOptions))
50+
);
51+
52+
}
53+
if (this.chart !== undefined) {
54+
this.chart.build();
55+
console.log('redrew chart')
56+
} else {
57+
this.chart = functionPlot(this.fnPlotOptions);
58+
console.log('new chart')
59+
}
4860
} catch (err) {
4961
console.error(`Error rendering plot: ${err}`);
5062
}
5163
}
5264

5365
resetView(): void {
54-
this.id = undefined;
66+
this.fnPlotOptions = undefined;
67+
this.chart = undefined;
5568
this.render();
5669
}
5770
}

0 commit comments

Comments
 (0)