转换时svg rect的动画位置

时间:2017-03-08 16:17:26

标签: javascript d3.js

编辑:这是一个示例小提琴:https://jsfiddle.net/3c9dtLyh/6/

我有一个布局,我试图动画来比较两个不同日期的安排。我想要完成的是一个过渡,其中第二个日期的x,y位置不同的项目顺利地飞到新位置。我试图使用设置为触发onclick的updateData函数来执行此操作。

布局如下:

enter image description here

我并不一定希望这种方法有效,因为过渡如何知道哪些(x,y)对与新排列中的正确项目名称相对应。关于这些过渡如何发挥作用以及如何改进我的方法,我错过了什么?

这是我正在使用的代码。这是一个相对简单的追加和svg元素序列,绘制矩形,然后(失败)更新它们在点击时的位置。

<!DOCTYPE html>
<meta charset="utf-8">
<style>

body {
  font: 10px sans-serif;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.dot {
  stroke: #000;
}
</style>
<body>
    <div id = "chart">
    </div>
    <div id = "select_params">

    <input name="updateButton"
           type="button"
           value="Update"
           onclick="updateData()" />

    </div>
</body>

<!-- load js libraries -->
<script src="https://d3js.org/d3.v4.min.js"></script>               <!-- uses v4 of d3 -->
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>       <!-- need to use this older version for tipsy -->
<script type="text/javascript" src="jquery.tipsy.js"></script>  <!-- load from locally hosted source code -->

<!-- build the visualization -->
<script type='text/javascript'>
var item_width = 40, item_height = 60;

var margin = {top: 20, right: 50, bottom: 75, left: 40},
    width = 700 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var x = d3.scaleLinear()
    .range([0, width]);

var y = d3.scaleLinear()
    .range([height, 0]);

var color = d3.scaleOrdinal(d3.schemeCategory10);

var svg = d3.select("#chart").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 + ")");

d3.csv("http://localhost:8080/udacity_test_vis_1/combined_item_pos.csv", function(data) {

  // cast string to numeric
  data.forEach(function(d) {
    d.x_pos = +d.x_pos;
    d.y_pos = +d.y_pos;
    d.sales = +d.sales;
  });

  console.log(data);

  var x_offset = 5, y_offset = 5;

  x.domain(d3.extent(data, function(d) { return d.x_pos; }));        // set the x domain
  y.domain(d3.extent(data, function(d) { return d.y_pos; }));            // set the y domain

  svg.selectAll("g")
      .data(data)
    .enter()
      .append("rect")
      .filter(function(d){ return d.date == '1-20-2017'})
      .attr("class", "dot")
      .attr("width", item_width)
      .attr("height", item_height)
      .attr("x", function(d) { return x(d.x_pos) + x_offset; })              // x position of dots
      .attr("y", function(d) { return y(d.y_pos) + y_offset; })             // y position of dots
      .attr("rx", 5)
      .attr("ry", 5)
      .style("fill", "#1f5fc6")     // color factor variable
      .style("fill-opacity", 0.5);

  svg.selectAll("g")
       .data(data)
    .enter()
       .append("text")
       .filter(function(d){ return d.date == '1-20-2017'})
       .attr("x", function(d) { return x(d.x_pos) + item_width/2 + x_offset; })
       .attr("y", function(d) { return y(d.y_pos) + item_height/2 + y_offset; })
       .attr("font-size", 10)
       .attr("text-anchor", "middle")
       .attr("fill", "black")
       .text(function(d){ return d.item_name});

});


function updateData() {

    // grab the data again
    d3.csv("http://localhost:8080/udacity_test_vis_1/combined_item_pos.csv", function(data) {

      // cast string to numeric
      data.forEach(function(d) {
        d.x_pos = +d.x_pos;
        d.y_pos = +d.y_pos;
        d.sales = +d.sales;
      });

      var svg = d3.select("#chart").transition();

      svg.selectAll("g")
          .data(data)
            .enter()
          .append("rect")
          .filter(function(d){ return d.date == '2-10-2017'})
          .attr("class", "dot")
          .attr("width", item_width)
          .attr("height", item_height)
          .attr("x", function(d) { return x(d.x_pos) + x_offset; })              // x position of dots
          .attr("y", function(d) { return y(d.y_pos) + y_offset; })             // y position of dots
          .attr("rx", 5)
          .attr("ry", 5)
          .style("fill", "#1f5fc6")     // color factor variable
          .style("fill-opacity", 0.5);

    svg.selectAll("g")
        .data(data)
          .enter()
        .append("text")
        .filter(function(d){ return d.date == '2-10-2017'})
        .attr("x", function(d) { return x(d.x_pos) + item_width/2 + x_offset; })
        .attr("y", function(d) { return y(d.y_pos) + item_height/2 + y_offset; })
        .attr("font-size", 10)
        .attr("text-anchor", "middle")
        .attr("fill", "black")
        .text(function(d){ return d.item_name});


});
}

</script>

这是我的数据:

,x_pos,y_pos,item_name,sales,date
0,1,1,S8221,2022,1-20-2017
1,2,1,NLC11,518,1-20-2017
2,3,1,35UUY,1614,1-20-2017
3,4,1,PPTNV,1059,1-20-2017
4,5,1,G0CWS,2183,1-20-2017
5,6,1,3JHUA,2513,1-20-2017
6,7,1,4HXGA,2251,1-20-2017
7,8,1,RYM9K,2330,1-20-2017
8,9,1,T8PUB,1476,1-20-2017
9,10,1,PLULW,1225,1-20-2017
10,1,2,YJ6S0,2403,1-20-2017
11,2,2,E9RGD,1361,1-20-2017
12,3,2,E2SW4,1131,1-20-2017
13,4,2,BZPGX,698,1-20-2017
14,5,2,0K682,1855,1-20-2017
15,6,2,D8UZW,2371,1-20-2017
16,7,2,USKY7,1851,1-20-2017
17,8,2,D0L0Y,1767,1-20-2017
18,9,2,P1AGP,1025,1-20-2017
19,10,2,9LT7O,1380,1-20-2017
20,1,3,1J184,1108,1-20-2017
21,2,3,RJDEG,2106,1-20-2017
22,3,3,LTSLR,1980,1-20-2017
23,4,3,ET3DF,2700,1-20-2017
24,5,3,42W1W,2194,1-20-2017
25,6,3,5QTJN,958,1-20-2017
26,7,3,O8XKY,2381,1-20-2017
27,8,3,LS9NW,516,1-20-2017
28,9,3,0MPZ7,2198,1-20-2017
29,10,3,R4E3J,2494,1-20-2017
30,1,4,WFPPY,2349,1-20-2017
31,2,4,MT2DB,2525,1-20-2017
32,3,4,6DRYS,600,1-20-2017
33,4,4,NVV0S,1556,1-20-2017
34,5,4,ODGZ2,912,1-20-2017
35,6,4,E3NLS,931,1-20-2017
36,7,4,9FFZ7,722,1-20-2017
37,8,4,UKZGF,2170,1-20-2017
38,9,4,XXORI,896,1-20-2017
39,10,4,QYU9Q,1104,1-20-2017
40,1,5,4KQPU,1562,1-20-2017
41,2,5,S3AYK,2298,1-20-2017
42,3,5,5W3CE,2580,1-20-2017
43,4,5,T0S7H,1677,1-20-2017
44,5,5,02SJG,1972,1-20-2017
45,6,5,GBMNZ,1845,1-20-2017
46,7,5,2Y7KH,982,1-20-2017
47,8,5,3WMOL,1952,1-20-2017
48,9,5,93KLU,2240,1-20-2017
49,10,5,K80OQ,2467,1-20-2017
50,1,6,2SIJS,1788,1-20-2017
51,2,6,5ZJ7V,2277,1-20-2017
52,3,6,HTL99,873,1-20-2017
53,4,6,C06QP,2185,1-20-2017
54,5,6,2S1YI,580,1-20-2017
55,6,6,IQ0L8,2395,1-20-2017
56,7,6,PEE2Y,2299,1-20-2017
57,8,6,6DEWK,2019,1-20-2017
58,9,6,9FY5B,1517,1-20-2017
59,10,6,NZQ54,2624,1-20-2017
60,1,7,C4SVV,1823,1-20-2017
61,2,7,Q4C4I,2339,1-20-2017
62,3,7,996OQ,1621,1-20-2017
63,4,7,PISK6,895,1-20-2017
64,5,7,KOKHE,1315,1-20-2017
65,6,7,6P4FT,1467,1-20-2017
66,7,7,3FY75,2085,1-20-2017
67,8,7,9YCNB,992,1-20-2017
68,9,7,NXXK1,2080,1-20-2017
69,10,7,4RDHV,2031,1-20-2017
0,6,1,9FFZ7,592,2-10-2017
1,1,6,E2SW4,622,2-10-2017
2,6,7,PLULW,1699,2-10-2017
3,8,3,ET3DF,784,2-10-2017
4,9,4,KOKHE,1092,2-10-2017
5,2,6,5ZJ7V,1691,2-10-2017
6,4,5,9FY5B,630,2-10-2017
7,9,4,G0CWS,1523,2-10-2017
8,9,2,PISK6,1778,2-10-2017
9,6,4,35UUY,2107,2-10-2017
10,3,5,5QTJN,1751,2-10-2017
11,6,6,NLC11,526,2-10-2017
12,8,2,C06QP,2308,2-10-2017
13,8,3,XXORI,1453,2-10-2017
14,5,1,E9RGD,1864,2-10-2017
15,7,2,HTL99,1222,2-10-2017
16,3,3,PEE2Y,2050,2-10-2017
17,9,7,GBMNZ,1941,2-10-2017
18,3,1,T8PUB,1440,2-10-2017
19,5,1,3WMOL,2692,2-10-2017
20,7,7,S3AYK,523,2-10-2017
21,1,5,BZPGX,2245,2-10-2017
22,2,1,S8221,2241,2-10-2017
23,9,7,IQ0L8,566,2-10-2017
24,8,5,D8UZW,1769,2-10-2017
25,3,1,RYM9K,1044,2-10-2017
26,4,6,4HXGA,2650,2-10-2017
27,2,2,WFPPY,2203,2-10-2017
28,2,4,93KLU,2289,2-10-2017
29,7,3,P1AGP,1084,2-10-2017
30,4,3,3JHUA,1364,2-10-2017
31,1,4,9LT7O,1198,2-10-2017
32,4,6,4RDHV,771,2-10-2017
33,10,7,T0S7H,873,2-10-2017
34,3,6,NXXK1,2391,2-10-2017
35,8,2,2SIJS,811,2-10-2017
36,8,4,LTSLR,1670,2-10-2017
37,6,7,02SJG,1880,2-10-2017
38,9,3,0MPZ7,2090,2-10-2017
39,2,6,E3NLS,2350,2-10-2017
40,7,6,QYU9Q,1092,2-10-2017
41,6,3,0K682,894,2-10-2017
42,1,5,LS9NW,1928,2-10-2017
43,7,7,NVV0S,951,2-10-2017
44,9,4,996OQ,670,2-10-2017
45,7,6,USKY7,706,2-10-2017
46,10,4,Q4C4I,2270,2-10-2017
47,4,2,UKZGF,1691,2-10-2017
48,10,3,RJDEG,597,2-10-2017
49,10,2,1J184,1921,2-10-2017
50,2,3,5W3CE,2604,2-10-2017
51,5,5,3FY75,1260,2-10-2017
52,1,1,6DEWK,2491,2-10-2017
53,7,5,9YCNB,1743,2-10-2017
54,4,7,6DRYS,2450,2-10-2017
55,5,2,MT2DB,1292,2-10-2017
56,8,5,C4SVV,1395,2-10-2017
57,3,7,ODGZ2,2685,2-10-2017
58,10,4,2S1YI,2617,2-10-2017
59,1,2,YJ6S0,1611,2-10-2017
60,6,3,2Y7KH,2188,2-10-2017
61,5,4,4KQPU,1413,2-10-2017
62,10,1,D0L0Y,2291,2-10-2017
63,5,1,NZQ54,1405,2-10-2017
64,5,2,6P4FT,1885,2-10-2017
65,3,1,PPTNV,1442,2-10-2017
66,1,5,K80OQ,2140,2-10-2017
67,4,5,42W1W,1697,2-10-2017
68,2,7,O8XKY,1007,2-10-2017
69,10,6,R4E3J,887,2-10-2017

