【问题标题】:Issue with updating d3 interactive matrix scatterplot to v4将 d3 交互式矩阵散点图更新到 v4 的问题
【发布时间】:2017-01-04 11:37:24
【问题描述】:

我正在尝试通过将我感兴趣的各种 v3 脚本更新到 v4 来提高我的 D3.js 技能,但我在尝试“移植” Mike Bostok 在此处发布的交互式散点图矩阵时遇到了困难:@987654321 @

虽然我已经毫无问题地移植了代码的静态版本(没有画笔),但在尝试以与 v3 中相同的方式实现画笔时,我发现一个问题似乎是一个实际的 D3 问题与我的 D3 新手有关:画笔似乎粘在散点图矩阵上的错误单元格上! 特别是,如果我删除了画笔移动部分,我只是记录以控制 p.i 和 p.j 的数量(它们标识了我们正在刷的散点图矩阵中的哪个单元格),我得到了一个 i = 3 索引。

var width = 960,
  size = 230,
  padding = 20;

var x = d3.scaleLinear()
  .range([padding / 2, size - padding / 2]);

var y = d3.scaleLinear()
  .range([size - padding / 2, padding / 2]);

var xAxis = d3.axisBottom()
  .scale(x)
  .ticks(6);

var yAxis = d3.axisLeft()
  .scale(y)
  .ticks(6);

var color = d3.scaleOrdinal(d3.schemeCategory10);

//d3.csv("flowers.csv", function(error, data) {
//  if (error) throw error;

data = iris;

var domainByTrait = {},
  traits = d3.keys(data[0]).filter(function(d) { return d !== "species"; }),
  n = traits.length;

traits.forEach(function(trait) {
  domainByTrait[trait] = d3.extent(data, function(d) { return d[trait]; });
});

xAxis.tickSize(size * n);
yAxis.tickSize(-size * n);

var brush = d3.brush()
  .on("start", brushstart)
  .on("brush", brushmove)
  .on("end", brushend);

var svg = d3.select("body").append("svg")
  .attr("width", size * n + padding)
  .attr("height", size * n + padding)
  .append("g")
  .attr("transform", "translate(" + padding + "," + padding / 2 + ")");

svg.selectAll(".x.axis")
  .data(traits)
  .enter().append("g")
  .attr("class", "x axis")
  .attr("transform", function(d, i) { return "translate(" + (n - i - 1) * size + ",0)"; })
  .each(function(d) { x.domain(domainByTrait[d]); d3.select(this).call(xAxis); });

svg.selectAll(".y.axis")
  .data(traits)
  .enter().append("g")
  .attr("class", "y axis")
  .attr("transform", function(d, i) { return "translate(0," + i * size + ")"; })
  .each(function(d) { y.domain(domainByTrait[d]); d3.select(this).call(yAxis); });

var cell = svg.selectAll(".cell")
  .data(cross(traits, traits))
  .enter().append("g")
  .attr("class", "cell")
  .attr("transform", function(d) { return "translate(" + (n - d.i - 1) * size + "," + d.j * size + ")"; })
  .each(plot);

// Titles for the diagonal.
cell.filter(function(d) { return d.i === d.j; }).append("text")
  .attr("x", padding)
  .attr("y", padding)
  .attr("dy", ".71em")
  .text(function(d) { return d.x; });

cell.call(brush);

function plot(p) {
  var cell = d3.select(this);

  x.domain(domainByTrait[p.x]);
  y.domain(domainByTrait[p.y]);

  cell.append("rect")
    .attr("class", "frame")
    .attr("x", padding / 2)
    .attr("y", padding / 2)
    .attr("width", size - padding)
    .attr("height", size - padding);

  cell.selectAll("circle")
    .data(data)
    .enter().append("circle")
    .attr("cx", function(d) { return x(d[p.x]); })
    .attr("cy", function(d) { return y(d[p.y]); })
    .attr("r", 4)
    .style("fill", function(d) { return color(d.species); });
}

var brushCell;

// Clear the previously-active brush, if any.
function brushstart(p) {
  if (brushCell !== this) {
    d3.select(brushCell).call(brush.move, null);
    x.domain(domainByTrait[p.x]);
    y.domain(domainByTrait[p.y]);
    brushCell = this;
  }
}

// Highlight the selected circles.
function brushmove(p) {
  // ??
  console.log(p.i +" " + p.j)
}

// If the brush is empty, select all circles.
function brushend(p) {
  if (!d3.event.selection) svg.selectAll(".hidden").classed("hidden", false);
}
//});

