nv.models.line = function() { //============================================================ // Public Variables with Default Settings //------------------------------------------------------------ var margin = {top: 0, right: 0, bottom: 0, left: 0}, width = 960, height = 500, color = d3.scale.category20().range(), // array of colors to be used in order id = Math.floor(Math.random() * 10000), //Create semi-unique ID incase user doesn't select one getX = function(d) { return d.x }, // accessor to get the x value from a data point getY = function(d) { return d.y }, // accessor to get the y value from a data point defined = function(d,i) { return !isNaN(getY(d,i)) && getY(d,i) !== null }, // allows a line to be not continous when it is not defined isArea = function(d) { return d.area }, // decides if a line is an area or just a line clipEdge = false, // if true, masks lines within x and y scale x, y, //can be accessed via chart.scatter.[x/y]Scale() interpolate = "linear"; // controls the line interpolation //============================================================ // Private Variables //------------------------------------------------------------ var scatter = nv.models.scatter() .id(id) .size(16) // default size .sizeDomain([16,256]), //set to speed up calculation, needs to be unset if there is a cstom size accessor x0, y0, timeoutID; function chart(selection) { selection.each(function(data) { var availableWidth = width - margin.left - margin.right, availableHeight = height - margin.top - margin.bottom, container = d3.select(this); x = scatter.xScale(); y = scatter.yScale(); x0 = x0 || x; y0 = y0 || y; var wrap = container.selectAll('g.wrap.line').data([data]); var wrapEnter = wrap.enter().append('g').attr('class', 'wrap nvd3 line'); var defsEnter = wrapEnter.append('defs'); var gEnter = wrapEnter.append('g'); var g = wrap.select('g') gEnter.append('g').attr('class', 'groups'); gEnter.append('g').attr('class', 'scatterWrap'); var scatterWrap = wrap.select('.scatterWrap');//.datum(data); scatter .width(availableWidth) .height(availableHeight) d3.transition(scatterWrap).call(scatter); wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')'); defsEnter.append('clipPath') .attr('id', 'edge-clip-' + id) .append('rect'); wrap.select('#edge-clip-' + id + ' rect') .attr('width', availableWidth) .attr('height', availableHeight); g .attr('clip-path', clipEdge ? 'url(#edge-clip-' + id + ')' : ''); scatterWrap .attr('clip-path', clipEdge ? 'url(#edge-clip-' + id + ')' : ''); var groups = wrap.select('.groups').selectAll('.group') .data(function(d) { return d }, function(d) { return d.key }); groups.enter().append('g') .style('stroke-opacity', 1e-6) .style('fill-opacity', 1e-6); d3.transition(groups.exit()) .style('stroke-opacity', 1e-6) .style('fill-opacity', 1e-6) .remove(); groups .attr('class', function(d,i) { return 'group series-' + i }) .classed('hover', function(d) { return d.hover }) .style('fill', function(d,i){ return color[i % color.length] }) .style('stroke', function(d,i){ return color[i % color.length] }); d3.transition(groups) .style('stroke-opacity', 1) .style('fill-opacity', .5); var areaPaths = groups.selectAll('path.area') .data(function(d) { return [d] }); // this is done differently than lines because I need to check if series is an area areaPaths.enter().append('path') .filter(isArea) .attr('class', 'area') .attr('d', function(d) { return d3.svg.area() .interpolate(interpolate) .defined(defined) .x(function(d,i) { return x0(getX(d,i)) }) .y0(function(d,i) { return y0(getY(d,i)) }) .y1(function(d,i) { return y0( y.domain()[0] <= 0 ? y.domain()[1] >= 0 ? 0 : y.domain()[1] : y.domain()[0] ) }) //.y1(function(d,i) { return y0(0) }) //assuming 0 is within y domain.. may need to tweak this .apply(this, [d.values]) }); d3.transition(groups.exit().selectAll('path.area')) .attr('d', function(d) { return d3.svg.area() .interpolate(interpolate) .defined(defined) .x(function(d,i) { return x0(getX(d,i)) }) .y0(function(d,i) { return y0(getY(d,i)) }) .y1(function(d,i) { return y0( y.domain()[0] <= 0 ? y.domain()[1] >= 0 ? 0 : y.domain()[1] : y.domain()[0] ) }) //.y1(function(d,i) { return y0(0) }) //assuming 0 is within y domain.. may need to tweak this .apply(this, [d.values]) }); d3.transition(areaPaths.filter(isArea)) .attr('d', function(d) { return d3.svg.area() .interpolate(interpolate) .defined(defined) .x(function(d,i) { return x0(getX(d,i)) }) .y0(function(d,i) { return y0(getY(d,i)) }) .y1(function(d,i) { return y0( y.domain()[0] <= 0 ? y.domain()[1] >= 0 ? 0 : y.domain()[1] : y.domain()[0] ) }) //.y1(function(d,i) { return y0(0) }) //assuming 0 is within y domain.. may need to tweak this .apply(this, [d.values]) }); var linePaths = groups.selectAll('path.line') .data(function(d) { return [d.values] }); linePaths.enter().append('path') .attr('class', function(d) { return 'line' }) .attr('d', d3.svg.line() .interpolate(interpolate) .defined(defined) .x(function(d,i) { return x0(getX(d,i)) }) .y(function(d,i) { return y0(getY(d,i)) }) ); d3.transition(groups.exit().selectAll('path.line')) .attr('d', d3.svg.line() .interpolate(interpolate) .defined(defined) .x(function(d,i) { return x(getX(d,i)) }) .y(function(d,i) { return y(getY(d,i)) }) ); d3.transition(linePaths) .attr('d', d3.svg.line() .interpolate(interpolate) .defined(defined) .x(function(d,i) { return x(getX(d,i)) }) .y(function(d,i) { return y(getY(d,i)) }) ); //store old scales for use in transitions on update x0 = x.copy(); y0 = y.copy(); }); return chart; } //============================================================ // Global getters and setters //------------------------------------------------------------ chart.dispatch = scatter.dispatch; d3.rebind(chart, scatter, 'interactive', 'size', 'xScale', 'yScale', 'zScale', 'xDomain', 'yDomain', 'sizeDomain', 'forceX', 'forceY', 'forceSize', 'clipVoronoi', 'clipRadius'); chart.margin = function(_) { if (!arguments.length) return margin; margin = _; return chart; }; chart.width = function(_) { if (!arguments.length) return width; width = _; return chart; }; chart.height = function(_) { if (!arguments.length) return height; height = _; return chart; }; chart.x = function(_) { if (!arguments.length) return getX; getX = _; scatter.x(_); return chart; }; chart.y = function(_) { if (!arguments.length) return getY; getY = _; scatter.y(_); return chart; }; chart.clipEdge = function(_) { if (!arguments.length) return clipEdge; clipEdge = _; return chart; }; chart.color = function(_) { if (!arguments.length) return color; color = _; scatter.color(_); return chart; }; chart.id = function(_) { if (!arguments.length) return id; id = _; return chart; }; chart.interpolate = function(_) { if (!arguments.length) return interpolate; interpolate = _; return chart; }; chart.defined = function(_) { if (!arguments.length) return defined; defined = _; return chart; }; chart.isArea = function(_) { if (!arguments.length) return isArea; isArea = d3.functor(_); return chart; }; return chart; }