-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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 category label definition at axis level #4506
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea of adding something to the documentation about this. One thing I will say is that having data.labels
set does not always mean that the x axis will be a category
axis. If the user, or chart defaults, set it to something else then setting data.labels
won't change that.
I also think we should add unit tests to cover this as needed.
My feeling is that the whole implicit/explicit logic should go into a more general section as it probably applies to other chart/aces types as well. I'd leave that to you as I don't understand the entire bigger picture (which settings are inherited, generate axes etc).
Will make this clear, please let me know about the first part though. I'll look into tests, too. |
In terms of the explicit/implicit stuff, maybe it could live in http://www.chartjs.org/docs/latest/configuration/#global-configuration ? @simonbrunel any thoughts or preferences on this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, just a few minor changes. I think it's a dup of #3732, could be interesting to check why the other PR hasn't been merged (there is also one more code change).
I'm fine with labels
under scale options, not sure I like the implicit/explicit terms though (maybe simply "data labels" vs "scale labels").
@@ -136,6 +136,37 @@ describe('Category scale tests', function() { | |||
expect(scale.ticks).toEqual(mockData.yLabels); | |||
}); | |||
|
|||
it('Should generate ticks from the axis labels', function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mock data and manual construction of the scale are not the most trustful way to setup unit tests. I think we should instantiate a real chart if that's possible (which should be almost always the case).
var chart = window.acquireChart({
type: 'line',
data: {
data: [10, 5, 0, 25, 78]
},
options: {
scales: {
xAxes: [{
id: 'x',
type: 'category',
labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick5']
}]
}
}
});
expect(chart.scales.x.ticks).toEqual(['tick1', 'tick2', 'tick3', 'tick4', 'tick5']);
It's even less code and more robust since you don't manually manipulate the scale to obtain the values to check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
docs/axes/cartesian/category.md
Outdated
The category scale will be familiar to those who have used v1.0. Labels are drawn from one of the label arrays included in the chart data. If only `data.labels` is defined, this will be used. If `data.xLabels` is defined and the axis is horizontal, this will be used. Similarly, if `data.yLabels` is defined and the axis is vertical, this property will be used. Using both `xLabels` and `yLabels` together can create a chart that uses strings for both the X and Y axes. | ||
The category scale will be familiar to those who have used v1.0. If global configuration is used, labels are drawn from one of the label arrays included in the chart data. If only `data.labels` is defined, this will be used. If `data.xLabels` is defined and the axis is horizontal, this will be used. Similarly, if `data.yLabels` is defined and the axis is vertical, this property will be used. Using both `xLabels` and `yLabels` together can create a chart that uses strings for both the X and Y axes. | ||
|
||
Specifying any of the settings above implicitly defines the x axis as `type: category`if not defined otherwise. For more fine-grained control of category labels it is also possible to add `labels` as part of the category axis definition. Doing so does not apply the global defaults for bar charts. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing space between type: category
and if not defined otherwise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why only "bar charts" in ... the global defaults for bar charts
docs/axes/cartesian/category.md
Outdated
@@ -1,6 +1,38 @@ | |||
# Category Cartesian Axis | |||
|
|||
The category scale will be familiar to those who have used v1.0. Labels are drawn from one of the label arrays included in the chart data. If only `data.labels` is defined, this will be used. If `data.xLabels` is defined and the axis is horizontal, this will be used. Similarly, if `data.yLabels` is defined and the axis is vertical, this property will be used. Using both `xLabels` and `yLabels` together can create a chart that uses strings for both the X and Y axes. | |||
The category scale will be familiar to those who have used v1.0. If global configuration is used, labels are drawn from one of the label arrays included in the chart data. If only `data.labels` is defined, this will be used. If `data.xLabels` is defined and the axis is horizontal, this will be used. Similarly, if `data.yLabels` is defined and the axis is vertical, this property will be used. Using both `xLabels` and `yLabels` together can create a chart that uses strings for both the X and Y axes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I would remove the first sentence: The category scale will be familiar to those who have used v1.0.
, @etimberg
Change this to match global/local
I've setup this test in line with all the other ones I've found. If to be changed then it should be throughout the board?
Removed
Removed that. To be honest the global logic is cloudy to me. "labels" alone seems sufficient to generate a bar chart without even setting type? Update I still need to check #3732 Whatever is wrong there would probably warrant another test, too. |
From #3732:
It seems this notion is still open and outside the scope of this PR (or #3732 for that). I've left the original code untouched, #3732 has a change there. Both PRs seem fair to merge imho. |
It's fine to write your new tests with |
@simonbrunel all changes have been gathered. If you can live with the test for now it's ready to merge and close the old one. |
I could also volunteer to look into converting some of the tests. However, quite a number of those are failing locally for me on OSX, especially the responsive ones? |
I would prefer to get your tests using the new way instead of the mock data before merging (mostly copy/paste code from my comment). Curious to know why the responsive tests are failing in your environment. We will be happy to review another PR with some tests upgraded :) Are you sure we don't need this check? |
I have 4 tests that fail on Chrome on OSX only. I always just assumed they were because my machine is 5 years old and so it's just too slow to render properly because the error is Regarding that extra check that Simon mentioned, the way to test if it is needed would be to create a chart with a vertical category axis that has labels specified in this new way and see if the tooltip shows the correct values. I will agree about the test using |
I've had another look into this, see https://jsfiddle.net/andig2/3c7f3zxb/5/. The current logic (before this PR) is already broken. If My suggestion would be to merge this PR, close #3732 and open another issue for inconsistent behaviour of |
I agree on that course of action since it keeps the PRs smaller and clearer. Thanks for updating the test. I've marked it as approved. |
Currently category labels can only be fined globally. This implies that
This PR fixes category labels being passed through moment.js when time axis exists (part of #4498). Fiddle https://jsfiddle.net/andig2/b2a2ddsd/ shows js console warning that is removed by this PR.
Also allows defining multiple category axis which is currently not possible.
I have only limited experience with chartjs right now so I cannot be sure of cross-effects I'm not seeing- please review carefully.