简体   繁体   English

D3 V4多系列折线图:将数据点添加到颜色与折线相同的折线上

[英]D3 V4 Multi series line chart: Adding data points to lines with same color as lines

I am trying to add points to multi series line chart with D3 V4 in Angular-cli. 我正在尝试在Angular-cli中使用D3 V4向多系列折线图添加点。 Following is what I am trying. 以下是我正在尝试的。

var lookBookData = z.domain().map(function (name) {
      return {
        name: name,
        values: data.map(function (d) {
          return {date: d.date, lookbookcount: d[name]};
        })
      };
    });

    console.log(lookBookData);

    x.domain(d3.extent(data, function (d) {
      console.log(d)
      return d.date;
    }));

    y.domain([
      d3.min(lookBookData, function (c) {
        return d3.min(c.values, function (d) {
          return d.lookbookcount;
        });
      }),
      d3.max(lookBookData, function (c) {
        return d3.max(c.values,
          function (d) {
            return d.lookbookcount;
          });
      })
    ]);

    z.domain(lookBookData.map(function (c) {
      console.log(c);
      return c.name;
    }));

    // Add the X Axis
    svg.append("g")
      .style("font", "14px open-sans")
      .attr("transform", "translate(0," + height + ")")
      .call(d3.axisBottom(x).tickFormat(d3.timeFormat("%d/%m")));

    // Add the Y Axis
    svg.append("g")
      .style("font", "14px open-sans")
      .call(d3.axisLeft(y));

    // Add Axis labels
    svg.append("text")
      .style("font", "14px open-sans")
      .attr("text-anchor", "end")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", ".71em")
      .text("Sales / Searches");

    svg.append("text")
      .style("font", "14px open-sans")
      .attr("text-anchor", "end")
      // .attr("x", 6)
      .attr("dx", ".71em")
      .attr("transform", "translate(" + width + "," + (height +
        (margin.bottom)) + ")")
      .text("Departure Date");

    var chartdata = svg.selectAll(".chartdata")
      .data(lookBookData)
      .enter().append("g")
      .attr("class", "chartdata");

    chartdata.append("path")
      .attr("class", "line")
      .attr("d", function (d) {
        return line(d.values);
      })
      .style("fill", "none")
      .style("stroke", function (d) {
        return z(d.name);
      })
      .style("stroke-width", "2px");

    chartdata.append("text")
      .datum(function (d) {
        return {
          name: d.name, value: d.values[d.values.length - 1]
        };
      })
      .attr("transform", function (d) {
        return "translate(" +
          x(d.value.date) + "," + y(d.value.lookbookcount) + ")";
      })
      .attr("x", 3)
      .attr("dy", "0.35em")
      .style("font", "14px open-sans")
      .text(function (d) {
        return d.name;
      });

    // add the dots with tooltips
    chartdata.selectAll(".circle")
      .data(function (d) {
        return d.values;
      })
      .enter().append("circle")
      .attr("class", "circle")
      .attr("r", 4)
      .attr("cx", function (d) {
        console.log(d);
        return x(d.date);
      })
      .attr("cy", function (d) {
        return y(d.lookbookcount);
      })
      .style("fill", function(d) { // Add the colours dynamically
        console.log(d);
        return z(d.name);
      });

Chart is creating... Points are there but their colors are not matching with the colors of lines. 图表正在创建...点在那里,但是它们的颜色与线条的颜色不匹配。 Something like below. 像下面这样。

在此处输入图片说明

Any suggestions of what I have done wrong with my code. 关于我的代码做错了什么的任何建议。

Thank You 谢谢

The problem is that the data points that are used to draw the circles don't contain name parameter. 问题在于用于绘制圆的数据点不包含name参数。 For all the circles d.name is undefined and they are all colored the same (third color from the z scale, which happens to be green). 对于所有圆, d.name都是undefined并且都被着色为相同的颜色(z比例中的第三种颜色,恰好是绿色)。 The easiest solution should be to add the name parameter to data points like this: 最简单的解决方案应该是将name参数添加到数据点,如下所示:

var lookBookData = z.domain().map(function (name) {
  return {
    name: name,
    values: data.map(function (d) {
      return {date: d.date, lookbookcount: d[name], name: name};
    })
  };
});

If you apply the fill to the group the children will inherit that style. 如果将fill应用于组,则子代将继承该样式。

chartdata.style("fill", function(d) {
    return z(d.name);
   })
  .selectAll(".circle")
  .data(function (d) {
    return d.values;
  })
  .enter().append("circle")
  .attr("class", "circle")
  .attr("r", 4)
  .attr("cx", function (d) {
    console.log(d);
    return x(d.date);
  })
  .attr("cy", function (d) {
    return y(d.lookbookcount);
  });

The way your code is doing right now, after you bind the data using just the values , the circles have no reference to name anymore. 在仅使用values绑定数据之后,代码现在的行为方式是,圆圈不再引用name

PS: applying styles to groups will change all their children. PS:将样式应用于小组会改变所有孩子。 So, a solution following @Tormi approach may suit you better. 因此,遵循@Tormi方法的解决方案可能更适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM