Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

Pie Graph legend on left or right is not vertically centered.

Open RZeni opened this issue 10 years ago • 28 comments

I have fixed this in my local copy by adding the following line into the draw function

y += (ctx.canvas.clientHeight - (itemHeight * me.legendItems.length)) / 2;

RZeni avatar Aug 20 '16 23:08 RZeni

@RZeni can you post screenshots of before and after your fix? I'd be happy to merge a PR fixing this

etimberg avatar Aug 22 '16 01:08 etimberg

If not resolved yet, here is what he did:

var textWidth = ctx.measureText(legendItem.text).width, width = boxWidth + (fontSize / 2) + textWidth, x = cursor.x, y = cursor.y; y += (ctx.canvas.clientHeight - (itemHeight * me.legendItems.length)) / 2;

And the results:

Before:

sem-ajuste

After: com-ajuste

Thanks btw, I was looking for it.

I added the following because it seems to bug the legend when it is not on the side of the chart.

if(me.options.position == 'left' || me.options.position == 'right') y += (ctx.canvas.clientHeight - (itemHeight * me.legendItems.length)) / 2;

guschnwg avatar Dec 23 '16 13:12 guschnwg

I took a look at this and noticed that the proposed solution would exhibit the following issues

  1. ctx.canvas.clientHeight is not guaranteed to be the render height of the canvas. When window.devicePixelRatio > 1 the canvas will have more pixels that it's CSS height
  2. itemHeight * me.legendItems.length will only give the legend height when there is one column of items. If there are enough data points, there will be multiple columns, and it will be incorrect

Proposed Solution

I think the better solution to this problem would be to update fit to store the height of each column. We already know it here. What we could do is save it into a new me._columnHeights array.

Then, we provide a new config option

legendOptions = {
  // options are 'start', 'end', 'center'. Default is 'start' which matches old behaviour
  align: 'start' 
}

During drawing, if the option is 'start' the legend draws at the top. If it's 'end' its at the bottom. 'center' does as expected. This option would apply to both horizontal and vertical legends and would need to be implemented accordingly for the horizontal case.

CC @chartjs/maintainers for input

etimberg avatar Jan 11 '17 01:01 etimberg

I think that allowing legend location configuration options as specified by @etimberg would be the best route.

panzarino avatar Jan 11 '17 02:01 panzarino

@etimberg sounds good, I prefer align, shorter and commonly used (e.g CSS box-align, text-align, vertical-align)?

simonbrunel avatar Jan 12 '17 15:01 simonbrunel

@simonbrunel that works for me

etimberg avatar Jan 12 '17 16:01 etimberg

Some additional thoughts:

image

Edit: updated proposal, pack renamed to align, align renamed to labels.align, labels.boxAlign replaced by labels.stretch

simonbrunel avatar Jan 14 '17 11:01 simonbrunel

Interesting ideas @simonbrunel

I like the box align settings. I like the idea of reverse, but I'm not sure about the name. To me reverse could be confused with legendOpts.reverse which reverses the order of items in the legend.

In terms of the pack setting, I'm not sure how feasible that is for horizontal legends. Right now, each row is filled first before going to the next. That doesn't really play well with aligning columns

etimberg avatar Jan 14 '17 16:01 etimberg

Initially, I pick legend.labels.direction (normal/reverse) to be consistent with the CSS box-direction. But finally chose legend.labels.reverse to be consistent with the legend.reverse option. Can be changed for direction if better.

legend.align is alignement inside the label (so always horizontal), so would not solve the reported issue (but solves #3367). align.pack is the solution for this ticket (the one you previously called alignment/align). I think pack is a better name since it really mimics the CSS box-pack feature.

simonbrunel avatar Jan 14 '17 19:01 simonbrunel

Hmm, I think we'll have to see how reverse ends up in practice. I see what you mean about keeping them consistent.

I like the name pack as well. I was thinking about this more. I do like how the options for pack change the legend. Though I certainly see start, center, and end as being the most used.

etimberg avatar Jan 14 '17 21:01 etimberg

+1

rightisleft avatar Feb 23 '17 20:02 rightisleft

I'm looking for this exact behavior. Is the above hack still the only way to fix it?

zerox1212 avatar Jul 23 '17 01:07 zerox1212

Was this ever implemented?

barrychapman avatar Jul 25 '17 05:07 barrychapman

+1

jakeboyles avatar Jul 27 '17 18:07 jakeboyles

What is the resolution on this issue? 4577 and 3175 (this) were both marked as duplicates and closed.

adammatthewsmith avatar Aug 29 '17 03:08 adammatthewsmith

@adammatthewsmith this issue has not been fixed

etimberg avatar Aug 29 '17 10:08 etimberg

@adammatthewsmith : @etimberg meant that this issue is still open (not closed). Unfortunatelly there is nobody on it right now.

Tatranskymedved avatar Aug 29 '17 12:08 Tatranskymedved

@simonbrunel the legend.align property has not been implemented yet right? are there any plans to when and if they will be implemented? is there any other oportunity to algin center a left/right legend?

EMFHDinf avatar Nov 17 '17 10:11 EMFHDinf

Any news on this? Currently I'm turning off the built-in legend and using the legendCallback to build my own for this reason -- which works well, but there are certain contexts where I want to use the built-in version

bathyscaaf avatar Nov 25 '17 04:11 bathyscaaf

is there any working model, like in JSfiddle or code pen

var textWidth = ctx.measureText(legendItem.text).width, width = boxWidth + (fontSize / 2) + textWidth, x = cursor.x, y = cursor.y; y += (ctx.canvas.clientHeight - (itemHeight * me.legendItems.length)) / 2;

like when i checked the chart.js docs for legend i found that default boxWidth is 40 and fontSize is 12 but from where are you getting the legendItem , x , y

and what is me for the below code ? also legendItems

if(me.options.position == 'left' || me.options.position == 'right') y += (ctx.canvas.clientHeight - (itemHeight * me.legendItems.length)) / 2;

mmdileepthomas avatar May 17 '18 13:05 mmdileepthomas