function cross(a, b) {
  var c = [], n = a.length, m = b.length, i, j;
  for (i = -1; ++i < n;) for (j = -1; ++j < m;) c.push({x: a[i], i: i, y: b[j], j: j});
  return c;
}

代码与 iris 对象一起在 JSFiddle 上进行审查:https://jsfiddle.net/fabio_p/pmpjawmm/

请注意,在函数brushstart中定义的变量brushcell也是错误的(表明传递给画笔函数的“this”是错误的)

更奇怪的是(至少在我没有经验的人看来),如果我更改添加单元格的顺序,事情似乎会变得更好,正如你在另一个小提琴中看到的那样:https://jsfiddle.net/fabio_p/7pf4cqrg/ 在这里,我只是在第 220 行更改了索引(并在第 206 行更改了规模一致性),并且索引 i 不再停留在 3...

关于我做错了什么或 D3 哪里出了问题的任何见解?

【问题讨论】:

    标签: scatter-plot brush d3.js


    【解决方案1】:

    d3 没有问题,您的代码中只有两个小问题。

    1) 从 v3 到 v4 的移动(如 CHANGELOG 中所述)添加了用于表示选择区域的 .extent() 方法。默认范围回退到 svg 的大小。如果您查看每个覆盖对象,它们就是整个 svg 文档的大小,这就是导致您的索引不稳定的原因。简单的解决方法是设置范围,

    var brush = d3.brush()
      .on("start", brushstart)
      .on("brush", brushmove)
      .on("end", brushend)
      .extent([[0, 0], [size, size]]);
    

    现在,当您进行选择时,该区域再次被限制为每个图表,并且它们具有正确的索引。

    问题 2) 由于从使用比例更改为使用坐标,您需要将选择中的绝对坐标转换为您的比例,如上面链接的更改日志中所述,这可以在定义的比例上使用.invert() 完成。

     // Highlight the selected circles.
    function brushmove(p) {
     if(d3.event.selection){
      var e = d3.event.selection;
      svg.selectAll("circle").classed("hidden", function(d) {
       return x.invert(e[0][0]) > d[p.x] || x.invert(e[1][0]) < d[p.x] 
       || y.invert(e[0][1]) < d[p.y] || y.invert(e[1][1]) > d[p.y];
      });
     }
    }
    

    加入这两个修复程序可以让我们恢复正确的功能,

    iris = [{
      "sepal.length": "5.1",
      "sepal.width": "3.5",
      "petal.length": "1.4",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "4.9",
      "sepal.width": "3",
      "petal.length": "1.4",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "4.7",
      "sepal.width": "3.2",
      "petal.length": "1.3",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "4.6",
      "sepal.width": "3.1",
      "petal.length": "1.5",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "5",
      "sepal.width": "3.6",
      "petal.length": "1.4",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "4.9",
      "sepal.width": "3.1",
      "petal.length": "1.5",
      "petal.width": "0.1",
      "species": "setosa"
    }, {
      "sepal.length": "5.4",
      "sepal.width": "3.7",
      "petal.length": "1.5",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "4.8",
      "sepal.width": "3.4",
      "petal.length": "1.6",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "4.8",
      "sepal.width": "3",
      "petal.length": "1.4",
      "petal.width": "0.1",
      "species": "setosa"
    }, {
      "sepal.length": "4.3",
      "sepal.width": "3",
      "petal.length": "1.1",
      "petal.width": "0.1",
      "species": "setosa"
    }, {
      "sepal.length": "5.8",
      "sepal.width": "4",
      "petal.length": "1.2",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "5.7",
      "sepal.width": "4.4",
      "petal.length": "1.5",
      "petal.width": "0.4",
      "species": "setosa"
    }, {
      "sepal.length": "5.4",
      "sepal.width": "3.9",
      "petal.length": "1.3",
      "petal.width": "0.4",
      "species": "setosa"
    }, {
      "sepal.length": "5.1",
      "sepal.width": "3.5",
      "petal.length": "1.4",
      "petal.width": "0.3",
      "species": "setosa"
    }, {
      "sepal.length": "5.7",
      "sepal.width": "3.8",
      "petal.length": "1.7",
      "petal.width": "0.3",
      "species": "setosa"
    }, {
      "sepal.length": "5.1",
      "sepal.width": "3.8",
      "petal.length": "1.5",
      "petal.width": "0.3",
      "species": "setosa"
    }, {
      "sepal.length": "5.4",
      "sepal.width": "3.4",
      "petal.length": "1.7",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "5.1",
      "sepal.width": "3.7",
      "petal.length": "1.5",
      "petal.width": "0.4",
      "species": "setosa"
    }, {
      "sepal.length": "4.6",
      "sepal.width": "3.6",
      "petal.length": "1",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "5.1",
      "sepal.width": "3.3",
      "petal.length": "1.7",
      "petal.width": "0.5",
      "species": "setosa"
    }, {
      "sepal.length": "4.8",
      "sepal.width": "3.4",
      "petal.length": "1.9",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "5",
      "sepal.width": "3",
      "petal.length": "1.6",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "5",
      "sepal.width": "3.4",
      "petal.length": "1.6",
      "petal.width": "0.4",
      "species": "setosa"
    }, {
      "sepal.length": "5.2",
      "sepal.width": "3.5",
      "petal.length": "1.5",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "5.2",
      "sepal.width": "3.4",
      "petal.length": "1.4",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "4.7",
      "sepal.width": "3.2",
      "petal.length": "1.6",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "4.8",
      "sepal.width": "3.1",
      "petal.length": "1.6",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "5.4",
      "sepal.width": "3.4",
      "petal.length": "1.5",
      "petal.width": "0.4",
      "species": "setosa"
    }, {
      "sepal.length": "5.2",
      "sepal.width": "4.1",
      "petal.length": "1.5",
      "petal.width": "0.1",
      "species": "setosa"
    }, {
      "sepal.length": "5.5",
      "sepal.width": "4.2",
      "petal.length": "1.4",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "4.9",
      "sepal.width": "3.1",
      "petal.length": "1.5",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "5",
      "sepal.width": "3.2",
      "petal.length": "1.2",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "5.5",
      "sepal.width": "3.5",
      "petal.length": "1.3",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "4.9",
      "sepal.width": "3.6",
      "petal.length": "1.4",
      "petal.width": "0.1",
      "species": "setosa"
    }, {
      "sepal.length": "4.4",
      "sepal.width": "3",
      "petal.length": "1.3",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "5.1",
      "sepal.width": "3.4",
      "petal.length": "1.5",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "5",
      "sepal.width": "3.5",
      "petal.length": "1.3",
      "petal.width": "0.3",
      "species": "setosa"
    }, {
      "sepal.length": "4.5",
      "sepal.width": "2.3",
      "petal.length": "1.3",
      "petal.width": "0.3",
      "species": "setosa"
    }, {
      "sepal.length": "4.4",
      "sepal.width": "3.2",
      "petal.length": "1.3",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "5",
      "sepal.width": "3.5",
      "petal.length": "1.6",
      "petal.width": "0.6",
      "species": "setosa"
    }, {
      "sepal.length": "5.1",
      "sepal.width": "3.8",
      "petal.length": "1.9",
      "petal.width": "0.4",
      "species": "setosa"
    }, {
      "sepal.length": "4.8",
      "sepal.width": "3",
      "petal.length": "1.4",
      "petal.width": "0.3",
      "species": "setosa"
    }, {
      "sepal.length": "5.1",
      "sepal.width": "3.8",
      "petal.length": "1.6",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "4.6",
      "sepal.width": "3.2",
      "petal.length": "1.4",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "5.3",
      "sepal.width": "3.7",
      "petal.length": "1.5",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "5",
      "sepal.width": "3.3",
      "petal.length": "1.4",
      "petal.width": "0.2",
      "species": "setosa"
    }, {
      "sepal.length": "7",
      "sepal.width": "3.2",
      "petal.length": "4.7",
      "petal.width": "1.4",
      "species": "versicolor"
    }, {
      "sepal.length": "6.4",
      "sepal.width": "3.2",
      "petal.length": "4.5",
      "petal.width": "1.5",
      "species": "versicolor"
    }, {
      "sepal.length": "6.9",
      "sepal.width": "3.1",
      "petal.length": "4.9",
      "petal.width": "1.5",
      "species": "versicolor"
    }, {
      "sepal.length": "5.5",
      "sepal.width": "2.3",
      "petal.length": "4",
      "petal.width": "1.3",
      "species": "versicolor"
    }, {
      "sepal.length": "6.5",
      "sepal.width": "2.8",
      "petal.length": "4.6",
      "petal.width": "1.5",
      "species": "versicolor"
    }, {
      "sepal.length": "5.7",
      "sepal.width": "2.8",
      "petal.length": "4.5",
      "petal.width": "1.3",
      "species": "versicolor"
    }, {
      "sepal.length": "6.3",
      "sepal.width": "3.3",
      "petal.length": "4.7",
      "petal.width": "1.6",
      "species": "versicolor"
    }, {
      "sepal.length": "4.9",
      "sepal.width": "2.4",
      "petal.length": "3.3",
      "petal.width": "1",
      "species": "versicolor"
    }, {
      "sepal.length": "6.6",
      "sepal.width": "2.9",
      "petal.length": "4.6",
      "petal.width": "1.3",
      "species": "versicolor"
    }, {
      "sepal.length": "5.2",
      "sepal.width": "2.7",
      "petal.length": "3.9",
      "petal.width": "1.4",
      "species": "versicolor"
    }, {
      "sepal.length": "5",
      "sepal.width": "2",
      "petal.length": "3.5",
      "petal.width": "1",
      "species": "versicolor"
    }, {
      "sepal.length": "5.9",
      "sepal.width": "3",
      "petal.length": "4.2",
      "petal.width": "1.5",
      "species": "versicolor"
    }, {
      "sepal.length": "6",
      "sepal.width": "2.2",
      "petal.length": "4",
      "petal.width": "1",
      "species": "versicolor"
    }, {
      "sepal.length": "6.1",
      "sepal.width": "2.9",
      "petal.length": "4.7",
      "petal.width": "1.4",
      "species": "versicolor"
    }, {
      "sepal.length": "5.6",
      "sepal.width": "2.9",
      "petal.length": "3.6",
      "petal.width": "1.3",
      "species": "versicolor"
    }, {
      "sepal.length": "6.7",
      "sepal.width": "3.1",
      "petal.length": "4.4",
      "petal.width": "1.4",
      "species": "versicolor"
    }, {
      "sepal.length": "5.6",
      "sepal.width": "3",
      "petal.length": "4.5",
      "petal.width": "1.5",
      "species": "versicolor"
    }, {
      "sepal.length": "5.8",
      "sepal.width": "2.7",
      "petal.length": "4.1",
      "petal.width": "1",
      "species": "versicolor"
    }, {
      "sepal.length": "6.2",
      "sepal.width": "2.2",
      "petal.length": "4.5",
      "petal.width": "1.5",
      "species": "versicolor"
    }, {
      "sepal.length": "5.6",
      "sepal.width": "2.5",
      "petal.length": "3.9",
      "petal.width": "1.1",
      "species": "versicolor"
    }, {
      "sepal.length": "5.9",
      "sepal.width": "3.2",
      "petal.length": "4.8",
      "petal.width": "1.8",
      "species": "versicolor"
    }, {
      "sepal.length": "6.1",
      "sepal.width": "2.8",
      "petal.length": "4",
      "petal.width": "1.3",
      "species": "versicolor"
    }, {
      "sepal.length": "6.3",
      "sepal.width": "2.5",
      "petal.length": "4.9",
      "petal.width": "1.5",
      "species": "versicolor"
    }, {
      "sepal.length": "6.1",
      "sepal.width": "2.8",
      "petal.length": "4.7",
      "petal.width": "1.2",
      "species": "versicolor"
    }, {
      "sepal.length": "6.4",
      "sepal.width": "2.9",
      "petal.length": "4.3",
      "petal.width": "1.3",
      "species": "versicolor"
    }, {
      "sepal.length": "6.6",
      "sepal.width": "3",
      "petal.length": "4.4",
      "petal.width": "1.4",
      "species": "versicolor"
    }, {
      "sepal.length": "6.8",
      "sepal.width": "2.8",
      "petal.length": "4.8",
      "petal.width": "1.4",
      "species": "versicolor"
    }, {
      "sepal.length": "6.7",
      "sepal.width": "3",
      "petal.length": "5",
      "petal.width": "1.7",
      "species": "versicolor"
    }, {
      "sepal.length": "6",
      "sepal.width": "2.9",
      "petal.length": "4.5",
      "petal.width": "1.5",
      "species": "versicolor"
    }, {
      "sepal.length": "5.7",
      "sepal.width": "2.6",
      "petal.length": "3.5",
      "petal.width": "1",
      "species": "versicolor"
    }, {
      "sepal.length": "5.5",
      "sepal.width": "2.4",
      "petal.length": "3.8",
      "petal.width": "1.1",
      "species": "versicolor"
    }, {
      "sepal.length": "5.5",
      "sepal.width": "2.4",
      "petal.length": "3.7",
      "petal.width": "1",
      "species": "versicolor"
    }, {
      "sepal.length": "5.8",
      "sepal.width": "2.7",
      "petal.length": "3.9",
      "petal.width": "1.2",
      "species": "versicolor"
    }, {
      "sepal.length": "6",
      "sepal.width": "2.7",
      "petal.length": "5.1",
      "petal.width": "1.6",
      "species": "versicolor"
    }, {
      "sepal.length": "5.4",
      "sepal.width": "3",
      "petal.length": "4.5",
      "petal.width": "1.5",
      "species": "versicolor"
    }, {
      "sepal.length": "6",
      "sepal.width": "3.4",
      "petal.length": "4.5",
      "petal.width": "1.6",
      "species": "versicolor"
    }, {
      "sepal.length": "6.7",
      "sepal.width": "3.1",
      "petal.length": "4.7",
      "petal.width": "1.5",
      "species": "versicolor"
    }, {
      "sepal.length": "6.3",
      "sepal.width": "2.3",
      "petal.length": "4.4",
      "petal.width": "1.3",
      "species": "versicolor"
    }, {
      "sepal.length": "5.6",
      "sepal.width": "3",
      "petal.length": "4.1",
      "petal.width": "1.3",
      "species": "versicolor"
    }, {
      "sepal.length": "5.5",
      "sepal.width": "2.5",
      "petal.length": "4",
      "petal.width": "1.3",
      "species": "versicolor"
    }, {
      "sepal.length": "5.5",
      "sepal.width": "2.6",
      "petal.length": "4.4",
      "petal.width": "1.2",
      "species": "versicolor"
    }, {
      "sepal.length": "6.1",
      "sepal.width": "3",
      "petal.length": "4.6",
      "petal.width": "1.4",
      "species": "versicolor"
    }, {
      "sepal.length": "5.8",
      "sepal.width": "2.6",
      "petal.length": "4",
      "petal.width": "1.2",
      "species": "versicolor"
    }, {
      "sepal.length": "5",
      "sepal.width": "2.3",
      "petal.length": "3.3",
      "petal.width": "1",
      "species": "versicolor"
    }, {
      "sepal.length": "5.6",
      "sepal.width": "2.7",
      "petal.length": "4.2",
      "petal.width": "1.3",
      "species": "versicolor"
    }, {
      "sepal.length": "5.7",
      "sepal.width": "3",
      "petal.length": "4.2",
      "petal.width": "1.2",
      "species": "versicolor"
    }, {
      "sepal.length": "5.7",
      "sepal.width": "2.9",
      "petal.length": "4.2",
      "petal.width": "1.3",
      "species": "versicolor"
    }, {
      "sepal.length": "6.2",
      "sepal.width": "2.9",
      "petal.length": "4.3",
      "petal.width": "1.3",
      "species": "versicolor"
    }, {
      "sepal.length": "5.1",
      "sepal.width": "2.5",
      "petal.length": "3",
      "petal.width": "1.1",
      "species": "versicolor"
    }, {
      "sepal.length": "5.7",
      "sepal.width": "2.8",
      "petal.length": "4.1",
      "petal.width": "1.3",
      "species": "versicolor"
    }, {
      "sepal.length": "6.3",
      "sepal.width": "3.3",
      "petal.length": "6",
      "petal.width": "2.5",
      "species": "virginica"
    }, {
      "sepal.length": "5.8",
      "sepal.width": "2.7",
      "petal.length": "5.1",
      "petal.width": "1.9",
      "species": "virginica"
    }, {
      "sepal.length": "7.1",
      "sepal.width": "3",
      "petal.length": "5.9",
      "petal.width": "2.1",
      "species": "virginica"
    }, {
      "sepal.length": "6.3",
      "sepal.width": "2.9",
      "petal.length": "5.6",
      "petal.width": "1.8",
      "species": "virginica"
    }, {
      "sepal.length": "6.5",
      "sepal.width": "3",
      "petal.length": "5.8",
      "petal.width": "2.2",
      "species": "virginica"
    }, {
      "sepal.length": "7.6",
      "sepal.width": "3",
      "petal.length": "6.6",
      "petal.width": "2.1",
      "species": "virginica"
    }, {
      "sepal.length": "4.9",
      "sepal.width": "2.5",
      "petal.length": "4.5",
      "petal.width": "1.7",
      "species": "virginica"
    }, {
      "sepal.length": "7.3",
      "sepal.width": "2.9",
      "petal.length": "6.3",
      "petal.width": "1.8",
      "species": "virginica"
    }, {
      "sepal.length": "6.7",
      "sepal.width": "2.5",
      "petal.length": "5.8",
      "petal.width": "1.8",
      "species": "virginica"
    }, {
      "sepal.length": "7.2",
      "sepal.width": "3.6",
      "petal.length": "6.1",
      "petal.width": "2.5",
      "species": "virginica"
    }, {
      "sepal.length": "6.5",
      "sepal.width": "3.2",
      "petal.length": "5.1",
      "petal.width": "2",
      "species": "virginica"
    }, {
      "sepal.length": "6.4",
      "sepal.width": "2.7",
      "petal.length": "5.3",
      "petal.width": "1.9",
      "species": "virginica"
    }, {
      "sepal.length": "6.8",
      "sepal.width": "3",
      "petal.length": "5.5",
      "petal.width": "2.1",
      "species": "virginica"
    }, {
      "sepal.length": "5.7",
      "sepal.width": "2.5",
      "petal.length": "5",
      "petal.width": "2",
      "species": "virginica"
    }, {
      "sepal.length": "5.8",
      "sepal.width": "2.8",
      "petal.length": "5.1",
      "petal.width": "2.4",
      "species": "virginica"
    }, {
      "sepal.length": "6.4",
      "sepal.width": "3.2",
      "petal.length": "5.3",
      "petal.width": "2.3",
      "species": "virginica"
    }, {
      "sepal.length": "6.5",
      "sepal.width": "3",
      "petal.length": "5.5",
      "petal.width": "1.8",
      "species": "virginica"
    }, {
      "sepal.length": "7.7",
      "sepal.width": "3.8",
      "petal.length": "6.7",
      "petal.width": "2.2",
      "species": "virginica"
    }, {
      "sepal.length": "7.7",
      "sepal.width": "2.6",
      "petal.length": "6.9",
      "petal.width": "2.3",
      "species": "virginica"
    }, {
      "sepal.length": "6",
      "sepal.width": "2.2",
      "petal.length": "5",
      "petal.width": "1.5",
      "species": "virginica"
    }, {
      "sepal.length": "6.9",
      "sepal.width": "3.2",
      "petal.length": "5.7",
      "petal.width": "2.3",
      "species": "virginica"
    }, {
      "sepal.length": "5.6",
      "sepal.width": "2.8",
      "petal.length": "4.9",
      "petal.width": "2",
      "species": "virginica"
    }, {
      "sepal.length": "7.7",
      "sepal.width": "2.8",
      "petal.length": "6.7",
      "petal.width": "2",
      "species": "virginica"
    }, {
      "sepal.length": "6.3",
      "sepal.width": "2.7",
      "petal.length": "4.9",
      "petal.width": "1.8",
      "species": "virginica"
    }, {
      "sepal.length": "6.7",
      "sepal.width": "3.3",
      "petal.length": "5.7",
      "petal.width": "2.1",
      "species": "virginica"
    }, {
      "sepal.length": "7.2",
      "sepal.width": "3.2",
      "petal.length": "6",
      "petal.width": "1.8",
      "species": "virginica"
    }, {
      "sepal.length": "6.2",
      "sepal.width": "2.8",
      "petal.length": "4.8",
      "petal.width": "1.8",
      "species": "virginica"
    }, {
      "sepal.length": "6.1",
      "sepal.width": "3",
      "petal.length": "4.9",
      "petal.width": "1.8",
      "species": "virginica"
    }, {
      "sepal.length": "6.4",
      "sepal.width": "2.8",
      "petal.length": "5.6",
      "petal.width": "2.1",
      "species": "virginica"
    }, {
      "sepal.length": "7.2",
      "sepal.width": "3",
      "petal.length": "5.8",
      "petal.width": "1.6",
      "species": "virginica"
    }, {
      "sepal.length": "7.4",
      "sepal.width": "2.8",
      "petal.length": "6.1",
      "petal.width": "1.9",
      "species": "virginica"
    }, {
      "sepal.length": "7.9",
      "sepal.width": "3.8",
      "petal.length": "6.4",
      "petal.width": "2",
      "species": "virginica"
    }, {
      "sepal.length": "6.4",
      "sepal.width": "2.8",
      "petal.length": "5.6",
      "petal.width": "2.2",
      "species": "virginica"
    }, {
      "sepal.length": "6.3",
      "sepal.width": "2.8",
      "petal.length": "5.1",
      "petal.width": "1.5",
      "species": "virginica"
    }, {
      "sepal.length": "6.1",
      "sepal.width": "2.6",
      "petal.length": "5.6",
      "petal.width": "1.4",
      "species": "virginica"
    }, {
      "sepal.length": "7.7",
      "sepal.width": "3",
      "petal.length": "6.1",
      "petal.width": "2.3",
      "species": "virginica"
    }, {
      "sepal.length": "6.3",
      "sepal.width": "3.4",
      "petal.length": "5.6",
      "petal.width": "2.4",
      "species": "virginica"
    }, {
      "sepal.length": "6.4",
      "sepal.width": "3.1",
      "petal.length": "5.5",
      "petal.width": "1.8",
      "species": "virginica"
    }, {
      "sepal.length": "6",
      "sepal.width": "3",
      "petal.length": "4.8",
      "petal.width": "1.8",
      "species": "virginica"
    }, {
      "sepal.length": "6.9",
      "sepal.width": "3.1",
      "petal.length": "5.4",
      "petal.width": "2.1",
      "species": "virginica"
    }, {
      "sepal.length": "6.7",
      "sepal.width": "3.1",
      "petal.length": "5.6",
      "petal.width": "2.4",
      "species": "virginica"
    }, {
      "sepal.length": "6.9",
      "sepal.width": "3.1",
      "petal.length": "5.1",
      "petal.width": "2.3",
      "species": "virginica"
    }, {
      "sepal.length": "5.8",
      "sepal.width": "2.7",
      "petal.length": "5.1",
      "petal.width": "1.9",
      "species": "virginica"
    }, {
      "sepal.length": "6.8",
      "sepal.width": "3.2",
      "petal.length": "5.9",
      "petal.width": "2.3",
      "species": "virginica"
    }, {
      "sepal.length": "6.7",
      "sepal.width": "3.3",
      "petal.length": "5.7",
      "petal.width": "2.5",
      "species": "virginica"
    }, {
      "sepal.length": "6.7",
      "sepal.width": "3",
      "petal.length": "5.2",
      "petal.width": "2.3",
      "species": "virginica"
    }, {
      "sepal.length": "6.3",
      "sepal.width": "2.5",
      "petal.length": "5",
      "petal.width": "1.9",
      "species": "virginica"
    }, {
      "sepal.length": "6.5",
      "sepal.width": "3",
      "petal.length": "5.2",
      "petal.width": "2",
      "species": "virginica"
    }, {
      "sepal.length": "6.2",
      "sepal.width": "3.4",
      "petal.length": "5.4",
      "petal.width": "2.3",
      "species": "virginica"
    }, {
      "sepal.length": "5.9",
      "sepal.width": "3",
      "petal.length": "5.1",
      "petal.width": "1.8",
      "species": "virginica"
    }];
    
    
    var width = 960,
      size = 230,
      padding = 20;
    
    var x = d3.scaleLinear()
      .range([padding / 2, size - padding / 2]);
    
    var y = d3.scaleLinear()
      .range([size - padding / 2, padding / 2]);
    
    var xAxis = d3.axisBottom()
      .scale(x)
      .ticks(6);
    
    var yAxis = d3.axisLeft()
      .scale(y)
      .ticks(6);
    
    var color = d3.scaleOrdinal(d3.schemeCategory10);
    
    //d3.csv("flowers.csv", function(error, data) {
    //  if (error) throw error;
    
    data = iris;
    
    var domainByTrait = {},
      traits = d3.keys(data[0]).filter(function(d) {
        return d !== "species";
      }),
      n = traits.length;
    
    traits.forEach(function(trait) {
      domainByTrait[trait] = d3.extent(data, function(d) {
        return d[trait];
      });
    });
    
    xAxis.tickSize(size * n);
    yAxis.tickSize(-size * n);
    
    var brush = d3.brush()
      .on("start", brushstart)
      .on("brush", brushmove)
      .on("end", brushend)
      .extent([
        [0, 0],
        [size, size]
      ]);
    
    var svg = d3.select("body").append("svg")
      .attr("width", size * n + padding)
      .attr("height", size * n + padding)
      .append("g")
      .attr("transform", "translate(" + padding + "," + padding / 2 + ")");
    
    svg.selectAll(".x.axis")
      .data(traits)
      .enter().append("g")
      .attr("class", "x axis")
      .attr("transform", function(d, i) {
        return "translate(" + (n - i - 1) * size + ",0)";
      })
      .each(function(d) {
        x.domain(domainByTrait[d]);
        d3.select(this).call(xAxis);
      });
    
    svg.selectAll(".y.axis")
      .data(traits)
      .enter().append("g")
      .attr("class", "y axis")
      .attr("transform", function(d, i) {
        return "translate(0," + i * size + ")";
      })
      .each(function(d) {
        y.domain(domainByTrait[d]);
        d3.select(this).call(yAxis);
      });
    
    var cell = svg.selectAll(".cell")
      .data(cross(traits, traits))
      .enter().append("g")
      .attr("class", "cell")
      .attr("transform", function(d) {
        return "translate(" + (n - d.i - 1) * size + "," + d.j * size + ")";
      })
      .each(plot);
    
    // Titles for the diagonal.
    cell.filter(function(d) {
        return d.i === d.j;
      }).append("text")
      .attr("x", padding)
      .attr("y", padding)
      .attr("dy", ".71em")
      .text(function(d) {
        return d.x;
      });
    
    cell.call(brush);
    
    //console.log(traits.map(function(d) { return domainByTrait[d];}));
    
    function plot(p) {
      var cell = d3.select(this);
    
      x.domain(domainByTrait[p.x]);
      y.domain(domainByTrait[p.y]);
    
      cell.append("rect")
        .attr("class", "frame")
        .attr("x", padding / 2)
        .attr("y", padding / 2)
        .attr("width", size - padding)
        .attr("height", size - padding);
    
      cell.selectAll("circle")
        .data(data)
        .enter().append("circle")
        .attr("cx", function(d) {
          return x(d[p.x]);
        })
        .attr("cy", function(d) {
          return y(d[p.y]);
        })
        .attr("r", 4)
        .style("fill", function(d) {
          return color(d.species);
        });
    }
    
    var brushCell;
    
    // Clear the previously-active brush, if any.
    function brushstart(p) {
      if (brushCell !== this) {
        d3.select(brushCell).call(brush.move, null);
        x.domain(domainByTrait[p.x]);
        y.domain(domainByTrait[p.y]);
        brushCell = this;
      }
    }
    
    // Highlight the selected circles.
    function brushmove(p) {
      if (d3.event.selection) {
        var e = d3.event.selection;
        svg.selectAll("circle").classed("hidden", function(d) {
          return x.invert(e[0][0]) > d[p.x] || x.invert(e[1][0]) < d[p.x] || y.invert(e[0][1]) < d[p.y] || y.invert(e[1][1]) > d[p.y];
        });
      }
    }
    
    // If the brush is empty, select all circles.
    function brushend(p) {
        if (!d3.event.selection) svg.selectAll(".hidden").classed("hidden", false);
      }
      //});
    
    function cross(a, b) {
      var c = [],
        n = a.length,
        m = b.length,
        i, j;
      for (i = -1; ++i < n;)
        for (j = -1; ++j < m;) c.push({
          x: a[i],
          i: i,
          y: b[j],
          j: j
        });
      return c;
    }
    svg {
      font: 10px sans-serif;
      padding: 10px;
    }
    .axis,
    .frame {
      shape-rendering: crispEdges;
    }
    .axis line {
      stroke: #ddd;
    }
    .axis path {
      display: none;
    }
    .cell text {
      font-weight: bold;
      text-transform: capitalize;
    }
    .frame {
      fill: none;
      stroke: #aaa;
    }
    circle {
      fill-opacity: .7;
    }
    circle.hidden {
      fill: #ccc !important;
    }
    .extent {
      fill: #000;
      fill-opacity: .125;
      stroke: #fff;
    }
    <!DOCTYPE html>
    
    <body>
      <script src="//d3js.org/d3.v4.min.js"></script>
    </body>

    【讨论】:

    • 先生,你让我很开心 :-) 正如我所写,我仍在学习 D3,因此我已经尽可能仔细地阅读了从 v3 到 v4 的变化......仍然我错过了一些关于默认范围的含义(尤其是在这种情况下!)
    • 关于突出显示散点图中的点,我使用过(即使它不在小提琴中)这个代码函数 brushmove(p) { var e = d3.event.selection; svg.selectAll("circle").classed("hidden", function(d) { return e[0][0] > x(d[p.x]) || x(d[p.x]) > e[1] [0] || e[0][1] > y(d[p.y]) || y(d[p.y]) > e[1][1]; });我希望是等价的。但也非常感谢您对此发表评论:-)
    猜你喜欢
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    相关资源
    最近更新 更多