Skip to content

Commit

Permalink
Handle animating stacked bars from null values (#8872)
Browse files Browse the repository at this point in the history
* Handle animating stacked bars from null values

* Skipped bars / points should be in the reset state
  • Loading branch information
etimberg committed Apr 10, 2021
1 parent 7fab66c commit a84347b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/controllers/controller.bar.js
Expand Up @@ -266,7 +266,8 @@ export default class BarController extends DatasetController {
me.updateSharedOptions(sharedOptions, mode, firstOpts);

for (let i = start; i < start + count; i++) {
const vpixels = reset ? {base, head: base} : me._calculateBarValuePixels(i);
const parsed = me.getParsed(i);
const vpixels = reset || isNullOrUndef(parsed[vScale.axis]) ? {base, head: base} : me._calculateBarValuePixels(i);
const ipixels = me._calculateBarIndexPixels(i, ruler);

const properties = {
Expand Down
6 changes: 4 additions & 2 deletions src/controllers/controller.line.js
@@ -1,4 +1,5 @@
import DatasetController from '../core/core.datasetController';
import {isNullOrUndef} from '../helpers';
import {_limitValue, isNumber} from '../helpers/helpers.math';
import {_lookupByKey} from '../helpers/helpers.collection';

Expand Down Expand Up @@ -59,9 +60,10 @@ export default class LineController extends DatasetController {
const point = points[i];
const parsed = me.getParsed(i);
const properties = directUpdate ? point : {};
const nullData = isNullOrUndef(parsed.y);
const x = properties.x = xScale.getPixelForValue(parsed.x, i);
const y = properties.y = reset ? yScale.getBasePixel() : yScale.getPixelForValue(_stacked ? me.applyStack(yScale, parsed, _stacked) : parsed.y, i);
properties.skip = isNaN(x) || isNaN(y);
const y = properties.y = reset || nullData ? yScale.getBasePixel() : yScale.getPixelForValue(_stacked ? me.applyStack(yScale, parsed, _stacked) : parsed.y, i);
properties.skip = isNaN(x) || isNaN(y) || nullData;
properties.stop = i > 0 && (parsed.x - prevParsed.x) > maxGapLength;
properties.parsed = parsed;

Expand Down
20 changes: 20 additions & 0 deletions test/specs/controller.bar.tests.js
Expand Up @@ -30,6 +30,26 @@ describe('Chart.controllers.bar', function() {
expect(meta.controller.index).toBe(0);
});

it('should set null bars to the reset state', function() {
var chart = window.acquireChart({
type: 'bar',
data: {
datasets: [{
data: [10, null, 0, -4],
label: 'dataset1',
}],
labels: ['label1', 'label2', 'label3', 'label4']
}
});

var meta = chart.getDatasetMeta(0);
var bar = meta.data[1];
var {x, y, base} = bar.getProps(['x', 'y', 'base'], true);
expect(isNaN(x)).toBe(false);
expect(isNaN(y)).toBe(false);
expect(isNaN(base)).toBe(false);
});

it('should use the first scale IDs if the dataset does not specify them', function() {
var chart = window.acquireChart({
type: 'bar',
Expand Down
21 changes: 21 additions & 0 deletions test/specs/controller.line.tests.js
Expand Up @@ -917,4 +917,25 @@ describe('Chart.controllers.line', function() {
}
expect(createChart).not.toThrow();
});

it('should set skipped points to the reset state', function() {
var chart = window.acquireChart({
type: 'line',
data: {
datasets: [{
data: [10, null, 0, -4],
label: 'dataset1',
pointBorderWidth: [1, 2, 3, 4]
}],
labels: ['label1', 'label2', 'label3', 'label4']
}
});

var meta = chart.getDatasetMeta(0);
var point = meta.data[1];
var {x, y} = point.getProps(['x', 'y'], true);
expect(point.skip).toBe(true);
expect(isNaN(x)).toBe(false);
expect(isNaN(y)).toBe(false);
});
});

0 comments on commit a84347b

Please sign in to comment.