]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix automatic `min` value of `stacked` scale. (#6851)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Thu, 19 Dec 2019 19:58:23 +0000 (21:58 +0200)
committerEvert Timberg <evert.timberg+github@gmail.com>
Thu, 19 Dec 2019 19:58:23 +0000 (14:58 -0500)
src/core/core.datasetController.js
test/fixtures/controller.bar/stacking/logarithmic-strings.js [new file with mode: 0644]
test/fixtures/controller.bar/stacking/logarithmic-strings.png [new file with mode: 0644]
test/fixtures/controller.bar/stacking/logarithmic.js [new file with mode: 0644]
test/fixtures/controller.bar/stacking/logarithmic.png [new file with mode: 0644]
test/specs/controller.bar.tests.js
test/specs/scale.logarithmic.tests.js

index f8e39b482c002e99defe5c92d9ce8c5346a2ec65..7ce9c0d66aa97ba112a09be8a9d0b07d17300e2f 100644 (file)
@@ -648,6 +648,10 @@ helpers.extend(DatasetController.prototype, {
                                        keys: indices,
                                        values: parsed._stacks[scale.id]
                                };
+                               // Need to consider individual stack values for data range,
+                               // in addition to the stacked value
+                               min = Math.min(min, value);
+                               max = Math.max(max, value);
                                value = applyStack(stack, value, meta.index, true);
                        }
                        min = Math.min(min, value);
diff --git a/test/fixtures/controller.bar/stacking/logarithmic-strings.js b/test/fixtures/controller.bar/stacking/logarithmic-strings.js
new file mode 100644 (file)
index 0000000..e23a01c
--- /dev/null
@@ -0,0 +1,36 @@
+module.exports = {
+       config: {
+               type: 'bar',
+               data: {
+                       datasets: [{
+                               data: ['10', '100', '10', '100'],
+                               backgroundColor: '#ff0000'
+                       }, {
+                               data: ['100', '10', '0', '100'],
+                               backgroundColor: '#00ff00'
+                       }],
+                       labels: ['label1', 'label2', 'label3', 'label4']
+               },
+               options: {
+                       legend: false,
+                       title: false,
+                       datasets: {
+                               bar: {
+                                       barPercentage: 1,
+                               }
+                       },
+                       scales: {
+                               x: {
+                                       type: 'category',
+                                       display: false,
+                                       stacked: true,
+                               },
+                               y: {
+                                       type: 'logarithmic',
+                                       display: false,
+                                       stacked: true
+                               }
+                       }
+               }
+       }
+};
diff --git a/test/fixtures/controller.bar/stacking/logarithmic-strings.png b/test/fixtures/controller.bar/stacking/logarithmic-strings.png
new file mode 100644 (file)
index 0000000..2ce8d06
Binary files /dev/null and b/test/fixtures/controller.bar/stacking/logarithmic-strings.png differ
diff --git a/test/fixtures/controller.bar/stacking/logarithmic.js b/test/fixtures/controller.bar/stacking/logarithmic.js
new file mode 100644 (file)
index 0000000..34e0c6c
--- /dev/null
@@ -0,0 +1,36 @@
+module.exports = {
+       config: {
+               type: 'bar',
+               data: {
+                       datasets: [{
+                               data: [10, 100, 10, 100],
+                               backgroundColor: '#ff0000'
+                       }, {
+                               data: [100, 10, 0, 100],
+                               backgroundColor: '#00ff00'
+                       }],
+                       labels: ['label1', 'label2', 'label3', 'label4']
+               },
+               options: {
+                       legend: false,
+                       title: false,
+                       datasets: {
+                               bar: {
+                                       barPercentage: 1,
+                               }
+                       },
+                       scales: {
+                               x: {
+                                       type: 'category',
+                                       display: false,
+                                       stacked: true,
+                               },
+                               y: {
+                                       type: 'logarithmic',
+                                       display: false,
+                                       stacked: true
+                               }
+                       }
+               }
+       }
+};
diff --git a/test/fixtures/controller.bar/stacking/logarithmic.png b/test/fixtures/controller.bar/stacking/logarithmic.png
new file mode 100644 (file)
index 0000000..2ce8d06
Binary files /dev/null and b/test/fixtures/controller.bar/stacking/logarithmic.png differ
index 76314b4636740c0d9a588ca992c679f3e18397cf..e6d199452f5e53012e025cca57a08401f8617628 100644 (file)
@@ -1179,136 +1179,6 @@ describe('Chart.controllers.bar', function() {
                });
        });
 
-       it('should update elements when the scales are stacked and the y axis is logarithmic', function() {
-               var chart = window.acquireChart({
-                       type: 'bar',
-                       data: {
-                               datasets: [{
-                                       data: [10, 100, 10, 100],
-                                       label: 'dataset1'
-                               }, {
-                                       data: [100, 10, 0, 100],
-                                       label: 'dataset2'
-                               }],
-                               labels: ['label1', 'label2', 'label3', 'label4']
-                       },
-                       options: {
-                               legend: false,
-                               title: false,
-                               datasets: {
-                                       bar: {
-                                               barPercentage: 1,
-                                       }
-                               },
-                               scales: {
-                                       x: {
-                                               type: 'category',
-                                               display: false,
-                                               stacked: true,
-                                       },
-                                       y: {
-                                               type: 'logarithmic',
-                                               display: false,
-                                               stacked: true
-                                       }
-                               }
-                       }
-               });
-
-               var meta0 = chart.getDatasetMeta(0);
-
-               [
-                       {b: 512, w: 102, x: 64, y: 512},
-                       {b: 512, w: 102, x: 192, y: 118},
-                       {b: 512, w: 102, x: 320, y: 512},
-                       {b: 512, w: 102, x: 449, y: 118}
-               ].forEach(function(values, i) {
-                       expect(meta0.data[i]._model.base).toBeCloseToPixel(values.b);
-                       expect(meta0.data[i]._model.width).toBeCloseToPixel(values.w);
-                       expect(meta0.data[i]._model.x).toBeCloseToPixel(values.x);
-                       expect(meta0.data[i]._model.y).toBeCloseToPixel(values.y);
-               });
-
-               var meta1 = chart.getDatasetMeta(1);
-
-               [
-                       {b: 512, w: 102, x: 64, y: 102},
-                       {b: 118, w: 102, x: 192, y: 102},
-                       {b: 512, w: 102, x: 320, y: 512},
-                       {b: 118, w: 102, x: 449, y: 0}
-               ].forEach(function(values, i) {
-                       expect(meta1.data[i]._model.base).toBeCloseToPixel(values.b);
-                       expect(meta1.data[i]._model.width).toBeCloseToPixel(values.w);
-                       expect(meta1.data[i]._model.x).toBeCloseToPixel(values.x);
-                       expect(meta1.data[i]._model.y).toBeCloseToPixel(values.y);
-               });
-       });
-
-       it('should update elements when the scales are stacked and the y axis is logarithmic and data is strings', function() {
-               var chart = window.acquireChart({
-                       type: 'bar',
-                       data: {
-                               datasets: [{
-                                       data: ['10', '100', '10', '100'],
-                                       label: 'dataset1'
-                               }, {
-                                       data: ['100', '10', '0', '100'],
-                                       label: 'dataset2'
-                               }],
-                               labels: ['label1', 'label2', 'label3', 'label4']
-                       },
-                       options: {
-                               legend: false,
-                               title: false,
-                               datasets: {
-                                       bar: {
-                                               barPercentage: 1,
-                                       }
-                               },
-                               scales: {
-                                       x: {
-                                               type: 'category',
-                                               display: false,
-                                               stacked: true,
-                                       },
-                                       y: {
-                                               type: 'logarithmic',
-                                               display: false,
-                                               stacked: true
-                                       }
-                               }
-                       }
-               });
-
-               var meta0 = chart.getDatasetMeta(0);
-
-               [
-                       {b: 512, w: 102, x: 64, y: 512},
-                       {b: 512, w: 102, x: 192, y: 118},
-                       {b: 512, w: 102, x: 320, y: 512},
-                       {b: 512, w: 102, x: 449, y: 118}
-               ].forEach(function(values, i) {
-                       expect(meta0.data[i]._model.base).toBeCloseToPixel(values.b);
-                       expect(meta0.data[i]._model.width).toBeCloseToPixel(values.w);
-                       expect(meta0.data[i]._model.x).toBeCloseToPixel(values.x);
-                       expect(meta0.data[i]._model.y).toBeCloseToPixel(values.y);
-               });
-
-               var meta1 = chart.getDatasetMeta(1);
-
-               [
-                       {b: 512, w: 102, x: 64, y: 102},
-                       {b: 118, w: 102, x: 192, y: 102},
-                       {b: 512, w: 102, x: 320, y: 512},
-                       {b: 118, w: 102, x: 449, y: 0}
-               ].forEach(function(values, i) {
-                       expect(meta1.data[i]._model.base).toBeCloseToPixel(values.b);
-                       expect(meta1.data[i]._model.width).toBeCloseToPixel(values.w);
-                       expect(meta1.data[i]._model.x).toBeCloseToPixel(values.x);
-                       expect(meta1.data[i]._model.y).toBeCloseToPixel(values.y);
-               });
-       });
-
        it('should draw all bars', function() {
                var chart = window.acquireChart({
                        type: 'bar',
index 5c0fa9cdc16095331573f78bec22f573a8f44dfc..23e41d513735cac22a0516412192d98b858690b4 100644 (file)
@@ -259,7 +259,7 @@ describe('Logarithmic Scale tests', function() {
                chart.options.scales.y.stacked = true;
                chart.update();
 
-               expect(chart.scales.y.min).toBe(10);
+               expect(chart.scales.y.min).toBe(1);
                expect(chart.scales.y.max).toBe(6000);
 
                expect(chart.scales.y1).not.toEqual(undefined); // must construct
@@ -369,7 +369,7 @@ describe('Logarithmic Scale tests', function() {
                        }
                });
 
-               expect(chart.scales.y.min).toBe(10);
+               expect(chart.scales.y.min).toBe(1);
                expect(chart.scales.y.max).toBe(200);
        });
 
@@ -411,7 +411,7 @@ describe('Logarithmic Scale tests', function() {
                        }
                });
 
-               expect(chart.scales.y.min).toBe(10);
+               expect(chart.scales.y.min).toBe(1);
                expect(chart.scales.y.max).toBe(200);
        });