本文介绍了d3对给定半径内的气泡图进行动画制作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//气泡图表基础。

//bubble chart base.http://jsfiddle.net/NYEaX/1450/

我想通过改变他们的尺度来动画气泡 - 如果可能的话,淡入淡出。在某个阶段,我需要用某种重力将它们聚集,以占据更多的圆周。

I am trying to animate the bubbles - via changing their scale -- and if possible fade them in and out. At some stage I need to cluster them with some kind of gravity to occupy more of a containing circumference.

(function() {

    var diameter = 250;

    var svg = d3.select('#graph').append('svg')
      .attr('width', diameter)
      .attr('height', diameter);

    var bubble = d3.layout.pack()
      .size([diameter, diameter])
      .value(function(d) {
        return d.size;
      })
      .padding(3);

    var color = d3.scale.ordinal()
      .domain(["Lorem ipsum", "dolor sit", "amet", "consectetur", "adipisicing"])
      .range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56"]);


  function randomData() {

      var data1 = {
        "children": [
                {
                  name: "AA",
                  className: "aa",
                  size: 170
                },
                {
                  name: "BB",
                  className: "bb",
                  size: 393
                },
                {
                  name: "CC",
                  className: "cc",
                  size: 293
                },
                {
                  name: "DD",
                  className: "dd",
                  size: 89
                }
              ]
      };

      var data2 = {
        "children": [
                {
                  name: "AA",
                  className: "aa",
                  size: 120
                },
                {
                  name: "BB",
                  className: "bb",
                  size: 123
                },
                {
                  name: "CC",
                  className: "cc",
                  size: 193
                },
                {
                  name: "DD",
                  className: "dd",
                  size: 289
                }
              ]
      };



          var j = Math.floor((Math.random() * 2) + 1);
          console.log("j", j);

          if (j == 1) {
            return data1;
          } else {
            return data2;
          }

        }

  change(randomData());

  d3.select(".randomize")
    .on("click", function() {
      change(randomData());
    });

  function change(data) {
    console.log("data", data);

    // generate data with calculated layout values
    var nodes = bubble.nodes(data)
      .filter(function(d) {
         return !d.children;
      }); // filter out the outer bubble


    var vis = svg.selectAll('circle')
      .data(nodes);

    vis.enter()
      .insert("circle")
      .attr('transform', function(d) {
        return 'translate(' + d.x + ',' + d.y + ')';
      })
      .attr('r', function(d) {
        return d.r;
      })
      .style("fill", function(d) {
        return color(d.name);
      })
      .attr('class', function(d) {
        return d.className;
      });

    vis
      .transition().duration(1000)

    vis.exit()
      .remove();

  };

  })();


推荐答案

首先尝试饼图并制作动画,然后查看气泡动画

Try and animate the pie chart first, then review the bubble animation

这篇关于d3对给定半径内的气泡图进行动画制作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 20:47