]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix using above/below filling option with discontinuous lines (#10024)
authorCharles Masson <72135060+charlesmass2@users.noreply.github.com>
Sat, 1 Jan 2022 21:42:45 +0000 (22:42 +0100)
committerGitHub <noreply@github.com>
Sat, 1 Jan 2022 21:42:45 +0000 (23:42 +0200)
Added a test in the boundary folder

src/plugins/plugin.filler.js
test/fixtures/plugin.filler/line/boundary/above-below-line-null-start.json [new file with mode: 0644]
test/fixtures/plugin.filler/line/boundary/above-below-line-null-start.png [new file with mode: 0644]
test/fixtures/plugin.filler/line/boundary/above-below-line-null.json [new file with mode: 0644]
test/fixtures/plugin.filler/line/boundary/above-below-line-null.png [new file with mode: 0644]

index 8650248cfb4536d2fa920c2905a7e872fbe0035d..75fea2ff7dc5a30abde09bb86ffb28fc58113dad 100644 (file)
@@ -383,9 +383,30 @@ function resolveTarget(sources, index, propagate) {
 }
 
 function _clip(ctx, target, clipY) {
+  const {segments, points} = target;
+  let first = true;
+  let lineLoop = false;
+
   ctx.beginPath();
-  target.path(ctx);
-  ctx.lineTo(target.last().x, clipY);
+  for (const segment of segments) {
+    const {start, end} = segment;
+    const firstPoint = points[start];
+    const lastPoint = points[findSegmentEnd(start, end, points)];
+    if (first) {
+      ctx.moveTo(firstPoint.x, firstPoint.y);
+      first = false;
+    } else {
+      ctx.lineTo(firstPoint.x, clipY);
+      ctx.lineTo(firstPoint.x, firstPoint.y);
+    }
+    lineLoop = !!target.pathSegment(ctx, segment, {move: lineLoop});
+    if (lineLoop) {
+      ctx.closePath();
+    } else {
+      ctx.lineTo(lastPoint.x, clipY);
+    }
+  }
+
   ctx.lineTo(target.first().x, clipY);
   ctx.closePath();
   ctx.clip();
diff --git a/test/fixtures/plugin.filler/line/boundary/above-below-line-null-start.json b/test/fixtures/plugin.filler/line/boundary/above-below-line-null-start.json
new file mode 100644 (file)
index 0000000..e0bce4b
--- /dev/null
@@ -0,0 +1,53 @@
+{
+    "config": {
+        "type": "line",
+        "data": {
+            "labels": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"],
+            "datasets": [{
+                "borderColor": "rgb(42, 90, 145)",
+                "data": [null, 12, 30, 36, 45, 53, 68, 79, null, 95, 18, 18, 180],
+                "fill": {
+                  "target": "+1",
+                  "above": "rgba(4, 142, 43, 0.5)",
+                  "below": "rgba(241, 49, 34, 0.5)"
+                }
+            }, {
+                "borderColor": "#00ADEE",
+                "data": [null, 0, 0, 0, 0, 0, 20, 108, null, 72, 72, 72, 72],
+                "fill": false
+            }]
+        },
+        "options": {
+            "responsive": false,
+            "spanGaps": false,
+            "scales": {
+                "x": {
+                    "display": false
+                },
+                "y": {
+                    "display": false
+                }
+            },
+            "elements": {
+                "point": {
+                    "radius": 0
+                },
+                "line": {
+                    "cubicInterpolationMode": "monotone",
+                    "borderColor": "transparent"
+                }
+            },
+            "plugins": {
+                "legend": false,
+                "title": false,
+                "tooltip": false
+            }
+        }
+    },
+    "options": {
+        "canvas": {
+            "height": 256,
+            "width": 512
+        }
+    }
+}
diff --git a/test/fixtures/plugin.filler/line/boundary/above-below-line-null-start.png b/test/fixtures/plugin.filler/line/boundary/above-below-line-null-start.png
new file mode 100644 (file)
index 0000000..5414dc9
Binary files /dev/null and b/test/fixtures/plugin.filler/line/boundary/above-below-line-null-start.png differ
diff --git a/test/fixtures/plugin.filler/line/boundary/above-below-line-null.json b/test/fixtures/plugin.filler/line/boundary/above-below-line-null.json
new file mode 100644 (file)
index 0000000..405542e
--- /dev/null
@@ -0,0 +1,53 @@
+{
+    "config": {
+        "type": "line",
+        "data": {
+            "labels": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"],
+            "datasets": [{
+                "borderColor": "rgb(42, 90, 145)",
+                "data": [4, 12, 30, 36, 45, 53, 68, 79, null, 95, 18, null, 18, 180],
+                "fill": {
+                  "target": "+1",
+                  "above": "rgba(4, 142, 43, 0.5)",
+                  "below": "rgba(241, 49, 34, 0.5)"
+                }
+            }, {
+                "borderColor": "#00ADEE",
+                "data": [0, 0, 0, 0, 0, 0, 20, 108, null, 72, 72, null, 72, 72],
+                "fill": false
+            }]
+        },
+        "options": {
+            "responsive": false,
+            "spanGaps": false,
+            "scales": {
+                "x": {
+                    "display": false
+                },
+                "y": {
+                    "display": false
+                }
+            },
+            "elements": {
+                "point": {
+                    "radius": 0
+                },
+                "line": {
+                    "cubicInterpolationMode": "monotone",
+                    "borderColor": "transparent"
+                }
+            },
+            "plugins": {
+                "legend": false,
+                "title": false,
+                "tooltip": false
+            }
+        }
+    },
+    "options": {
+        "canvas": {
+            "height": 256,
+            "width": 512
+        }
+    }
+}
diff --git a/test/fixtures/plugin.filler/line/boundary/above-below-line-null.png b/test/fixtures/plugin.filler/line/boundary/above-below-line-null.png
new file mode 100644 (file)
index 0000000..7118324
Binary files /dev/null and b/test/fixtures/plugin.filler/line/boundary/above-below-line-null.png differ