]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Add drawTime: beforeDraw option to filler (#8973)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Sat, 24 Apr 2021 19:27:52 +0000 (22:27 +0300)
committerGitHub <noreply@github.com>
Sat, 24 Apr 2021 19:27:52 +0000 (15:27 -0400)
docs/charts/area.md
docs/samples/area/line-drawtime.md
src/plugins/plugin.filler.js
test/fixtures/plugin.filler/radar/beforeDraw.js [new file with mode: 0644]
test/fixtures/plugin.filler/radar/beforeDraw.png [new file with mode: 0644]

index f37515e4905025a6fe914e88c5279b79a22102f5..39e7f22218a51bc7ef4b9180fb8e5700f907e4f3 100644 (file)
@@ -68,7 +68,7 @@ Namespace: `options.plugins.filler`
 
 | Option | Type | Default | Description |
 | :--- | :--- | :--- | :--- |
-| `drawTime` | `string` | `beforeDatasetDraw` | Filler draw time. Supported values: `'beforeDatasetDraw'`, `'beforeDatasetsDraw'`
+| `drawTime` | `string` | `beforeDatasetDraw` | Filler draw time. Supported values: `'beforeDraw'`, `'beforeDatasetDraw'`, `'beforeDatasetsDraw'`
 | [`propagate`](#propagate) | `boolean` | `true` | Fill propagation when target is hidden.
 
 ### propagate
index 1c45fcb0c178c74f42c9756cc28c33a7c9a4c5b3..564ebc348f8dbe3de70075c87392b074b6a86aba 100644 (file)
@@ -26,7 +26,7 @@ const data = {
       label: 'Dataset 1',
       data: generateData(),
       borderColor: Utils.CHART_COLORS.red,
-      backgroundColor: Utils.transparentize(Utils.CHART_COLORS.red),
+      backgroundColor: Utils.CHART_COLORS.red,
       fill: true
     },
     {
@@ -58,6 +58,13 @@ const actions = [
       chart.update();
     }
   },
+  {
+    name: 'drawTime: beforeDraw',
+    handler: (chart) => {
+      chart.options.plugins.filler.drawTime = 'beforeDraw';
+      chart.update();
+    }
+  },
   {
     name: 'Randomize',
     handler(chart) {
index 1bd851da47b11d64d596ec1b4c25b9bcd616fbba..36467b33f7d6f2a43e895f39a94fc4cb0ee5a19a 100644 (file)
@@ -565,19 +565,32 @@ export default {
     }
   },
 
-  beforeDatasetsDraw(chart, _args, options) {
+  beforeDraw(chart, _args, options) {
+    const draw = options.drawTime === 'beforeDraw';
     const metasets = chart.getSortedVisibleDatasetMetas();
     const area = chart.chartArea;
-
     for (let i = metasets.length - 1; i >= 0; --i) {
       const source = metasets[i].$filler;
+      if (!source) {
+        continue;
+      }
 
-      if (source) {
-        source.line.updateControlPoints(area);
+      source.line.updateControlPoints(area);
+      if (draw) {
+        drawfill(chart.ctx, source, area);
+      }
+    }
+  },
 
-        if (options.drawTime === 'beforeDatasetsDraw') {
-          drawfill(chart.ctx, source, area);
-        }
+  beforeDatasetsDraw(chart, _args, options) {
+    if (options.drawTime !== 'beforeDatasetsDraw') {
+      return;
+    }
+    const metasets = chart.getSortedVisibleDatasetMetas();
+    for (let i = metasets.length - 1; i >= 0; --i) {
+      const source = metasets[i].$filler;
+      if (source) {
+        drawfill(chart.ctx, source, chart.chartArea);
       }
     }
   },
diff --git a/test/fixtures/plugin.filler/radar/beforeDraw.js b/test/fixtures/plugin.filler/radar/beforeDraw.js
new file mode 100644 (file)
index 0000000..956c6a0
--- /dev/null
@@ -0,0 +1,47 @@
+module.exports = {
+  config: {
+    type: 'radar',
+    data: {
+      labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
+      datasets: [{
+        label: '# of Votes',
+        data: [9, 7, 3, 5, 2, 3],
+        fill: 'origin',
+        borderColor: 'red',
+        backgroundColor: 'green',
+        pointRadius: 12,
+        pointBackgroundColor: 'red'
+      }]
+    },
+    options: {
+      layout: {
+        padding: 20
+      },
+      plugins: {
+        legend: false,
+        filler: {
+          drawTime: 'beforeDraw'
+        }
+      },
+      scales: {
+        r: {
+          angleLines: {
+            color: 'rgba(0,0,0,0.5)',
+            lineWidth: 2
+          },
+          grid: {
+            color: 'rgba(0,0,0,0.5)',
+            lineWidth: 2
+          },
+          pointLabels: {
+            display: false
+          },
+          ticks: {
+            beginAtZero: true,
+            display: false
+          },
+        }
+      }
+    }
+  }
+};
diff --git a/test/fixtures/plugin.filler/radar/beforeDraw.png b/test/fixtures/plugin.filler/radar/beforeDraw.png
new file mode 100644 (file)
index 0000000..886a204
Binary files /dev/null and b/test/fixtures/plugin.filler/radar/beforeDraw.png differ