|
var horizBarchart = { |
|
init: function(opts){ |
|
this.data = opts.data; |
|
this.dataX = this.dataX || opts.dataX; |
|
this.dataY = this.dataY || opts.dataY; |
|
this.element = opts.element; |
|
this.draw(); |
|
}, |
|
draw: function(){ |
|
this.width = this.element.offsetWidth; |
|
this.height = this.data.length * 60; |
|
this.margin = { |
|
top:40, |
|
left:0, |
|
right:0, |
|
bottom:0 |
|
}; |
|
|
|
this.element.innerHTML = ''; |
|
|
|
var svg = d3.select(this.element).append('svg'); |
|
svg.attr('width', (this.width + this.margin.left + this.margin.right)); |
|
svg.attr('height', (this.height + this.margin.top + this.margin.bottom)); |
|
|
|
this.plot = svg.append('g') |
|
.attr('transform',`translate(${this.margin.left},${this.margin.top})`); |
|
|
|
this.createScales(); |
|
this.addAxes(); |
|
this.addGrey(); |
|
this.addRect(); |
|
this.addLabels(); |
|
}, |
|
createScales: function() { |
|
var self = this; |
|
var yscaleDomain = this.data.map(function(d) { return d[self.dataY]; }); |
|
var valArray = this.data.map(function(d){ |
|
return d[self.dataX]; |
|
}); |
|
this.yscale = d3.scaleBand() |
|
.rangeRound([0,this.height]) |
|
.domain(yscaleDomain) |
|
.paddingInner(0.5); |
|
|
|
this.xscale = d3.scaleLinear() |
|
.domain([0, d3.max(valArray)]) |
|
.range([0,this.width]); |
|
}, |
|
addAxes: function(){ |
|
var self = this; |
|
var yaxis = d3.axisLeft(this.yscale); |
|
var xaxis = d3.axisBottom() |
|
.scale(this.xscale) |
|
.tickSize(this.height); |
|
|
|
this.plot.append("g.y-axis").call(yaxis); |
|
this.plot.append("g.x-axis").call(xaxis); |
|
}, |
|
addRect: function() { |
|
var self = this; |
|
this.gRects = this.plot.selectAll(".g-comp-rect") |
|
.data(this.data) |
|
.enter() |
|
.append("g.g-comp-rect") |
|
.append("rect.comp-rect") |
|
.attr("y",function(d){ |
|
return self.yscale(d[self.dataY]); |
|
}) |
|
.attr("x",0) |
|
.attr("height",self.yscale.bandwidth()) |
|
.attr("width",0) |
|
.style('fill', this.lineColor || '#0080b4') |
|
.transition().delay(function(d, i) { return i * 120; }) |
|
.attr("width", function(d){ |
|
return self.xscale(d[self.dataX]); |
|
}); |
|
}, |
|
addGrey: function(){ |
|
var self = this; |
|
this.plot.selectAll(".g-comp-rect") |
|
.data(this.data) |
|
.enter() |
|
.append("rect.bg-bar") |
|
.attr("y", function(d){ |
|
return self.yscale(d[self.dataY]); |
|
}) |
|
.attr("x",0) |
|
.attr("height",this.yscale.bandwidth()) |
|
.attr("width", this.width) |
|
.style("fill","#efefef"); |
|
}, |
|
addLabels: function(){ |
|
var self = this; |
|
this.rectLabels = d3.selectAll(".g-comp-rect") |
|
.append("text.bar-label") |
|
.attr("y", function(d) { return self.yscale(d[self.dataY]) + self.yscale.bandwidth()/2;}) |
|
.attr("x", function(d) { |
|
if (d[self.dataX] < 3) { |
|
return (self.xscale(d[self.dataX]) + 5); |
|
} else { |
|
return (self.xscale(d[self.dataX]) - 10); |
|
} |
|
}) |
|
.attr("dy", "5px") |
|
.style("text-anchor",function(d){ |
|
if (d[self.dataX] < 3) { |
|
return "start"; |
|
} else { |
|
return "end"; |
|
} |
|
}) |
|
.style("fill",function(d){ |
|
if (d[self.dataX] < 3) { |
|
return "#45555f"; |
|
} else { |
|
return "#ffffff"; |
|
} |
|
}) |
|
.text(function(d) { |
|
return "$" + d[self.dataX] + " Billion"; |
|
}); |
|
this.plot.selectAll(".domain").style("display","none"); |
|
yText = this.plot.selectAll(".y-axis .tick text"), |
|
xText = this.plot.selectAll(".x-axis .tick text"), |
|
yTick = this.plot.selectAll(".y-axis .tick"), |
|
xTick = this.plot.selectAll(".x-axis .tick"), |
|
yLine = this.plot.selectAll(".y-axis .tick line"), |
|
xLine = this.plot.selectAll(".x-axis .tick line"); |
|
yText |
|
.style("text-anchor","start") |
|
.attr("x",0) |
|
.attr("y", function(d) { return -self.yscale.bandwidth() + 6; }); |
|
yLine |
|
.style("display","none"); |
|
xTick |
|
.style("display","none"); |
|
}, |
|
// the following are "public methods" |
|
// which can be used by code outside of this file |
|
setColor:function(newColor) { |
|
this.plot.selectAll('rect.comp-rect') |
|
.style('fill', newColor); |
|
|
|
// store for use when redrawing |
|
this.lineColor = newColor; |
|
}, |
|
setDataX:function(val) { |
|
this.dataX = val; |
|
this.draw(); |
|
}, |
|
setDataY:function(val) { |
|
this.dataY = val; |
|
this.draw(); |
|
}, |
|
setData: function(newData) { |
|
this.data = newData; |
|
this.draw(); |
|
} |
|
|
|
} |