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 category scales to use locally-set labels #3732

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/02-Scales.md
Expand Up @@ -118,14 +118,15 @@ var chartInstance = new Chart(ctx, {

### Category Scale

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. Labels are drawn from one of the label arrays included in the chart data, or set in the scale options. If `options.labels` is defined, this will take priority and be used. Otherwise, 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.

#### Configuration Options

The category scale has the following additional options that can be set.

Name | Type | Default | Description
--- | --- | --- | ---
labels | Array[String] | - | An array of labels to display.
ticks.min | String | - | The minimum item to display. Must be a value in the `labels` array
ticks.max | String | - | The maximum item to display. Must be a value in the `labels` array

Expand Down
5 changes: 3 additions & 2 deletions src/scales/scale.category.js
Expand Up @@ -16,7 +16,7 @@ module.exports = function(Chart) {
*/
getLabels: function() {
var data = this.chart.data;
return (this.isHorizontal() ? data.xLabels : data.yLabels) || data.labels;
return this.options.labels || (this.isHorizontal() ? data.xLabels : data.yLabels) || data.labels;
},
// Implement this so that
determineDataLimits: function() {
Expand Down Expand Up @@ -54,9 +54,10 @@ module.exports = function(Chart) {
var data = me.chart.data;
var isHorizontal = me.isHorizontal();

if (data.yLabels && !isHorizontal) {
if ((me.options.labels || data.yLabels) && !isHorizontal) {
return me.getRightValue(data.datasets[datasetIndex].data[index]);
}

return me.ticks[index - me.minIndex];
},

Expand Down
29 changes: 29 additions & 0 deletions test/scale.category.tests.js
Expand Up @@ -132,6 +132,35 @@ describe('Category scale tests', function() {
expect(scale.ticks).toEqual(mockData.yLabels);
});

it('Should generate ticks from the scale labels', function() {
var scaleID = 'myScale';

var mockData = {
datasets: [{
yAxisID: scaleID,
data: [10, 5, 0, 25, 78]
}]
};

var config = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('category'));
config.position = 'left'; // y axis
config.labels = ['tock1', 'tock2', 'tock3', 'tock4', 'tock5'];

var Constructor = Chart.scaleService.getScaleConstructor('category');
var scale = new Constructor({
ctx: {},
options: config,
chart: {
data: mockData
},
id: scaleID
});

scale.determineDataLimits();
scale.buildTicks();
expect(scale.ticks).toEqual(config.labels);
});

it ('should get the correct label for the index', function() {
var scaleID = 'myScale';

Expand Down