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

Allow specifying the time axis via t attribute #4533

Merged
merged 1 commit into from Jul 22, 2017

Conversation

benmccann
Copy link
Contributor

For time series charts it may make more sense to specify the horizontal axis using the variable t

This change will make it much easier to use the time scale with the financial chart, which takes in the data points { t, o, h, l, c }

* Allow x-axis to be accessed via x or t attribute
*/
getRightValue: function(rawValue) {
if (typeof(rawValue) === 'object') {
Copy link
Member

Choose a reason for hiding this comment

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

helpers.isObject(value) seems a better choice since typeof null === 'object'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

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 had to revert this change. See #4538

*/
getRightValue: function(rawValue) {
if (typeof(rawValue) === 'object') {
if ((rawValue instanceof Date) || rawValue.isValid) {
Copy link
Member

Choose a reason for hiding this comment

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

I guess .isValid verifies if rawValue is a moment object? if so, can we explicitly check for it (rawValue instance moment) instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm. We do this same check in core.scale.js. I'm not sure I want to change it to rawValue instance moment because then we'd be introducing a dependency on moment in core

Copy link
Member

Choose a reason for hiding this comment

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

I agree that core.scale.js should not depend of moment, but I realize that we should remove these checks from getRightValue in core.scale.js and keep only: object ? horizontal ? value.x : value.y. There is no reason to have them in the base class, it's time scale specific and this new override is the way to go.

Since there is already a dependency of moment in scale.time.js, I prefer an explicit check which is more robust and readable (I'm also changing it in parseTime).

Copy link
Member

Choose a reason for hiding this comment

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

And to be correct, it should be something like that in core.scale.js :

var me = this;

// ...

if (helpers.isObject(rawValue)) {
    if (me.isHorizontal()) {
        if (rawValue.x !== undefined) {
            return me.getRightValue(rawValue.x);
        }
    } else if (rawValue.y !== undefined) {
        return me.getRightValue(rawValue.y);
    }
}

// ...

return rawValue;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call on moving the date handling from core to time. I've done that. I didn't make the other changes. partially because helpers.isObject seems bugged. #4538

Copy link
Member

Choose a reason for hiding this comment

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

helpers.isObject doesn't handle Date as an object. I'm still thinking that my previous comment is valid: only handle defined object.x and object.y else return rawValue. If rawValue is a date, then it works as before, no need for a special case. Also, it should not call me.getRightValue if rawValue doesn't define x (or y) properties.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

if ((rawValue instanceof Date) || rawValue.isValid) {
return rawValue;
}
return this.getRightValue(this.isHorizontal() ? (!helpers.isNullOrUndef(rawValue.t) ? rawValue.t : rawValue.x) : rawValue.y);
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 fallback to x/y only if t === undefined. Also t should be considered for vertical scale:

this.getRightValue(helpers.valueOrDefault(rawValue.t, this.isHorizontal() ? rawValue.x : rawValue.y));

if ((rawValue instanceof Date) || rawValue.isValid) {
return rawValue;
}
return this.getRightValue(this.isHorizontal() ? (!helpers.isNullOrUndef(rawValue.t) ? rawValue.t : rawValue.x) : rawValue.y);
Copy link
Member

Choose a reason for hiding this comment

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

Can we also fallback to the base implementation for x/y:

// ...
if (rawValue.t !== undefined) {
    return this.getRightValue(rawValue.t);
}
// ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@simonbrunel simonbrunel merged commit 48d76b2 into chartjs:master Jul 22, 2017
@benmccann benmccann deleted the t-axis branch August 1, 2017 04:05
yofreke pushed a commit to yofreke/Chart.js that referenced this pull request Dec 30, 2017
For time series charts it may make more sense to specify the horizontal axis using the variable `t`. This change will make it much easier to use the time scale with the financial chart, which takes in the data points `{t, o, h, l, c}`.
exwm pushed a commit to exwm/Chart.js that referenced this pull request Apr 30, 2021
For time series charts it may make more sense to specify the horizontal axis using the variable `t`. This change will make it much easier to use the time scale with the financial chart, which takes in the data points `{t, o, h, l, c}`.
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

3 participants