1
1
import { Editor , parseYaml } from "obsidian" ;
2
2
import type ObsidianFunctionPlot from "../main" ;
3
+ import type { Selection } from "d3" ;
3
4
import type {
4
5
FunctionInputs ,
5
6
PlotInputs ,
@@ -18,6 +19,7 @@ import type {
18
19
FunctionPlotOptions ,
19
20
} from "function-plot/dist/types" ;
20
21
import { FunctionPlot } from "../fnplot" ;
22
+ import type { Chart } from "function-plot" ;
21
23
22
24
export function gcd ( a : number , b : number ) : number {
23
25
return ! b ? a : gcd ( b , a % b ) ;
@@ -104,6 +106,23 @@ export function toFunctionPlotOptions(
104
106
options . yAxis . domain . max ?? FALLBACK_PLOT_INPUTS . yAxis . domain . max ,
105
107
] ,
106
108
} ,
109
+ plugins : [ ( chart : Chart ) => {
110
+ if ( ! options . legends ) return ;
111
+ chart . root . append ( "text" ) . attr ( "class" , "top-left-legend" ) ;
112
+ let text_color = "#00ff00" ;
113
+ let legends : { name : string , color : string } [ ] = [ ] ;
114
+ chart . options . data ?. forEach ( ( datum , index , arr ) => {
115
+ legends . push ( { name : ( options . data [ index ] . name ) || "" , color : datum . color || "" } ) ;
116
+ } )
117
+ const tll : any = chart . root . select ( ".top-left-legend" ) ;
118
+ console . log ( legends ) ;
119
+ tll . selectAll ( "tspan" ) . remove ( ) ;
120
+ legends . forEach ( ( legend , index , arr ) => {
121
+ tll . attr ( "y" , ( chart . meta . margin ?. top || 20 ) / 2 )
122
+ . attr ( "x" , chart . meta . margin ?. left || 10 )
123
+ tll . append ( "tspan" ) . attr ( 'fill' , legend . color ) . text ( "█ " + legend . name + "\n" ) ; ;
124
+ } )
125
+ } ] ,
107
126
grid : options . grid ?? undefined ,
108
127
disableZoom : options . disableZoom ?? undefined ,
109
128
} ;
@@ -230,6 +249,7 @@ export function parseYAMLCodeBlock(content: string): PlotInputs {
230
249
return {
231
250
constants : { } ,
232
251
title : header . title ?? DEFAULT_PLOT_INPUTS . title ,
252
+ legends : false ,
233
253
xAxis : {
234
254
label : header . xLabel ?? FALLBACK_PLOT_INPUTS . xAxis . label ,
235
255
domain : header . bounds
0 commit comments