2 nv.models.multiBarChart = function() {
4 //============================================================
5 // Public Variables with Default Settings
6 //------------------------------------------------------------
8 var multibar = nv.models.multiBar()
9 , xAxis = nv.models.axis()
10 , yAxis = nv.models.axis()
11 , legend = nv.models.legend()
12 , controls = nv.models.legend()
15 var margin = {top: 30, right: 20, bottom: 50, left: 60}
18 , color = nv.utils.defaultColor()
23 , rightAlignYAxis = false
24 , reduceXTicks = true // if false a tick will show for every data point
25 , staggerLabels = false
28 , tooltip = function(key, x, y, e, graph) {
29 return '<h3>' + key + '</h3>' +
30 '<p>' + y + ' on ' + x + '</p>'
32 , x //can be accessed via chart.xScale()
33 , y //can be accessed via chart.yScale()
34 , state = { stacked: false }
36 , noData = "No Data Available."
37 , dispatch = d3.dispatch('tooltipShow', 'tooltipHide', 'stateChange', 'changeState')
38 , controlWidth = function() { return showControls ? 180 : 0 }
39 , transitionDuration = 250
50 .tickFormat(function(d) { return d })
53 .orient((rightAlignYAxis) ? 'right' : 'left')
54 .tickFormat(d3.format(',.1f'))
57 controls.updateState(false);
58 //============================================================
61 //============================================================
63 //------------------------------------------------------------
65 var showTooltip = function(e, offsetElement) {
66 var left = e.pos[0] + ( offsetElement.offsetLeft || 0 ),
67 top = e.pos[1] + ( offsetElement.offsetTop || 0),
68 x = xAxis.tickFormat()(multibar.x()(e.point, e.pointIndex)),
69 y = yAxis.tickFormat()(multibar.y()(e.point, e.pointIndex)),
70 content = tooltip(e.series.key, x, y, e, chart);
72 nv.tooltip.show([left, top], content, e.value < 0 ? 'n' : 's', null, offsetElement);
75 //============================================================
78 function chart(selection) {
79 selection.each(function(data) {
80 var container = d3.select(this),
83 var availableWidth = (width || parseInt(container.style('width')) || 960)
84 - margin.left - margin.right,
85 availableHeight = (height || parseInt(container.style('height')) || 400)
86 - margin.top - margin.bottom;
88 chart.update = function() { container.transition().duration(transitionDuration).call(chart) };
89 chart.container = this;
92 state.disabled = data.map(function(d) { return !!d.disabled });
98 if (state[key] instanceof Array)
99 defaultState[key] = state[key].slice(0);
101 defaultState[key] = state[key];
104 //------------------------------------------------------------
105 // Display noData message if there's nothing to show.
107 if (!data || !data.length || !data.filter(function(d) { return d.values.length }).length) {
108 var noDataText = container.selectAll('.nv-noData').data([noData]);
110 noDataText.enter().append('text')
111 .attr('class', 'nvd3 nv-noData')
113 .style('text-anchor', 'middle');
116 .attr('x', margin.left + availableWidth / 2)
117 .attr('y', margin.top + availableHeight / 2)
118 .text(function(d) { return d });
122 container.selectAll('.nv-noData').remove();
125 //------------------------------------------------------------
128 //------------------------------------------------------------
131 x = multibar.xScale();
132 y = multibar.yScale();
134 //------------------------------------------------------------
137 //------------------------------------------------------------
138 // Setup containers and skeleton of chart
140 var wrap = container.selectAll('g.nv-wrap.nv-multiBarWithLegend').data([data]);
141 var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-multiBarWithLegend').append('g');
142 var g = wrap.select('g');
144 gEnter.append('g').attr('class', 'nv-x nv-axis');
145 gEnter.append('g').attr('class', 'nv-y nv-axis');
146 gEnter.append('g').attr('class', 'nv-barsWrap');
147 gEnter.append('g').attr('class', 'nv-legendWrap');
148 gEnter.append('g').attr('class', 'nv-controlsWrap');
150 //------------------------------------------------------------
153 //------------------------------------------------------------
157 legend.width(availableWidth - controlWidth());
159 if (multibar.barColor())
160 data.forEach(function(series,i) {
161 series.color = d3.rgb('#ccc').darker(i * 1.5).toString();
164 g.select('.nv-legendWrap')
168 if ( margin.top != legend.height()) {
169 margin.top = legend.height();
170 availableHeight = (height || parseInt(container.style('height')) || 400)
171 - margin.top - margin.bottom;
174 g.select('.nv-legendWrap')
175 .attr('transform', 'translate(' + controlWidth() + ',' + (-margin.top) +')');
178 //------------------------------------------------------------
181 //------------------------------------------------------------
186 { key: 'Grouped', disabled: multibar.stacked() },
187 { key: 'Stacked', disabled: !multibar.stacked() }
190 controls.width(controlWidth()).color(['#444', '#444', '#444']);
191 g.select('.nv-controlsWrap')
193 .attr('transform', 'translate(0,' + (-margin.top) +')')
197 //------------------------------------------------------------
200 wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
202 if (rightAlignYAxis) {
203 g.select(".nv-y.nv-axis")
204 .attr("transform", "translate(" + availableWidth + ",0)");
207 //------------------------------------------------------------
208 // Main Chart Component(s)
211 .disabled(data.map(function(series) { return series.disabled }))
212 .width(availableWidth)
213 .height(availableHeight)
214 .color(data.map(function(d,i) {
215 return d.color || color(d, i);
216 }).filter(function(d,i) { return !data[i].disabled }))
219 var barsWrap = g.select('.nv-barsWrap')
220 .datum(data.filter(function(d) { return !d.disabled }))
222 barsWrap.transition().call(multibar);
224 //------------------------------------------------------------
227 //------------------------------------------------------------
233 .ticks( availableWidth / 100 )
234 .tickSize(-availableHeight, 0);
236 g.select('.nv-x.nv-axis')
237 .attr('transform', 'translate(0,' + y.range()[0] + ')');
238 g.select('.nv-x.nv-axis').transition()
241 var xTicks = g.select('.nv-x.nv-axis > g').selectAll('g');
244 .selectAll('line, text')
248 var getTranslate = function(x,y) {
249 return "translate(" + x + "," + y + ")";
252 var staggerUp = 5, staggerDown = 17; //pixels to stagger by
256 .attr('transform', function(d,i,j) {
257 return getTranslate(0, (j % 2 == 0 ? staggerUp : staggerDown));
260 var totalInBetweenTicks = d3.selectAll(".nv-x.nv-axis .nv-wrap g g text")[0].length;
261 g.selectAll(".nv-x.nv-axis .nv-axisMaxMin text")
262 .attr("transform", function(d,i) {
263 return getTranslate(0, (i === 0 || totalInBetweenTicks % 2 !== 0) ? staggerDown : staggerUp);
269 .filter(function(d,i) {
270 return i % Math.ceil(data[0].values.length / (availableWidth / 100)) !== 0;
272 .selectAll('text, line')
273 .style('opacity', 0);
277 .selectAll('.tick text')
278 .attr('transform', 'rotate(' + rotateLabels + ' 0,0)')
279 .style('text-anchor', rotateLabels > 0 ? 'start' : 'end');
281 g.select('.nv-x.nv-axis').selectAll('g.nv-axisMaxMin text')
282 .style('opacity', 1);
289 .ticks( availableHeight / 36 )
290 .tickSize( -availableWidth, 0);
292 g.select('.nv-y.nv-axis').transition()
297 //------------------------------------------------------------
301 //============================================================
302 // Event Handling/Dispatching (in chart's scope)
303 //------------------------------------------------------------
305 legend.dispatch.on('stateChange', function(newState) {
307 dispatch.stateChange(state);
311 controls.dispatch.on('legendClick', function(d,i) {
312 if (!d.disabled) return;
313 controlsData = controlsData.map(function(s) {
321 multibar.stacked(false);
324 multibar.stacked(true);
328 state.stacked = multibar.stacked();
329 dispatch.stateChange(state);
334 dispatch.on('tooltipShow', function(e) {
335 if (tooltips) showTooltip(e, that.parentNode)
338 // Update chart from a state object passed to event handler
339 dispatch.on('changeState', function(e) {
341 if (typeof e.disabled !== 'undefined') {
342 data.forEach(function(series,i) {
343 series.disabled = e.disabled[i];
346 state.disabled = e.disabled;
349 if (typeof e.stacked !== 'undefined') {
350 multibar.stacked(e.stacked);
351 state.stacked = e.stacked;
357 //============================================================
366 //============================================================
367 // Event Handling/Dispatching (out of chart's scope)
368 //------------------------------------------------------------
370 multibar.dispatch.on('elementMouseover.tooltip', function(e) {
371 e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
372 dispatch.tooltipShow(e);
375 multibar.dispatch.on('elementMouseout.tooltip', function(e) {
376 dispatch.tooltipHide(e);
378 dispatch.on('tooltipHide', function() {
379 if (tooltips) nv.tooltip.cleanup();
382 //============================================================
385 //============================================================
386 // Expose Public Variables
387 //------------------------------------------------------------
389 // expose chart's sub-components
390 chart.dispatch = dispatch;
391 chart.multibar = multibar;
392 chart.legend = legend;
396 d3.rebind(chart, multibar, 'x', 'y', 'xDomain', 'yDomain', 'xRange', 'yRange', 'forceX', 'forceY', 'clipEdge',
397 'id', 'stacked', 'stackOffset', 'delay', 'barColor','groupSpacing');
399 chart.options = nv.utils.optionsFunc.bind(chart);
401 chart.margin = function(_) {
402 if (!arguments.length) return margin;
403 margin.top = typeof _.top != 'undefined' ? _.top : margin.top;
404 margin.right = typeof _.right != 'undefined' ? _.right : margin.right;
405 margin.bottom = typeof _.bottom != 'undefined' ? _.bottom : margin.bottom;
406 margin.left = typeof _.left != 'undefined' ? _.left : margin.left;
410 chart.width = function(_) {
411 if (!arguments.length) return width;
416 chart.height = function(_) {
417 if (!arguments.length) return height;
422 chart.color = function(_) {
423 if (!arguments.length) return color;
424 color = nv.utils.getColor(_);
429 chart.showControls = function(_) {
430 if (!arguments.length) return showControls;
435 chart.showLegend = function(_) {
436 if (!arguments.length) return showLegend;
441 chart.showXAxis = function(_) {
442 if (!arguments.length) return showXAxis;
447 chart.showYAxis = function(_) {
448 if (!arguments.length) return showYAxis;
453 chart.rightAlignYAxis = function(_) {
454 if(!arguments.length) return rightAlignYAxis;
456 yAxis.orient( (_) ? 'right' : 'left');
460 chart.reduceXTicks= function(_) {
461 if (!arguments.length) return reduceXTicks;
466 chart.rotateLabels = function(_) {
467 if (!arguments.length) return rotateLabels;
472 chart.staggerLabels = function(_) {
473 if (!arguments.length) return staggerLabels;
478 chart.tooltip = function(_) {
479 if (!arguments.length) return tooltip;
484 chart.tooltips = function(_) {
485 if (!arguments.length) return tooltips;
490 chart.tooltipContent = function(_) {
491 if (!arguments.length) return tooltip;
496 chart.state = function(_) {
497 if (!arguments.length) return state;
502 chart.defaultState = function(_) {
503 if (!arguments.length) return defaultState;
508 chart.noData = function(_) {
509 if (!arguments.length) return noData;
514 chart.transitionDuration = function(_) {
515 if (!arguments.length) return transitionDuration;
516 transitionDuration = _;
520 //============================================================