Skip to content

Instantly share code, notes, and snippets.

@RalucaNicola
Created November 21, 2019 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RalucaNicola/b93337de83290e73168c6d6e7e5b5def to your computer and use it in GitHub Desktop.
Save RalucaNicola/b93337de83290e73168c6d6e7e5b5def to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
#g1 {
color: #e83a66;
}
#g2 {
color: #3a7de8;
}
</style>
<body>
<h1 id="g1"></h1>
<h1 id="g2"></h1>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = { top: 10, right: 30, bottom: 30, left: 30 },
width = 960 - margin.left - margin.right,
height = 600 - margin.top - margin.bottom;
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// generate random data
var data = [];
for (i = 0; i < 100; i++) {
data.push({ g1: Math.floor(Math.random() * 20), g2: Math.floor(Math.random() * 30) })
}
var municipality = svg.selectAll("g")
.data(data)
.enter()
.append("g");
municipality.append("circle")
.attr("cy", function (d, i) { return Math.floor(i / 10) * 50 + 30; })
.attr("cx", function (d, i) { return i % 10 * 50 + 30; })
.attr("r", function (d) { const radius = d.g1 > d.g2 ? d.g1 : d.g2; return radius; })
.style("fill", function (d) { const color = d.g1 > d.g2 ? "#e83a66" : "#3a7de8"; return color; });
municipality.append("circle")
.attr("cy", function (d, i) { return Math.floor(i / 10) * 50 + 30; })
.attr("cx", function (d, i) { return i % 10 * 50 + 30; })
.attr("r", function (d) { const radius = d.g1 > d.g2 ? d.g2 : d.g1; return radius; })
.style("fill", function (d) { return "#eeeeee"; });
sumG1 = data.reduce(function (s, e) { return s + e.g1 }, 0);
sumG2 = data.reduce(function (s, e) { return s + e.g2 }, 0);
g1.innerHTML = sumG1 + " votes";
g2.innerHTML = sumG2 + " votes";
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment