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 arbitrary rotation of datapoints #5319

Merged
merged 9 commits into from Jul 7, 2018
Merged

Enable arbitrary rotation of datapoints #5319

merged 9 commits into from Jul 7, 2018

Conversation

joel-hamilton
Copy link
Contributor

@joel-hamilton joel-hamilton commented Mar 6, 2018

Thought I'd take a stab at #5213, it seems like a really useful feature. I added dataset.pointRotation, and point.pointRotation options that accept positive or negative angle (in degrees). The canvas is translated to (0,0) and then rotated before drawing the point, so x & y should now be zero for the tests.

Here is a link to a quick example

I'm new to the codebase so I might have missed something. Happy for any input or suggestions!

@joel-hamilton
Copy link
Contributor Author

CodeClimate is chirping me about similar code blocks. I was trying to follow the existing conventions, but we may have reached a point where it's getting too repetitive. It might be worth refactoring all the getPointXxxxx(point, index) functions to a single getPointValue(point, index, propertyName) function. I can do that in this PR if desired.

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.

@joelhamilton5 this is looking good. Just the one comment in the line controller code

pointRotation = custom.pointRotation;
} else if (!isNaN(dataset.pointRotation) || helpers.isArray(dataset.pointRotation)) {
pointRotation = helpers.valueAtIndexOrDefault(dataset.pointRotation, index, pointRotation);
} else if (!isNaN(dataset.pointRotation)) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this case will ever be hit. If !isNaN(dataset.pointRotation) is true, the previous else if will run.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, right you are! Fixed.

@benmccann
Copy link
Contributor

Looks good to me. Do you have any thoughts on this one @simonbrunel?

etimberg
etimberg previously approved these changes Apr 5, 2018
@benmccann
Copy link
Contributor

@joelhamilton5 could you rebase this? thanks!

Copy link
Member

@simonbrunel simonbrunel left a comment

Choose a reason for hiding this comment

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

Looks good, I would rename the element option for elements.point.rotation, pointRotation being redundant in this case (at some point we should deprecate element.point.pointStyle for element.point.style).

@@ -148,6 +148,19 @@ module.exports = function(Chart) {
return borderWidth;
},

getPointRotation: function(point, index) {
var pointRotation = this.chart.options.elements.point.pointRotation;
Copy link
Member

Choose a reason for hiding this comment

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

var rotation = this.chart.options.elements.point.rotation;

Copy link
Member

Choose a reason for hiding this comment

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

This also needs to be added to the element documentation as rotation.

@@ -106,6 +106,7 @@ module.exports = function(Chart) {
borderColor: custom.borderColor ? custom.borderColor : helpers.valueAtIndexOrDefault(dataset.pointBorderColor, index, pointElementOptions.borderColor),
borderWidth: custom.borderWidth ? custom.borderWidth : helpers.valueAtIndexOrDefault(dataset.pointBorderWidth, index, pointElementOptions.borderWidth),
pointStyle: custom.pointStyle ? custom.pointStyle : helpers.valueAtIndexOrDefault(dataset.pointStyle, index, pointElementOptions.pointStyle),
pointRotation: custom.pointRotation ? custom.pointRotation : helpers.valueAtIndexOrDefault(dataset.pointRotation, index, pointElementOptions.pointRotation),
Copy link
Member

Choose a reason for hiding this comment

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

rotation: custom.pointRotation ? ...

@@ -59,6 +60,12 @@ var exports = module.exports = {
return;
}

ctx.save();
ctx.translate(x, y);
ctx.rotate(rotation * Math.PI / 180);
Copy link
Member

Choose a reason for hiding this comment

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

I don't see how we can do differently right now but I'm a bit worry of these extra save / translate / rotate / restore because drawing point is already pretty slow. Anyway, if we are translating we should remove the use of x & y everywhere in the switch (and remove x = 0 and y = 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.

Sounds good!

@@ -185,6 +198,7 @@ module.exports = function(Chart) {
// Appearance
radius: custom.radius || helpers.valueAtIndexOrDefault(dataset.pointRadius, index, pointOptions.radius),
pointStyle: custom.pointStyle || helpers.valueAtIndexOrDefault(dataset.pointStyle, index, pointOptions.pointStyle),
pointRotation: me.getPointRotation(point, index),
Copy link
Member

Choose a reason for hiding this comment

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

rotation: me.getPointRotation(point, index),

@@ -68,6 +68,7 @@ module.exports = Element.extend({
var model = this._model;
var ctx = this._chart.ctx;
var pointStyle = vm.pointStyle;
var rotation = vm.pointRotation;
Copy link
Member

Choose a reason for hiding this comment

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

var rotation = vm.rotation;

jcopperfield and others added 6 commits April 13, 2018 08:28
…10 (#5275)

* Bugfix: Improve polyfill function of log10 to return whole powers of 10
        as integer values, as it caused endless loop in IE11 in the tick
        creation loop.

* Compare floating-point numbers directly instead of using unnecessary division.

rotate

works

added docs

delete line
@@ -67,6 +68,7 @@ The style of each bubble can be controlled with the following properties:
| `borderColor` | bubble border color
| `borderWidth` | bubble border width (in pixels)
| `pointStyle` | bubble [shape style](../configuration/elements#point-styles)
| `rotation` | bubble rotation (in degrees)

This comment was marked as outdated.

@simonbrunel simonbrunel requested a review from etimberg May 22, 2018 20:41
@@ -154,7 +155,8 @@ module.exports = function(Chart) {
'hoverBorderWidth',
'hoverRadius',
'hitRadius',
'pointStyle'
'pointStyle',
'rotation'

This comment was marked as resolved.

@simonbrunel
Copy link
Member

@joelhamilton5 I'm deeply sorry but I'm realizing that for the bubble controller other options are not prefixed (except pointStyle) and thus I think it should be rotation. For the line controller, pointRotation is the correct name.

@etimberg @benmccann what do you think: should we keep it consistent and simple by using rotation for the bubble controller only (basically revert da23599 and fix the documentation)?

Side note: pointStyle should actually be style but that's an error introduced at the element level element.point.pointStyle (instead of element.point.style). In 3.0, we should certainly scope those options, for example: dataset.elements.point.rotation (easier to merge options).

@benmccann
Copy link
Contributor

rotation for bubble chart sounds fine to me

I filed an issue to track the cleanup for 3.0: #5572

@joel-hamilton
Copy link
Contributor Author

@simonbrunel No problem, it's all fixed up now!

Copy link
Member

@simonbrunel simonbrunel left a comment

Choose a reason for hiding this comment

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

@joelhamilton5 I think you missed a few changes, right?

@@ -67,6 +68,7 @@ The style of each bubble can be controlled with the following properties:
| `borderColor` | bubble border color
| `borderWidth` | bubble border width (in pixels)
| `pointStyle` | bubble [shape style](../configuration/elements#point-styles)
| `pointRotation` | bubble rotation (in degrees)
Copy link
Member

Choose a reason for hiding this comment

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

pointRotation -> rotation

@@ -154,7 +155,8 @@ module.exports = function(Chart) {
'hoverBorderWidth',
'hoverRadius',
'hitRadius',
'pointStyle'
'pointStyle',
'pointRotation'
Copy link
Member

Choose a reason for hiding this comment

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

pointRotation -> rotation

@simonbrunel simonbrunel merged commit 0ddd0ee into chartjs:master Jul 7, 2018
@simonbrunel
Copy link
Member

Thanks @joelhamilton5

nagix added a commit to nagix/Chart.js that referenced this pull request Nov 23, 2018
- Calculate the vertices of the shapes so that they are inscribed in the circle that has the radius of `pointRadius`
- Remove `translate()` and `rotate()` to fix the regression introduced by chartjs#5319
nagix added a commit to nagix/Chart.js that referenced this pull request Nov 25, 2018
- Calculate the vertices of the shapes so that they are inscribed in the circle that has the radius of `pointRadius`
- Remove `translate()` and `rotate()` to fix the regression introduced by chartjs#5319
- Refactor `rectRounded` for better performance
nagix added a commit to nagix/Chart.js that referenced this pull request Nov 26, 2018
- Calculate the vertices of the shapes so that they are inscribed in the circle that has the radius of `pointRadius`
- Remove `translate()` and `rotate()` to fix the regression introduced by chartjs#5319
- Refactor `rectRounded` for better performance
nagix added a commit to nagix/Chart.js that referenced this pull request Nov 26, 2018
- Calculate the vertices of the shapes so that they are inscribed in the circle that has the radius of `pointRadius`
- Remove `translate()` and `rotate()` to fix the regression introduced by chartjs#5319
- Refactor `rectRounded` for better performance
nagix added a commit to nagix/Chart.js that referenced this pull request Nov 27, 2018
- Calculate the vertices of the shapes so that they are inscribed in the circle that has the radius of `pointRadius`
- Remove `translate()` and `rotate()` to fix the regression introduced by chartjs#5319
- Refactor `rectRounded` for better performance
simonbrunel pushed a commit that referenced this pull request Nov 28, 2018
- Calculate the vertices of the shapes so that they are inscribed in the circle that has the radius of `pointRadius`
- Remove `translate()` and `rotate()` to fix the regression introduced by #5319
- Refactor `rectRounded` for better performance
exwm pushed a commit to exwm/Chart.js that referenced this pull request Apr 30, 2021
exwm pushed a commit to exwm/Chart.js that referenced this pull request Apr 30, 2021
…tjs#5858)

- Calculate the vertices of the shapes so that they are inscribed in the circle that has the radius of `pointRadius`
- Remove `translate()` and `rotate()` to fix the regression introduced by chartjs#5319
- Refactor `rectRounded` for better performance
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