Issue #68 updated linePlusBarChart model to standard format

master-patched
Bob Monteverde 12 years ago
parent 869a22c5bf
commit caf5c121ca

@ -1662,7 +1662,7 @@ nv.models.cumulativeLineChart = function() {
//------------------------------------------------------------ //------------------------------------------------------------
g.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')'); wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
//------------------------------------------------------------ //------------------------------------------------------------
@ -3678,32 +3678,55 @@ nv.models.lineChart = function() {
} }
nv.models.linePlusBarChart = function() { nv.models.linePlusBarChart = function() {
var margin = {top: 30, right: 60, bottom: 50, left: 60},
width = null, //============================================================
height = null, // Public Variables with Default Settings
getX = function(d) { return d.x }, //------------------------------------------------------------
getY = function(d) { return d.y },
color = nv.utils.defaultColor(), var lines = nv.models.line()
showLegend = true, , bars = nv.models.historicalBar()
tooltips = true, , xAxis = nv.models.axis()
tooltip = function(key, x, y, e, graph) { , yAxis1 = nv.models.axis()
, yAxis2 = nv.models.axis()
, legend = nv.models.legend()
;
var margin = {top: 30, right: 60, bottom: 50, left: 60}
, width = null
, height = null
, getX = function(d) { return d.x }
, getY = function(d) { return d.y }
, color = nv.utils.defaultColor()
, showLegend = true
, tooltips = true
, tooltip = function(key, x, y, e, graph) {
return '<h3>' + key + '</h3>' + return '<h3>' + key + '</h3>' +
'<p>' + y + ' at ' + x + '</p>' '<p>' + y + ' at ' + x + '</p>';
}, }
noData = "No Data Available." , x = d3.scale.linear() // needs to be both line and historicalBar x Axis
; , y1
, y2
, noData = "No Data Available."
, dispatch = d3.dispatch('tooltipShow', 'tooltipHide')
;
xAxis
.orient('bottom')
.tickPadding(5)
;
yAxis1
.orient('left')
;
yAxis2
.orient('right')
;
//============================================================
var lines = nv.models.line(),
bars = nv.models.historicalBar(), //============================================================
x = d3.scale.linear(), // needs to be both line and historicalBar x Axis // Private Variables
y1 = bars.yScale(), //------------------------------------------------------------
y2 = lines.yScale(),
xAxis = nv.models.axis().scale(x).orient('bottom').tickPadding(5),
yAxis1 = nv.models.axis().scale(y1).orient('left'),
yAxis2 = nv.models.axis().scale(y2).orient('right'),
legend = nv.models.legend().height(30),
dispatch = d3.dispatch('tooltipShow', 'tooltipHide');
var showTooltip = function(e, offsetElement) { var showTooltip = function(e, offsetElement) {
var left = e.pos[0] + ( offsetElement.offsetLeft || 0 ), var left = e.pos[0] + ( offsetElement.offsetLeft || 0 ),
@ -3715,6 +3738,8 @@ nv.models.linePlusBarChart = function() {
nv.tooltip.show([left, top], content, e.value < 0 ? 'n' : 's', null, offsetElement); nv.tooltip.show([left, top], content, e.value < 0 ? 'n' : 's', null, offsetElement);
}; };
//------------------------------------------------------------
function chart(selection) { function chart(selection) {
@ -3747,7 +3772,12 @@ nv.models.linePlusBarChart = function() {
//------------------------------------------------------------ //------------------------------------------------------------
//------------------------------------------------------------
// Setup Scales
y1 = bars.yScale();
y2 = lines.yScale();
var dataBars = data.filter(function(d) { return !d.disabled && d.bar }); var dataBars = data.filter(function(d) { return !d.disabled && d.bar });
@ -3787,10 +3817,15 @@ nv.models.linePlusBarChart = function() {
.range([availableHeight, 0]); .range([availableHeight, 0]);
*/ */
//------------------------------------------------------------
//------------------------------------------------------------
// Setup containers and skeleton of chart
var wrap = d3.select(this).selectAll('g.nv-wrap.nv-linePlusBar').data([data]); var wrap = d3.select(this).selectAll('g.nv-wrap.nv-linePlusBar').data([data]);
var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-linePlusBar').append('g'); var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-linePlusBar').append('g');
var g = wrap.select('g');
gEnter.append('g').attr('class', 'nv-x nv-axis'); gEnter.append('g').attr('class', 'nv-x nv-axis');
gEnter.append('g').attr('class', 'nv-y1 nv-axis'); gEnter.append('g').attr('class', 'nv-y1 nv-axis');
@ -3799,10 +3834,11 @@ nv.models.linePlusBarChart = function() {
gEnter.append('g').attr('class', 'nv-linesWrap'); gEnter.append('g').attr('class', 'nv-linesWrap');
gEnter.append('g').attr('class', 'nv-legendWrap'); gEnter.append('g').attr('class', 'nv-legendWrap');
//------------------------------------------------------------
var g = wrap.select('g'); //------------------------------------------------------------
// Legend
if (showLegend) { if (showLegend) {
legend.width( availableWidth / 2 ); legend.width( availableWidth / 2 );
@ -3825,7 +3861,14 @@ nv.models.linePlusBarChart = function() {
.attr('transform', 'translate(' + ( availableWidth / 2 ) + ',' + (-margin.top) +')'); .attr('transform', 'translate(' + ( availableWidth / 2 ) + ',' + (-margin.top) +')');
} }
//------------------------------------------------------------
wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
//------------------------------------------------------------
// Main Chart Component(s)
lines lines
@ -3850,15 +3893,17 @@ nv.models.linePlusBarChart = function() {
var linesWrap = g.select('.nv-linesWrap') var linesWrap = g.select('.nv-linesWrap')
.datum(dataLines.length ? dataLines : [{values:[]}]) .datum(dataLines.length ? dataLines : [{values:[]}])
d3.transition(barsWrap).call(bars); d3.transition(barsWrap).call(bars);
d3.transition(linesWrap).call(lines); d3.transition(linesWrap).call(lines);
//------------------------------------------------------------
g.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
//------------------------------------------------------------
// Setup Axes
xAxis xAxis
.scale(x)
.ticks( availableWidth / 100 ) .ticks( availableWidth / 100 )
.tickSize(-availableHeight, 0); .tickSize(-availableHeight, 0);
@ -3869,6 +3914,7 @@ nv.models.linePlusBarChart = function() {
yAxis1 yAxis1
.scale(y1)
.ticks( availableHeight / 36 ) .ticks( availableHeight / 36 )
.tickSize(-availableWidth, 0); .tickSize(-availableWidth, 0);
@ -3878,6 +3924,7 @@ nv.models.linePlusBarChart = function() {
yAxis2 yAxis2
.scale(y2)
.ticks( availableHeight / 36 ) .ticks( availableHeight / 36 )
.tickSize(dataBars.length ? 0 : -availableWidth, 0); // Show the y2 rules only if y1 has none .tickSize(dataBars.length ? 0 : -availableWidth, 0); // Show the y2 rules only if y1 has none
@ -3888,8 +3935,13 @@ nv.models.linePlusBarChart = function() {
d3.transition(g.select('.nv-y2.nv-axis')) d3.transition(g.select('.nv-y2.nv-axis'))
.call(yAxis2); .call(yAxis2);
//------------------------------------------------------------
//============================================================
// Event Handling/Dispatching (in chart's scope)
//------------------------------------------------------------
legend.dispatch.on('legendClick', function(d,i) { legend.dispatch.on('legendClick', function(d,i) {
d.disabled = !d.disabled; d.disabled = !d.disabled;
@ -3904,39 +3956,55 @@ nv.models.linePlusBarChart = function() {
selection.transition().call(chart); selection.transition().call(chart);
}); });
dispatch.on('tooltipShow', function(e) {
lines.dispatch.on('elementMouseover.tooltip', function(e) { if (tooltips) showTooltip(e, that.parentNode);
e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
dispatch.tooltipShow(e);
});
if (tooltips) dispatch.on('tooltipShow', function(e) { showTooltip(e, that.parentNode) } ); // TODO: maybe merge with above?
lines.dispatch.on('elementMouseout.tooltip', function(e) {
dispatch.tooltipHide(e);
});
if (tooltips) dispatch.on('tooltipHide', nv.tooltip.cleanup);
bars.dispatch.on('elementMouseover.tooltip', function(e) {
e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
dispatch.tooltipShow(e);
});
if (tooltips) dispatch.on('tooltipShow', function(e) { showTooltip(e, that.parentNode) } ); // TODO: maybe merge with above?
bars.dispatch.on('elementMouseout.tooltip', function(e) {
dispatch.tooltipHide(e);
}); });
if (tooltips) dispatch.on('tooltipHide', nv.tooltip.cleanup);
//============================================================
chart.update = function() { selection.transition().call(chart) }; chart.update = function() { selection.transition().call(chart) };
chart.container = this; // I need a reference to the container in order to have outside code check if the chart is visible or not chart.container = this;
}); });
return chart; return chart;
} }
//============================================================
// Event Handling/Dispatching (out of chart's scope)
//------------------------------------------------------------
lines.dispatch.on('elementMouseover.tooltip', function(e) {
e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
dispatch.tooltipShow(e);
});
lines.dispatch.on('elementMouseout.tooltip', function(e) {
dispatch.tooltipHide(e);
});
bars.dispatch.on('elementMouseover.tooltip', function(e) {
e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
dispatch.tooltipShow(e);
});
bars.dispatch.on('elementMouseout.tooltip', function(e) {
dispatch.tooltipHide(e);
});
dispatch.on('tooltipHide', function() {
if (tooltips) nv.tooltip.cleanup();
});
//============================================================
//============================================================
// Expose Public Variables
//------------------------------------------------------------
// expose chart's sub-components
chart.dispatch = dispatch; chart.dispatch = dispatch;
chart.legend = legend; chart.legend = legend;
chart.lines = lines; chart.lines = lines;
@ -3949,9 +4017,6 @@ nv.models.linePlusBarChart = function() {
//TODO: consider rebinding x, y and some other stuff, and simply do soemthign lile bars.x(lines.x()), etc. //TODO: consider rebinding x, y and some other stuff, and simply do soemthign lile bars.x(lines.x()), etc.
//d3.rebind(chart, lines, 'x', 'y', 'size', 'xDomain', 'yDomain', 'forceX', 'forceY', 'interactive', 'clipEdge', 'clipVoronoi', 'id'); //d3.rebind(chart, lines, 'x', 'y', 'size', 'xDomain', 'yDomain', 'forceX', 'forceY', 'interactive', 'clipEdge', 'clipVoronoi', 'id');
//d3.rebind(chart, lines, 'interactive');
//consider rebinding x and y as well
chart.x = function(_) { chart.x = function(_) {
if (!arguments.length) return getX; if (!arguments.length) return getX;
getX = _; getX = _;
@ -4017,6 +4082,8 @@ nv.models.linePlusBarChart = function() {
return chart; return chart;
}; };
//============================================================
return chart; return chart;
} }

8
nv.d3.min.js vendored

File diff suppressed because one or more lines are too long

@ -209,7 +209,7 @@ nv.models.cumulativeLineChart = function() {
//------------------------------------------------------------ //------------------------------------------------------------
g.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')'); wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
//------------------------------------------------------------ //------------------------------------------------------------

@ -1,31 +1,54 @@
nv.models.linePlusBarChart = function() { nv.models.linePlusBarChart = function() {
var margin = {top: 30, right: 60, bottom: 50, left: 60},
width = null, //============================================================
height = null, // Public Variables with Default Settings
getX = function(d) { return d.x }, //------------------------------------------------------------
getY = function(d) { return d.y },
color = nv.utils.defaultColor(), var lines = nv.models.line()
showLegend = true, , bars = nv.models.historicalBar()
tooltips = true, , xAxis = nv.models.axis()
tooltip = function(key, x, y, e, graph) { , yAxis1 = nv.models.axis()
, yAxis2 = nv.models.axis()
, legend = nv.models.legend()
;
var margin = {top: 30, right: 60, bottom: 50, left: 60}
, width = null
, height = null
, getX = function(d) { return d.x }
, getY = function(d) { return d.y }
, color = nv.utils.defaultColor()
, showLegend = true
, tooltips = true
, tooltip = function(key, x, y, e, graph) {
return '<h3>' + key + '</h3>' + return '<h3>' + key + '</h3>' +
'<p>' + y + ' at ' + x + '</p>' '<p>' + y + ' at ' + x + '</p>';
}, }
noData = "No Data Available." , x = d3.scale.linear() // needs to be both line and historicalBar x Axis
; , y1
, y2
, noData = "No Data Available."
var lines = nv.models.line(), , dispatch = d3.dispatch('tooltipShow', 'tooltipHide')
bars = nv.models.historicalBar(), ;
x = d3.scale.linear(), // needs to be both line and historicalBar x Axis
y1 = bars.yScale(), xAxis
y2 = lines.yScale(), .orient('bottom')
xAxis = nv.models.axis().scale(x).orient('bottom').tickPadding(5), .tickPadding(5)
yAxis1 = nv.models.axis().scale(y1).orient('left'), ;
yAxis2 = nv.models.axis().scale(y2).orient('right'), yAxis1
legend = nv.models.legend().height(30), .orient('left')
dispatch = d3.dispatch('tooltipShow', 'tooltipHide'); ;
yAxis2
.orient('right')
;
//============================================================
//============================================================
// Private Variables
//------------------------------------------------------------
var showTooltip = function(e, offsetElement) { var showTooltip = function(e, offsetElement) {
var left = e.pos[0] + ( offsetElement.offsetLeft || 0 ), var left = e.pos[0] + ( offsetElement.offsetLeft || 0 ),
@ -37,6 +60,8 @@ nv.models.linePlusBarChart = function() {
nv.tooltip.show([left, top], content, e.value < 0 ? 'n' : 's', null, offsetElement); nv.tooltip.show([left, top], content, e.value < 0 ? 'n' : 's', null, offsetElement);
}; };
//------------------------------------------------------------
function chart(selection) { function chart(selection) {
@ -69,7 +94,12 @@ nv.models.linePlusBarChart = function() {
//------------------------------------------------------------ //------------------------------------------------------------
//------------------------------------------------------------
// Setup Scales
y1 = bars.yScale();
y2 = lines.yScale();
var dataBars = data.filter(function(d) { return !d.disabled && d.bar }); var dataBars = data.filter(function(d) { return !d.disabled && d.bar });
@ -109,10 +139,15 @@ nv.models.linePlusBarChart = function() {
.range([availableHeight, 0]); .range([availableHeight, 0]);
*/ */
//------------------------------------------------------------
//------------------------------------------------------------
// Setup containers and skeleton of chart
var wrap = d3.select(this).selectAll('g.nv-wrap.nv-linePlusBar').data([data]); var wrap = d3.select(this).selectAll('g.nv-wrap.nv-linePlusBar').data([data]);
var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-linePlusBar').append('g'); var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-linePlusBar').append('g');
var g = wrap.select('g');
gEnter.append('g').attr('class', 'nv-x nv-axis'); gEnter.append('g').attr('class', 'nv-x nv-axis');
gEnter.append('g').attr('class', 'nv-y1 nv-axis'); gEnter.append('g').attr('class', 'nv-y1 nv-axis');
@ -121,10 +156,11 @@ nv.models.linePlusBarChart = function() {
gEnter.append('g').attr('class', 'nv-linesWrap'); gEnter.append('g').attr('class', 'nv-linesWrap');
gEnter.append('g').attr('class', 'nv-legendWrap'); gEnter.append('g').attr('class', 'nv-legendWrap');
//------------------------------------------------------------
var g = wrap.select('g'); //------------------------------------------------------------
// Legend
if (showLegend) { if (showLegend) {
legend.width( availableWidth / 2 ); legend.width( availableWidth / 2 );
@ -147,8 +183,15 @@ nv.models.linePlusBarChart = function() {
.attr('transform', 'translate(' + ( availableWidth / 2 ) + ',' + (-margin.top) +')'); .attr('transform', 'translate(' + ( availableWidth / 2 ) + ',' + (-margin.top) +')');
} }
//------------------------------------------------------------
wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
//------------------------------------------------------------
// Main Chart Component(s)
lines lines
.width(availableWidth) .width(availableWidth)
@ -172,15 +215,17 @@ nv.models.linePlusBarChart = function() {
var linesWrap = g.select('.nv-linesWrap') var linesWrap = g.select('.nv-linesWrap')
.datum(dataLines.length ? dataLines : [{values:[]}]) .datum(dataLines.length ? dataLines : [{values:[]}])
d3.transition(barsWrap).call(bars); d3.transition(barsWrap).call(bars);
d3.transition(linesWrap).call(lines); d3.transition(linesWrap).call(lines);
//------------------------------------------------------------
g.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
//------------------------------------------------------------
// Setup Axes
xAxis xAxis
.scale(x)
.ticks( availableWidth / 100 ) .ticks( availableWidth / 100 )
.tickSize(-availableHeight, 0); .tickSize(-availableHeight, 0);
@ -191,6 +236,7 @@ nv.models.linePlusBarChart = function() {
yAxis1 yAxis1
.scale(y1)
.ticks( availableHeight / 36 ) .ticks( availableHeight / 36 )
.tickSize(-availableWidth, 0); .tickSize(-availableWidth, 0);
@ -200,6 +246,7 @@ nv.models.linePlusBarChart = function() {
yAxis2 yAxis2
.scale(y2)
.ticks( availableHeight / 36 ) .ticks( availableHeight / 36 )
.tickSize(dataBars.length ? 0 : -availableWidth, 0); // Show the y2 rules only if y1 has none .tickSize(dataBars.length ? 0 : -availableWidth, 0); // Show the y2 rules only if y1 has none
@ -210,7 +257,12 @@ nv.models.linePlusBarChart = function() {
d3.transition(g.select('.nv-y2.nv-axis')) d3.transition(g.select('.nv-y2.nv-axis'))
.call(yAxis2); .call(yAxis2);
//------------------------------------------------------------
//============================================================
// Event Handling/Dispatching (in chart's scope)
//------------------------------------------------------------
legend.dispatch.on('legendClick', function(d,i) { legend.dispatch.on('legendClick', function(d,i) {
d.disabled = !d.disabled; d.disabled = !d.disabled;
@ -226,39 +278,55 @@ nv.models.linePlusBarChart = function() {
selection.transition().call(chart); selection.transition().call(chart);
}); });
dispatch.on('tooltipShow', function(e) {
lines.dispatch.on('elementMouseover.tooltip', function(e) { if (tooltips) showTooltip(e, that.parentNode);
e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
dispatch.tooltipShow(e);
});
if (tooltips) dispatch.on('tooltipShow', function(e) { showTooltip(e, that.parentNode) } ); // TODO: maybe merge with above?
lines.dispatch.on('elementMouseout.tooltip', function(e) {
dispatch.tooltipHide(e);
}); });
if (tooltips) dispatch.on('tooltipHide', nv.tooltip.cleanup);
bars.dispatch.on('elementMouseover.tooltip', function(e) {
e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
dispatch.tooltipShow(e);
});
if (tooltips) dispatch.on('tooltipShow', function(e) { showTooltip(e, that.parentNode) } ); // TODO: maybe merge with above?
bars.dispatch.on('elementMouseout.tooltip', function(e) {
dispatch.tooltipHide(e);
});
if (tooltips) dispatch.on('tooltipHide', nv.tooltip.cleanup);
//============================================================
chart.update = function() { selection.transition().call(chart) }; chart.update = function() { selection.transition().call(chart) };
chart.container = this; // I need a reference to the container in order to have outside code check if the chart is visible or not chart.container = this;
}); });
return chart; return chart;
} }
//============================================================
// Event Handling/Dispatching (out of chart's scope)
//------------------------------------------------------------
lines.dispatch.on('elementMouseover.tooltip', function(e) {
e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
dispatch.tooltipShow(e);
});
lines.dispatch.on('elementMouseout.tooltip', function(e) {
dispatch.tooltipHide(e);
});
bars.dispatch.on('elementMouseover.tooltip', function(e) {
e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
dispatch.tooltipShow(e);
});
bars.dispatch.on('elementMouseout.tooltip', function(e) {
dispatch.tooltipHide(e);
});
dispatch.on('tooltipHide', function() {
if (tooltips) nv.tooltip.cleanup();
});
//============================================================
//============================================================
// Expose Public Variables
//------------------------------------------------------------
// expose chart's sub-components
chart.dispatch = dispatch; chart.dispatch = dispatch;
chart.legend = legend; chart.legend = legend;
chart.lines = lines; chart.lines = lines;
@ -271,9 +339,6 @@ nv.models.linePlusBarChart = function() {
//TODO: consider rebinding x, y and some other stuff, and simply do soemthign lile bars.x(lines.x()), etc. //TODO: consider rebinding x, y and some other stuff, and simply do soemthign lile bars.x(lines.x()), etc.
//d3.rebind(chart, lines, 'x', 'y', 'size', 'xDomain', 'yDomain', 'forceX', 'forceY', 'interactive', 'clipEdge', 'clipVoronoi', 'id'); //d3.rebind(chart, lines, 'x', 'y', 'size', 'xDomain', 'yDomain', 'forceX', 'forceY', 'interactive', 'clipEdge', 'clipVoronoi', 'id');
//d3.rebind(chart, lines, 'interactive');
//consider rebinding x and y as well
chart.x = function(_) { chart.x = function(_) {
if (!arguments.length) return getX; if (!arguments.length) return getX;
getX = _; getX = _;
@ -339,6 +404,8 @@ nv.models.linePlusBarChart = function() {
return chart; return chart;
}; };
//============================================================
return chart; return chart;
} }

Loading…
Cancel
Save