UNPKG

nvd3-fork

Version:

FORK! of NVD3, a reusable charting library written in d3.js

233 lines (189 loc) 8.9 kB
nv.models.boxPlotChart = function() { "use strict"; //============================================================ // Public Variables with Default Settings //------------------------------------------------------------ var boxplot = nv.models.boxPlot(), xAxis = nv.models.axis(), yAxis = nv.models.axis(); var margin = {top: 15, right: 10, bottom: 50, left: 60}, width = null, height = null, color = nv.utils.getColor(), showXAxis = true, showYAxis = true, rightAlignYAxis = false, staggerLabels = false, tooltip = nv.models.tooltip(), x, y, noData = 'No Data Available.', dispatch = d3.dispatch('beforeUpdate', 'renderEnd'), duration = 250; xAxis .orient('bottom') .showMaxMin(false) .tickFormat(function(d) { return d }) ; yAxis .orient((rightAlignYAxis) ? 'right' : 'left') .tickFormat(d3.format(',.1f')) ; tooltip.duration(0); //============================================================ // Private Variables //------------------------------------------------------------ var renderWatch = nv.utils.renderWatch(dispatch, duration); function chart(selection) { renderWatch.reset(); renderWatch.models(boxplot); if (showXAxis) renderWatch.models(xAxis); if (showYAxis) renderWatch.models(yAxis); selection.each(function(data) { var container = d3.select(this), that = this; nv.utils.initSVG(container); var availableWidth = (width || parseInt(container.style('width')) || 960) - margin.left - margin.right; var availableHeight = (height || parseInt(container.style('height')) || 400) - margin.top - margin.bottom; chart.update = function() { dispatch.beforeUpdate(); container.transition().duration(duration).call(chart); }; chart.container = this; // TODO still need to find a way to validate quartile data presence using boxPlot callbacks. // Display No Data message if there's nothing to show. (quartiles required at minimum). if (!data || !data.length) { var noDataText = container.selectAll('.nv-noData').data([noData]); noDataText.enter().append('text') .attr('class', 'nvd3 nv-noData') .attr('dy', '-.7em') .style('text-anchor', 'middle'); noDataText .attr('x', margin.left + availableWidth / 2) .attr('y', margin.top + availableHeight / 2) .text(function(d) { return d }); return chart; } else { container.selectAll('.nv-noData').remove(); } // Setup Scales x = boxplot.xScale(); y = boxplot.yScale().clamp(true); // Setup containers and skeleton of chart var wrap = container.selectAll('g.nv-wrap.nv-boxPlotWithAxes').data([data]); var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-boxPlotWithAxes').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') .append('g').attr('class', 'nv-zeroLine') .append('line'); gEnter.append('g').attr('class', 'nv-barsWrap'); g.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')'); if (rightAlignYAxis) { g.select('.nv-y.nv-axis') .attr('transform', 'translate(' + availableWidth + ',0)'); } // Main Chart Component(s) boxplot.width(availableWidth).height(availableHeight); var barsWrap = g.select('.nv-barsWrap') .datum(data.filter(function(d) { return !d.disabled })) barsWrap.transition().call(boxplot); defsEnter.append('clipPath') .attr('id', 'nv-x-label-clip-' + boxplot.id()) .append('rect'); g.select('#nv-x-label-clip-' + boxplot.id() + ' rect') .attr('width', x.rangeBand() * (staggerLabels ? 2 : 1)) .attr('height', 16) .attr('x', -x.rangeBand() / (staggerLabels ? 1 : 2 )); // Setup Axes if (showXAxis) { xAxis .scale(x) .ticks( nv.utils.calcTicksX(availableWidth/100, data) ) .tickSize(-availableHeight, 0); g.select('.nv-x.nv-axis').attr('transform', 'translate(0,' + y.range()[0] + ')'); g.select('.nv-x.nv-axis').call(xAxis); var xTicks = g.select('.nv-x.nv-axis').selectAll('g'); if (staggerLabels) { xTicks .selectAll('text') .attr('transform', function(d,i,j) { return 'translate(0,' + (j % 2 === 0 ? '5' : '17') + ')' }) } } if (showYAxis) { yAxis .scale(y) .ticks( Math.floor(availableHeight/36) ) // can't use nv.utils.calcTicksY with Object data .tickSize( -availableWidth, 0); g.select('.nv-y.nv-axis').call(yAxis); } // Zero line g.select('.nv-zeroLine line') .attr('x1',0) .attr('x2',availableWidth) .attr('y1', y(0)) .attr('y2', y(0)) ; //============================================================ // Event Handling/Dispatching (in chart's scope) //------------------------------------------------------------ }); renderWatch.renderEnd('nv-boxplot chart immediate'); return chart; } //============================================================ // Event Handling/Dispatching (out of chart's scope) //------------------------------------------------------------ boxplot.dispatch.on('elementMouseover.tooltip', function(evt) { tooltip.data(evt).hidden(false); }); boxplot.dispatch.on('elementMouseout.tooltip', function(evt) { tooltip.data(evt).hidden(true); }); boxplot.dispatch.on('elementMousemove.tooltip', function(evt) { tooltip(); }); //============================================================ // Expose Public Variables //------------------------------------------------------------ chart.dispatch = dispatch; chart.boxplot = boxplot; chart.xAxis = xAxis; chart.yAxis = yAxis; chart.tooltip = tooltip; chart.options = nv.utils.optionsFunc.bind(chart); chart._options = Object.create({}, { // simple options, just get/set the necessary values width: {get: function(){return width;}, set: function(_){width=_;}}, height: {get: function(){return height;}, set: function(_){height=_;}}, staggerLabels: {get: function(){return staggerLabels;}, set: function(_){staggerLabels=_;}}, showXAxis: {get: function(){return showXAxis;}, set: function(_){showXAxis=_;}}, showYAxis: {get: function(){return showYAxis;}, set: function(_){showYAxis=_;}}, tooltipContent: {get: function(){return tooltip;}, set: function(_){tooltip=_;}}, noData: {get: function(){return noData;}, set: function(_){noData=_;}}, // options that require extra logic in the setter margin: {get: function(){return margin;}, set: function(_){ margin.top = _.top !== undefined ? _.top : margin.top; margin.right = _.right !== undefined ? _.right : margin.right; margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom; margin.left = _.left !== undefined ? _.left : margin.left; }}, duration: {get: function(){return duration;}, set: function(_){ duration = _; renderWatch.reset(duration); boxplot.duration(duration); xAxis.duration(duration); yAxis.duration(duration); }}, color: {get: function(){return color;}, set: function(_){ color = nv.utils.getColor(_); boxplot.color(color); }}, rightAlignYAxis: {get: function(){return rightAlignYAxis;}, set: function(_){ rightAlignYAxis = _; yAxis.orient( (_) ? 'right' : 'left'); }} }); nv.utils.inheritOptions(chart, boxplot); nv.utils.initOptions(chart); return chart; }