Created
May 31, 2019 08:42
-
-
Save Akhigbe-E/6dc6b66fe3184c6564627b0622c867b1 to your computer and use it in GitHub Desktop.
Scaling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let dataset = [30,12,103,170,45,95,190,75]; | |
let svgHeight= 200, svgWidth = 500, barSpacing = 5; | |
let totalBarWidth = (svgWidth/dataset.length); | |
let barWidth = totalBarWidth-barSpacing; | |
let svg = d3.select('svg') | |
.attr('width', svgWidth) | |
.attr('height', svgHeight); | |
let scale = d3.scaleLinear() | |
.domain([0,d3.max(dataset)]) | |
.range([0,svgHeight]) | |
let barchart = svg.selectAll('rect') | |
.data(dataset) | |
.enter() | |
.append('rect') | |
.attr('y',d=>svgHeight-scale(d)) | |
.attr('height',d=>scale(d)) | |
.attr('width',barWidth) | |
.attr('transform', (d,i)=>{ | |
let translate = [totalBarWidth*i, 0]; | |
return `translate(${translate})`; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment