2017-09-20 6 views
2

私が取り組んでいるd3プロジェクトでアニメーションの量を減らそうとしています。d3ライブラリのアニメーション機能を最小化/制限します

これまでアニメーションの力を抑えるために、さまざまな組み込み関数を使用しようとしました。たとえば、alpha(.1) gravity(1) charge(-1000)などですが、これらの機能の中には大きな違いがないようです。

最終結果: アニメーションの開始時に巨大なバウンスを取り除きたいと思います。これは可能ですか?ここで

は私の完全なjsの関数である:私は私が投稿する管理SO質問hereと私の元のコードをマージしようとしたとhere

jsbinリンクjsfiddle

// return data for relationships between database tables 
returnTableRelationshipData = function(){ 

    data = { 
     "nodes":[ 
      { 
      "platform":"Source DB", 
      "description":"RELATIONSHIPS BETWEEN BUSINESS REFERENCES", 
      "ingested":"No", 
      "tableId":"RELAC_REFER", 
      "level1":"DAEG", 
      "level2":"url", 
      "nodeId":0 
      }, 
      { 
      "platform":"Source DB", 
      // see jsfiddle for full data 
     ] 
    }; 

    //find the node index 
    function find(f){ 
     var i = -1 
     data.nodes.forEach(function(node, index){ 
      if(node.nodeId == f) 
       i = index; 
     }); 
     return i; 
    } 

    //set the source and target index 
    data.links.forEach(function(d){ 
     d.source = find(d.source); 
     d.target = find(d.target); 
    }) 
    // used to store the number of links between two nodes. 
    var mLinkNum = {}; 

    // sort links first 
    sortLinks();         

    // set up linkIndex and linkNumer, because it may possible multiple links share the same source and target node 
    setLinkIndexAndNum(); 

    // check that we don't have empty or null values 
    checkDataNotEmpty(); 

    var w = 600, 
     h = 500; 

    var force = d3.layout.force() 
     .nodes(data.nodes) 
     .links(data.links) 
     .alpha(.1) 
     .gravity(1) 
     //.distance(150) 
     .charge(-1000) 
     .size([w, h]) 
     .start(); 

    var svg = d3.select(".graphContainer").append("svg:svg") 
     .attr("width", w) 
     .attr("height", h); 

    var path = svg.append("svg:g") 
     .selectAll("path") 
     .data(force.links()) 
     .enter().append("line") 
     .attr("class", "link") 
     .attr("x1", function(d) { 
      return d.source.x; 
     }) 
     .attr("y1", function(d) { 
      return d.source.y; 
     }) 
     .attr("x2", function(d) { 
      return d.target.x; 
     }) 
     .attr("y2", function(d) { 
      return d.target.y; 
     }); 

    var node_drag = d3.behavior.drag() 
     .on("dragstart", dragstart) 
     .on("drag", dragmove) 
     .on("dragend", dragend); 

    var circle = svg.append("svg:g") 
     .selectAll("circle") 
     .data(force.nodes()) 
     .enter().append("svg:circle") 
     .attr("r", 6) 
     .call(node_drag); 

    var text = svg.append("svg:g")         
     .selectAll("g") 
     .data(force.nodes()) 
     .enter().append("svg:g"); 

    text.append("svg:text") 
     .text(function(d){ return d.description; }); 

    /*circle.on("mousedown", function(d) { d.fixed = true; });*/ 

    force.on("tick", tick); 

    function tick() { 

     path.attr("x1", function(d) { 
      return d.source.x; 
     }) 
     .attr("y1", function(d) { 
      return d.source.y; 
     }) 
     .attr("x2", function(d) { 
      return d.target.x; 
     }) 
     .attr("y2", function(d) { 
      return d.target.y; 
     }); 

     circle.attr("transform", function(d){ 
      return "translate(" + d.x + "," + d.y + ")"; 
     }); 

     text.attr("transform", function(d){ 
      return "translate(" + d.x + "," + d.y + ")"; 
     }); 
    } 

    function dragstart(d, i) { 
     force.stop(); // stops the force auto positioning before you start dragging 
    } 

    function dragmove(d, i) { 
     d.px += d3.event.dx; 
     d.py += d3.event.dy; 
     d.x += d3.event.dx; 
     d.y += d3.event.dy; 
     tick(); 
    } 

    function dragend(d, i) { 
     //nodes.fixed = true; // fix all nodes after single drag 
     d.fixed = true; // of course set the node to fixed so the force doesn't include the node in its auto positioning stuff 
     tick(); 
     force.resume(); 
    } 

    // sort the links by source, then target 
    function sortLinks(){ 
     if(data.links != null){       
      data.links.sort(function(a,b){ 
       if(a.source > b.source){ 
        return 1; 
       }else if(a.source < b.source){ 
        return -1; 
       }else{ 
        if(a.target > b.target){ 
         return 1; 
        }if(a.target < b.target){ 
         return -1; 
        }else{ 
         return 0; 
        } 
       } 
      }); 
     } 
    } 

    //any links with duplicate source and target get an incremented 'linknum' 
    function setLinkIndexAndNum(){        
     for(var i = 0; i < data.links.length; i++){ 
      if(i != 0 && 
       data.links[i].source == data.links[i-1].source && 
       data.links[i].target == data.links[i-1].target){ 
       data.links[i].linkindex = data.links[i-1].linkindex + 1; 
      }else{ 
       data.links[i].linkindex = 1; 
      }// save the total number of links between two nodes 
      if(mLinkNum[data.links[i].target + "," + data.links[i].source] !== undefined){ 
       mLinkNum[data.links[i].target + "," + data.links[i].source] = data.links[i].linkindex; 
      }else{ 
       mLinkNum[data.links[i].source + "," + data.links[i].target] = data.links[i].linkindex; 
      } 
     } 
    } 

    function checkDataNotEmpty(){ 
     data.links.forEach(function(link, index, list) { 
      if (typeof link.source === 'undefined') { 
       console.log('undefined link', data.nodes[link.source]); 
      } 
      if (typeof link.target === 'undefined') { 
       console.log('undefined source', data.nodes[link.target]); 
      } 
     }); 
    } 
} 

returnTableRelationshipData(); 

リンク未使用のライブラリをpastebinにコピーし、jsfiddleにリンクしてください。ここではフルjsライブラリが参考になります:Unminified d3 Library v3

アニメーション機能は、ラインの周りに5807

var d3_ease_default = function() { return d3_identity; };

を開始しても、私はそれらの機能の多くをコメントアウトしようとしている、とアニメーションがそのまま実行するように見えるように見えます。

私はこれが最善の問題ではないことを認識していますが、誰かがこれを経験していると大変感謝しています。

さらに、固定ノードを使用するのはうれしいですが、フォースアニメーションが完了した後でしか修正できないと思います。

答えて

3

一つは、すべてのノードが起動していることであることを削除しません同じ位置のシミュレーション。

は、それらを広めるために試してみてください。

data.nodes.forEach(function(node){ 
    node.x = 200 + Math.random()*200; 
    node.y = 150 + Math.random()*200; 
} 

ここで、マジックナンバー200150は、単にw/2h/2マイナス100です。

ここに更新されたフィドルです:https://jsfiddle.net/nd8e5m9s/

+0

です。ありがとうございました。 – lharby

1

あなたはそれを調整し、それをバウンスさせるのいくつかのポイントに強制作るdragendイベントにd3.layoutを強制、なぜその巨大なバウンスの理由の

function dragend(d, i) { 
    //nodes.fixed = true; // fix all nodes after single drag 
    d.fixed = true; // of course set the node to fixed so the force doesn't include the node in its auto positioning stuff 
    tick(); 
    //force.resume(); 
} 
+0

ありがとうございます。いったんアニメーション化されると、ノードを修正するように見えます。私はまだアニメーションの開始時に巨大なバウンスを減らしたいと思うでしょう。 – lharby

+0

私はd3.layout.forceをバウンスさせて、リンクとノードをリサイズするようにアニメーションしたいと思うかもしれません。アニメーション化したくない場合は、d3.forceSimulation()とd3.forceLinkを使用できますが、大雑把な解決策は、完全にロードされる前に要素を隠すことです。ハードなもの(でも、たぶん多分)はd3.interpolateを使用していますが、あなたのケースに追加する方法は – KEKUATAN

関連する問題