2 nv.models.linePlusBarChart = function() {
4 //============================================================
5 // Public Variables with Default Settings
6 //------------------------------------------------------------
8 var lines = nv.models.line()
9 , bars = nv.models.historicalBar()
10 , xAxis = nv.models.axis()
11 , y1Axis = nv.models.axis()
12 , y2Axis = nv.models.axis()
13 , legend = nv.models.legend()
16 var margin = {top: 30, right: 60, bottom: 50, left: 60}
19 , getX = function(d) { return d.x }
20 , getY = function(d) { return d.y }
21 , color = nv.utils.defaultColor()
24 , tooltip = function(key, x, y, e, graph) {
25 return '<h3>' + key + '</h3>' +
26 '<p>' + y + ' at ' + x + '</p>';
33 , noData = "No Data Available."
34 , dispatch = d3.dispatch('tooltipShow', 'tooltipHide', 'stateChange', 'changeState')
56 //============================================================
59 //============================================================
61 //------------------------------------------------------------
63 var showTooltip = function(e, offsetElement) {
64 var left = e.pos[0] + ( offsetElement.offsetLeft || 0 ),
65 top = e.pos[1] + ( offsetElement.offsetTop || 0),
66 x = xAxis.tickFormat()(lines.x()(e.point, e.pointIndex)),
67 y = (e.series.bar ? y1Axis : y2Axis).tickFormat()(lines.y()(e.point, e.pointIndex)),
68 content = tooltip(e.series.key, x, y, e, chart);
70 nv.tooltip.show([left, top], content, e.value < 0 ? 'n' : 's', null, offsetElement);
74 //------------------------------------------------------------
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().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];
105 //------------------------------------------------------------
106 // Display No Data message if there's nothing to show.
108 if (!data || !data.length || !data.filter(function(d) { return d.values.length }).length) {
109 var noDataText = container.selectAll('.nv-noData').data([noData]);
111 noDataText.enter().append('text')
112 .attr('class', 'nvd3 nv-noData')
114 .style('text-anchor', 'middle');
117 .attr('x', margin.left + availableWidth / 2)
118 .attr('y', margin.top + availableHeight / 2)
119 .text(function(d) { return d });
123 container.selectAll('.nv-noData').remove();
126 //------------------------------------------------------------
129 //------------------------------------------------------------
132 var dataBars = data.filter(function(d) { return !d.disabled && d.bar });
133 var dataLines = data.filter(function(d) { return !d.bar }); // removed the !d.disabled clause here to fix Issue #240
136 x = dataLines.filter(function(d) { return !d.disabled; }).length && dataLines.filter(function(d) { return !d.disabled; })[0].values.length ? lines.xScale() : bars.xScale();
137 //x = dataLines.filter(function(d) { return !d.disabled; }).length ? lines.xScale() : bars.xScale(); //old code before change above
141 //------------------------------------------------------------
143 //------------------------------------------------------------
144 // Setup containers and skeleton of chart
146 var wrap = d3.select(this).selectAll('g.nv-wrap.nv-linePlusBar').data([data]);
147 var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-linePlusBar').append('g');
148 var g = wrap.select('g');
150 gEnter.append('g').attr('class', 'nv-x nv-axis');
151 gEnter.append('g').attr('class', 'nv-y1 nv-axis');
152 gEnter.append('g').attr('class', 'nv-y2 nv-axis');
153 gEnter.append('g').attr('class', 'nv-barsWrap');
154 gEnter.append('g').attr('class', 'nv-linesWrap');
155 gEnter.append('g').attr('class', 'nv-legendWrap');
157 //------------------------------------------------------------
160 //------------------------------------------------------------
164 legend.width( availableWidth / 2 );
166 g.select('.nv-legendWrap')
167 .datum(data.map(function(series) {
168 series.originalKey = series.originalKey === undefined ? series.key : series.originalKey;
169 series.key = series.originalKey + (series.bar ? ' (left axis)' : ' (right axis)');
174 if ( margin.top != legend.height()) {
175 margin.top = legend.height();
176 availableHeight = (height || parseInt(container.style('height')) || 400)
177 - margin.top - margin.bottom;
180 g.select('.nv-legendWrap')
181 .attr('transform', 'translate(' + ( availableWidth / 2 ) + ',' + (-margin.top) +')');
184 //------------------------------------------------------------
187 wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
190 //------------------------------------------------------------
191 // Main Chart Component(s)
195 .width(availableWidth)
196 .height(availableHeight)
197 .color(data.map(function(d,i) {
198 return d.color || color(d, i);
199 }).filter(function(d,i) { return !data[i].disabled && !data[i].bar }))
202 .width(availableWidth)
203 .height(availableHeight)
204 .color(data.map(function(d,i) {
205 return d.color || color(d, i);
206 }).filter(function(d,i) { return !data[i].disabled && data[i].bar }))
210 var barsWrap = g.select('.nv-barsWrap')
211 .datum(dataBars.length ? dataBars : [{values:[]}])
213 var linesWrap = g.select('.nv-linesWrap')
214 .datum(dataLines[0] && !dataLines[0].disabled ? dataLines : [{values:[]}] );
215 //.datum(!dataLines[0].disabled ? dataLines : [{values:dataLines[0].values.map(function(d) { return [d[0], null] }) }] );
217 d3.transition(barsWrap).call(bars);
218 d3.transition(linesWrap).call(lines);
220 //------------------------------------------------------------
223 //------------------------------------------------------------
228 .ticks( availableWidth / 100 )
229 .tickSize(-availableHeight, 0);
231 g.select('.nv-x.nv-axis')
232 .attr('transform', 'translate(0,' + y1.range()[0] + ')');
233 d3.transition(g.select('.nv-x.nv-axis'))
239 .ticks( availableHeight / 36 )
240 .tickSize(-availableWidth, 0);
242 d3.transition(g.select('.nv-y1.nv-axis'))
243 .style('opacity', dataBars.length ? 1 : 0)
249 .ticks( availableHeight / 36 )
250 .tickSize(dataBars.length ? 0 : -availableWidth, 0); // Show the y2 rules only if y1 has none
252 g.select('.nv-y2.nv-axis')
253 .style('opacity', dataLines.length ? 1 : 0)
254 .attr('transform', 'translate(' + availableWidth + ',0)');
255 //.attr('transform', 'translate(' + x.range()[1] + ',0)');
257 d3.transition(g.select('.nv-y2.nv-axis'))
260 //------------------------------------------------------------
263 //============================================================
264 // Event Handling/Dispatching (in chart's scope)
265 //------------------------------------------------------------
267 legend.dispatch.on('stateChange', function(newState) {
269 dispatch.stateChange(state);
273 dispatch.on('tooltipShow', function(e) {
274 if (tooltips) showTooltip(e, that.parentNode);
278 // Update chart from a state object passed to event handler
279 dispatch.on('changeState', function(e) {
281 if (typeof e.disabled !== 'undefined') {
282 data.forEach(function(series,i) {
283 series.disabled = e.disabled[i];
286 state.disabled = e.disabled;
292 //============================================================
301 //============================================================
302 // Event Handling/Dispatching (out of chart's scope)
303 //------------------------------------------------------------
305 lines.dispatch.on('elementMouseover.tooltip', function(e) {
306 e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
307 dispatch.tooltipShow(e);
310 lines.dispatch.on('elementMouseout.tooltip', function(e) {
311 dispatch.tooltipHide(e);
314 bars.dispatch.on('elementMouseover.tooltip', function(e) {
315 e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
316 dispatch.tooltipShow(e);
319 bars.dispatch.on('elementMouseout.tooltip', function(e) {
320 dispatch.tooltipHide(e);
323 dispatch.on('tooltipHide', function() {
324 if (tooltips) nv.tooltip.cleanup();
327 //============================================================
330 //============================================================
331 // Expose Public Variables
332 //------------------------------------------------------------
334 // expose chart's sub-components
335 chart.dispatch = dispatch;
336 chart.legend = legend;
340 chart.y1Axis = y1Axis;
341 chart.y2Axis = y2Axis;
343 d3.rebind(chart, lines, 'defined', 'size', 'clipVoronoi', 'interpolate');
344 //TODO: consider rebinding x, y and some other stuff, and simply do soemthign lile bars.x(lines.x()), etc.
345 //d3.rebind(chart, lines, 'x', 'y', 'size', 'xDomain', 'yDomain', 'xRange', 'yRange', 'forceX', 'forceY', 'interactive', 'clipEdge', 'clipVoronoi', 'id');
347 chart.options = nv.utils.optionsFunc.bind(chart);
349 chart.x = function(_) {
350 if (!arguments.length) return getX;
357 chart.y = function(_) {
358 if (!arguments.length) return getY;
365 chart.margin = function(_) {
366 if (!arguments.length) return margin;
367 margin.top = typeof _.top != 'undefined' ? _.top : margin.top;
368 margin.right = typeof _.right != 'undefined' ? _.right : margin.right;
369 margin.bottom = typeof _.bottom != 'undefined' ? _.bottom : margin.bottom;
370 margin.left = typeof _.left != 'undefined' ? _.left : margin.left;
374 chart.width = function(_) {
375 if (!arguments.length) return width;
380 chart.height = function(_) {
381 if (!arguments.length) return height;
386 chart.color = function(_) {
387 if (!arguments.length) return color;
388 color = nv.utils.getColor(_);
393 chart.showLegend = function(_) {
394 if (!arguments.length) return showLegend;
399 chart.tooltips = function(_) {
400 if (!arguments.length) return tooltips;
405 chart.tooltipContent = function(_) {
406 if (!arguments.length) return tooltip;
411 chart.state = function(_) {
412 if (!arguments.length) return state;
417 chart.defaultState = function(_) {
418 if (!arguments.length) return defaultState;
423 chart.noData = function(_) {
424 if (!arguments.length) return noData;
429 //============================================================