1 nv.models.multiBarTimeSeriesChart = function() {
3 //============================================================
4 // Public Variables with Default Settings
5 //------------------------------------------------------------
7 var multibar = nv.models.multiBarTimeSeries()
8 , xAxis = nv.models.axis()
9 , yAxis = nv.models.axis()
10 , legend = nv.models.legend()
11 , controls = nv.models.legend()
14 var margin = {top: 30, right: 20, bottom: 50, left: 60}
17 , color = nv.utils.defaultColor()
20 , reduceXTicks = true // if false a tick will show for every data point
23 , tooltip = function(key, x, y, e, graph) {
24 return '<h3>' + key + '</h3>' +
25 '<p>' + y + ' on ' + x + '</p>'
27 , x //can be accessed via chart.xScale()
28 , y //can be accessed via chart.yScale()
29 , noData = "No Data Available."
30 , dispatch = d3.dispatch('tooltipShow', 'tooltipHide')
44 .tickFormat(d3.format(',.1f'))
47 //============================================================
50 //============================================================
52 //------------------------------------------------------------
54 var showTooltip = function(e, offsetElement) {
55 var left = e.pos[0] + ( offsetElement.offsetLeft || 0 ),
56 top = e.pos[1] + ( offsetElement.offsetTop || 0),
57 x = xAxis.tickFormat()(multibar.x()(e.point, e.pointIndex)),
58 y = yAxis.tickFormat()(multibar.y()(e.point, e.pointIndex)),
59 content = tooltip(e.series.key, x, y, e, chart);
61 nv.tooltip.show([left, top], content, e.value < 0 ? 'n' : 's', null, offsetElement);
64 //============================================================
67 function chart(selection) {
68 selection.each(function(data) {
69 var container = d3.select(this),
72 var availableWidth = (width || parseInt(container.style('width')) || 960)
73 - margin.left - margin.right,
74 availableHeight = (height || parseInt(container.style('height')) || 400)
75 - margin.top - margin.bottom;
77 chart.update = function() { selection.transition().call(chart) };
78 chart.container = this;
81 //------------------------------------------------------------
82 // Display noData message if there's nothing to show.
84 if (!data || !data.length || !data.filter(function(d) { return d.values.length }).length) {
85 var noDataText = container.selectAll('.nv-noData').data([noData]);
87 noDataText.enter().append('text')
88 .attr('class', 'nvd3 nv-noData')
90 .style('text-anchor', 'middle');
93 .attr('x', margin.left + availableWidth / 2)
94 .attr('y', margin.top + availableHeight / 2)
95 .text(function(d) { return d });
99 container.selectAll('.nv-noData').remove();
102 //------------------------------------------------------------
105 //------------------------------------------------------------
108 x = multibar.xScale();
109 y = multibar.yScale();
111 //------------------------------------------------------------
114 //------------------------------------------------------------
115 // Setup containers and skeleton of chart
117 var wrap = container.selectAll('g.nv-wrap.nv-multiBarWithLegend').data([data]);
118 var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-multiBarWithLegend').append('g');
119 var g = wrap.select('g');
121 gEnter.append('g').attr('class', 'nv-x nv-axis');
122 gEnter.append('g').attr('class', 'nv-y nv-axis');
123 gEnter.append('g').attr('class', 'nv-barsWrap');
124 gEnter.append('g').attr('class', 'nv-legendWrap');
125 gEnter.append('g').attr('class', 'nv-controlsWrap');
127 //------------------------------------------------------------
130 //------------------------------------------------------------
134 legend.width(availableWidth / 2);
136 g.select('.nv-legendWrap')
140 if ( margin.top != legend.height()) {
141 margin.top = legend.height();
142 availableHeight = (height || parseInt(container.style('height')) || 400)
143 - margin.top - margin.bottom;
146 g.select('.nv-legendWrap')
147 .attr('transform', 'translate(' + (availableWidth / 2) + ',' + (-margin.top) +')');
150 //------------------------------------------------------------
153 //------------------------------------------------------------
158 { key: 'Grouped', disabled: multibar.stacked() },
159 { key: 'Stacked', disabled: !multibar.stacked() }
162 controls.width(180).color(['#444', '#444', '#444']);
163 g.select('.nv-controlsWrap')
165 .attr('transform', 'translate(0,' + (-margin.top) +')')
169 //------------------------------------------------------------
172 wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
175 //------------------------------------------------------------
176 // Main Chart Component(s)
179 .width(availableWidth)
180 .height(availableHeight)
181 .color(data.map(function(d,i) {
182 return d.color || color(d, i);
183 }).filter(function(d,i) { return !data[i].disabled }))
186 var barsWrap = g.select('.nv-barsWrap')
187 .datum(data.filter(function(d) { return !d.disabled }))
189 d3.transition(barsWrap).call(multibar);
191 //------------------------------------------------------------
194 //------------------------------------------------------------
199 .ticks(availableWidth / 100)
200 .tickSize(-availableHeight, 0);
202 g.select('.nv-x.nv-axis')
203 .attr('transform', 'translate(0,' + y.range()[0] + ')');
204 d3.transition(g.select('.nv-x.nv-axis'))
207 var xTicks = g.select('.nv-x.nv-axis > g').selectAll('g');
210 .selectAll('line, text')
215 .filter(function(d,i) {
216 return i % Math.ceil(data[0].values.length / (availableWidth / 100)) !== 0;
218 .selectAll('text, line')
219 .style('opacity', 0);
224 .attr('transform', function(d,i,j) { return 'rotate('+rotateLabels+' 0,0)' })
225 .attr('text-transform', rotateLabels > 0 ? 'start' : 'end');
229 .ticks( availableHeight / 36 )
230 .tickSize( -availableWidth, 0);
232 d3.transition(g.select('.nv-y.nv-axis'))
235 //------------------------------------------------------------
239 //============================================================
240 // Event Handling/Dispatching (in chart's scope)
241 //------------------------------------------------------------
243 legend.dispatch.on('legendClick', function(d,i) {
244 d.disabled = !d.disabled;
246 if (!data.filter(function(d) { return !d.disabled }).length) {
247 data.map(function(d) {
249 wrap.selectAll('.nv-series').classed('disabled', false);
254 selection.transition().call(chart);
257 controls.dispatch.on('legendClick', function(d,i) {
258 if (!d.disabled) return;
259 controlsData = controlsData.map(function(s) {
267 multibar.stacked(false);
270 multibar.stacked(true);
274 selection.transition().call(chart);
277 dispatch.on('tooltipShow', function(e) {
278 if (tooltips) showTooltip(e, that.parentNode)
281 //============================================================
290 //============================================================
291 // Event Handling/Dispatching (out of chart's scope)
292 //------------------------------------------------------------
294 multibar.dispatch.on('elementMouseover.tooltip', function(e) {
295 e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
296 dispatch.tooltipShow(e);
299 multibar.dispatch.on('elementMouseout.tooltip', function(e) {
300 dispatch.tooltipHide(e);
302 dispatch.on('tooltipHide', function() {
303 if (tooltips) nv.tooltip.cleanup();
306 //============================================================
309 //============================================================
310 // Expose Public Variables
311 //------------------------------------------------------------
313 // expose chart's sub-components
314 chart.dispatch = dispatch;
315 chart.multibar = multibar;
316 chart.legend = legend;
320 d3.rebind(chart, multibar, 'x', 'y', 'xDomain', 'yDomain', 'xRange', 'yRange', 'forceX', 'forceY', 'clipEdge', 'id', 'stacked', 'delay');
322 chart.options = nv.utils.optionsFunc.bind(chart);
324 chart.margin = function(_) {
325 if (!arguments.length) return margin;
326 margin.top = typeof _.top != 'undefined' ? _.top : margin.top;
327 margin.right = typeof _.right != 'undefined' ? _.right : margin.right;
328 margin.bottom = typeof _.bottom != 'undefined' ? _.bottom : margin.bottom;
329 margin.left = typeof _.left != 'undefined' ? _.left : margin.left;
333 chart.width = function(_) {
334 if (!arguments.length) return width;
339 chart.height = function(_) {
340 if (!arguments.length) return height;
345 chart.color = function(_) {
346 if (!arguments.length) return color;
347 color = nv.utils.getColor(_);
352 chart.showControls = function(_) {
353 if (!arguments.length) return showControls;
358 chart.showLegend = function(_) {
359 if (!arguments.length) return showLegend;
364 chart.reduceXTicks= function(_) {
365 if (!arguments.length) return reduceXTicks;
370 chart.rotateLabels = function(_) {
371 if (!arguments.length) return rotateLabels;
376 chart.tooltip = function(_) {
377 if (!arguments.length) return tooltip;
382 chart.tooltips = function(_) {
383 if (!arguments.length) return tooltips;
388 chart.tooltipContent = function(_) {
389 if (!arguments.length) return tooltip;
394 chart.noData = function(_) {
395 if (!arguments.length) return noData;
400 //============================================================