Skip to content

Instantly share code, notes, and snippets.

@boriscy
Created June 11, 2014 14:08
Show Gist options
  • Save boriscy/a7e5742860fa1151173b to your computer and use it in GitHub Desktop.
Save boriscy/a7e5742860fa1151173b to your computer and use it in GitHub Desktop.
blue = '#16A9E3'
green = '#7FC06B'
width = 300
height = 150
space = 40
x = d3.scale.ordinal()
.rangeRoundBands([0, width], 10)
y = d3.scale.linear()
.rangeRound([height, 0])
xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(d3.format(".2s"))
space = 40
data = [
{tot: 30, permits: 29, completions: 1, operator: 'Oxy'},
{tot: 32, permits: 25, completions: 7, operator: 'Chevron'}
{tot: 29, permits: 25, completions: 4, operator: 'Tech'},
{tot: 32, permits: 20, completions: 12, operator: 'Omega'}
]
svg = d3.select('#chart')
.append('svg');
# Permits
svg.selectAll('.permits')
.data(data)
.enter()
.append('rect')
.attr({
x: 10
y: (d, index) ->
space * index
width: (d) ->
d.permits * 10
height: 30
class: 'permits'
fill: blue
})
# Completions
svg.selectAll('.completions')
.data(data)
.enter()
.append('rect')
.attr({
x: (d) ->
10 + d.permits * 10
y: (d, index) ->
console.log 'y'
space * index
width: (d) ->
console.log 'comp', d.completions
d.completions * 10
height: 30
fill: green,
attr: 'completions'
})
svg.selectAll('text')
.data(data)
.enter()
.append("text")
.text((d) -> d.operator)
.attr({
x: 14
y: (d, index) ->
space + index * space - 20
fill: 'white'
})
# X axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
# Y axis
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(10, 0)")
.call(yAxis)
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<script src="http://d3js.org/d3.v3.min.js"></script>
<style>
.axis path,
.axis line {
fill: none;
shape-rendering: crispEdges;
}
</style>
</head>
<body>
<div id="chart"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment