]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Support mirror option on x-axis (#8867)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Sat, 10 Apr 2021 13:01:38 +0000 (16:01 +0300)
committerGitHub <noreply@github.com>
Sat, 10 Apr 2021 13:01:38 +0000 (09:01 -0400)
src/core/core.scale.js
test/fixtures/core.scale/ticks-mirror-x.js [new file with mode: 0644]
test/fixtures/core.scale/ticks-mirror-x.png [new file with mode: 0644]
test/fixtures/core.scale/ticks-mirror.png

index dbdbb18c32a744325cfb9e1174401187f9a1925c..cca092319beadeda621021c3a3ec618a59b7875e 100644 (file)
@@ -640,7 +640,7 @@ export default class Scale extends Element {
 
         if (isHorizontal) {
         // A horizontal axis is more constrained by the height.
-          const labelHeight = sin * widest.width + cos * highest.height;
+          const labelHeight = tickOpts.mirror ? 0 : sin * widest.width + cos * highest.height;
           minSize.height = Math.min(me.maxHeight, minSize.height + labelHeight + tickPadding);
         } else {
         // A vertical axis is more constrained by the width. Labels are the
@@ -1122,26 +1122,27 @@ export default class Scale extends Element {
     const {position, ticks: optionTicks} = options;
     const isHorizontal = me.isHorizontal();
     const ticks = me.ticks;
-    const {align, crossAlign, padding} = optionTicks;
+    const {align, crossAlign, padding, mirror} = optionTicks;
     const tl = getTickMarkLength(options.grid);
     const tickAndPadding = tl + padding;
+    const hTickAndPadding = mirror ? -padding : tickAndPadding;
     const rotation = -toRadians(me.labelRotation);
     const items = [];
     let i, ilen, tick, label, x, y, textAlign, pixel, font, lineHeight, lineCount, textOffset;
     let textBaseline = 'middle';
 
     if (position === 'top') {
-      y = me.bottom - tickAndPadding;
+      y = me.bottom - hTickAndPadding;
       textAlign = me._getXAxisLabelAlignment();
     } else if (position === 'bottom') {
-      y = me.top + tickAndPadding;
+      y = me.top + hTickAndPadding;
       textAlign = me._getXAxisLabelAlignment();
     } else if (position === 'left') {
-      const ret = this._getYAxisLabelAlignment(tl);
+      const ret = me._getYAxisLabelAlignment(tl);
       textAlign = ret.textAlign;
       x = ret.x;
     } else if (position === 'right') {
-      const ret = this._getYAxisLabelAlignment(tl);
+      const ret = me._getYAxisLabelAlignment(tl);
       textAlign = ret.textAlign;
       x = ret.x;
     } else if (axis === 'x') {
@@ -1161,7 +1162,7 @@ export default class Scale extends Element {
         const value = position[positionAxisID];
         x = me.chart.scales[positionAxisID].getPixelForValue(value);
       }
-      textAlign = this._getYAxisLabelAlignment(tl).textAlign;
+      textAlign = me._getYAxisLabelAlignment(tl).textAlign;
     }
 
     if (axis === 'y') {
@@ -1207,6 +1208,9 @@ export default class Scale extends Element {
             textOffset = labelSizes.highest.height - lineCount * lineHeight;
           }
         }
+        if (mirror) {
+          textOffset *= -1;
+        }
       } else {
         y = pixel;
         textOffset = (1 - lineCount) * lineHeight / 2;
@@ -1262,7 +1266,7 @@ export default class Scale extends Element {
     if (position === 'left') {
       if (mirror) {
         textAlign = 'left';
-        x = me.right - padding;
+        x = me.right + padding;
       } else {
         x = me.right - tickAndPadding;
 
diff --git a/test/fixtures/core.scale/ticks-mirror-x.js b/test/fixtures/core.scale/ticks-mirror-x.js
new file mode 100644 (file)
index 0000000..ec151e5
--- /dev/null
@@ -0,0 +1,30 @@
+module.exports = {
+  config: {
+    type: 'line',
+    data: {
+      datasets: [{
+        data: [1, -1, 3],
+      }],
+      labels: ['Label1', 'Label2', 'Label3']
+    },
+    options: {
+      scales: {
+        x: {
+          ticks: {
+            mirror: true
+          }
+        },
+        y: {
+          display: false
+        }
+      }
+    }
+  },
+  options: {
+    spriteText: true,
+    canvas: {
+      height: 256,
+      width: 512
+    }
+  }
+};
diff --git a/test/fixtures/core.scale/ticks-mirror-x.png b/test/fixtures/core.scale/ticks-mirror-x.png
new file mode 100644 (file)
index 0000000..e9fe653
Binary files /dev/null and b/test/fixtures/core.scale/ticks-mirror-x.png differ
index dc483b516377e20e4e5766fb604f7456b19fb789..35fba6e521883829d5e8e76f36bc04d2a9aca728 100644 (file)
Binary files a/test/fixtures/core.scale/ticks-mirror.png and b/test/fixtures/core.scale/ticks-mirror.png differ