mirror of
https://github.com/chartjs/Chart.js.git
synced 2024-10-06 12:19:08 +02:00
Fix category scale invalid data handling (#8668)
* Fix category scale invalid data handling * Fix NaN
This commit is contained in:
parent
97136d0cbf
commit
851861e9cc
@ -1,10 +1,14 @@
|
|||||||
import Scale from '../core/core.scale';
|
import Scale from '../core/core.scale';
|
||||||
import {valueOrDefault} from '../helpers';
|
import {isNullOrUndef, valueOrDefault} from '../helpers';
|
||||||
|
|
||||||
|
const addIfString = (labels, raw, index) => typeof raw === 'string'
|
||||||
|
? labels.push(raw) - 1
|
||||||
|
: isNaN(raw) ? null : index;
|
||||||
|
|
||||||
function findOrAddLabel(labels, raw, index) {
|
function findOrAddLabel(labels, raw, index) {
|
||||||
const first = labels.indexOf(raw);
|
const first = labels.indexOf(raw);
|
||||||
if (first === -1) {
|
if (first === -1) {
|
||||||
return typeof raw === 'string' ? labels.push(raw) - 1 : index;
|
return addIfString(labels, raw, index);
|
||||||
}
|
}
|
||||||
const last = labels.lastIndexOf(raw);
|
const last = labels.lastIndexOf(raw);
|
||||||
return first !== last ? index : first;
|
return first !== last ? index : first;
|
||||||
@ -21,6 +25,9 @@ export default class CategoryScale extends Scale {
|
|||||||
}
|
}
|
||||||
|
|
||||||
parse(raw, index) {
|
parse(raw, index) {
|
||||||
|
if (isNullOrUndef(raw)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
const labels = this.getLabels();
|
const labels = this.getLabels();
|
||||||
return isFinite(index) && labels[index] === raw
|
return isFinite(index) && labels[index] === raw
|
||||||
? index : findOrAddLabel(labels, raw, valueOrDefault(index, raw));
|
? index : findOrAddLabel(labels, raw, valueOrDefault(index, raw));
|
||||||
@ -96,7 +103,7 @@ export default class CategoryScale extends Scale {
|
|||||||
value = me.parse(value);
|
value = me.parse(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return me.getPixelForDecimal((value - me._startValue) / me._valueRange);
|
return value === null ? NaN : me.getPixelForDecimal((value - me._startValue) / me._valueRange);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Must override base implementation because it calls getPixelForValue
|
// Must override base implementation because it calls getPixelForValue
|
||||||
|
41
test/fixtures/scale.category/invalid-data.js
vendored
Normal file
41
test/fixtures/scale.category/invalid-data.js
vendored
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
module.exports = {
|
||||||
|
config: {
|
||||||
|
type: 'line',
|
||||||
|
data: {
|
||||||
|
labels: ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
|
||||||
|
datasets: [{
|
||||||
|
data: [
|
||||||
|
{x: 'a', y: 1},
|
||||||
|
{x: null, y: 1},
|
||||||
|
{x: 2, y: 1},
|
||||||
|
{x: undefined, y: 1},
|
||||||
|
{x: 4, y: 1},
|
||||||
|
{x: NaN, y: 1},
|
||||||
|
{x: 6, y: 1}
|
||||||
|
],
|
||||||
|
backgroundColor: 'red',
|
||||||
|
borderColor: 'red',
|
||||||
|
borderWidth: 5
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
options: {
|
||||||
|
scales: {
|
||||||
|
y: {
|
||||||
|
display: false
|
||||||
|
},
|
||||||
|
x: {
|
||||||
|
grid: {
|
||||||
|
display: false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
options: {
|
||||||
|
spriteText: true,
|
||||||
|
canvas: {
|
||||||
|
width: 256,
|
||||||
|
height: 256
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
BIN
test/fixtures/scale.category/invalid-data.png
vendored
Normal file
BIN
test/fixtures/scale.category/invalid-data.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.9 KiB |
Loading…
Reference in New Issue
Block a user