Skip to content

Instantly share code, notes, and snippets.

@henryjameslau
Created April 8, 2020 09:28
Show Gist options
  • Save henryjameslau/f4c109097826ae8cabf599414cfece97 to your computer and use it in GitHub Desktop.
Save henryjameslau/f4c109097826ae8cabf599414cfece97 to your computer and use it in GitHub Desktop.
Fill patterns
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<script>
// Feel free to change or delete any of the code you see in this editor!
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500)
svg.append('pattern')
.attr("id","hatch1")
.attr("patternUnits","userSpaceOnUse")
.attr("patternTransform","rotate(45 0 0)")
.attr("width",4)
.attr("height",4)
.append("line")
.attr("x1",0)
.attr("y1",0)
.attr("x2",0)
.attr("y2",2)
.style("stroke","black")
.style("stroke-width",1)
svg.append('pattern')
.attr("id","hatch2")
.attr("patternUnits","userSpaceOnUse")
.attr("patternTransform","rotate(-45 0 0)")
.attr("width",4)
.attr("height",4)
.append("line")
.attr("x1",0)
.attr("y1",0)
.attr("x2",0)
.attr("y2",2)
.style("stroke","black")
.style("stroke-width",1)
svg.append('pattern')
.attr("id","hatch3")
.attr("patternUnits","userSpaceOnUse")
.attr("patternTransform","rotate(-90 0 0)")
.attr("width",4)
.attr("height",4)
.append("line")
.attr("x1",0)
.attr("y1",0)
.attr("x2",0)
.attr("y2",2)
.style("stroke","black")
.style("stroke-width",1)
svg.append('pattern')
.attr("id","hatch4")
.attr("patternUnits","userSpaceOnUse")
.attr("patternTransform","rotate(00 0 0)")
.attr("width",4)
.attr("height",4)
.append("line")
.attr("x1",0)
.attr("y1",0)
.attr("x2",0)
.attr("y2",2)
.style("stroke","black")
.style("stroke-width",1)
data=d3.range(5)
svg.selectAll('rect')
.data(data)
.enter()
.append('rect')
.attr("y", 70)
.attr("x", function(d){return d*100+20})
.attr("width",50)
.attr("height",50)
.attr("fill",function(d){return "url(#hatch"+d+")"})
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment