2012-03-20 23:43:21 +00:00
|
|
|
|
|
|
|
nv.models.bar = function() {
|
2012-05-10 22:13:31 +00:00
|
|
|
var margin = {top: 20, right: 10, bottom: 80, left: 60},
|
2012-03-20 23:43:21 +00:00
|
|
|
width = 960,
|
|
|
|
height = 500,
|
2012-05-09 19:08:25 +00:00
|
|
|
animate = 500,
|
|
|
|
label ='label',
|
2012-05-10 22:13:31 +00:00
|
|
|
rotatedLabel = true,
|
|
|
|
showLabels = true,
|
2012-05-10 16:10:15 +00:00
|
|
|
id = Math.floor(Math.random() * 10000), //Create semi-unique ID in case user doesn't select one
|
2012-05-10 22:13:31 +00:00
|
|
|
color = d3.scale.category20(),
|
|
|
|
field ='y',
|
|
|
|
title = '';
|
2012-03-20 23:43:21 +00:00
|
|
|
|
|
|
|
var x = d3.scale.ordinal(),
|
|
|
|
y = d3.scale.linear(),
|
2012-05-09 19:08:25 +00:00
|
|
|
xAxis = d3.svg.axis().scale(x).orient('bottom'),
|
2012-05-10 16:10:15 +00:00
|
|
|
yAxis = d3.svg.axis().scale(y).orient('left'),
|
2012-05-10 22:13:31 +00:00
|
|
|
dispatch = d3.dispatch('chartClick', 'elementClick', 'elementDblClick', 'tooltipShow', 'tooltipHide');
|
2012-03-20 23:43:21 +00:00
|
|
|
|
|
|
|
|
2012-05-10 22:13:31 +00:00
|
|
|
function chart(selection) {
|
2012-05-10 16:10:15 +00:00
|
|
|
selection.each(function(data) {
|
|
|
|
x .domain(data.map(function(d,i) { return d[label]; }))
|
2012-03-20 23:43:21 +00:00
|
|
|
.rangeRoundBands([0, width - margin.left - margin.right], .1);
|
|
|
|
|
2012-05-10 22:13:31 +00:00
|
|
|
var min = d3.min(data, function(d) { return d[field] });
|
|
|
|
var max = d3.max(data, function(d) { return d[field] });
|
2012-05-10 16:10:15 +00:00
|
|
|
var x0 = Math.max(-min, max);
|
|
|
|
var x1 = -x0;
|
|
|
|
|
2012-05-10 22:13:31 +00:00
|
|
|
// If we have no negative values, then lets stack this with just positive bars
|
|
|
|
if (min >= 0) x1 = 0;
|
|
|
|
|
|
|
|
y .domain([x1, x0])
|
2012-05-10 16:10:15 +00:00
|
|
|
.range([height - margin.top - margin.bottom, 0])
|
|
|
|
.nice();
|
2012-03-20 23:43:21 +00:00
|
|
|
|
|
|
|
xAxis.ticks( width / 100 );
|
|
|
|
yAxis.ticks( height / 36 ).tickSize(-(width - margin.right - margin.left), 0);
|
|
|
|
|
2012-05-10 16:10:15 +00:00
|
|
|
var parent = d3.select(this)
|
|
|
|
.on("click", function(d,i) {
|
|
|
|
dispatch.chartClick({
|
|
|
|
data: d,
|
|
|
|
index: i,
|
|
|
|
pos: d3.event,
|
|
|
|
id: id
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-05-11 15:38:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
var wrap = parent.selectAll('g.wrap').data([data]);
|
|
|
|
var gEnter = wrap.enter();
|
|
|
|
gEnter.append("text")
|
2012-05-10 22:13:31 +00:00
|
|
|
.attr("class", "title")
|
|
|
|
.attr("dy", ".91em")
|
|
|
|
.attr("text-anchor", "start")
|
|
|
|
.text(title);
|
2012-05-11 15:38:19 +00:00
|
|
|
gEnter = gEnter.append('g').attr('class', 'wrap').attr('id','wrap-'+id).append('g');
|
2012-03-20 23:43:21 +00:00
|
|
|
|
|
|
|
|
2012-05-10 22:13:31 +00:00
|
|
|
|
2012-03-20 23:43:21 +00:00
|
|
|
gEnter.append('g').attr('class', 'x axis');
|
|
|
|
gEnter.append('g').attr('class', 'y axis');
|
|
|
|
gEnter.append('g').attr('class', 'bars');
|
|
|
|
|
|
|
|
|
|
|
|
wrap.attr('width', width)
|
|
|
|
.attr('height', height);
|
|
|
|
|
|
|
|
var g = wrap.select('g')
|
|
|
|
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
|
|
|
|
|
|
|
|
|
|
|
|
var bars = wrap.select('.bars').selectAll('.bar')
|
|
|
|
.data(function(d) { return d });
|
|
|
|
|
2012-05-10 16:10:15 +00:00
|
|
|
bars.exit().remove();
|
2012-03-20 23:43:21 +00:00
|
|
|
|
|
|
|
var barsEnter = bars.enter().append('g')
|
2012-05-10 16:10:15 +00:00
|
|
|
.attr("class", function(d, i) { return d[field] < 0 ? "bar negative" : "bar positive"; })
|
|
|
|
.on('mouseover', function(d,i){
|
|
|
|
d3.select(this).classed('hover', true);
|
|
|
|
dispatch.tooltipShow({
|
|
|
|
label: d[label],
|
|
|
|
value: d[field],
|
|
|
|
data: d,
|
|
|
|
index: i,
|
|
|
|
// TODO: Calculate the center to the bar
|
|
|
|
pos: [d3.event.pageX, d3.event.pageY],
|
|
|
|
id: id
|
|
|
|
});
|
|
|
|
|
|
|
|
})
|
|
|
|
.on('mouseout', function(d,i){
|
|
|
|
d3.select(this).classed('hover', false);
|
|
|
|
dispatch.tooltipHide({
|
|
|
|
label: d[label],
|
|
|
|
value: d[field],
|
|
|
|
data: d,
|
|
|
|
index: i,
|
|
|
|
id: id
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.on('click', function(d,i) {
|
|
|
|
dispatch.elementClick({
|
|
|
|
label: d[label],
|
|
|
|
value: d[field],
|
|
|
|
data: d,
|
|
|
|
index: i,
|
|
|
|
pos: d3.event,
|
|
|
|
id: id
|
|
|
|
});
|
|
|
|
d3.event.stopPropagation();
|
2012-05-10 22:13:31 +00:00
|
|
|
})
|
|
|
|
.on('dblclick', function(d,i) {
|
|
|
|
dispatch.elementDblClick({
|
|
|
|
label: d.data[label],
|
|
|
|
value: d.data[field],
|
|
|
|
data: d.data,
|
|
|
|
index: i,
|
|
|
|
pos: d3.event,
|
|
|
|
id: id
|
|
|
|
});
|
|
|
|
d3.event.stopPropagation();
|
2012-05-10 16:10:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
barsEnter.append('rect')
|
2012-05-10 22:13:31 +00:00
|
|
|
.attr('y', function(d) { return y(0); })
|
|
|
|
.attr("fill", function(d, i) { return color(i); });
|
|
|
|
|
|
|
|
|
2012-05-10 16:10:15 +00:00
|
|
|
barsEnter.append('text')
|
2012-03-20 23:43:21 +00:00
|
|
|
.attr('text-anchor', 'middle')
|
|
|
|
.attr('dy', '-4px');
|
|
|
|
|
|
|
|
|
|
|
|
bars
|
2012-05-10 16:10:15 +00:00
|
|
|
.attr('transform', function(d,i) { return 'translate(' + x(d[label]) + ',0)'; });
|
|
|
|
|
2012-03-20 23:43:21 +00:00
|
|
|
bars.selectAll('rect')
|
|
|
|
.order()
|
|
|
|
.attr('width', x.rangeBand )
|
2012-05-10 16:10:15 +00:00
|
|
|
.transition()
|
2012-03-20 23:43:21 +00:00
|
|
|
.duration(animate)
|
|
|
|
.attr('x', 0 )
|
2012-05-10 16:10:15 +00:00
|
|
|
.attr('y', function(d) { return y(Math.max(0, d[field])) })
|
|
|
|
.attr('height', function(d) { return Math.abs(y(d[field]) - y(0)); });
|
|
|
|
// function(d) { return y.range()[0] - y(d[field]) });
|
|
|
|
|
2012-05-10 22:13:31 +00:00
|
|
|
/* if (hasLabel) {
|
2012-05-10 16:10:15 +00:00
|
|
|
bars.selectAll('text')
|
2012-03-20 23:43:21 +00:00
|
|
|
.attr('x', 0 )
|
2012-05-09 19:08:25 +00:00
|
|
|
.attr('y', function(d) { return y(d[field]) })
|
2012-03-20 23:43:21 +00:00
|
|
|
.attr('dx', x.rangeBand() / 2)
|
2012-05-09 19:08:25 +00:00
|
|
|
.text(function(d) { return d[field] });
|
2012-05-10 22:13:31 +00:00
|
|
|
} */
|
2012-03-20 23:43:21 +00:00
|
|
|
|
|
|
|
g.select('.x.axis')
|
|
|
|
.attr('transform', 'translate(0,' + y.range()[0] + ')')
|
|
|
|
.call(xAxis);
|
|
|
|
|
2012-05-10 22:13:31 +00:00
|
|
|
|
|
|
|
if (rotatedLabel) {
|
|
|
|
g.select('.x.axis').selectAll('text').attr('text-anchor','start').attr("transform", function(d) {
|
|
|
|
return "rotate(35)translate(" + this.getBBox().height/2 + "," + '0' + ")";
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (!showLabels) {
|
|
|
|
g.select('.x.axis').selectAll('text').attr('fill', 'rgba(0,0,0,0)');
|
|
|
|
g.select('.x.axis').selectAll('line').attr('style', 'opacity: 0');
|
|
|
|
}
|
|
|
|
/*else {
|
|
|
|
g.select('.x.axis').selectAll('text').attr('fill', 'rgba(0,0,0,1)');
|
|
|
|
g.select('.x.axis').selectAll('line').attr('style', 'opacity: 1');
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
g.select('.y.axis')
|
2012-03-20 23:43:21 +00:00
|
|
|
.call(yAxis);
|
|
|
|
});
|
|
|
|
|
|
|
|
return chart;
|
|
|
|
}
|
|
|
|
|
|
|
|
chart.margin = function(_) {
|
|
|
|
if (!arguments.length) return margin;
|
|
|
|
margin = _;
|
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
|
|
|
chart.width = function(_) {
|
|
|
|
if (!arguments.length) return width;
|
|
|
|
if (margin.left + margin.right + 20 > _)
|
2012-05-10 16:10:15 +00:00
|
|
|
width = margin.left + margin.right + 20; // Min width
|
2012-03-20 23:43:21 +00:00
|
|
|
else
|
|
|
|
width = _;
|
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
|
|
|
chart.height = function(_) {
|
|
|
|
if (!arguments.length) return height;
|
|
|
|
if (margin.top + margin.bottom + 20 > _)
|
2012-05-10 16:10:15 +00:00
|
|
|
height = margin.top + margin.bottom + 20; // Min height
|
2012-03-20 23:43:21 +00:00
|
|
|
else
|
|
|
|
height = _;
|
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
|
|
|
chart.animate = function(_) {
|
|
|
|
if (!arguments.length) return animate;
|
|
|
|
animate = _;
|
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
2012-05-09 19:08:25 +00:00
|
|
|
chart.labelField = function(_) {
|
|
|
|
if (!arguments.length) return (label);
|
|
|
|
label = _;
|
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
|
|
|
chart.dataField = function(_) {
|
|
|
|
if (!arguments.length) return (field);
|
|
|
|
field = _;
|
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
2012-05-10 16:10:15 +00:00
|
|
|
chart.id = function(_) {
|
|
|
|
if (!arguments.length) return id;
|
|
|
|
id = _;
|
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
2012-05-10 22:13:31 +00:00
|
|
|
chart.rotatedLabel = function(_) {
|
|
|
|
if (!arguments.length) return rotatedLabel;
|
|
|
|
rotatedLabel = _;
|
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
|
|
|
chart.showLabels = function(_) {
|
|
|
|
if (!arguments.length) return (showLabels);
|
|
|
|
showLabels = _;
|
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
|
|
|
chart.title = function(_) {
|
|
|
|
if (!arguments.length) return (title);
|
|
|
|
title = _;
|
|
|
|
return chart;
|
|
|
|
};
|
2012-05-10 16:10:15 +00:00
|
|
|
|
2012-05-10 22:13:31 +00:00
|
|
|
chart.xaxis = {};
|
2012-03-20 23:43:21 +00:00
|
|
|
// Expose the x-axis' tickFormat method.
|
|
|
|
d3.rebind(chart.xaxis, xAxis, 'tickFormat');
|
|
|
|
|
|
|
|
chart.yaxis = {};
|
|
|
|
// Expose the y-axis' tickFormat method.
|
|
|
|
d3.rebind(chart.yaxis, yAxis, 'tickFormat');
|
|
|
|
|
2012-05-10 16:10:15 +00:00
|
|
|
chart.dispatch = dispatch;
|
|
|
|
|
2012-03-20 23:43:21 +00:00
|
|
|
return chart;
|
|
|
|
}
|