I have 2 simple area graphs I've created using D3.js with the data & code below - Let's call them Graph A
& Graph B
. I would like to use them to create 3 new paths/polygons based on how they intersect.
Path 1
=Graph A
-Graph B
Path 2
=Graph B
-Graph A
Path 3
=Graph B
-Path 2
orGraph A
andGraph B
intersection
Most visual editors allow you to perform these boolean operations, see: https://en.wikipedia.org/wiki/Boolean_operations_on_polygons
Is it possible to do this in D3.js?
jsfiddle: https://jsfiddle.net/jvf1utmx/
Graph Definitions:
// data
var dataA = [
{ x: 0, y: 100, },
{ x: 100, y: 150, },
{ x: 200, y: 350, },
{ x: 300, y: 200, },
];
var dataB = [
{ x: 0, y: 200, },
{ x: 100, y: 100, },
{ x: 200, y: 250, },
{ x: 300, y: 150, },
];
// Graph shapes
var graphA = svg.append("path")
.datum(dataA)
.attr("class", "area")
.attr("d", area)
.style({fill: '#bbbb00', opacity: 0.8});
var graphB = svg.append("path")
.datum(dataB)
.attr("class", "area")
.attr("d", area)
.style({fill: '#666666', opacity: 0.8});
My attempt at clip paths:
// Clipping attempts
var graphBClip = svg.append("clipPath")
.attr('id','graphBClip')
graphBClip.append(graphB);
graphA.attr("clip-path","url(#graphBClip)");