]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix center axis title positions (#9413)
authorEvert Timberg <evert.timberg+github@gmail.com>
Thu, 15 Jul 2021 12:20:22 +0000 (08:20 -0400)
committerGitHub <noreply@github.com>
Thu, 15 Jul 2021 12:20:22 +0000 (08:20 -0400)
* Fix the title positions for center positioned axes

* Handle center position for vertical axes

* Tests

src/core/core.scale.js
test/fixtures/core.scale/title/horizontal-center.js [new file with mode: 0644]
test/fixtures/core.scale/title/horizontal-center.png [new file with mode: 0644]
test/fixtures/core.scale/title/horizontal-value.js [new file with mode: 0644]
test/fixtures/core.scale/title/horizontal-value.png [new file with mode: 0644]
test/fixtures/core.scale/title/vertical-center.js [new file with mode: 0644]
test/fixtures/core.scale/title/vertical-center.png [new file with mode: 0644]
test/fixtures/core.scale/title/vertical-value.js [new file with mode: 0644]
test/fixtures/core.scale/title/vertical-value.png [new file with mode: 0644]

index 25c4b0ebe2ce86ebe7af9dd17d3faf8341984a0a..c04899d25a1505a34429a0c9afc671b6a7a06cb1 100644 (file)
@@ -132,16 +132,36 @@ function titleAlign(align, position, reverse) {
 }
 
 function titleArgs(scale, offset, position, align) {
-  const {top, left, bottom, right} = scale;
+  const {top, left, bottom, right, chart} = scale;
+  const {chartArea, scales} = chart;
   let rotation = 0;
   let maxWidth, titleX, titleY;
+  const height = bottom - top;
+  const width = right - left;
 
   if (scale.isHorizontal()) {
     titleX = _alignStartEnd(align, left, right);
-    titleY = offsetFromEdge(scale, position, offset);
+
+    if (isObject(position)) {
+      const positionAxisID = Object.keys(position)[0];
+      const value = position[positionAxisID];
+      titleY = scales[positionAxisID].getPixelForValue(value) + height - offset;
+    } else if (position === 'center') {
+      titleY = (chartArea.bottom + chartArea.top) / 2 + height - offset;
+    } else {
+      titleY = offsetFromEdge(scale, position, offset);
+    }
     maxWidth = right - left;
   } else {
-    titleX = offsetFromEdge(scale, position, offset);
+    if (isObject(position)) {
+      const positionAxisID = Object.keys(position)[0];
+      const value = position[positionAxisID];
+      titleX = scales[positionAxisID].getPixelForValue(value) - width + offset;
+    } else if (position === 'center') {
+      titleX = (chartArea.left + chartArea.right) / 2 - width + offset;
+    } else {
+      titleX = offsetFromEdge(scale, position, offset);
+    }
     titleY = _alignStartEnd(align, bottom, top);
     rotation = position === 'left' ? -HALF_PI : HALF_PI;
   }
@@ -1557,7 +1577,7 @@ export default class Scale extends Element {
     const align = title.align;
     let offset = font.lineHeight / 2;
 
-    if (position === 'bottom') {
+    if (position === 'bottom' || position === 'center' || isObject(position)) {
       offset += padding.bottom;
       if (isArray(title.text)) {
         offset += font.lineHeight * (title.text.length - 1);
diff --git a/test/fixtures/core.scale/title/horizontal-center.js b/test/fixtures/core.scale/title/horizontal-center.js
new file mode 100644 (file)
index 0000000..68e488d
--- /dev/null
@@ -0,0 +1,49 @@
+module.exports = {
+  config: {
+    type: 'line',
+    options: {
+      events: [],
+      scales: {
+        y: {
+          type: 'linear',
+          position: 'left',
+          min: 0,
+          max: 100,
+          ticks: {
+            display: false
+          },
+          grid: {
+            display: false
+          },
+          title: {
+            display: true,
+            text: 'vertical'
+          }
+        },
+        x: {
+          type: 'linear',
+          position: 'center',
+          min: 0,
+          max: 100,
+          ticks: {
+            display: false
+          },
+          grid: {
+            display: false
+          },
+          title: {
+            display: true,
+            text: 'horizontal'
+          }
+        },
+      }
+    }
+  },
+  options: {
+    spriteText: true,
+    canvas: {
+      height: 256,
+      width: 256
+    },
+  }
+};
diff --git a/test/fixtures/core.scale/title/horizontal-center.png b/test/fixtures/core.scale/title/horizontal-center.png
new file mode 100644 (file)
index 0000000..b3abf34
Binary files /dev/null and b/test/fixtures/core.scale/title/horizontal-center.png differ
diff --git a/test/fixtures/core.scale/title/horizontal-value.js b/test/fixtures/core.scale/title/horizontal-value.js
new file mode 100644 (file)
index 0000000..7cb97f6
--- /dev/null
@@ -0,0 +1,51 @@
+module.exports = {
+  config: {
+    type: 'line',
+    options: {
+      events: [],
+      scales: {
+        y: {
+          type: 'linear',
+          position: 'left',
+          min: 0,
+          max: 100,
+          ticks: {
+            display: false
+          },
+          grid: {
+            display: false
+          },
+          title: {
+            display: true,
+            text: 'vertical'
+          }
+        },
+        x: {
+          type: 'linear',
+          position: {
+            y: 40,
+          },
+          min: 0,
+          max: 100,
+          ticks: {
+            display: false
+          },
+          grid: {
+            display: false
+          },
+          title: {
+            display: true,
+            text: 'horizontal'
+          }
+        },
+      }
+    }
+  },
+  options: {
+    spriteText: true,
+    canvas: {
+      height: 256,
+      width: 256
+    },
+  }
+};
diff --git a/test/fixtures/core.scale/title/horizontal-value.png b/test/fixtures/core.scale/title/horizontal-value.png
new file mode 100644 (file)
index 0000000..9d3c5dd
Binary files /dev/null and b/test/fixtures/core.scale/title/horizontal-value.png differ
diff --git a/test/fixtures/core.scale/title/vertical-center.js b/test/fixtures/core.scale/title/vertical-center.js
new file mode 100644 (file)
index 0000000..89669be
--- /dev/null
@@ -0,0 +1,49 @@
+module.exports = {
+  config: {
+    type: 'line',
+    options: {
+      events: [],
+      scales: {
+        y: {
+          type: 'linear',
+          position: 'center',
+          min: 0,
+          max: 100,
+          ticks: {
+            display: false
+          },
+          grid: {
+            display: false
+          },
+          title: {
+            display: true,
+            text: 'vertical'
+          }
+        },
+        x: {
+          type: 'linear',
+          position: 'bottom',
+          min: 0,
+          max: 100,
+          ticks: {
+            display: false
+          },
+          grid: {
+            display: false
+          },
+          title: {
+            display: true,
+            text: 'horizontal'
+          }
+        },
+      }
+    }
+  },
+  options: {
+    spriteText: true,
+    canvas: {
+      height: 256,
+      width: 256
+    },
+  }
+};
diff --git a/test/fixtures/core.scale/title/vertical-center.png b/test/fixtures/core.scale/title/vertical-center.png
new file mode 100644 (file)
index 0000000..8ebdad4
Binary files /dev/null and b/test/fixtures/core.scale/title/vertical-center.png differ
diff --git a/test/fixtures/core.scale/title/vertical-value.js b/test/fixtures/core.scale/title/vertical-value.js
new file mode 100644 (file)
index 0000000..b8a0de6
--- /dev/null
@@ -0,0 +1,51 @@
+module.exports = {
+  config: {
+    type: 'line',
+    options: {
+      events: [],
+      scales: {
+        y: {
+          type: 'linear',
+          position: {
+            x: 40
+          },
+          min: 0,
+          max: 100,
+          ticks: {
+            display: false
+          },
+          grid: {
+            display: false
+          },
+          title: {
+            display: true,
+            text: 'vertical'
+          }
+        },
+        x: {
+          type: 'linear',
+          position: 'bottom',
+          min: 0,
+          max: 100,
+          ticks: {
+            display: false
+          },
+          grid: {
+            display: false
+          },
+          title: {
+            display: true,
+            text: 'horizontal'
+          }
+        },
+      }
+    }
+  },
+  options: {
+    spriteText: true,
+    canvas: {
+      height: 256,
+      width: 256
+    },
+  }
+};
diff --git a/test/fixtures/core.scale/title/vertical-value.png b/test/fixtures/core.scale/title/vertical-value.png
new file mode 100644 (file)
index 0000000..eb4b201
Binary files /dev/null and b/test/fixtures/core.scale/title/vertical-value.png differ