2 个答案:

答案 0 :(得分:3)

因此,我花了几分钟时间将代码完全重构为正确的d3样式。这旨在展示一些事情:

  1. 正确使用enterupdateexit模式。
  2. 删除剪切/粘贴重复代码。
  3. 使用g对元素进行分组并将它们放在一起的正确方法。
  4. 如何添加转场。
  5. 这是code running

    评论代码:

    <!DOCTYPE html>
    <meta charset="utf-8">
    <style>
      body {
        font: 10px sans-serif;
      }
    
      .axis path,
      .axis line {
        fill: none;
        stroke: #000;
        shape-rendering: crispEdges;
      }
    
      .dot {
        stroke: #000;
      }
    </style>
    
    <body>
      <div id="chart">
      </div>
      <div id="select_params">
    
        <input name="updateButton" type="button" value="Update" onclick="updateData()" />
    
      </div>
    </body>
    
    <!-- load js libraries -->
    <script src="https://d3js.org/d3.v4.min.js"></script>
    <!-- uses v4 of d3 -->
    
    <!-- build the visualization -->
    <script type='text/javascript'>
      var item_width = 40,
        item_height = 60;
    
      var margin = {
          top: 20,
          right: 50,
          bottom: 75,
          left: 40
        },
        width = 700 - margin.left - margin.right,
        height = 500 - margin.top - margin.bottom;
    
      var x = d3.scaleLinear()
        .range([0, width]);
    
      var y = d3.scaleLinear()
        .range([height, 0]);
    
      var color = d3.scaleOrdinal(d3.schemeCategory10);
    
      var svg = d3.select("#chart").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 + ")");
    
      // a single function to draw 
      function draw(data, someDate) {
    
        data.forEach(function(d) {
          d.x_pos = +d.x_pos;
          d.y_pos = +d.y_pos;
          d.sales = +d.sales;
        });
    
        // pre-filter data
        data = data.filter(function(d) {
          return d.date === someDate
        });
    
        var x_offset = 5,
          y_offset = 5;
    
        x.domain(d3.extent(data, function(d) {
          return d.x_pos;
        })); // set the x domain
        y.domain(d3.extent(data, function(d) {
          return d.y_pos;
        })); // set the y domain
    
        // create an update selection with a key function
        var g_sel = svg.selectAll("g")
          .data(data, function(d) {
            return d.item_name;
          });
    
        // get rid of those leaving the update
        g_sel.exit().remove();
    
        // our entering g
        var g_ent = g_sel.enter()
          .append("g");
    
        // add our rects to our g
        g_ent.append("rect")
          .attr("class", "dot")
          .attr("width", item_width)
          .attr("height", item_height)
          .attr("rx", 5)
          .attr("ry", 5)
          .style("fill", "#1f5fc6") // color factor variable
          .style("fill-opacity", 0.5);
    
        // add our text to our g
        g_ent.append("text")
          .attr("font-size", 10)
          .attr("text-anchor", "middle")
          .attr("fill", "black")
          .attr("dx", item_width / 2)
          .attr("dy", item_height / 2)
          .text(function(d) {
            return d.item_name
          });
    
        // UPDATE + ENTER selection
        g_sel = g_ent.merge(g_sel);
    
        // move them into position with transition
        g_sel
          .transition()
          .attr("transform", function(d) {
            return "translate(" + (x(d.x_pos) + x_offset) + "," + (y(d.y_pos) + y_offset) + ")";
          });
      }
    
      d3.csv("test.csv", function(data) {
    
        draw(data, '1-20-2017');
    
      });
    
    
      function updateData() {
    
        d3.csv("test.csv", function(data) {
          draw(data, '2-10-2017');
        });
    
      }
    </script>
    

