toolkit/javascript/d3/examples/contour/contour.html
changeset 47 c0b4a8b5a012
equal deleted inserted replaced
46:efd9c589177a 47:c0b4a8b5a012
       
     1 <!DOCTYPE html>
       
     2 <html>
       
     3   <head>
       
     4     <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
       
     5     <title>Contour</title>
       
     6     <script type="text/javascript" src="../../d3.js"></script>
       
     7     <script type="text/javascript" src="../../d3.geom.js"></script>
       
     8     <style type="text/css">
       
     9 svg {
       
    10   border: solid 1px #aaa;
       
    11 }
       
    12 path {
       
    13   stroke: red;
       
    14   stroke-width: 2px;
       
    15   fill: none;
       
    16 }
       
    17 rect {
       
    18   fill: lightsteelblue;
       
    19   stroke: #eee;
       
    20   stroke-width: 2px;
       
    21 }
       
    22 rect.d1 {
       
    23   fill: steelblue;
       
    24 }
       
    25 rect.d2 {
       
    26   fill: darkblue;
       
    27 }
       
    28     </style>
       
    29   </head>
       
    30   <body>
       
    31     <script type="text/javascript">
       
    32 
       
    33 function grid0(x,y) {
       
    34   if (x < 0 || y < 0 || x >= dw || y >= dh) return 0;
       
    35   return data[y][x];
       
    36 }
       
    37 function grid1(x,y) {
       
    38   if (x < 0 || y < 0 || x >= dw || y >= dh) return 0;
       
    39   return data[y][x]==1;
       
    40 }
       
    41 function grid2(x,y) {
       
    42   if (x < 0 || y < 0 || x >= dw || y >= dh) return 0;
       
    43   return data[y][x]==2;
       
    44 }
       
    45 	
       
    46 var data = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       
    47             [0, 0, 0, 2, 2, 2, 0, 0, 0, 0],
       
    48             [0, 0, 0, 2, 2, 2, 2, 0, 0, 0],
       
    49             [0, 0, 2, 2, 2, 2, 2, 2, 0, 0],
       
    50             [0, 0, 2, 2, 2, 2, 2, 2, 2, 0],
       
    51             [0, 0, 2, 1, 0, 0, 2, 2, 2, 0],
       
    52             [0, 0, 1, 1, 0, 0, 2, 2, 2, 0],
       
    53             [0, 0, 0, 0, 1, 1, 2, 2, 0, 0],
       
    54             [0, 0, 0, 0, 1, 2, 2, 2, 0, 0],
       
    55             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
       
    56     fn = [grid0, grid1, grid2],
       
    57     dw = data[0].length,
       
    58     dh = data.length;
       
    59     sz = 30,
       
    60     grid = grid0;
       
    61 
       
    62 function translateY(d, i) { return "translate(0,"+(i*sz)+")"; }
       
    63 function translateX(d, i) { return "translate("+(i*sz)+",0)"; }
       
    64 function scale(p) { return [p[0]*sz, p[1]*sz]; }
       
    65 
       
    66 var svg = d3.select("body")
       
    67  .append("svg:svg")
       
    68   .attr("width", dw*sz)
       
    69   .attr("height", dh*sz);
       
    70 
       
    71 var g = svg.selectAll("g")
       
    72   .data(data)
       
    73  .enter().append("svg:g")
       
    74   .attr("transform", translateY);
       
    75 
       
    76 g.selectAll("rect")
       
    77   .data(function(d) { return d; })
       
    78  .enter().append("svg:rect")
       
    79   .attr("transform", translateX)
       
    80   .attr("width", sz)
       
    81   .attr("height", sz)
       
    82   .attr("class", function(d) { return "d"+d; })
       
    83   .on("mouseover", mouseover);
       
    84 
       
    85 contour(grid);
       
    86 
       
    87 function contour(grid, start) {
       
    88   svg.selectAll("path")
       
    89     .data([d3.geom.contour(grid, start).map(scale)])
       
    90     .attr("d", function(d) { return "M" + d.join("L") + "Z"; })
       
    91    .enter().append("svg:path")
       
    92     .attr("d", function(d) { return "M" + d.join("L") + "Z"; });
       
    93 }
       
    94 
       
    95 function mouseover(d,i) {
       
    96   var s = undefined;
       
    97   grid = fn[d];
       
    98   if (d > 0) {
       
    99 	// map mouse to grid coords, then find left edge
       
   100     var p = d3.svg.mouse(svg[0][0]);
       
   101     s = [Math.floor(p[0]/sz), Math.floor(p[1]/sz)];
       
   102     while (grid(s[0]-1,s[1])) s[0]--;
       
   103   }
       
   104   contour(grid, s);
       
   105 }
       
   106     </script>
       
   107   </body>
       
   108 </html>