]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
RadialLinear: add padding option for point labels (#8604)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Wed, 10 Mar 2021 06:40:22 +0000 (08:40 +0200)
committerGitHub <noreply@github.com>
Wed, 10 Mar 2021 06:40:22 +0000 (08:40 +0200)
* RadialLinear: add padding option for point labels

* lint

* only resolve padding once

docs/docs/axes/radial/linear.mdx
src/scales/scale.radialLinear.js
test/fixtures/scale.radialLinear/pointLabels/padding.js [new file with mode: 0644]
test/fixtures/scale.radialLinear/pointLabels/padding.png [new file with mode: 0644]
test/specs/scale.radialLinear.tests.js

index 0dcd6caaadaf2831a067b3010bd3c4dcebe4f0e6..7925af007823cd6117d895473ce2c6ef46adcbac 100644 (file)
@@ -130,6 +130,7 @@ Namespace: `options.scales[scaleId].pointLabels`
 | `callback` | `function` | | | Callback function to transform data labels to point labels. The default implementation simply returns the current string.
 | `color` | [`Color`](../../general/colors.md) | Yes | `Chart.defaults.color` | Color of label.
 | `font` | `Font` | Yes | `Chart.defaults.font` | See [Fonts](./general/fonts.md)
+| `padding` | `number` | Yes | 5 | Padding between chart and point labels.
 
 The scriptable context is described in [Options](../../general/options.md#scale) section.
 
index cb1cdb63d8d3e82c7bf38f3bd79360c11d1313bc..2d04d365f9992719666ffc2969e20b2bb6e745ea 100644 (file)
@@ -91,11 +91,13 @@ function fitWithPointLabels(scale) {
   let i, textSize, pointPosition;
 
   const labelSizes = [];
+  const padding = [];
 
   const valueCount = scale.chart.data.labels.length;
   for (i = 0; i < valueCount; i++) {
-    pointPosition = scale.getPointPosition(i, scale.drawingArea + 5);
     const opts = scale.options.pointLabels.setContext(scale.getContext(i));
+    padding[i] = opts.padding;
+    pointPosition = scale.getPointPosition(i, scale.drawingArea + padding[i]);
     const plFont = toFont(opts.font);
     scale.ctx.font = plFont.string;
     textSize = measureLabelSize(scale.ctx, plFont.lineHeight, scale._pointLabels[i]);
@@ -140,7 +142,7 @@ function fitWithPointLabels(scale) {
   for (i = 0; i < valueCount; i++) {
     // Extra pixels out for some label spacing
     const extra = (i === 0 ? tickBackdropHeight / 2 : 0);
-    const pointLabelPosition = scale.getPointPosition(i, outerDistance + extra + 5);
+    const pointLabelPosition = scale.getPointPosition(i, outerDistance + extra + padding[i]);
 
     const angle = toDegrees(scale.getIndexAngle(i));
     const size = labelSizes[i];
@@ -592,7 +594,10 @@ RadialLinearScale.defaults = {
     // Function - Used to convert point labels
     callback(label) {
       return label;
-    }
+    },
+
+    // Number - Additionl padding between scale and pointLabel
+    padding: 5
   }
 };
 
diff --git a/test/fixtures/scale.radialLinear/pointLabels/padding.js b/test/fixtures/scale.radialLinear/pointLabels/padding.js
new file mode 100644 (file)
index 0000000..348ab53
--- /dev/null
@@ -0,0 +1,49 @@
+module.exports = {
+  config: {
+    type: 'radar',
+    data: {
+      labels: [
+        ['VENTE ET', 'COMMERCIALISATION'],
+        ['GESTION', 'FINANCIÈRE'],
+        'NUMÉRIQUE',
+        ['ADMINISTRATION', 'ET OPÉRATION'],
+        ['RESSOURCES', 'HUMAINES'],
+        'INNOVATION'
+      ],
+      datasets: [
+        {
+          radius: 12,
+          backgroundColor: '#E43E51',
+          label: 'Compétences entrepreunariales',
+          data: [3, 2, 2, 1, 3, 1]
+        }
+      ]
+    },
+    options: {
+      plugins: {
+        legend: false,
+        tooltip: false,
+        filler: false
+      },
+      scales: {
+        r: {
+          min: 0,
+          max: 3,
+          pointLabels: {
+            padding: 30
+          },
+          ticks: {
+            display: false,
+            stepSize: 1,
+            maxTicksLimit: 1
+          }
+        }
+      },
+      responsive: true,
+      maintainAspectRatio: false
+    }
+  },
+  options: {
+    spriteText: true
+  }
+};
diff --git a/test/fixtures/scale.radialLinear/pointLabels/padding.png b/test/fixtures/scale.radialLinear/pointLabels/padding.png
new file mode 100644 (file)
index 0000000..2109109
Binary files /dev/null and b/test/fixtures/scale.radialLinear/pointLabels/padding.png differ
index 68ed3224949785235d8eb4ef0a160209a46226d5..818d0f3de86fdbbef877107e99ced26eb88ffef5 100644 (file)
@@ -48,7 +48,8 @@ describe('Test the radial linear scale', function() {
         font: {
           size: 10
         },
-        callback: defaultConfig.pointLabels.callback
+        callback: defaultConfig.pointLabels.callback,
+        padding: 5
       }
     });