Skip to content

Instantly share code, notes, and snippets.

@curiousYi
Last active August 15, 2018 22:25
Show Gist options
  • Save curiousYi/3f9f8d2aeae39a338571232ed42d4f2e to your computer and use it in GitHub Desktop.
Save curiousYi/3f9f8d2aeae39a338571232ed42d4f2e to your computer and use it in GitHub Desktop.
D3 Playground
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:50px;right:50px;bottom:0;left:50px; }
svg {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<svg>
</svg>
<script>
// Feel free to change or delete any of the code you see in this editor!
const data = [200, 150, 200, 300, 325, 200],
rectWidth = 50,
height = 300;
const svg = d3.select('svg');
const currentSelection = 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', 'blue')
.attr('stroke', '#fff')
.attr('transform', 'translate(40,20)');
//Addressing the issue of making things fit on the screen
//eg super small values or large humonguous values that go off-screen
const extent = d3.extent(data, d => d);
console.log(extent);
const yScale = d3.scaleLinear()
.domain(extent) //input
.range([height, 0]) //output
//Now I need to do scale axis
const yAxis = d3.axisLeft()
.scale(yScale);
d3.select('svg')
.append('g')
.attr('transform', 'translate(40,20)')
.call(yAxis);
/*
const data = [100, 200, 250, 125, 175]
const rectWidth = 50;
const height = 300;
d3.selectAll('rect')
.data(data)
.attr('x', (d, i) => i * rectWidth)
.attr('y', d => height - d)
.attr('width', rectWidth)
.attr('height', d => d)
.attr('fill', 'blue')
.attr('stroke', '#fff');
*/
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment