Skip to content

Instantly share code, notes, and snippets.

@benlhy
Last active March 27, 2020 13:43
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 benlhy/9de4d25dfbe1e142acf346faed4c88a4 to your computer and use it in GitHub Desktop.
Save benlhy/9de4d25dfbe1e142acf346faed4c88a4 to your computer and use it in GitHub Desktop.
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; }
svg {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<svg></svg>
<script>
var data = [110,220,150,240,120]
var rectWidth = 100;
var height = 300;
// Feel free to change or delete any of the code you see in this editor!
var extent = d3.extent(data,d => d)
//console.log(extent)
var yScale = d3.scaleLinear().
domain(extent)
.range([height,0])
//console.log(yScale)
var yAxis = d3.axisLeft()
.scale(yScale);
var svg = d3.select('svg')
.append('g')
.attr('transform','translate(40,20)')
.call(yAxis);
//console.log(svg.node())
var text = svg.selectAll('text')
.attr('fill', function(d) {return d > 200 ? 'red' : 'blue'})
var enter = svg.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("x", (d,i)=> i*rectWidth)
.attr("y", d => height - d)
.attr("width",rectWidth)
.attr("height", d => d)
.attr("fill",function(d) {if (d<200) return 'red'; return 'blue'})
.attr("stroke","#fff")
console.log(enter.nodes())
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment