1

数か月間 d3.js から離れていました...そして、他の誰かが始めた機能を備えた単純な米国の地図を継承しました。

機能は、さまざまなサイズの単純なドットで表されます。

マイク・ボストックによる古典的なオニオンの例に似た、各ドットに発散する同心円を追加したい: http://bl.ocks.org/mbostock/4503672 (それほど不吉な見た目ではないかもしれません)

ここにブロックを設定しました: http://bl.ocks.org/mbostock/4503672

(状態がブロックで正しくレンダリングされない理由はわかりませんが、おそらく問題にはなりません。)

マイクの例では点が 1 つしかないので、彼が行ったことを私が得たもの (多くの点) に変換する方法を理解するのに苦労しています。

これが私のスクリプトです:

/**
 * Page initialization
 */
$(function() {
    renderMap('#map-container');
});

function renderMap(container) {
    var width = 960,
        height = 500,
        active;

    var projection = d3.geo.albersUsa()
        .scale(960)
        .translate([width / 2, height / 2]);

    var path = d3.geo.path()
        .projection(projection);

    var radius = d3.scale.sqrt()
        .domain([0, 1e7])
        .range([0, 10]);

    var path2 = d3.geo.path()
        .projection(projection);

    //  Remove svg, if already exist
    d3.select(container).select('svg').remove();

    var svg = d3.select(container).append("svg")
        .attr("width", width)
        .attr("height", height);

    svg.append("rect")
        .attr("width", width)
        .attr("height", height);
        //.on("click", reset);

    var g = svg.append("g");

    queue()
        .defer(d3.json, "/mbostock/raw/4090846/us.json")
        .defer(d3.json, "dots.json")
        .await(function (error, us, centroid) {
            g.append("g")
                .attr("id", "states")
                .selectAll("path")
                .data(topojson.feature(us, us.objects.states).features)
                .enter().append("path")
                .attr("d", path)
                .attr("class", "state");
                //.on('click', click);

            g.append('path')
                .attr("id", "state-borders")
                .datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
                .attr("d", path)
                .attr("class", "mesh");

            var dots = g.append("g")
                .attr("id", "dots")
                .selectAll("path")
                .data(centroid.data)
              .enter().append("path")
                .attr("class", "dot")
                .attr("d", path2.pointRadius(function(d) { return radius(d.properties.pool); }));
        }    
    );

 }

リングを作成するためのマイクの例の重要な部分は次のとおりです。

setInterval(function() {
  svg.append("circle")
      .attr("class", "ring")
      .attr("transform", "translate(" + projection([100, -8]) + ")")
      .attr("r", 6)
      .style("stroke-width", 3)
      .style("stroke", "red")
    .transition()
      .ease("linear")
      .duration(6000)
      .style("stroke-opacity", 1e-6)
      .style("stroke-width", 1)
      .style("stroke", "brown")
      .attr("r", 160)
      .remove();
}, 750);

リングをドットに配置するにはどうすればよいですか?

4

1 に答える 1

2

2 つの方法の違いを確認して、関数型/宣言型プログラミングが反復型プログラミングの苦痛をどのように抽象化するかについてもう少し学びましょう。

D3 イディオムによるアプローチ:

フィドル: http://jsfiddle.net/blakedietz/E66eT/1/

更新: D3 ウェイ

<!DOCTYPE html>
<html>
<head>
    <title></title>

    <style>
        body {
              background: #192887;
            }

            .graticule {
              fill: none;
              stroke: #fff;
              stroke-width: .5px;
            }

            .land {
              fill: #007421;
            }

            .dot {
              fill: #c7141a;
            }

            .ring {
              fill: none;
              stroke: #c7141a;
            }
    </style>

    <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>
<body>
<script>

var width = 960,
    height = 500;

var projection = d3.geo.mercator()
    .center([113, -3])
    .scale(1275)
    .translate([width / 2, height / 2])
    .clipExtent([[0, 0], [width, height]])
    .precision(.1);

var path = d3.geo.path()
    .projection(projection);

var graticule = d3.geo.graticule()
    .step([5, 5]);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

svg.append("path")
    .datum(graticule)
    .attr("class", "graticule")
    .attr("d", path);


var data = [{x:-8,y:100},{x:-10,y:110},{x: -12,y:120}];

svg.selectAll("circle")
    .data(data)
    .enter()
    .append("circle")
    .attr("class","dot")
    .attr("transform",translateCircle)
    .attr("r",8);


function translateCircle(datum, index)
          {
            return "translate(" +  projection([datum.y, datum.x]) + ")";
          };

setInterval(function(){
              svg
                .selectAll("ring")
                .data(data)
                .enter()
                .append("circle")
                  .attr("class", "ring")
                  .attr("transform", translateCircle)
                  .attr("r", 6)
                  .style("stroke-width", 3)
                  .style("stroke", "red")
                .transition()
                  .ease("linear")
                  .duration(6000)
                  .style("stroke-opacity", 1e-6)
                  .style("stroke-width", 1)
                  .style("stroke", "brown")
                  .attr("r", 160)
                  .remove();
      }, 750)


d3.select(self.frameElement).style("height", height + "px");    
</script>

</body>
</html>

したがって、このソリューション用に完全に d3 慣用的なアプローチを作成しませんでしたが、機能します。これを svg.selectAll("circle"/"unique selection"...) などの中で暗黙的に機能させることができれば、さらに素晴らしいでしょう。それまでの間、私はそれに取り組みます。それまでは、このより明示的な反復アプローチがあります。

setIntervalMike の例では、呼び出しで単一の要素を DOM に追加するだけです。バインディング プロセスを促進するために、一連の座標で動作する射影メソッドを作成しました。translateCircle は、各コレクション要素の内部属性へのアクセスを可能にする座標のコレクション内のデータで動作します。

setInterval呼び出し内で、forEachメソッドは座標のコレクションを反復処理し、setIntervalMike によって最初に呼び出されたメソッド内の同じ内部を呼び出します。

そうではない D3

    <style>
        body {
              background: #192887;
            }

            .graticule {
              fill: none;
              stroke: #fff;
              stroke-width: .5px;
            }

            .land {
              fill: #007421;
            }

            .dot {
              fill: #c7141a;
            }

            .ring {
              fill: none;
              stroke: #c7141a;
            }
    </style>

    <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>
<body>
<script>

var width = 960,
    height = 500;

var projection = d3.geo.mercator()
    .center([113, -3])
    .scale(1275)
    .translate([width / 2, height / 2])
    .clipExtent([[0, 0], [width, height]])
    .precision(.1);

var path = d3.geo.path()
    .projection(projection);

var graticule = d3.geo.graticule()
    .step([5, 5]);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

svg.append("path")
    .datum(graticule)
    .attr("class", "graticule")
    .attr("d", path);


var data = [{x:-8,y:100},{x:-10,y:110},{x: -12,y:120}];

svg.selectAll("circle")
    .data(data)
    .enter()
    .append("circle")
    .attr("class","dot")
    .attr("transform",translateCircle)
    .attr("r",8);


function translateCircle(datum, index)
          {
            return "translate(" +  projection([datum.y, datum.x]) + ")";
          };

setInterval(function(){
          data.forEach(function(datum)
          {
              svg
                .append("circle")
                  .attr("class", "ring")
                  .attr("transform", translateCircle(datum))
                  .attr("r", 6)
                  .style("stroke-width", 3)
                  .style("stroke", "red")
                .transition()
                  .ease("linear")
                  .duration(6000)
                  .style("stroke-opacity", 1e-6)
                  .style("stroke-width", 1)
                  .style("stroke", "brown")
                  .attr("r", 160)
                  .remove();
          })
      }, 750)


d3.select(self.frameElement).style("height", height + "px");    
</script>

</body>
</html>
于 2014-02-01T03:02:01.510 に答える