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

Category: parse to valid index values only #8697

Merged
merged 1 commit into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/scales/scale.category.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Scale from '../core/core.scale';
import {isNullOrUndef, valueOrDefault} from '../helpers';
import {isNullOrUndef, valueOrDefault, _limitValue} from '../helpers';

const addIfString = (labels, raw, index) => typeof raw === 'string'
? labels.push(raw) - 1
Expand All @@ -14,6 +14,8 @@ function findOrAddLabel(labels, raw, index) {
return first !== last ? index : first;
}

const validIndex = (index, max) => index === null ? null : _limitValue(Math.round(index), 0, max);

export default class CategoryScale extends Scale {

constructor(cfg) {
Expand All @@ -29,8 +31,9 @@ export default class CategoryScale extends Scale {
return null;
}
const labels = this.getLabels();
return isFinite(index) && labels[index] === raw
? index : findOrAddLabel(labels, raw, valueOrDefault(index, raw));
index = isFinite(index) && labels[index] === raw ? index
: findOrAddLabel(labels, raw, valueOrDefault(index, raw));
return validIndex(index, labels.length - 1);
}

determineDataLimits() {
Expand Down
37 changes: 36 additions & 1 deletion test/specs/scale.category.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ describe('Category scale tests', function() {
});
});


it('Should generate ticks from the data xLabels', function() {
var labels = ['tick1', 'tick2', 'tick3', 'tick4', 'tick5'];
var chart = window.acquireChart({
Expand Down Expand Up @@ -114,6 +113,42 @@ describe('Category scale tests', function() {

});

it('should parse only to a valid index', function() {
var chart = window.acquireChart({
type: 'line',
data: {
datasets: [{
xAxisID: 'x',
yAxisID: 'y',
data: [10, 5, 0, 25, 78]
}],
labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick5']
},
options: {
scales: {
x: {
type: 'category',
position: 'bottom'
},
y: {
type: 'linear'
}
}
}
});

var scale = chart.scales.x;

expect(scale.parse(-10)).toEqual(0);
expect(scale.parse(-0.1)).toEqual(0);
expect(scale.parse(4.1)).toEqual(4);
expect(scale.parse(5)).toEqual(4);
expect(scale.parse(1)).toEqual(1);
expect(scale.parse(1.4)).toEqual(1);
expect(scale.parse(1.5)).toEqual(2);
expect(scale.parse('tick2')).toEqual(1);
});

it('should get the correct label for the index', function() {
var chart = window.acquireChart({
type: 'line',
Expand Down