function makeScaleLegend(svg, root, width, colors) { var gradient = svg.append('svg:defs') .append('svg:linearGradient') .attr('id', 'gradient') .attr('x1', '0%') .attr('y1', '0%') .attr('x2', '100%') .attr('y2', '0%') .attr('spreadMethod', 'pad'); gradient.append('svg:stop') .attr('offset', '0%') .attr('stop-color', colors(0)) .attr('stop-opacity', 1); gradient.append('svg:stop') .attr('offset', '100%') .attr('stop-color', colors(1)) .attr('stop-opacity', 1); root.append('svg:rect') .attr('width', width) .attr('height', 6) .style('stroke-width', 0) .style('fill', 'url(#gradient)'); var scale = d3.scale.linear() .domain([1, 2]) .range([0, width]); var axis = d3.svg.axis() .scale(scale) .ticks(1) .tickSize(6) .orient('bottom'); var legend = root.append('g') .attr('transform', 'translate(0,6)') .attr('class', 'axis') .call(axis); function update(minMax, duration) { scale.domain(!(minMax.max - minMax.min) ? [minMax.min - 0.5, minMax.max + 0.5] : [minMax.min, minMax.max]); var ticks = Math.min(8, (minMax.max - minMax.min) || 1); axis.ticks(ticks); legend.transition().duration(duration).call(axis); } return { update: update }; }