Skip to content

Instantly share code, notes, and snippets.

@eduardopoleo
Created July 26, 2016 00:09
Show Gist options
  • Select an option

  • Save eduardopoleo/f994f9ae27bec79393fccf7082a93afa to your computer and use it in GitHub Desktop.

Select an option

Save eduardopoleo/f994f9ae27bec79393fccf7082a93afa to your computer and use it in GitHub Desktop.
//Sets plot's dimensions and other relevant values as paddings
//and margins
var w = 1000
var h = 1250
var xPadding = 10
var yPadding = 20
var xMargin = 200
var yMargin = 0
var rectMargin = 5
//Caculates the scales used for x, y and yAxis
function calculateScales(dataSet) {
var xScale = d3.scale.linear()
.domain([0, 246000])
.range([xPadding, w - xMargin - xPadding])
//This scale ensures that the rectangles will be placed in
//the right spot along the y axis
var yScale = d3.scale.ordinal()
.domain(d3.range(dataSet.length))
.rangeRoundBands([0, (h - 1.2 * yPadding )], 0.05)
//This is an special scale for the yaxis which uses ordinal
//values corresponding to the name of the universities
var yAxisScale = d3.scale.ordinal()
.domain(dataSet.map(function (d) {
return d.university
}))
.rangeRoundBands([0, (h - 1.2 * yPadding )], 0.05)
return [xScale, yScale, yAxisScale]
}
//Calculates the plot axes using the previously defined scale
//values
function calculateAxes(xScale, yAxisScale) {
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(5)
var yAxis = d3.svg.axis()
.scale(yAxisScale)
.orient("left")
return [xAxis, yAxis]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment