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

feature: legend navigation #11825

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

adrianbrs
Copy link

@adrianbrs adrianbrs commented Jul 1, 2024

I needed this feature to manage many legends on the same chart, but I couldn't find anything besides the HTML legends. Therefore, I decided to implement this feature, and I hope it can be useful for the Chart.js core, as I believe it is essential.

An example of the navigation working, and also the documentation created, can be found here: https://cerbaro.dev/Chart.js/samples/legend/navigation.html

image

Furthermore, during the process I encountered some issues and ended up solving them.

#11280 - Horizontal padding of the legend title does not work
#11824 - Horizontal legend multiline labels overlaps

I am willing to collaborate with any changes you deem necessary.

Issues related to legend pagination: #10893, #7100, #6545, #3761, #5561, #11425 (There's probably others as well).

There is an open issue for a refactor in the legend plugin (#9342), but I don't know when version 5 will be released, perhaps it would be interesting to add this navigation to the current version anyway.

I would be happy to contribute to the new version if needed.

@@ -56,6 +57,26 @@ export class Legend extends Element {
*/
this._hoveredItem = null;

this.navigation = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wouldn't it be more logical if this was undefined unless someone has its feature enabled?

@@ -117,7 +139,272 @@ export class Legend extends Element {
legendItems.reverse();
}

this.legendItems = legendItems;
this.navigation.legendItems = this.legendItems = legendItems;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Personally I think doing this in 2 steps is a lot more readable and less error prone:

Suggested change
this.navigation.legendItems = this.legendItems = legendItems;
this.legendItems = legendItems;
this.navigation.legendItems = legendItems;

Comment on lines +188 to +190
this.navigation._width = this.navigation._height = 0;
this.navigation._maxWidth = this.navigation._maxHeight = 0;
this.navigation.itemWidth = this.navigation.itemHeight = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

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

idem

@@ -0,0 +1,56 @@
{
Copy link
Member

Choose a reason for hiding this comment

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

In this test the arrows are vertical, but in the previous test legend-navigation-horizontal-auto-visible they are horizontal. Is that expected?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants