Skip to content

Instantly share code, notes, and snippets.

@e1jo
Last active January 3, 2020 14:07
Show Gist options
  • Save e1jo/2584fb6e17b97bd36683c8fb8d001bf3 to your computer and use it in GitHub Desktop.
Save e1jo/2584fb6e17b97bd36683c8fb8d001bf3 to your computer and use it in GitHub Desktop.
d3-test-1
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body {
margin:0;
padding: 0;
height: 100vh;
background-color: #009688b8;
display: flex;
align-items: center;
}
.test {
flex: 1 1 100%;
height: 400px;
background-color: #838f04b8;
/* padding: 50px; */
}
svg {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<section class="test">
<svg>
<rect />
<rect />
<rect />
<rect />
<rect />
<rect />
<rect />
<rect />
<rect />
<rect />
</svg>
</section>
<script>
const data = [100,250,175,200,150,50,40,30,64,295],
rW = 960 / data.length, //960px - width window (stroke - not to consider !!!)
rH = 300;
const rect = d3.selectAll('rect') //iteration by each rect
.data(data) // bind data to iteration rectangle
.attr('x', (d,i) => i * rW) //i -> index iteration
.attr('y', (d) => rH - d) //d -> data value selected itm
.attr("width", rW) // calc fixed val -> wide / quantity colums
.attr("height", (d) => d) // d
.attr('stroke', '#ff19eb')
.attr('fill', 'blue');
console.info(rect);
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment