Skip to content

Commit

Permalink
Ensure that controllers derived from the bar controller work correct …
Browse files Browse the repository at this point in the history
…in stacked charts (#9587)

* change parameter of functions
* argument and parameter change in DatasetController.js
* changing variable name to proper convention
* Update controller.bar.js
  • Loading branch information
shubham242k committed Sep 4, 2021
1 parent 50ad163 commit 4af9851
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
15 changes: 8 additions & 7 deletions src/controllers/controller.bar.js
Expand Up @@ -4,13 +4,13 @@ import {
valueOrDefault, resolveObjectKey, sign, defined
} from '../helpers';

function getAllScaleValues(scale) {
function getAllScaleValues(scale, type) {
if (!scale._cache.$bar) {
const metas = scale.getMatchingVisibleMetas('bar');
const visibleMetas = scale.getMatchingVisibleMetas(type);
let values = [];

for (let i = 0, ilen = metas.length; i < ilen; i++) {
values = values.concat(metas[i].controller.getAllParsedValues(scale));
for (let i = 0, ilen = visibleMetas.length; i < ilen; i++) {
values = values.concat(visibleMetas[i].controller.getAllParsedValues(scale));
}
scale._cache.$bar = _arrayUnique(values.sort((a, b) => a - b));
}
Expand All @@ -21,8 +21,9 @@ function getAllScaleValues(scale) {
* Computes the "optimal" sample size to maintain bars equally sized while preventing overlap.
* @private
*/
function computeMinSampleSize(scale) {
const values = getAllScaleValues(scale);
function computeMinSampleSize(meta) {
const scale = meta.iScale;
const values = getAllScaleValues(scale, meta.type);
let min = scale._length;
let i, ilen, curr, prev;
const updateMinAndPrev = () => {
Expand Down Expand Up @@ -479,7 +480,7 @@ export default class BarController extends DatasetController {
}

const barThickness = opts.barThickness;
const min = barThickness || computeMinSampleSize(iScale);
const min = barThickness || computeMinSampleSize(meta);

return {
min,
Expand Down
8 changes: 4 additions & 4 deletions src/core/core.datasetController.js
Expand Up @@ -128,8 +128,8 @@ function getOrCreateStack(stacks, stackKey, indexValue) {
return subStack[indexValue] || (subStack[indexValue] = {});
}

function getLastIndexInStack(stack, vScale, positive) {
for (const meta of vScale.getMatchingVisibleMetas('bar').reverse()) {
function getLastIndexInStack(stack, vScale, positive, type) {
for (const meta of vScale.getMatchingVisibleMetas(type).reverse()) {
const value = stack[meta.index];
if ((positive && value > 0) || (!positive && value < 0)) {
return meta.index;
Expand All @@ -156,8 +156,8 @@ function updateStacks(controller, parsed) {
stack = itemStacks[vAxis] = getOrCreateStack(stacks, key, index);
stack[datasetIndex] = value;

stack._top = getLastIndexInStack(stack, vScale, true);
stack._bottom = getLastIndexInStack(stack, vScale, false);
stack._top = getLastIndexInStack(stack, vScale, true, meta.type);
stack._bottom = getLastIndexInStack(stack, vScale, false, meta.type);
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/specs/core.datasetController.tests.js
Expand Up @@ -829,7 +829,7 @@ describe('Chart.DatasetController', function() {
});

var meta = chart.getDatasetMeta(0);
expect(meta._parsed[0]._stacks).toEqual(jasmine.objectContaining({y: {0: 10, 1: 20, _top: null, _bottom: null}}));
expect(meta._parsed[0]._stacks).toEqual(jasmine.objectContaining({y: {0: 10, 1: 20, _top: 1, _bottom: null}}));
});

describe('resolveDataElementOptions', function() {
Expand Down

0 comments on commit 4af9851

Please sign in to comment.