@@ -3,13 +3,14 @@ import type { PlotInputs } from "./common/types";
33import createStylingPlugin from "./plugins/styling" ;
44import { toFunctionPlotOptions } from "./common/utils" ;
55import type ObsidianFunctionPlot from "./main" ;
6- import functionPlot from "function-plot" ;
6+ import functionPlot , { Chart } from "function-plot" ;
77
88export 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