Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's one implementation: <a href="http://jsfiddle.net/tBHyD/2/" rel="nofollow">http://jsfiddle.net/tBHyD/2/</a></p> <p>I only tried to address the setup in the question, not the full implementation noted in your comment. There are lots of ways to accomplish this; this one uses an event-driven model, using <code>d3.dispatch</code>:</p> <pre><code> var evt = d3.dispatch("change"); </code></pre> <p>The key here is to update the scale extents globally, then fire an event if they've changed. Here I use a function, <code>updateExtent</code>, for this purpose:</p> <pre><code> var x0 = Infinity, x1 = -Infinity, y0 = Infinity, y1 = -Infinity; function updateExtent(data) { var extx = d3.extent(data, function(d) { return d[0]; }), exty = d3.extent(data, function(d) { return d[1]; }), changed; // update if (extx[0] &lt; x0) { x0 = extx[0]; changed = true; } if (extx[1] &gt; x1) { x1 = extx[1]; changed = true; } if (exty[0] &lt; y0) { y0 = exty[0]; changed = true; } if (exty[1] &gt; y1) { y1 = exty[1]; changed = true; } // if changed, update scales and fire event if (changed) { // update scales x.domain([x0, x1]); y.domain([y1, y0]); // update axes vis.select(".x.axis").call(xAxis); vis.select(".y.axis").call(yAxis); // fire event evt.change(); } } </code></pre> <p>Then the <code>redraw</code> function sets a listener:</p> <pre><code> function redraw(selection, data, style) { var bar = selection.selectAll(".bar") .data(data); // enter bar.enter().append("rect") .attr('class', "bar") .attr("width", 5) .style(style); function updateBar() { // update bar .attr("x", function(d) { return x(d[0]) - .5; }) .attr("y", function(d) { return y(d[1]); }) .attr("height", function(d) { return height - y(d[1]); }); } // initial call updateBar(); // handler call evt.on("change", updateBar); }; </code></pre> <p>Note that now you don't need to set the extent explicitly:</p> <pre><code> var data1 = [[2,0.5], [4,0.8], [6,0.6], [8,0.7], [12,0.8]]; updateExtent(data1); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload