Can d3.js draw two scatterplots on the same graph using data from the same source?

后端 未结 2 1113
忘掉有多难
忘掉有多难 2021-02-02 00:37

All of the d3 tutorials I\'ve found use data arranged in arrays of objects from which they graph one point for each object in the array. Given data in the following structure:<

2条回答
  •  醉梦人生
    2021-02-02 01:15

    Place the two circles for each data point into a single svg:g element. This produces a one-to-one mapping for the data to elements but still allows you to show two different points.

    var nodeEnter = vis1.selectAll("circle")
          .data(dataSet)
          .enter()
          .insert("svg:g");
    
    nodeEnter.insert("svg:circle")
               .attr("cx", function (d) { return 100 - d.xVar})
               .attr("cy", function (d) { return 100 - d.yVar1})
               .attr("r", 2)
               .style("fill", "green");
    
    nodeEnter.insert("svg:circle")
               .attr("cx", function (d) { return 100 - d.xVar})
               .attr("cy", function (d) { return 100 - d.yVar2})
               .attr("r", 2)
               .style("fill", "blue");
    

    Working JSFiddle.

提交回复
热议问题