Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tooltip boxWidth and boxHeight options #6995

Merged
merged 1 commit into from
Mar 6, 2020

Conversation

bjones526
Copy link
Contributor

Adds a new options to set colorBoxHeight and colorBoxWidth on tooltips.

Relates to #6654

Copy link
Contributor

@benmccann benmccann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't really gotten a chance to review or consider yet, but my first reaction is that this generally seems like it'd be okay, but I think we call this just "box" in the legend (instead of "colorBox"): https://www.chartjs.org/docs/latest/configuration/legend.html

src/plugins/plugin.tooltip.js Outdated Show resolved Hide resolved
@@ -275,7 +278,9 @@ function getTooltipSize(tooltip) {
+ options.titleMarginBottom;
}
if (combinedBodyLength) {
height += combinedBodyLength * bodyFontSize
// Body lines may include some extra height depending on boxHeight
const bodyLineHeight = bodyFontSize > boxHeight ? bodyFontSize : boxHeight;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const bodyLineHeight = Math.max(boxHeight, bodyFontSize)

height += combinedBodyLength * bodyFontSize
// Body lines may include some extra height depending on boxHeight
const bodyLineHeight = bodyFontSize > boxHeight ? bodyFontSize : boxHeight;
height += combinedBodyLength * bodyLineHeight
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the change in #6984, this isn't correct when boxHeight > bodyFontSize. I think the correct version is

height += combinedBodyLength ? bodyLineHeight + ((combinedBodyLength - 1) * bodyFontSize) : 0;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@etimberg Makes sense, you don't want to allocate all that space for the colorBox on lines where the colorbox is no longer drawn. Do you happen to have the configuration you were using for the chart in your screenshot handy?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not off hand. I believe I used https://github.com/chartjs/Chart.js/blob/master/samples/charts/bar/vertical.html and changed the labels on https://github.com/chartjs/Chart.js/blob/master/samples/charts/bar/vertical.html#L30 to

['January', 'February', 'March', ['April', 'line 2'], 'May', 'June', 'July']

@@ -753,14 +760,15 @@ class Tooltip extends Element {
drawBody(pt, ctx) {
const me = this;
const {body, options} = me;
const {bodyFontSize, bodySpacing, bodyAlign, displayColors} = options;
const {bodyFontSize, bodySpacing, bodyAlign, displayColors, boxHeight, boxWidth} = options;
const bodyLineHeight = bodyFontSize > boxHeight ? bodyFontSize : boxHeight;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here re. Math.max()

src/plugins/plugin.tooltip.js Show resolved Hide resolved
@bjones526
Copy link
Contributor Author

@etimberg I pushed a change to support multiline tooltips that should work in cases where some lines include the color box (with a custom height) and others don't. I did assume that body.length would be a reliable way to count the number of lines that include the colorBox. It works well with the samples in the codebase, but I wasn't sure if something like {before: ['line 1'], lines: [], after: ['line 2']} is technically a legal value for bodyItem. Let me know if I need to fix it.

Screen Shot 2020-02-04 at 1 21 16 PM

@etimberg
Copy link
Member

etimberg commented Feb 4, 2020

@bjones526 looking good. Would it be possible to align the second and third lines of text with the parent row as well? I think it'll look a lot better

@benmccann
Copy link
Contributor

@bjones526 this PR will need to be rebased. Hopefully it won't be too much trouble!

@benmccann
Copy link
Contributor

@bjones526 just checking in to see if you're still interested in pursuing this PR. thanks!

@bjones526
Copy link
Contributor Author

@benmccann thanks for the reminder - rebased and pushed since things had gotten stale.

@bjones526
Copy link
Contributor Author

@etimberg For the alignment - the unaligned rows in the screenshot are the before and after callbacks, which don't have the same indentation as lines that include the colorboxes. I'm happy to open another issue for more alignment options though!
Screen Shot 2020-03-04 at 10 27 30 PM

@etimberg
Copy link
Member

etimberg commented Mar 5, 2020

Ah, ok @bjones526 thanks for the clarification

@benmccann benmccann changed the title expose colorBoxWidth and colorBoxHeight on tooltip Add tooltip boxWidth and boxHeight options Mar 6, 2020
@etimberg etimberg added this to the Version 3.0 milestone Mar 6, 2020
@etimberg etimberg merged commit e1796d3 into chartjs:master Mar 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants