Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

javascript - d3 line/circle chart

Does anyone know how to create a line/circle chart that has lines extended to different quadrants that represent different data points using JavaScript SVG?

The image below demonstrates what I'm seeking.

enter image description here

Started out by mapping a dougnut chart with placeholders where the line(s) could ricochet off.

//latest fiddle

http://jsfiddle.net/Qh9X5/10152/

The next step is to plot a line to bounce off the markers.

  var slice = svg.select(".slices").selectAll("path.slice")
    .data(pie(data));

  slice.enter()
    .insert("path")
    .attr('fill', function(d, i) {
      console.log("d", d);
      return colores_google(i);
    })
    .attr("class", "slice");

  slice
    .transition().duration(1000)
    .attrTween("d", function(d) {
      this._current = this._current || d;
      var interpolate = d3.interpolate(this._current, d);
      this._current = interpolate(0);
      return function(t) {
        return arc(interpolate(t));
      };
    })

  slice.exit()
    .remove();




  var placeholders = svg.select(".placeholders").selectAll("circle.placeholder")
    .data(pie(data));

  placeholders.enter()
    .insert("circle")
    .style("fill", function(d) {
      return "white";
    })
    .attr("transform", function(d) {
      return "translate(" + arc.centroid(d) + ")";
    })
    .attr("r", "3")
    .attr("class", function(d) {
      return "placeholder " + d.data.group;
    });

  placeholders
    .transition().duration(1000)

  placeholders.exit()
    .remove();

  var gapplaceholders = svg.select(".placeholders").selectAll("circle.placeholder.gap");

  gapplaceholders.remove();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've managed to start plotting multiple lines against the nodes of this doughnut chart.

enter image description here http://jsfiddle.net/Qh9X5/10208/

  var slice = svg.select(".slices").selectAll("path.slice")
    .data(pie(data));

  slice.enter()
    .insert("path")
    .attr('fill', function(d, i) {
      return colores_google(i);
    })
    .attr("class", "slice");

  slice
    .transition().duration(1000)
    .attrTween("d", function(d) {
      this._current = this._current || d;
      var interpolate = d3.interpolate(this._current, d);
      this._current = interpolate(0);
      return function(t) {
        return arc(interpolate(t));
      };
    })

  slice.exit()
    .remove();



  var placeholders = svg.select(".placeholders").selectAll("circle.placeholder")
    .data(pie(data));

  placeholders.enter()
    .insert("circle")
    .style("fill", function(d) {
      return "white";
    })
    .attr("transform", function(d) {
      return "translate(" + arc.centroid(d) + ")";
    })
    .attr("x", function(d) {
      return arc.centroid(d)[0];
    })
    .attr("y", function(d) {
      return arc.centroid(d)[1];
    })
    .attr("r", "3")
    .attr("id", function(d) {
      return "p" + d.data.id;
    })
    .attr("class", function(d) {
      return "placeholder " + d.data.group;
    });

  placeholders
    .transition().duration(1000)

  placeholders.exit()
    .remove();



  var labelholders = svg.select(".labelholders").selectAll("text.labelholder")
    .data(pie(data));

  labelholders.enter()
    .insert("text")
    .attr("transform", function(d) {
      return "translate(" + arc.centroid(d) + ")";
    })
    .attr("dy", -5)
    .text(function(d) {
      return d.data.id;
    })
    .attr("class", function(d) {
      return "labelholder " + d.data.group;
    });

  labelholders
    .transition().duration(1000)

  labelholders.exit()
    .remove();




  var gapplaceholders = svg.select(".placeholders").selectAll("circle.placeholder.gap");

  gapplaceholders.remove();


var lineColors = ["e2d7c7",
    "d7b093",
    "c04830",
    "fffcef",
    "4aaba2",
    "c94f42",
    "52b0a2"
  ];


  var lineData = [{
      group: 1,
      plots: [21, 22, 18, 1]
    }, {
      group: 2,
      plots: [20, 23, 17, 9]
    }, {
      group: 3,
      plots: [21, 24, 16, 15]
    }, {
      group: 3,
      plots: [19, 23, 16, 0]
    }, {
      group: 5,
      plots: [19, 24, 18, 6]
    }, {
      group: 6,
      plots: [19, 24, 17, 14]
    }, {
      group: 3,
      plots: [20, 23, 16, 2]
    }, {
      group: 5,
      plots: [19, 23, 16, 4]
    }

  ]



  $.each(lineData, function(index, value) {

    var coords = [];

    var group = value.group;

    $.each(value.plots, function(i, v) {
      $("#p" + v).each(function() {
        var x = $(this).attr("x");
        var y = $(this).attr("y");

        var obj = {
          "x": x,
          "y": y
        };
        coords.push(obj);
      });
    });


    var first = {
      "x": 5 * index,
      "y": radius - 35
    };
    coords.push(first);

    var first = {
      "x": 5 * index,
      "y": radius + 200
    };
    coords.push(first);

    var maxLeng = coords.length - 2;

    $.each(coords, function(i, v) {

      var line = svg.append("line")
        .style("stroke", function(d) {
          return lineColors[group];
        })
        .attr("x1", coords[i]["x"])
        .attr("y1", coords[i]["y"])
        .attr("x2", coords[i + 1]["x"])
        .attr("y2", coords[i + 1]["y"]);

      if (i == maxLeng) {
        return false;
      }
    });

  });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...