]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Legend: fix layout padding handling (#9290)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Sat, 19 Jun 2021 22:31:33 +0000 (01:31 +0300)
committerGitHub <noreply@github.com>
Sat, 19 Jun 2021 22:31:33 +0000 (18:31 -0400)
src/core/core.layouts.js
src/plugins/plugin.legend.js
test/fixtures/plugin.legend/padding/2cols-with-padding.js [new file with mode: 0644]
test/fixtures/plugin.legend/padding/2cols-with-padding.png [new file with mode: 0644]
test/fixtures/plugin.legend/padding/add-column.js [new file with mode: 0644]
test/fixtures/plugin.legend/padding/add-column.png [new file with mode: 0644]

index e0069fe2e98ff8de2f64d375334ee1c4a1fb83d7..7206db0d99057767567a0d75a76e1ac79f9c32e6 100644 (file)
@@ -200,7 +200,7 @@ function placeBoxes(boxes, chartArea, params) {
       box.left = x;
       box.right = x + box.width;
       box.top = box.fullSize ? userPadding.top : chartArea.top;
-      box.bottom = box.fullSize ? params.outerHeight - userPadding.right : chartArea.top + chartArea.h;
+      box.bottom = box.fullSize ? params.outerHeight - userPadding.bottom : chartArea.top + chartArea.h;
       box.height = box.bottom - box.top;
       x = box.right;
     }
index 206f1add94f5b8b2e30ff880cfeedd72bbfff0bb..03f7fe4f21bd8f14be6ddbb86e815f2260b54139 100644 (file)
@@ -88,11 +88,11 @@ export class Legend extends Element {
 
     if (me.isHorizontal()) {
       me.width = me.maxWidth;
-      me.left = 0;
+      me.left = me._margins.left;
       me.right = me.width;
     } else {
       me.height = me.maxHeight;
-      me.top = 0;
+      me.top = me._margins.top;
       me.bottom = me.height;
     }
   }
@@ -199,29 +199,26 @@ export class Legend extends Element {
     let currentColHeight = 0;
 
     let left = 0;
-    let top = 0;
     let col = 0;
 
     me.legendItems.forEach((legendItem, i) => {
       const itemWidth = boxWidth + (fontSize / 2) + ctx.measureText(legendItem.text).width;
 
       // If too tall, go to new column
-      if (i > 0 && currentColHeight + fontSize + 2 * padding > heightLimit) {
+      if (i > 0 && currentColHeight + itemHeight + 2 * padding > heightLimit) {
         totalWidth += currentColWidth + padding;
         columnSizes.push({width: currentColWidth, height: currentColHeight}); // previous column size
         left += currentColWidth + padding;
         col++;
-        top = 0;
         currentColWidth = currentColHeight = 0;
       }
 
+      // Store the hitbox width and height here. Final position will be updated in `draw`
+      hitboxes[i] = {left, top: currentColHeight, col, width: itemWidth, height: itemHeight};
+
       // Get max width
       currentColWidth = Math.max(currentColWidth, itemWidth);
-      currentColHeight += fontSize + padding;
-
-      // Store the hitbox width and height here. Final position will be updated in `draw`
-      hitboxes[i] = {left, top, col, width: itemWidth, height: itemHeight};
-      top += itemHeight + padding;
+      currentColHeight += itemHeight + padding;
     });
 
     totalWidth += currentColWidth;
diff --git a/test/fixtures/plugin.legend/padding/2cols-with-padding.js b/test/fixtures/plugin.legend/padding/2cols-with-padding.js
new file mode 100644 (file)
index 0000000..604ade6
--- /dev/null
@@ -0,0 +1,35 @@
+module.exports = {
+  description: 'https://github.com/chartjs/Chart.js/issues/9278',
+  config: {
+    type: 'pie',
+    data: {
+      labels: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'],
+      datasets: [{
+        data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
+        backgroundColor: 'red'
+      }]
+    },
+    options: {
+      plugins: {
+        legend: {
+          position: 'left'
+        }
+      },
+      layout: {
+        padding: {
+          top: 50,
+          left: 30,
+          right: 30,
+          bottom: 50
+        }
+      }
+    }
+  },
+  options: {
+    spriteText: true,
+    canvas: {
+      width: 400,
+      height: 300
+    },
+  }
+};
diff --git a/test/fixtures/plugin.legend/padding/2cols-with-padding.png b/test/fixtures/plugin.legend/padding/2cols-with-padding.png
new file mode 100644 (file)
index 0000000..8871ccd
Binary files /dev/null and b/test/fixtures/plugin.legend/padding/2cols-with-padding.png differ
diff --git a/test/fixtures/plugin.legend/padding/add-column.js b/test/fixtures/plugin.legend/padding/add-column.js
new file mode 100644 (file)
index 0000000..7a1e6d5
--- /dev/null
@@ -0,0 +1,39 @@
+module.exports = {
+  description: 'https://github.com/chartjs/Chart.js/issues/9278',
+  config: {
+    type: 'pie',
+    data: {
+      labels: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'],
+      datasets: [{
+        data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
+        backgroundColor: 'red'
+      }]
+    },
+    options: {
+      plugins: {
+        legend: {
+          position: 'left'
+        }
+      },
+      layout: {
+        padding: {
+          top: 55,
+          left: 30,
+          right: 30
+        }
+      }
+    }
+  },
+  options: {
+    spriteText: true,
+    canvas: {
+      width: 400,
+      height: 300
+    },
+    run(chart) {
+      chart.data.labels.push('k');
+      chart.data.datasets[0].data.push(11);
+      chart.update();
+    }
+  }
+};
diff --git a/test/fixtures/plugin.legend/padding/add-column.png b/test/fixtures/plugin.legend/padding/add-column.png
new file mode 100644 (file)
index 0000000..187dcda
Binary files /dev/null and b/test/fixtures/plugin.legend/padding/add-column.png differ