11import { Editor , parseYaml } from "obsidian" ;
22import type ObsidianFunctionPlot from "../main" ;
3+ import type { Selection } from "d3" ;
34import type {
45 FunctionInputs ,
56 PlotInputs ,
@@ -18,6 +19,7 @@ import type {
1819 FunctionPlotOptions ,
1920} from "function-plot/dist/types" ;
2021import { FunctionPlot } from "../fnplot" ;
22+ import type { Chart } from "function-plot" ;
2123
2224export function gcd ( a : number , b : number ) : number {
2325 return ! b ? a : gcd ( b , a % b ) ;
@@ -104,6 +106,23 @@ export function toFunctionPlotOptions(
104106 options . yAxis . domain . max ?? FALLBACK_PLOT_INPUTS . yAxis . domain . max ,
105107 ] ,
106108 } ,
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+ } ] ,
107126 grid : options . grid ?? undefined ,
108127 disableZoom : options . disableZoom ?? undefined ,
109128 } ;
@@ -230,6 +249,7 @@ export function parseYAMLCodeBlock(content: string): PlotInputs {
230249 return {
231250 constants : { } ,
232251 title : header . title ?? DEFAULT_PLOT_INPUTS . title ,
252+ legends : false ,
233253 xAxis : {
234254 label : header . xLabel ?? FALLBACK_PLOT_INPUTS . xAxis . label ,
235255 domain : header . bounds
0 commit comments