Skip to content

Instantly share code, notes, and snippets.

@ruiyang123
Created January 17, 2020 13:58
Show Gist options
  • Save ruiyang123/fe9170acc8b3acf7f6bc502642cc6ff7 to your computer and use it in GitHub Desktop.
Save ruiyang123/fe9170acc8b3acf7f6bc502642cc6ff7 to your computer and use it in GitHub Desktop.
fresh block
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 margin = {top: 20,right: 10,bottom: 20,left: 10};
var width = 480 - margin.left - margin.right;
var height = 250 - margin.top - margin.bottom;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
var data = d3.range(30).map(Math.random);
var x = d3.scaleBand()
.domain(d3.range(data.length))
.range([margin.left,width+margin.left]);
var y = d3.scaleLinear()
.domain([0,1])
.range([margin.top,height + margin.top]);
svg.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("y", function(d,i){return height+margin.top+- y(d);})
.attr("x", function(d,i){return x(i);})
.attr("width",x.bandwidth())
.attr("height",function(d,i){return y(d) - margin.top;})
.style("fill","white")
.style("stroke","black");
var line = d3.line()
.curve(d3.curveMonotoneX)
.x(function(d,i){return x(i)+x.bandwidth()/2;})
.y(function(d,i){return height+margin.top- y(d);});
console.log(d3.max(data));
svg.selectAll("path").data([data]).enter()
.append("path").attr("d",line).style("fill","none").style("stroke","black");
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment