@@ -17,17 +17,22 @@ import { toPng } from "html-to-image";
1717import type {
1818 FunctionPlotDatum ,
1919 FunctionPlotOptions ,
20+ FunctionPlotTip ,
2021} from "function-plot/dist/types" ;
2122import { FunctionPlot } from "../fnplot" ;
2223
2324export function gcd ( a : number , b : number ) : number {
2425 return ! b ? a : gcd ( b , a % b ) ;
2526}
2627
27- function parseLaTeX ( latex : string | undefined ) : string | undefined {
28+ function parseLaTeX (
29+ latex : string | undefined ,
30+ plugin : ObsidianFunctionPlot
31+ ) : string | undefined {
2832 return latex === undefined
2933 ? latex
3034 : latex
35+ . replace ( plugin . settings . decimalSeparator , "." )
3136 . replace ( "f(x)=" , "" )
3237 . replace ( / \\ f r a c \{ ( [ ^ { } ] + ) \} \{ ( [ ^ { } ] + ) \} / g, "($1)/($2)" )
3338 . replace ( / \\ c d o t / g, "*" )
@@ -49,17 +54,19 @@ function parseLaTeX(latex: string | undefined): string | undefined {
4954// TODO make change to returned object reflect in input
5055export function toFunctionPlotOptions (
5156 options : PlotInputs ,
52- target : HTMLElement
57+ target : HTMLElement ,
58+ plugin : ObsidianFunctionPlot
5359) : FunctionPlotOptions {
5460 function functionInputsToFunctionPlotDatum (
55- inputs : FunctionInputs
61+ inputs : FunctionInputs ,
62+ plugin : ObsidianFunctionPlot
5663 ) : FunctionPlotDatum {
5764 const output : FunctionPlotDatum = {
5865 fnType : inputs . fnType ,
5966 graphType : inputs . graphType ?? undefined ,
6067 fn :
6168 inputs . fnType === "linear"
62- ? parseLaTeX ( inputs . fn ) ?? undefined
69+ ? parseLaTeX ( inputs . fn , plugin ) ?? undefined
6370 : undefined ,
6471 points :
6572 inputs . fnType === "points" && inputs . points !== DEFAULT_POINTS
@@ -98,6 +105,14 @@ export function toFunctionPlotOptions(
98105 nSamples : inputs . nSamples ? Math . min ( inputs . nSamples , 999 ) : undefined ,
99106 closed : inputs . closed ?? undefined ,
100107 skipTip : inputs . skipTip ?? undefined ,
108+ derivative :
109+ inputs . derivative ?. fn === ""
110+ ? undefined
111+ : {
112+ fn : parseLaTeX ( inputs . derivative ?. fn , plugin ) ,
113+ x0 : inputs . derivative ?. x0 ?? undefined ,
114+ updateOnMouseMove : inputs . derivative ?. updateOnMouseMove ?? true ,
115+ } ,
101116 } ;
102117
103118 Object . keys ( output ) . forEach (
@@ -123,7 +138,7 @@ export function toFunctionPlotOptions(
123138 target : target ,
124139 data : options . data
125140 . filter ( hasFunction )
126- . map ( functionInputsToFunctionPlotDatum ) ,
141+ . map ( ( data ) => functionInputsToFunctionPlotDatum ( data , plugin ) ) ,
127142 title : options . title ?? undefined ,
128143 xAxis : {
129144 label : options . xAxis . label ?? FALLBACK_PLOT_INPUTS . xAxis . label ,
@@ -141,6 +156,17 @@ export function toFunctionPlotOptions(
141156 } ,
142157 grid : options . grid ?? undefined ,
143158 disableZoom : options . disableZoom ?? undefined ,
159+ tip : {
160+ renderer : ( x : number , y : number , index : number ) => {
161+ return options . tip . renderer === undefined || options . tip . renderer === ""
162+ ? "(" + x . toFixed ( 2 ) . toString ( ) + "," + y . toFixed ( 2 ) . toString ( ) + ")"
163+ : options . tip . renderer
164+ . replace ( "x" , x . toFixed ( 2 ) . toString ( ) )
165+ . replace ( "y" , y . toFixed ( 2 ) . toString ( ) ) ;
166+ } ,
167+ xLine : options . tip . xLine ?? undefined ,
168+ yLine : options . tip . yLine ?? undefined ,
169+ } as FunctionPlotTip ,
144170 } ;
145171
146172 Object . keys ( output ) . forEach (
@@ -291,6 +317,7 @@ export function parseYAMLCodeBlock(content: string): PlotInputs {
291317 fn, // return as FunctionInputs since fn is specified here
292318 } ) as FunctionInputs ;
293319 } ) ,
320+ tip : { } ,
294321 } ;
295322}
296323
0 commit comments