Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

d3.js - Create points on a D3 multiline graph

I'm trying to add points to a line graph using d3 in this example: http://bl.ocks.org/mbostock/3884955

I was also trying to follow this post

How do you get the points to look like this picture from the documentation? http://github.com/mbostock/d3/wiki/line.png

The stroke of the circle should match the line color.

var color = d3.scale.category10();


  d3.csv("data.csv", function(error, data) {
  color.domain(d3.keys(data[0]).filter(function(key) { return key !== "date"; }));

  data.forEach(function(d) {
    d.date = parseDate(d.date);
  });

  var ranks = color.domain().map(function(name) {
    return {
      name: name,
      values: data.map(function(d) {
        return {date: d.date, ranking: +d[name]};
      })
    };
  });



  var rank = svg.selectAll(".rank")
      .data(ranks)
    .enter().append("g")
      .attr("class", "rank");
    rank.append("path")
      .attr("class", "line")
      .attr("d", function(d) { return line(d.values); })
      .style("stroke", function(d) { return color(d.name); });



var point = rank.append("g")
.attr("class", "line-point");

point.selectAll('circle')
.data(function(d){ return d.values})
.enter().append('circle')
.attr("cx", function(d) { return x(d.date) })
.attr("cy", function(d) { return y(d.ranking) })
.attr("r", 3.5)
.style("fill", "white")
.style("stroke", function(d) { return color(d.name); });
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)
.style("stroke", function(d) { return color(this.parentNode.__data__.name); })

See JSBin code
Found answer here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...