Skip to content

Instantly share code, notes, and snippets.

@rafa79
Created July 14, 2018 12:10
Show Gist options
  • Save rafa79/c9887a08eb2b5ca306194112a439243d to your computer and use it in GitHub Desktop.
Save rafa79/c9887a08eb2b5ca306194112a439243d to your computer and use it in GitHub Desktop.
Pie Chart
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>
<h1>Temperaturas Nueva York</h1>
<script>
//Altura y anchura con márgenes
const totalWidth = 800;
const totalHeight = 300;
var margin = {top: 20, right: 20, bottom: 50, left: 50};
var width = totalWidth - margin.left - margin.right;
var height = totalHeight - margin.top - margin.bottom;
var svg = d3.select("body")
.append("svg")
.attr("width", totalWidth)
.attr("height", totalHeight)
var chart = svg.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
const datos = [1, 1, 7, 13, 15, 28, 50]
var colorScale = d3.scaleOrdinal(d3.schemeCategory10)
var pies = d3.pie()(datos)
var arc = d3.arc()
.innerRadius(0)
.outerRadius(120)
.startAngle(d => d.startAngle)
.endAngle(d => d.endAngle)
chart.append("g")
.attr("transform", `translate(${width/2}, ${height/2})`)
.selectAll("path")
.data(pies)
.enter().append("path")
.attr("d", arc)
.attr("fill", (d,i) => colorScale(i))
.attr("stroke", "#000000")
.attr("stroke-width", 0.2)
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment