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

Enable line annotation using two scales #305

Merged
merged 8 commits into from Jan 13, 2021
Merged

Conversation

kurkle
Copy link
Member

@kurkle kurkle commented Jan 12, 2021

Changes:

  • position: 'start', 'end' or 'center'
  • alternative xScaleID, yScaleID, xMin, yMin, xMax, yMax options for line annotation
  • label hit testing by rotating the point (similar to datalabels plugin)
  • simpler label position adjustment by calculating the parameter t for pointInLine

@kurkle

This comment has been minimized.

@stockiNail
Copy link
Collaborator

@kurkle thank you very much!
I'm gonna have a look tomorrow.

@stockiNail

This comment has been minimized.

@kurkle

This comment has been minimized.

@stockiNail
Copy link
Collaborator

same with this pr: (note the text direction, that might be the next feedback :))

I have already done :D. See my previous note in the review close to the code.

@stockiNail
Copy link
Collaborator

In this case, removing the clipping would give a good result, but it does not help if there is no room outside the chart area.
I'm wondering if it would be better to consider this when setting the scale ranges?

Pr #282 solved this issue recalculating the label position in order to stay inside the chart area.

I know that it's not so simple (that's why I added chart reference into element in that PR).

Removing the cliparea could be configurable in order to address the issue #258 as well, something like that:

label: {
	clipEnabled: false, // <-- defaults to true
	position: 'start',
	rotation: -45,
	backgroundColor: 'red',
	content: 'This is a long test label',
	enabled: true
},

In this way you can delegate the user to have enough space for the label.

To change the scales limits (as is done now for the values) could be a solution but this could change the chart going to a picture that probably you don't like with additional space in chart area that you want to avoid.
Honestly, I don't like also the current implementation which is changing the min/max of scales when the values are out of scale, therefore we can maintain as is.
If this will be the solution, we must calculate the rotation and then the necessary space for the label before the element drawing is invoked, I guess.

@stockiNail
Copy link
Collaborator

Other feedback (not so important). All samples are still using the previous position option values (left, right, top and bottom).
If it can help, I can submit a PR to your branch, if you don't have time to do it.

@kurkle
Copy link
Member Author

kurkle commented Jan 13, 2021

Other feedback (not so important). All samples are still using the previous position option values (left, right, top and bottom).
If it can help, I can submit a PR to your branch, if you don't have time to do it.

Found only one sample using that. And it turs out it does not work correctly with this PR:

pr: (start)
image

master: (top)
image

@stockiNail
Copy link
Collaborator

@kurkle It seems that return Math.max(x, y) (in calculateTAdjust method) is wrong because is returning the maximun value instead it should return x (if line is horizontal) and y (if line is vertical)

@stockiNail
Copy link
Collaborator

Not sure if it could be correct but the return could be

return w > h ? x : y;

if width of line if greater than height means that the line is horizontal

@kurkle

This comment has been minimized.

@stockiNail

This comment has been minimized.

@kurkle
Copy link
Member Author

kurkle commented Jan 13, 2021

Ok, another try.

Adjust the label position in 2 phases, first along the line, up to 25% from the desired end, then with slightly modified current method.

image

image

@kurkle
Copy link
Member Author

kurkle commented Jan 13, 2021

Filename Size Change
dist/chartjs-plugin-annotation.esm.js 6.58 kB +9 B (0%)
dist/chartjs-plugin-annotation.js 6.7 kB +13 B (0%)
dist/chartjs-plugin-annotation.min.js 4.47 kB -152 B (-3%)

Copy link
Member

@etimberg etimberg left a comment

Choose a reason for hiding this comment

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

Didn't notice anything wrong. is the change to start, center, end considered breaking?

@stockiNail
Copy link
Collaborator

@etimberg in my opinion, yes. You don't have errors because if the position is not start or end it will use center, without any issue.
Nevertheless who is using now left, right, top or bottom will have the label in the center, therefore I think it should be considered a breaking change.
Am I wrong?

@kurkle
Copy link
Member Author

kurkle commented Jan 13, 2021

@etimberg thats the reason I labeled this breaking.

@stockiNail
Copy link
Collaborator

@kurkle apologize for delay of my feedback but I had other stuff to do it.
But finally I had time to test it and it sounds working correctly!
Thank you very much!

etimberg
etimberg previously approved these changes Jan 13, 2021
@kurkle kurkle linked an issue Jan 13, 2021 that may be closed by this pull request
@kurkle kurkle merged commit c0ac2ab into chartjs:master Jan 13, 2021
@kurkle kurkle deleted the line-2-scales branch January 14, 2021 19:55
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.

Support for finite lines (and diagonal lines?)
3 participants