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;
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;
Comments are closed.
Copyright © 2021 Fantas...hit
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie | Duration | Description |
---|---|---|
cookielawinfo-checbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
cookielawinfo-checbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
cookielawinfo-checbox-others | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. |
cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
cookielawinfo-checkbox-performance | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance". |
viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
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:
After:

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;
I took a look at this and noticed that the proposed solution would exhibit the following issues
ctx.canvas.clientHeight
is not guaranteed to be the render height of the canvas. Whenwindow.devicePixelRatio > 1
the canvas will have more pixels that it’s CSS heightitemHeight * 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 incorrectProposed 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 newme._columnHeights
array.Then, we provide a new config option
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
Some additional thoughts:
+1
I’m looking for this exact behavior. Is the above hack still the only way to fix it?
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
Any update on this guys? 🙂
I found that @guschnwg hack works a bit incorrectly when we have paddings
So I changed this code:
To the following: