]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Feature/active elements on top (#9920)
authorJacco van den Berg <39033624+LeeLenaleee@users.noreply.github.com>
Sun, 5 Dec 2021 13:56:53 +0000 (14:56 +0100)
committerGitHub <noreply@github.com>
Sun, 5 Dec 2021 13:56:53 +0000 (08:56 -0500)
* add flag to draw active items on top

* add documentation and types

* remove redundent check

* added test

* resolve linting errors

* increase tollerance

* remove axes for better test, hope no tolerance needed

docs/charts/bubble.md
docs/charts/line.md
src/core/core.datasetController.js
src/core/core.defaults.js
test/fixtures/core.interaction/drawActiveElementsOnTop-false.js [new file with mode: 0644]
test/fixtures/core.interaction/drawActiveElementsOnTop-false.png [new file with mode: 0644]
types/index.esm.d.ts

index 61710badf4bd045f95212c6d8833dc3c5e47ea65..3286127294544ee5411493cd15598233816cb061 100644 (file)
@@ -63,6 +63,7 @@ The bubble chart allows a number of properties to be specified for each dataset.
 | [`pointStyle`](#styling) | [`pointStyle`](../configuration/elements.md#types) | Yes | Yes | `'circle'`
 | [`rotation`](#styling) | `number` | Yes | Yes | `0`
 | [`radius`](#styling) | `number` | Yes | Yes | `3`
+| [`drawActiveElementsOnTop`](#styling) | `boolean` | Yes | Yes | `true`
 
 All these values, if `undefined`, fallback to the scopes described in [option resolution](../general/options)
 
@@ -86,6 +87,7 @@ The style of each bubble can be controlled with the following properties:
 | `pointStyle` | bubble [shape style](../configuration/elements.md#point-styles).
 | `rotation` | bubble rotation (in degrees).
 | `radius` | bubble radius (in pixels).
+| `drawActiveElementsOnTop` | Draw the active bubbles of a dataset over the other bubbles of the dataset
 
 All these values, if `undefined`, fallback to the associated [`elements.point.*`](../configuration/elements.md#point-configuration) options.
 
index 819799fe9b57d4b3e969a7c16d46b8c754dea56b..cf0787b9fe166045eebb3725e4b6e601a7cb1fbe 100644 (file)
@@ -84,6 +84,7 @@ The line chart allows a number of properties to be specified for each dataset. T
 | [`tension`](#line-styling) | `number` | - | - | `0`
 | [`xAxisID`](#general) | `string` | - | - | first x axis
 | [`yAxisID`](#general) | `string` | - | - | first y axis
+| [`drawActiveElementsOnTop`](#point-styling) | `boolean` | Yes | Yes | `true`
 
 All these values, if `undefined`, fallback to the scopes described in [option resolution](../general/options)
 
@@ -112,6 +113,7 @@ The style of each point can be controlled with the following properties:
 | `pointRadius` | The radius of the point shape. If set to 0, the point is not rendered.
 | `pointRotation` | The rotation of the point in degrees.
 | `pointStyle` | Style of the point. [more...](../configuration/elements.md#point-styles)
+| `drawActiveElementsOnTop` | Draw the active points of a dataset over the other points of the dataset
 
 All these values, if `undefined`, fallback first to the dataset options then to the associated [`elements.point.*`](../configuration/elements.md#point-configuration) options.
 
index 8ba52e1692615ffb9740be604788e1f1a8b33533..b6c6778d999017f73acf03ceddb6ef8b3123f301 100644 (file)
@@ -678,6 +678,7 @@ export default class DatasetController {
     const active = [];
     const start = this._drawStart || 0;
     const count = this._drawCount || (elements.length - start);
+    const drawActiveElementsOnTop = this.options.drawActiveElementsOnTop;
     let i;
 
     if (meta.dataset) {
@@ -689,7 +690,7 @@ export default class DatasetController {
       if (element.hidden) {
         continue;
       }
-      if (element.active) {
+      if (element.active && drawActiveElementsOnTop) {
         active.push(element);
       } else {
         element.draw(ctx, area);
index eb009289466a8163982b886167a14fc53ba7185c..9817e7aba55ef20936c5de5fd11724a7129d2594 100644 (file)
@@ -73,6 +73,7 @@ export class Defaults {
     this.scale = undefined;
     this.scales = {};
     this.showLine = true;
+    this.drawActiveElementsOnTop = true;
 
     this.describe(_descriptors);
   }
diff --git a/test/fixtures/core.interaction/drawActiveElementsOnTop-false.js b/test/fixtures/core.interaction/drawActiveElementsOnTop-false.js
new file mode 100644 (file)
index 0000000..7185553
--- /dev/null
@@ -0,0 +1,41 @@
+module.exports = {
+  config: {
+    type: 'bubble',
+    data: {
+      datasets: [{
+        data: [
+          {x: 1, y: 1, r: 80},
+          {x: 1, y: 1, r: 20}
+        ],
+        drawActiveElementsOnTop: false,
+        backgroundColor: (ctx) => (ctx.dataIndex === 1 ? 'red' : 'blue'),
+        hoverBackgroundColor: 'yellow',
+        hoverRadius: 0,
+      }]
+    },
+    options: {
+      scales: {
+        x: {
+          display: false
+        },
+        y: {
+          display: false
+        },
+      },
+      plugins: {
+        tooltip: false,
+        legend: false
+      },
+    }
+  },
+  options: {
+    canvas: {
+      width: 256,
+      height: 256
+    },
+    async run(chart) {
+      const point = chart.getDatasetMeta(0).data[0];
+      await jasmine.triggerMouseEvent(chart, 'click', {y: point.y, x: point.x + 25});
+    }
+  }
+};
diff --git a/test/fixtures/core.interaction/drawActiveElementsOnTop-false.png b/test/fixtures/core.interaction/drawActiveElementsOnTop-false.png
new file mode 100644 (file)
index 0000000..ecfe377
Binary files /dev/null and b/test/fixtures/core.interaction/drawActiveElementsOnTop-false.png differ
index 48f398e584a9e72389166db3145c42a537880eb9..29512c4cf0aa85c053dec20eb33639df00aec18a 100644 (file)
@@ -1845,6 +1845,11 @@ export interface PointOptions extends CommonElementOptions {
    * @default 0
    */
   rotation: number;
+  /**
+   * Draw the active elements over the other elements of the dataset,
+   * @default true
+   */
+  drawActiveElementsOnTop: boolean;
 }
 
 export interface PointHoverOptions extends CommonHoverOptions {