Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Support dynamically sized legend elements#9532

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
andyrichardson wants to merge1 commit intochartjs:master
base:master
Choose a base branch
Loading
fromandyrichardson:master

Conversation

andyrichardson
Copy link

@andyrichardsonandyrichardson commentedAug 10, 2021
edited
Loading

About

When providing custom legend elements, dynamic widths will overflow.

Fix#5665

Tried this out with an image element

Screenshot from 2021-08-10 18-07-42

@@ -170,7 +170,9 @@ export class Legend extends Element {
let row = -1;
let top = -lineHeight;
me.legendItems.forEach((legendItem, i) => {
const itemWidth = boxWidth + (fontSize / 2) + ctx.measureText(legendItem.text).width;
const pointStyle = legendItem?.pointStyle;
const pointWidth = pointStyle?.offsetWidth || pointStyle?.width || boxWidth;

Choose a reason for hiding this comment

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

Would make sense to render this to the DOM in avisibility: hidden element, get offsetWidth, then remove from dom

Copy link
Member

Choose a reason for hiding this comment

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

I think we should avoid DOM manipulation.

Copy link
Member

@etimbergetimberg left a comment

Choose a reason for hiding this comment

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

Generally, this looks like something we could support. To get it to prod, the docs and types would need an update along with test coverage.

@kurkle thoughts on this?

andyrichardson reacted with thumbs up emoji
@@ -135,7 +135,7 @@ export function drawPoint(ctx, options, x, y) {
ctx.save();
ctx.translate(x, y);
ctx.rotate(rad);
ctx.drawImage(style,-style.width / 2, -style.height / 2, style.width, style.height);
ctx.drawImage(style,0, -style.height / 2, style.width, style.height);
Copy link
Member

Choose a reason for hiding this comment

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

What's the reason for this change? When this function is normally called,x,y is the center of the point and thus the center of the image

Choose a reason for hiding this comment

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

Centering the image looks to be done because there is an assumed (fixed) width where the image will exist.

With this approach, because the space for the legend image is identical to the size of the legend image itself, there's no need to center it.

Screenshot from 2021-08-11 13-39-22

That being said this was me hacking around and it resulted in no padding so centering might actually be a good idea if the "boxWidth" (legend icon width) is expected to include padding.

w: boxWidth,
h: boxHeight,
radius: borderRadius,
w: legendItem.boxWidth || boxWidth,
Copy link
Member

Choose a reason for hiding this comment

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

What's the reason for this change?

Choose a reason for hiding this comment

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

I found it unusual that boxWidth could be set for all legend items, but not an individual one.

Might just be me - can revert if needed 👍

Copy link
Member

@kurklekurkle left a comment

Choose a reason for hiding this comment

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

To me it looks like changing thegetBoxSize function to read the size from pointStyle would be a good solution. Will need to move that call in the loops to allow different sized images, but should be quite simple.

@@ -170,7 +170,9 @@ export class Legend extends Element {
let row = -1;
let top = -lineHeight;
me.legendItems.forEach((legendItem, i) => {
const itemWidth = boxWidth + (fontSize / 2) + ctx.measureText(legendItem.text).width;
const pointStyle = legendItem?.pointStyle;
Copy link
Member

Choose a reason for hiding this comment

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

We are at es2018, so no optional chaining. Althought it looks like all the supported browsers already support optional chaining, we should wait for a major version before changing the env requirements.

andyrichardson reacted with thumbs up emoji
@@ -170,7 +170,9 @@ export class Legend extends Element {
let row = -1;
let top = -lineHeight;
me.legendItems.forEach((legendItem, i) => {
const itemWidth = boxWidth + (fontSize / 2) + ctx.measureText(legendItem.text).width;
const pointStyle = legendItem?.pointStyle;
const pointWidth = pointStyle?.offsetWidth || pointStyle?.width || boxWidth;
Copy link
Member

Choose a reason for hiding this comment

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

I think we should avoid DOM manipulation.

@andyrichardson
Copy link
Author

@kurkle@etimberg a massive thanks for taking a look at this! Here's a quick braindump of what I think we need to solve this problem.

Right now I don't think we have a standard way of saying

The legend box/point/graphic will beX wide andY tall

We only have this consideration for theboxWidth/boxHeight and that can only be declared on a for all legend items.

It would be dope if we could have a standardized way of declaring the icon and optionally sizing of a label.

Here's an example

// Box{text:"My label",icon:{type:"square",// example optional attrswidth:30,borderColor:'red',// ...}}// Point{text:"My label",icon:{type:"circle",// example optional attrsradius:30,// ...}}// Image{text:"My label",icon:{type:someImageElement,// example optional attrswidth:30,height:10,// ...}}// Canvas{text:"My label",icon:{type:someCanvasElement,// example optional attrswidth:30,height:10,// ...}}

@andyrichardson
Copy link
Author

Seconding this - I think it would be dope if we could resolve the width/height for all legend icons, not just boxes

constpaddingKeys=['left','right','top','bottom']asconst;/** Normalize padding argument to object. */constnormalizePadding=(arg:undefined|number|Record<typeofpaddingKeys[number],number|undefined>)=>{if(typeofarg==='undefined'){returnpaddingKeys.reduce((p,key)=>({ ...p,[key]:0}),{});}if(typeofarg==='number'){returnpaddingKeys.reduce((p,key)=>({ ...p,[key]:arg}),{});}returnpaddingKeys.reduce((p,key)=>({    ...p,[key]:arg[key]||0}),{})};/** Get total area reserved for legend icon */constgetLegendIconDimensions=(i)=>{constpadding=normalizePadding(i.padding);constpaddingX=padding.left+padding.right;constpaddingY=padding.top+padding.bottom;if(i.type==="square"){return{width:legendIcon.width+paddingX,height:legendIcon.height+paddingY.}}if(i.type==="circle"){constdiameter=i.radius*2;return{width:diameter+paddingX,height:diameter+paddingY,}}// HTML element with explicit height & widthif(i.typeinstanceofElement&&i.height&&i.width){return{width:i.width+paddingX,height:i.height+paddingY,}}// HTML element with unknown height and/or widthif(i.typeinstanceofElement){constelem=document.createElement('div');elem.style.visibility="hidden";elem.appendChild(i.type)// Temporarily render element (hidden) to get dimensionsdocument.body.appendChild(elem)constwidth=i.type.offsetWidth;constheight=i.type.offsetHeight;document.body.removeChild(elem)return{ width, height}}// ...}

@andyrichardson
Copy link
Author

@kurkle@etimberg have you had a chance to see the discussion above? Would love to move this forward

@kurkle
Copy link
Member

/** Normalize padding argument to object. */

/**
* Converts the given value into a padding object with pre-computed width/height.
*@param {number|object} value - If a number, set the value to all TRBL component,
* else, if an object, use defined properties and sets undefined ones to 0.
* x / y are shorthands for same value for left/right and top/bottom.
*@returns {object} The padding values (top, right, bottom, left, width, height)
*@since 2.7.0
*/
exportfunctiontoPadding(value){
constobj=toTRBL(value);
obj.width=obj.left+obj.right;
obj.height=obj.top+obj.bottom;
returnobj;
}

As the plugin is due to be refactored (#9342) for v4, I'd keep the changes minimal at this point.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@etimbergetimbergetimberg left review comments

@kurklekurklekurkle left review comments

At least 1 approving review is required to merge this pull request.

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

[FEATURE] Apply box width when using point style in legend labels
3 participants
@andyrichardson@kurkle@etimberg

[8]ページ先頭

©2009-2025 Movatter.jp