@@ -2,17 +2,59 @@ import {axisBottom, create, format, interpolate, interpolateRound, range, scaleB
22
33export function Legend (
44 { color : colorScale } ,
5- { color} = { }
5+ { color, title } = { }
66) {
7+
78 return {
8- ...( color && color . legend && { color : new ColorLegend ( colorScale , color ) } )
9+ ...( color && color . legend && { color : new ColorLegend ( colorScale , color . legend ) } ) ,
10+ ...( title && { title : new TitleLegend ( title ) } )
911 } ;
1012}
1113
14+ export class TitleLegend {
15+ constructor ( title ) {
16+ this . title = typeof title === "string" ? { text : title } : title ;
17+ if ( this . title . fontSize === undefined ) this . title . fontSize = 22 ;
18+ if ( this . title . top === undefined ) this . title . top = this . title . fontSize - 2 ;
19+ }
20+ render (
21+ index ,
22+ scales ,
23+ channels ,
24+ {
25+ width : canvasWidth ,
26+ height : canvasHeight
27+ }
28+ ) {
29+ const {
30+ text,
31+ bottom,
32+ fontSize,
33+ top,
34+ right,
35+ left = 0
36+ } = this . title ;
37+ const tx = right !== undefined ? canvasWidth - right : left ;
38+ const ty = bottom !== undefined ? canvasHeight - bottom - fontSize / 3 : top ;
39+ return create ( "svg:g" )
40+ . attr ( "transform" , `translate(${ tx } ,${ ty } )` )
41+ . call ( g => g . append ( "text" )
42+ . text ( text )
43+ . attr ( "fill" , "currentColor" )
44+ . attr ( "text-anchor" , right !== undefined ? "end" : "start" )
45+ . style ( "font-size" , fontSize )
46+ )
47+ . node ( ) ;
48+ }
49+ }
50+
1251export class ColorLegend {
13- constructor ( { name} , color ) {
52+ constructor ( { name, scale} , legend ) {
53+ this . color = scale ,
1454 this . name = name || "color" ;
15- this . color = color ;
55+ this . legend = typeof legend === "object" ? legend : { } ;
56+ if ( this . legend . tickSize === undefined ) this . legend . tickSize = 6 ;
57+ if ( this . legend . height === undefined ) this . legend . height = 44 + this . legend . tickSize ;
1658 }
1759 render (
1860 index ,
@@ -24,22 +66,20 @@ export class ColorLegend {
2466 height : canvasHeight
2567 }
2668 ) {
27- const { color : {
28- legend : {
29- title,
30- tickSize = 6 ,
31- width = 320 ,
32- height = 44 + tickSize ,
33- top = title === undefined ? - 20 : - 3 ,
34- right = 0 ,
35- bottom,
36- left,
37- ticks = width / 64 ,
38- tickFormat,
39- tickValues
40- } = { }
69+ const { legend : {
70+ title,
71+ tickSize = 6 ,
72+ width = 320 ,
73+ height = 44 + tickSize ,
74+ top = title === undefined ? - 20 : - 3 ,
75+ right = 0 ,
76+ bottom,
77+ left,
78+ ticks = width / 64 ,
79+ tickFormat,
80+ tickValues
4181 } = { } } = this ;
42- const tx = left !== undefined ? left : canvasWidth - width + right ;
82+ const tx = left !== undefined ? left : canvasWidth - width - right ;
4383 const ty = bottom !== undefined ? canvasHeight - bottom - height : top ;
4484 return create ( "svg:g" )
4585 . attr ( "transform" , `translate(${ tx } ,${ ty } )` )
0 commit comments