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

#10836 add getLabelItems public method #10837

Closed

Conversation

cmcnulty
Copy link
Contributor

@cmcnulty cmcnulty commented Oct 27, 2022

Proposed solution to #10836. This makes the dimensions of the axis labels public by way of a scale method: getLabelItems() - It solves the problem of customizing labels or creating interactions from them - For a use-case see the following demo, which is based off of this PR branch:

https://codepen.io/cmcnulty/pen/JjZPxmJ

Additionally, there have been several discussions here (and also some in stackoverflow) that attempt to use the private _labelItems in order to solve related problems:

https://github.com/chartjs/Chart.js/discussions?discussions_q=_labelItems

src/core/core.scale.js Outdated Show resolved Hide resolved
@cmcnulty cmcnulty force-pushed the feature-10836-expose-label-items branch from 9b618fb to c1b50a2 Compare October 28, 2022 16:36
@cmcnulty cmcnulty changed the title init checkin, work remains on radialLinear #10836 add getLabelItems public method Oct 28, 2022
src/core/core.scale.js Outdated Show resolved Hide resolved
src/core/core.scale.js Outdated Show resolved Hide resolved
src/core/core.scale.js Outdated Show resolved Hide resolved
@cmcnulty cmcnulty force-pushed the feature-10836-expose-label-items branch 2 times, most recently from 43c1160 to eaa0f75 Compare November 2, 2022 16:08
@cmcnulty cmcnulty force-pushed the feature-10836-expose-label-items branch from 38c329a to 8499920 Compare November 2, 2022 16:34
@cmcnulty cmcnulty requested review from LeeLenaleee and dangreen and removed request for LeeLenaleee and dangreen November 2, 2022 18:16
@cmcnulty cmcnulty requested review from dangreen and removed request for LeeLenaleee November 3, 2022 21:45
@cmcnulty
Copy link
Contributor Author

@etimberg can you take a look at this? It evolved out of #10759 - now that that bug is fixed, there's a lot more that we could do with the axis labels if we had API access to the label dimensions.

@etimberg etimberg requested a review from kurkle November 11, 2022 21:36
@etimberg
Copy link
Member

@kurkle @LeeLenaleee @dangreen any opinions on what we do with this? I think I'd want to put it in 4.1.0 at a minimum since 4.0.0 is so close

@kurkle kurkle added this to the Version 4.1 milestone Nov 12, 2022
Copy link
Member

@kurkle kurkle left a comment

Choose a reason for hiding this comment

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

No big objections from me.

textBaseline,
translation: [x, y],
backdrop,
renderTextOptions: {
Copy link
Member

Choose a reason for hiding this comment

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

The seems badly named as its referring to some method used to render the text and thus exposes (and freezes) internal logic.
I think plain options would work. Also the textOffset should go in there too.

IMO we should make the ticks and labels to be (animated) elements in future releases, that is why I suggest plain options. We could just return the elements as labelItems then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't disagree - obviously the renderTextOptions name came about because it stores a RenderTextOpts typed object. Why textOffset and, for that matter, font are passed separately into renderText() rather than in the options collection is also a mystery that I didn't dive into. I certainly agree that it seems to make sense to fold font and textOffset into the RenderTextOpts object, which simplifies the renderText signature.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kurkle, renamed to just options, didn't make any changes to textOffset because I didn't want this change to break any implementations that relied on renderText(). However, now lint tests are failing (passing locally) - seems like perhaps an intermittent issue? Can checks be retriggered?

@LeeLenaleee LeeLenaleee modified the milestones: Version 4.1, Version 4.2 Nov 14, 2022
@kurkle
Copy link
Member

kurkle commented Nov 16, 2022

I think it needs something like import { CanvasFontSpec } from '../src/helpers/helpers.options';

@cmcnulty
Copy link
Contributor Author

cmcnulty commented Nov 16, 2022

I think it needs something like import { CanvasFontSpec } from '../src/helpers/helpers.options';

I'll try it - weird that this passed a week or so ago without a problem:

import { RenderTextOpts } from './helpers/helpers.canvas';
import { CanvasFontSpec } from './helpers';

@cmcnulty
Copy link
Contributor Author

I think it needs something like import { CanvasFontSpec } from '../src/helpers/helpers.options';

local linter didn't like that - I've reverted to the known lint-passing from this run:
https://github.com/chartjs/Chart.js/pull/10837/checks?check_run_id=9254407423
using this code:
8499920

@cmcnulty
Copy link
Contributor Author

I don't know - I'm going around in circles. There's no reason commit 58cb9e6 should have broken the types imports. There seems to just be something finicky going on with the linter and I can't reproduce it locally.

@@ -359,6 +359,14 @@ export default class Scale extends Element {
return this.options.labels || (this.isHorizontal() ? data.xLabels : data.yLabels) || data.labels || [];
}

/**
* @return {import('../types').LabelItem[]}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
* @return {import('../types').LabelItem[]}
* @return {import('../types.js').LabelItem[]}

#10879

Comment on lines +2 to +4
import { Color } from '../color';
import { ChartArea, RoundedRect } from '../geometric';
import { CanvasFontSpec } from '../../src/helpers/helpers.options';
Copy link
Collaborator

Choose a reason for hiding this comment

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

please add js extension

Comment on lines +1 to +21
import { DeepPartial, DistributiveArray, UnionToIntersection } from './utils';

import { TimeUnit } from '../src/core/core.adapters';
import PointElement from '../src/elements/element.point';
import { EasingFunction } from '../src/helpers/helpers.easing';
import { AnimationEvent } from './animation';
import { AnyObject, EmptyObject } from './basic';
import { Color } from './color';
import Element from '../src/core/core.element';
import { ChartArea, Padding, Point } from './geometric';
import { LayoutItem, LayoutPosition } from './layout';
import { RenderTextOpts } from './helpers/helpers.canvas';
import { CanvasFontSpec } from '../src/helpers/helpers.options';

export { EasingFunction } from '../src/helpers/helpers.easing';
export { default as ArcElement, ArcProps } from '../src/elements/element.arc';
export { default as PointElement, PointProps } from '../src/elements/element.point';
export { Animation, Animations, Animator, AnimationEvent } from './animation';
export { Color } from './color';
export { ChartArea, Point } from './geometric';
export { LayoutItem, LayoutPosition } from './layout';
Copy link
Collaborator

Choose a reason for hiding this comment

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

the same

@cmcnulty cmcnulty deleted the feature-10836-expose-label-items branch November 3, 2023 20:29
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.

None yet

5 participants