Last active
June 26, 2016 08:04
-
-
Save mbostock/1067636 to your computer and use it in GitHub Desktop.
Venn Diagram with Clipping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: gpl-3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
body { | |
background: #333; | |
} | |
</style> | |
<body> | |
<script src="//d3js.org/d3.v3.min.js"></script> | |
<script> | |
var width = 960, | |
height = 500; | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
var defs = svg.append("defs"); | |
defs.append("clipPath") | |
.attr("id", "circle1") | |
.append("circle") | |
.attr("cx", 350) | |
.attr("cy", 200) | |
.attr("r", 180); | |
defs.append("clipPath") | |
.attr("id", "circle2") | |
.append("circle") | |
.attr("cx", 550) | |
.attr("cy", 200) | |
.attr("r", 180); | |
defs.append("clipPath") | |
.attr("id", "circle3") | |
.append("circle") | |
.attr("cx", 450) | |
.attr("cy", 300) | |
.attr("r", 180); | |
svg.append("rect") | |
.attr("clip-path", "url(#circle1)") | |
.attr("width", width) | |
.attr("height", height) | |
.style("fill", "#ff0000"); | |
svg.append("rect") | |
.attr("clip-path", "url(#circle2)") | |
.attr("width", width) | |
.attr("height", height) | |
.style("fill", "#00ff00"); | |
svg.append("rect") | |
.attr("clip-path", "url(#circle3)") | |
.attr("width", width) | |
.attr("height", height) | |
.style("fill", "#0000ff"); | |
svg.append("g") | |
.attr("clip-path", "url(#circle1)") | |
.append("rect") | |
.attr("clip-path", "url(#circle2)") | |
.attr("width", width) | |
.attr("height", height) | |
.style("fill", "#ffff00"); | |
svg.append("g") | |
.attr("clip-path", "url(#circle2)") | |
.append("rect") | |
.attr("clip-path", "url(#circle3)") | |
.attr("width", width) | |
.attr("height", height) | |
.style("fill", "#00ffff"); | |
svg.append("g") | |
.attr("clip-path", "url(#circle3)") | |
.append("rect") | |
.attr("clip-path", "url(#circle1)") | |
.attr("width", width) | |
.attr("height", height) | |
.style("fill", "#ff00ff"); | |
svg.append("g") | |
.attr("clip-path", "url(#circle3)") | |
.append("g") | |
.attr("clip-path", "url(#circle2)") | |
.append("rect") | |
.attr("clip-path", "url(#circle1)") | |
.attr("width", width) | |
.attr("height", height) | |
.style("fill", "#ffffff"); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi mbostock,
I am just starting to play around with SVG and have found D3.js very useful and enjoy learning from the examples you provide. I would like to add the ability to highlight the different sections of a venn diagram with an outline around the section's perimeter when the mouse hovers over it. To do this, I would think I need to create the 7 disjoint areas of a venn diagram. For example, A and B and not C would be the intersection of circle A and Circle B and the complement of Circle C. How would I create such a shape? I have been trying to use a mask for the complement of C but haven't found many examples of using mask within D3.js. Thanks for any help and for the great library.
Justin
Edit: Looks like Elliptical Arcs are the way to go to get the behavior I am looking for.