]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Ensure that controllers derived from the bar controller work correct in stacked chart...
authorShubham Aggarwal <67068685+shubham242k@users.noreply.github.com>
Sat, 4 Sep 2021 17:43:34 +0000 (23:13 +0530)
committerGitHub <noreply@github.com>
Sat, 4 Sep 2021 17:43:34 +0000 (13:43 -0400)
* change parameter of functions
* argument and parameter change in DatasetController.js
* changing variable name to proper convention
* Update controller.bar.js

src/controllers/controller.bar.js
src/core/core.datasetController.js
test/specs/core.datasetController.tests.js

index e97f174f4a8923177c53787f1609a85509452b2c..a146310672e72d0993a6efcca41edaffbae9ff52 100644 (file)
@@ -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));
   }
@@ -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 = () => {
@@ -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,
index 812ee96bc892755a61297ad7eeb657b288c00be4..05919714bff2c8aa0a6d64a5981d46a2404c9cc6 100644 (file)
@@ -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;
@@ -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);
   }
 }
 
index 372655c0ac385c5b72192f38d7251ab65b822565..3f08d209e26a79aed32791e117261debb65f1969 100644 (file)
@@ -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() {