Issue #68 updated discreteBarChart model to standard format

master-patched
Bob Monteverde 12 years ago
parent 62bfab14d6
commit 3933b3da69

@ -1569,6 +1569,10 @@ nv.models.cumulativeLineChart = function() {
- margin.top - margin.bottom;
chart.update = function() { chart(selection) };
chart.container = this;
//------------------------------------------------------------
// Display No Data message if there's nothing to show.
@ -1794,9 +1798,6 @@ nv.models.cumulativeLineChart = function() {
//============================================================
chart.update = function() { chart(selection) };
chart.container = this;
});
return chart;
@ -2238,30 +2239,49 @@ nv.models.discreteBar = function() {
}
nv.models.discreteBarChart = function() {
var margin = {top: 10, right: 10, bottom: 50, left: 60},
width = null,
height = null,
color = nv.utils.getColor(), //a function that gets color for a datum
staggerLabels = false,
tooltips = true,
tooltip = function(key, x, y, e, graph) {
//============================================================
// Public Variables with Default Settings
//------------------------------------------------------------
var discretebar = nv.models.discreteBar()
, xAxis = nv.models.axis()
, yAxis = nv.models.axis()
;
var margin = {top: 10, right: 10, bottom: 50, left: 60}
, width = null
, height = null
, color = nv.utils.getColor()
, staggerLabels = false
, tooltips = true
, tooltip = function(key, x, y, e, graph) {
return '<h3>' + x + '</h3>' +
'<p>' + y + '</p>'
},
noData = "No Data Available."
;
}
, x
, y
, noData = "No Data Available."
, dispatch = d3.dispatch('tooltipShow', 'tooltipHide')
;
xAxis
.orient('bottom')
.highlightZero(false)
.showMaxMin(false)
.tickFormat(function(d) { return d })
;
yAxis
.orient('left')
.tickFormat(d3.format(',.1f'))
;
var discretebar = nv.models.discreteBar(),
x = discretebar.xScale(),
y = discretebar.yScale(),
xAxis = nv.models.axis().scale(x).orient('bottom').highlightZero(false).showMaxMin(false),
yAxis = nv.models.axis().scale(y).orient('left'),
dispatch = d3.dispatch('tooltipShow', 'tooltipHide');
//============================================================
xAxis.tickFormat(function(d) { return d });
yAxis.tickFormat(d3.format(',.1f'));
//============================================================
// Private Variables
//------------------------------------------------------------
var showTooltip = function(e, offsetElement) {
var left = e.pos[0] + ( offsetElement.offsetLeft || 0 ),
@ -2273,12 +2293,8 @@ nv.models.discreteBarChart = function() {
nv.tooltip.show([left, top], content, e.value < 0 ? 'n' : 's', null, offsetElement);
};
//============================================================
//TODO: let user select default
var controlsData = [
{ key: 'Grouped' },
{ key: 'Stacked', disabled: true }
];
function chart(selection) {
selection.each(function(data) {
@ -2291,6 +2307,10 @@ nv.models.discreteBarChart = function() {
- margin.top - margin.bottom;
chart.update = function() { selection.transition().call(chart); };
chart.container = this;
//------------------------------------------------------------
// Display No Data message if there's nothing to show.
@ -2310,36 +2330,52 @@ nv.models.discreteBarChart = function() {
//------------------------------------------------------------
discretebar
.width(availableWidth)
.height(availableHeight);
//------------------------------------------------------------
// Setup Scales
x = discretebar.xScale();
y = discretebar.yScale();
//------------------------------------------------------------
//------------------------------------------------------------
// Setup containers and skeleton of chart
var wrap = container.selectAll('g.nv-wrap.nv-discreteBarWithAxes').data([data]);
var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-discreteBarWithAxes').append('g');
var defsEnter = gEnter.append('defs');
var g = wrap.select('g');
gEnter.append('g').attr('class', 'nv-x nv-axis');
gEnter.append('g').attr('class', 'nv-y nv-axis');
gEnter.append('g').attr('class', 'nv-barsWrap');
g.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
//------------------------------------------------------------
var g = wrap.select('g');
//------------------------------------------------------------
// Main Chart Component(s)
discretebar
.width(availableWidth)
.height(availableHeight);
g.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
var barsWrap = g.select('.nv-barsWrap')
.datum(data.filter(function(d) { return !d.disabled }))
d3.transition(barsWrap).call(discretebar);
//------------------------------------------------------------
defsEnter.append('clipPath')
.attr('id', 'nv-x-label-clip-' + discretebar.id())
.append('rect')
.append('rect');
g.select('#nv-x-label-clip-' + discretebar.id() + ' rect')
.attr('width', x.rangeBand() * (staggerLabels ? 2 : 1))
@ -2347,13 +2383,17 @@ nv.models.discreteBarChart = function() {
.attr('x', -x.rangeBand() / (staggerLabels ? 1 : 2 ));
//------------------------------------------------------------
// Setup Axes
xAxis
.scale(x)
.ticks( availableWidth / 100 )
.tickSize(-availableHeight, 0);
g.select('.nv-x.nv-axis')
.attr('transform', 'translate(0,' + (y.range()[0] + ((discretebar.showValues() && y.domain()[0] < 0) ? 16 : 0)) + ')')
//d3.transition(g.select('.x.axis'))
.attr('transform', 'translate(0,' + (y.range()[0] + ((discretebar.showValues() && y.domain()[0] < 0) ? 16 : 0)) + ')');
//d3.transition(g.select('.nv-x.nv-axis'))
g.select('.nv-x.nv-axis').transition().duration(0)
.call(xAxis);
@ -2366,43 +2406,64 @@ nv.models.discreteBarChart = function() {
.attr('transform', function(d,i,j) { return 'translate(0,' + (j % 2 == 0 ? '0' : '12') + ')' })
yAxis
.scale(y)
.ticks( availableHeight / 36 )
.tickSize( -availableWidth, 0);
d3.transition(g.select('.nv-y.nv-axis'))
.call(yAxis);
//------------------------------------------------------------
discretebar.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?
discretebar.dispatch.on('elementMouseout.tooltip', function(e) {
dispatch.tooltipHide(e);
//============================================================
// Event Handling/Dispatching (in chart's scope)
//------------------------------------------------------------
dispatch.on('tooltipShow', function(e) {
if (tooltips) showTooltip(e, that.parentNode);
});
if (tooltips) dispatch.on('tooltipHide', nv.tooltip.cleanup);
//============================================================
//TODO: decide if this makes sense to add into all the models for ease of updating (updating without needing the selection)
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
});
return chart;
}
//============================================================
// Event Handling/Dispatching (out of chart's scope)
//------------------------------------------------------------
discretebar.dispatch.on('elementMouseover.tooltip', function(e) {
e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
dispatch.tooltipShow(e);
});
discretebar.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.discretebar = discretebar; // really just makign the accessible for discretebar.dispatch, may rethink slightly
chart.discretebar = discretebar;
chart.xAxis = xAxis;
chart.yAxis = yAxis;
d3.rebind(chart, discretebar, 'x', 'y', 'xDomain', 'yDomain', 'forceX', 'forceY', 'id', 'showValues', 'valueFormat');
chart.margin = function(_) {
if (!arguments.length) return margin;
margin = _;
@ -2452,6 +2513,8 @@ nv.models.discreteBarChart = function() {
return chart;
};
//============================================================
return chart;
}

8
nv.d3.min.js vendored

File diff suppressed because one or more lines are too long

@ -93,6 +93,10 @@ nv.models.cumulativeLineChart = function() {
- margin.top - margin.bottom;
chart.update = function() { chart(selection) };
chart.container = this;
//------------------------------------------------------------
// Display No Data message if there's nothing to show.
@ -318,9 +322,6 @@ nv.models.cumulativeLineChart = function() {
//============================================================
chart.update = function() { chart(selection) };
chart.container = this;
});
return chart;

@ -1,29 +1,48 @@
nv.models.discreteBarChart = function() {
var margin = {top: 10, right: 10, bottom: 50, left: 60},
width = null,
height = null,
color = nv.utils.getColor(), //a function that gets color for a datum
staggerLabels = false,
tooltips = true,
tooltip = function(key, x, y, e, graph) {
//============================================================
// Public Variables with Default Settings
//------------------------------------------------------------
var discretebar = nv.models.discreteBar()
, xAxis = nv.models.axis()
, yAxis = nv.models.axis()
;
var margin = {top: 10, right: 10, bottom: 50, left: 60}
, width = null
, height = null
, color = nv.utils.getColor()
, staggerLabels = false
, tooltips = true
, tooltip = function(key, x, y, e, graph) {
return '<h3>' + x + '</h3>' +
'<p>' + y + '</p>'
},
noData = "No Data Available."
;
var discretebar = nv.models.discreteBar(),
x = discretebar.xScale(),
y = discretebar.yScale(),
xAxis = nv.models.axis().scale(x).orient('bottom').highlightZero(false).showMaxMin(false),
yAxis = nv.models.axis().scale(y).orient('left'),
dispatch = d3.dispatch('tooltipShow', 'tooltipHide');
xAxis.tickFormat(function(d) { return d });
yAxis.tickFormat(d3.format(',.1f'));
}
, x
, y
, noData = "No Data Available."
, dispatch = d3.dispatch('tooltipShow', 'tooltipHide')
;
xAxis
.orient('bottom')
.highlightZero(false)
.showMaxMin(false)
.tickFormat(function(d) { return d })
;
yAxis
.orient('left')
.tickFormat(d3.format(',.1f'))
;
//============================================================
//============================================================
// Private Variables
//------------------------------------------------------------
var showTooltip = function(e, offsetElement) {
var left = e.pos[0] + ( offsetElement.offsetLeft || 0 ),
@ -35,12 +54,8 @@ nv.models.discreteBarChart = function() {
nv.tooltip.show([left, top], content, e.value < 0 ? 'n' : 's', null, offsetElement);
};
//============================================================
//TODO: let user select default
var controlsData = [
{ key: 'Grouped' },
{ key: 'Stacked', disabled: true }
];
function chart(selection) {
selection.each(function(data) {
@ -53,6 +68,10 @@ nv.models.discreteBarChart = function() {
- margin.top - margin.bottom;
chart.update = function() { selection.transition().call(chart); };
chart.container = this;
//------------------------------------------------------------
// Display No Data message if there's nothing to show.
@ -72,36 +91,52 @@ nv.models.discreteBarChart = function() {
//------------------------------------------------------------
discretebar
.width(availableWidth)
.height(availableHeight);
//------------------------------------------------------------
// Setup Scales
x = discretebar.xScale();
y = discretebar.yScale();
//------------------------------------------------------------
//------------------------------------------------------------
// Setup containers and skeleton of chart
var wrap = container.selectAll('g.nv-wrap.nv-discreteBarWithAxes').data([data]);
var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-discreteBarWithAxes').append('g');
var defsEnter = gEnter.append('defs');
var g = wrap.select('g');
gEnter.append('g').attr('class', 'nv-x nv-axis');
gEnter.append('g').attr('class', 'nv-y nv-axis');
gEnter.append('g').attr('class', 'nv-barsWrap');
g.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
//------------------------------------------------------------
var g = wrap.select('g');
//------------------------------------------------------------
// Main Chart Component(s)
discretebar
.width(availableWidth)
.height(availableHeight);
g.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
var barsWrap = g.select('.nv-barsWrap')
.datum(data.filter(function(d) { return !d.disabled }))
d3.transition(barsWrap).call(discretebar);
//------------------------------------------------------------
defsEnter.append('clipPath')
.attr('id', 'nv-x-label-clip-' + discretebar.id())
.append('rect')
.append('rect');
g.select('#nv-x-label-clip-' + discretebar.id() + ' rect')
.attr('width', x.rangeBand() * (staggerLabels ? 2 : 1))
@ -109,13 +144,17 @@ nv.models.discreteBarChart = function() {
.attr('x', -x.rangeBand() / (staggerLabels ? 1 : 2 ));
//------------------------------------------------------------
// Setup Axes
xAxis
.scale(x)
.ticks( availableWidth / 100 )
.tickSize(-availableHeight, 0);
g.select('.nv-x.nv-axis')
.attr('transform', 'translate(0,' + (y.range()[0] + ((discretebar.showValues() && y.domain()[0] < 0) ? 16 : 0)) + ')')
//d3.transition(g.select('.x.axis'))
.attr('transform', 'translate(0,' + (y.range()[0] + ((discretebar.showValues() && y.domain()[0] < 0) ? 16 : 0)) + ')');
//d3.transition(g.select('.nv-x.nv-axis'))
g.select('.nv-x.nv-axis').transition().duration(0)
.call(xAxis);
@ -128,43 +167,64 @@ nv.models.discreteBarChart = function() {
.attr('transform', function(d,i,j) { return 'translate(0,' + (j % 2 == 0 ? '0' : '12') + ')' })
yAxis
.scale(y)
.ticks( availableHeight / 36 )
.tickSize( -availableWidth, 0);
d3.transition(g.select('.nv-y.nv-axis'))
.call(yAxis);
//------------------------------------------------------------
discretebar.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?
discretebar.dispatch.on('elementMouseout.tooltip', function(e) {
dispatch.tooltipHide(e);
//============================================================
// Event Handling/Dispatching (in chart's scope)
//------------------------------------------------------------
dispatch.on('tooltipShow', function(e) {
if (tooltips) showTooltip(e, that.parentNode);
});
if (tooltips) dispatch.on('tooltipHide', nv.tooltip.cleanup);
//============================================================
//TODO: decide if this makes sense to add into all the models for ease of updating (updating without needing the selection)
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
});
return chart;
}
//============================================================
// Event Handling/Dispatching (out of chart's scope)
//------------------------------------------------------------
discretebar.dispatch.on('elementMouseover.tooltip', function(e) {
e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
dispatch.tooltipShow(e);
});
discretebar.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.discretebar = discretebar; // really just makign the accessible for discretebar.dispatch, may rethink slightly
chart.discretebar = discretebar;
chart.xAxis = xAxis;
chart.yAxis = yAxis;
d3.rebind(chart, discretebar, 'x', 'y', 'xDomain', 'yDomain', 'forceX', 'forceY', 'id', 'showValues', 'valueFormat');
chart.margin = function(_) {
if (!arguments.length) return margin;
margin = _;
@ -214,6 +274,8 @@ nv.models.discreteBarChart = function() {
return chart;
};
//============================================================
return chart;
}

Loading…
Cancel
Save