@@ -2,7 +2,7 @@ import defaults from '../core/core.defaults';
2
2
import Element from '../core/core.element' ;
3
3
import layouts from '../core/core.layouts' ;
4
4
import { drawPoint } from '../helpers/helpers.canvas' ;
5
- import { callback as call , mergeIf , valueOrDefault } from '../helpers/helpers.core' ;
5
+ import { callback as call , mergeIf , valueOrDefault , isNullOrUndef } from '../helpers/helpers.core' ;
6
6
import { toFont , toPadding } from '../helpers/helpers.options' ;
7
7
import { getRtlAdapter , overrideTextDirection , restoreTextDirection } from '../helpers/helpers.rtl' ;
8
8
@@ -90,9 +90,23 @@ defaults.set('legend', {
90
90
* @return {number } width of the color box area
91
91
*/
92
92
function getBoxWidth ( labelOpts , fontSize ) {
93
- return labelOpts . usePointStyle && labelOpts . boxWidth > fontSize ?
93
+ const { boxWidth} = labelOpts ;
94
+ return ( labelOpts . usePointStyle && boxWidth > fontSize ) || isNullOrUndef ( boxWidth ) ?
94
95
fontSize :
95
- labelOpts . boxWidth ;
96
+ boxWidth ;
97
+ }
98
+
99
+ /**
100
+ * Helper function to get the box height
101
+ * @param {object } labelOpts - the label options on the legend
102
+ * @param {* } fontSize - the label font size
103
+ * @return {number } height of the color box area
104
+ */
105
+ function getBoxHeight ( labelOpts , fontSize ) {
106
+ const { boxHeight} = labelOpts ;
107
+ return ( labelOpts . usePointStyle && boxHeight > fontSize ) || isNullOrUndef ( boxHeight ) ?
108
+ fontSize :
109
+ boxHeight ;
96
110
}
97
111
98
112
export class Legend extends Element {
@@ -239,6 +253,9 @@ export class Legend extends Element {
239
253
const ctx = me . ctx ;
240
254
const labelFont = toFont ( labelOpts . font ) ;
241
255
const fontSize = labelFont . size ;
256
+ const boxWidth = getBoxWidth ( labelOpts , fontSize ) ;
257
+ const boxHeight = getBoxHeight ( labelOpts , fontSize ) ;
258
+ const itemHeight = Math . max ( boxHeight , fontSize ) ;
242
259
243
260
// Reset hit boxes
244
261
const hitboxes = me . legendHitBoxes = [ ] ;
@@ -271,11 +288,10 @@ export class Legend extends Element {
271
288
ctx . textBaseline = 'middle' ;
272
289
273
290
me . legendItems . forEach ( ( legendItem , i ) => {
274
- const boxWidth = getBoxWidth ( labelOpts , fontSize ) ;
275
291
const width = boxWidth + ( fontSize / 2 ) + ctx . measureText ( legendItem . text ) . width ;
276
292
277
293
if ( i === 0 || lineWidths [ lineWidths . length - 1 ] + width + 2 * labelOpts . padding > minSize . width ) {
278
- totalHeight += fontSize + labelOpts . padding ;
294
+ totalHeight += itemHeight + labelOpts . padding ;
279
295
lineWidths [ lineWidths . length - ( i > 0 ? 0 : 1 ) ] = 0 ;
280
296
}
281
297
@@ -284,7 +300,7 @@ export class Legend extends Element {
284
300
left : 0 ,
285
301
top : 0 ,
286
302
width,
287
- height : fontSize
303
+ height : itemHeight
288
304
} ;
289
305
290
306
lineWidths [ lineWidths . length - 1 ] += width + labelOpts . padding ;
@@ -302,7 +318,6 @@ export class Legend extends Element {
302
318
303
319
const heightLimit = minSize . height - titleHeight ;
304
320
me . legendItems . forEach ( ( legendItem , i ) => {
305
- const boxWidth = getBoxWidth ( labelOpts , fontSize ) ;
306
321
const itemWidth = boxWidth + ( fontSize / 2 ) + ctx . measureText ( legendItem . text ) . width ;
307
322
308
323
// If too tall, go to new column
@@ -323,7 +338,7 @@ export class Legend extends Element {
323
338
left : 0 ,
324
339
top : 0 ,
325
340
width : itemWidth ,
326
- height : fontSize
341
+ height : itemHeight ,
327
342
} ;
328
343
} ) ;
329
344
@@ -377,11 +392,13 @@ export class Legend extends Element {
377
392
ctx . font = labelFont . string ;
378
393
379
394
const boxWidth = getBoxWidth ( labelOpts , fontSize ) ;
395
+ const boxHeight = getBoxHeight ( labelOpts , fontSize ) ;
396
+ const height = Math . max ( fontSize , boxHeight ) ;
380
397
const hitboxes = me . legendHitBoxes ;
381
398
382
399
// current position
383
400
const drawLegendBox = function ( x , y , legendItem ) {
384
- if ( isNaN ( boxWidth ) || boxWidth <= 0 ) {
401
+ if ( isNaN ( boxWidth ) || boxWidth <= 0 || isNaN ( boxHeight ) || boxHeight < 0 ) {
385
402
return ;
386
403
}
387
404
@@ -417,9 +434,12 @@ export class Legend extends Element {
417
434
drawPoint ( ctx , drawOptions , centerX , centerY ) ;
418
435
} else {
419
436
// Draw box as legend symbol
420
- ctx . fillRect ( rtlHelper . leftForLtr ( x , boxWidth ) , y , boxWidth , fontSize ) ;
437
+ // Adjust position when boxHeight < fontSize (want it centered)
438
+ const yBoxTop = y + Math . max ( ( fontSize - boxHeight ) / 2 , 0 ) ;
439
+
440
+ ctx . fillRect ( rtlHelper . leftForLtr ( x , boxWidth ) , yBoxTop , boxWidth , boxHeight ) ;
421
441
if ( lineWidth !== 0 ) {
422
- ctx . strokeRect ( rtlHelper . leftForLtr ( x , boxWidth ) , y , boxWidth , fontSize ) ;
442
+ ctx . strokeRect ( rtlHelper . leftForLtr ( x , boxWidth ) , yBoxTop , boxWidth , boxHeight ) ;
423
443
}
424
444
}
425
445
@@ -429,8 +449,7 @@ export class Legend extends Element {
429
449
const fillText = function ( x , y , legendItem , textWidth ) {
430
450
const halfFontSize = fontSize / 2 ;
431
451
const xLeft = rtlHelper . xPlus ( x , boxWidth + halfFontSize ) ;
432
- const yMiddle = y + halfFontSize ;
433
-
452
+ const yMiddle = y + ( height / 2 ) ;
434
453
ctx . fillText ( legendItem . text , xLeft , yMiddle ) ;
435
454
436
455
if ( legendItem . hidden ) {
@@ -473,7 +492,7 @@ export class Legend extends Element {
473
492
474
493
overrideTextDirection ( me . ctx , opts . textDirection ) ;
475
494
476
- const itemHeight = fontSize + labelOpts . padding ;
495
+ const itemHeight = height + labelOpts . padding ;
477
496
me . legendItems . forEach ( ( legendItem , i ) => {
478
497
const textWidth = ctx . measureText ( legendItem . text ) . width ;
479
498
const width = boxWidth + ( fontSize / 2 ) + textWidth ;
0 commit comments