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 bar chart via xy data points #4565

Merged
merged 1 commit into from
Jul 29, 2017

Conversation

benmccann
Copy link
Contributor

Fixes #2892

@benmccann benmccann changed the title Allow specifying bar chart via data points Allow specifying bar chart via xy data points Jul 26, 2017
@@ -86,6 +86,10 @@ defaults._set('horizontalBar', {
}
});

function getValue(scale, dataPoint) {
return typeof dataPoint === 'string' ? Number(dataPoint) : scale.getRightValue(dataPoint);
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if this logic should be in scale.getRightValue or not

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 tried that and it seemed to break the tests. I also think it's rather silly that people would pass in a number as a string and would just as much consider deprecating support for that in 3.0 so wasn't sure I wanted to encourage to be done for even more scales

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 with @etimberg, would be better in getRightValue() but only for the linearbase impl. (as we did with the time scale). I don't think it's silly because +'1.2', Number('1.2') or parseFloat('1.2') return valid numbers. Linear scales expect numbers from the data, so as long as the values represent valid numbers, I'm not sure why we would want to make it less flexible (we don't know from where the user gets the data, could be from an API that returns numbers as string).

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'm not sure how to do that. If I add the following to scale.linearbase.js then all the tests start failing:

	getRightValue: function() {
		return Chart.Scale.prototype.getRightValue.call(this);
	},

Copy link
Member

Choose a reason for hiding this comment

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

getRightValue: function(value) {
    if (typeof value === 'string') {
        return +value;
    }
    return Chart.Scale.prototype.getRightValue.call(this, value);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thank you! i feel dumb now. too early after waking up I guess...

I've updated the PR with this change


helpers.canvas.clipArea(chart.ctx, chart.chartArea);

for (; i < ilen; ++i) {
d = dataset.data[i];
if (d !== null && d !== undefined && !isNaN(d)) {
Copy link
Member

Choose a reason for hiding this comment

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

What's the reason of this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because isNaN returns true when you pass in an object

Also, I don't know if it's really the bar controller's responsibility to do such a check anyway. It shouldn't need to know the logic for whether the rectangle element can be drawn. That logic should be encapsulated in the rectangle element.

Copy link
Member

@simonbrunel simonbrunel Jul 26, 2017

Choose a reason for hiding this comment

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

Looks like a regression, null/undefined/NaN data are currently used to skip element (@etimberg?) Should be the same logic as your other change: d = scale.getRightValue(dataset.data[i]).

I doubt that's the responsability of the element to decide whether it needs to drawn itself. The controller seems a better candidate, each controller can have different behavior.

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 the current behaviour for bars is that null/undefined/NaN are supposed to just not draw anything but I'm not 100% sure on that. I know we use those values to skip drawing grid lines in the axis though

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I like scale.getRightValue much better than the old check. I've updated it to that

@koolkao
Copy link

koolkao commented Jul 1, 2018

Is this feature functional? The 'bar' chart specified by xy in my code are not plotting.

newbie to chart.js here. Really appreciate this beautiful and easy-to-use library.

@benmccann
Copy link
Contributor Author

I believe it works unless you skip data points. There is currently an improvement pending for that: #5298

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.

4 participants