Skip to content

Instantly share code, notes, and snippets.

@Akhigbe-E
Created May 31, 2019 08:42
Show Gist options
  • Save Akhigbe-E/6dc6b66fe3184c6564627b0622c867b1 to your computer and use it in GitHub Desktop.
Save Akhigbe-E/6dc6b66fe3184c6564627b0622c867b1 to your computer and use it in GitHub Desktop.
Scaling
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