答案 1 :(得分:3)

继承我的尝试:https://jsfiddle.net/guanzo/3c9dtLyh/10/

有多个数据点共享相同的位置,这就是一些矩形重叠的原因。我对您的代码进行了很多更改,导致重复次数减少。

您的数据包含具有不同日期/位置的重复item_names,但在您的可视化中,您似乎只想在单个日期显示项目。因此,您只需要传递特定日期的d3数据,而不是传递d3所有数据然后过滤。

您的代码:

svg.selectAll("g")
      .data(data)
    .enter()
      .append("rect")
      .filter(function(d){ return d.date == '1-20-2017'})

我的代码:

var firstDateData = data.filter(d=>d.date == '1-20-2017');

  var groups = svg.selectAll("g")
      .data(firstDateData, d=> d.item_name)

这两者之间的区别在于,在我的示例中,D3只知道日期item_names上的一组1-20-2017。因此,当我在日期item_names上使用2-10-2017更新日期时,D3会自动将所有更新的矩形移动到新位置。怎么样?

以下是您的问题发生的地方:

  

我并不一定希望这种方法有效,因为会怎样   转换知道哪些(x,y)对对应于正确的项目   新安排中的名称

这是因为我将每个矩形与item_name相关联。 D3s data函数可以使用可选的第二个参数来指定数据如何绑定到矩形。这被称为关键功能。

svg.selectAll("g").data(firstDateData, d=> d.item_name)

在这种情况下,我告诉d3每个组(矩形及其文本)都绑定到item_name。因此,下次我将数据传递给D3时,它会尝试将现有元素(与item_name相关联)与数据(包含item_names)匹配。如果在我的新数据中,我传递了与现有元素对应的item_name,并且数据包含新的x和y位置,则D3将移动到元素到新位置。

请注意,即使我在谈论矩形,我也会将数据绑定到包含矩形和文本的g元素。

随意提出任何问题,我做了很多改变,我没有讨论过。