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

Ensure that controllers derived from the bar controller work correct in stacked charts #9587

Merged
merged 4 commits into from Sep 4, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 9 additions & 7 deletions src/controllers/controller.bar.js
Expand Up @@ -4,13 +4,14 @@ import {
valueOrDefault, resolveObjectKey, sign, defined
} from '../helpers';

function getAllScaleValues(scale) {
function getAllScaleValues(meta) {
let scale = meta.iScale;
if (!scale._cache.$bar) {
const metas = scale.getMatchingVisibleMetas('bar');
const visibleMetas = scale.getMatchingVisibleMetas(meta._type);
shubham242k marked this conversation as resolved.
Show resolved Hide resolved
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 +22,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) {
shubham242k marked this conversation as resolved.
Show resolved Hide resolved
const scale = meta.iScale;
const values = getAllScaleValues(meta);
let min = scale._length;
let i, ilen, curr, prev;
const updateMinAndPrev = () => {
Expand Down Expand Up @@ -479,7 +481,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