Create points on a D3 multiline graph

2019-01-12 05:56发布

问题:

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); });

回答1:

.style("stroke", function(d) { return color(this.parentNode.__data__.name); })

See JSBin code
Found answer here



标签: d3.js