@@ -3,11 +3,11 @@ import Element from '../core/core.element';
33import layouts from '../core/core.layouts' ;
44import { drawPoint , renderText } from '../helpers/helpers.canvas' ;
55import {
6- callback as call , valueOrDefault , toFont , isObject ,
6+ callback as call , valueOrDefault , toFont ,
77 toPadding , getRtlAdapter , overrideTextDirection , restoreTextDirection ,
88 clipArea , unclipArea
99} from '../helpers/index' ;
10- import { _toLeftRightCenter , _alignStartEnd } from '../helpers/helpers.extras' ;
10+ import { _toLeftRightCenter , _alignStartEnd , _textX } from '../helpers/helpers.extras' ;
1111/**
1212 * @typedef { import("../platform/platform.base").ChartEvent } ChartEvent
1313 */
@@ -244,6 +244,7 @@ export class Legend extends Element {
244244 const labelFont = toFont ( labelOpts . font ) ;
245245 const { color : fontColor , padding} = labelOpts ;
246246 const fontSize = labelFont . size ;
247+ const halfFontSize = fontSize / 2 ;
247248 let cursor ;
248249
249250 me . drawTitle ( ) ;
@@ -287,7 +288,7 @@ export class Legend extends Element {
287288 borderWidth : lineWidth
288289 } ;
289290 const centerX = rtlHelper . xPlus ( x , boxWidth / 2 ) ;
290- const centerY = y + fontSize / 2 ;
291+ const centerY = y + halfFontSize ;
291292
292293 // Draw pointStyle as legend symbol
293294 drawPoint ( ctx , drawOptions , centerX , centerY ) ;
@@ -306,9 +307,10 @@ export class Legend extends Element {
306307 } ;
307308
308309 const fillText = function ( x , y , legendItem ) {
309- const halfFontSize = fontSize / 2 ;
310- const xLeft = rtlHelper . xPlus ( x , boxWidth + halfFontSize ) ;
311- renderText ( ctx , legendItem . text , xLeft , y + ( itemHeight / 2 ) , labelFont , { strikethrough : legendItem . hidden } ) ;
310+ renderText ( ctx , legendItem . text , x , y + ( itemHeight / 2 ) , labelFont , {
311+ strikethrough : legendItem . hidden ,
312+ textAlign : legendItem . textAlign
313+ } ) ;
312314 } ;
313315
314316 // Horizontal
@@ -333,6 +335,7 @@ export class Legend extends Element {
333335 const lineHeight = itemHeight + padding ;
334336 me . legendItems . forEach ( ( legendItem , i ) => {
335337 const textWidth = ctx . measureText ( legendItem . text ) . width ;
338+ const textAlign = rtlHelper . textAlign ( legendItem . textAlign || ( legendItem . textAlign = labelOpts . textAlign ) ) ;
336339 const width = boxWidth + ( fontSize / 2 ) + textWidth ;
337340 let x = cursor . x ;
338341 let y = cursor . y ;
@@ -358,8 +361,10 @@ export class Legend extends Element {
358361 legendHitBoxes [ i ] . left = rtlHelper . leftForLtr ( realX , legendHitBoxes [ i ] . width ) ;
359362 legendHitBoxes [ i ] . top = y ;
360363
364+ x = _textX ( textAlign , x + boxWidth + halfFontSize , me . right ) ;
365+
361366 // Fill the actual label
362- fillText ( realX , y , legendItem ) ;
367+ fillText ( rtlHelper . x ( x ) , y , legendItem ) ;
363368
364369 if ( isHorizontal ) {
365370 cursor . x += width + padding ;
@@ -577,13 +582,11 @@ export default {
577582 // lineWidth :
578583 generateLabels ( chart ) {
579584 const datasets = chart . data . datasets ;
580- const { labels} = chart . legend . options ;
581- const usePointStyle = labels . usePointStyle ;
582- const overrideStyle = labels . pointStyle ;
585+ const { labels : { usePointStyle, pointStyle, textAlign} } = chart . legend . options ;
583586
584587 return chart . _getSortedDatasetMetas ( ) . map ( ( meta ) => {
585588 const style = meta . controller . getStyle ( usePointStyle ? 0 : undefined ) ;
586- const borderWidth = isObject ( style . borderWidth ) ? ( valueOrDefault ( style . borderWidth . top , 0 ) + valueOrDefault ( style . borderWidth . left , 0 ) + valueOrDefault ( style . borderWidth . bottom , 0 ) + valueOrDefault ( style . borderWidth . right , 0 ) ) / 4 : style . borderWidth ;
589+ const borderWidth = toPadding ( style . borderWidth ) ;
587590
588591 return {
589592 text : datasets [ meta . index ] . label ,
@@ -593,10 +596,11 @@ export default {
593596 lineDash : style . borderDash ,
594597 lineDashOffset : style . borderDashOffset ,
595598 lineJoin : style . borderJoinStyle ,
596- lineWidth : borderWidth ,
599+ lineWidth : ( borderWidth . width + borderWidth . height ) / 4 ,
597600 strokeStyle : style . borderColor ,
598- pointStyle : overrideStyle || style . pointStyle ,
601+ pointStyle : pointStyle || style . pointStyle ,
599602 rotation : style . rotation ,
603+ textAlign : textAlign || style . textAlign ,
600604
601605 // Below is extra data used for toggling the datasets
602606 datasetIndex : meta . index
0 commit comments