Skip to content

Instantly share code, notes, and snippets.

@adelaide01
Created March 28, 2020 19:07
Show Gist options
  • Save adelaide01/bd3a10e2eac2d3be2cd2d17dddae21b5 to your computer and use it in GitHub Desktop.
Save adelaide01/bd3a10e2eac2d3be2cd2d17dddae21b5 to your computer and use it in GitHub Desktop.
d3 test
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v5.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<script>
let svg = d3.select("body").append("svg")
.attr("width", 500)
.attr("height", 200);
var shapes = [{shape: "circle",y: 40, fill: "darkblue"},{shape:"square", y: 35},{y:40}]
svg.selectAll()
.data(shapes)
.enter()
.append(shapemaker);
function shapemaker(options = {}) {
let svg = document.createElementNS(d3.namespaces.svg, "svg")
var shape;
if (options.shape == "circle") {
shape = d3.select(svg).append("circle")
.attr("cx", options.x ? options.x : 50)
.attr("cy", options.y ? options.y : 50)
.attr("r", options.r ? options.r : 10)
.attr("fill", options.fill ? options.fill : "steelblue" )
}
else if (options.shape == "square") {
shape = d3.select(svg).append("rect")
.attr("x", options.x ? options.x : 100)
.attr("y", options.y ? options.y : 50)
.attr("width", options.width ? options.size : 10)
.attr("height", options.width ? options.size : 10)
.attr("fill", options.fill ? options.fill : "orange" )
}
else {
let x = options.x ? options.x : 150, y = options.y ? options.y : 50;
shape = d3.select(svg).append("path")
.attr("d", d3.symbol().type(d3.symbolStar).size(options.size ? options.size : 100))
.attr("transform","translate("+[x,y]+")")
.attr("fill", options.fill ? options.fill : "crimson")
}
return shape.node();
}
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment