Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<meta charset="utf-8">
<title>Zoom + Pan</title>
<style>
svg {
  font: 10px sans-serif;
  shape-rendering: crispEdges;
}
.bg {
  fill: #ddd;
}
.axis path, .axis line {
  fill: none;
  stroke: #fff;
}
  #labels text {
    font: 11px sans-serif
    stroke: #000;
  }
</style>
<body>
  <button id="add">Add</button>
  <button id="reset">Reset</button>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var color = d3.scale.category20();
  
var position = [0, 1, 2, 3, 4, 5, 6, 7];
  
var unit = 10,
    boardWidth = 100, //in units
    boardHeight = 50, 
    tileWidth = 10,
    tileHeight = 10,
    labelPadding = 1;
  
var margin = {top: 20, right: 20, bottom: 30, left: 40},
    width = boardWidth * unit,
    height = boardHeight * unit;
var tileX = function(i) { return position[i] % boardWidth * tileWidth; }
var tileY = function(i) { return Math.floor(position[i] / boardHeight) * tileHeight;}
  
var x = d3.scale.linear()
    .domain([-50, 50])
    .range([0, width]);
var y = d3.scale.linear()
    .domain([-50, 50])
    .range([height, 0]);
var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom")
    .tickSize(-height);
var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left")
    .ticks(5)
    .tickSize(-width);
  
  var zoom = d3.behavior.zoom()
      .x(x)
      .y(y)
      .scaleExtent([1, 2])
      .on("zoom", zoomed);
  var svg = d3.select("body").append("svg")
      .attr("width", width + margin.left + margin.right)
      .attr("height", height + margin.top + margin.bottom)
    .append("g")
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
      .call(zoom);
  svg.append("rect")
      .attr("class", "bg")
      .attr("width", width)
      .attr("height", height);
  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);
  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis);
  
  var dataset = [];
  var tiles = svg.append("g")
    .attr("clip-path", "url(#clip)")
    .attr("id", "blocks")
    .selectAll("rect")
    .data(dataset)
    .enter()
    .append("rect")
    .attr("fill", function(d, i) { return color(i) })
    .attr("x", function(d, i) { return x(tileX(i))})
    .attr("y", function(d, i) { return y(tileY(i)+tileHeight)} )
    .attr("width", tileWidth * unit)
    .attr("height", tileHeight * unit);
  
  function zoomed() {
    svg.select(".x.axis").call(xAxis);
    svg.select(".y.axis").call(yAxis);
    svg.select("#blocks").attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");    
  }
 
  d3.select("#add").on("click", function(){
       
     dataset.push(Math.random());
    
        d3.select("#blocks").selectAll("rect")
        .data(dataset)
        .enter()
        .append("rect")
        .attr("fill", function(d, i){ return color(i) })
        .attr("x", function(d, i) { return x(tileX(i)) })
        .attr("y", function(d, i) { return y(tileY(i)) } )
        .attr("width", tileWidth * unit * .01)
        .attr("height", tileHeight * unit)      
        .attr("transform", "translate(" + zoom.translate().map(function(a) { return -a })+ ")")      
        .transition(1000)
        .attr("width", tileWidth * unit).call(zoom);   
  });
  d3.select("#reset").on("click", function(){
    dataset = [];
    d3.select("#blocks").selectAll("rect").data(dataset).exit().remove();
  });
</script>
</body>
<html>
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
alphageekpro
0viewers