将 d3 交互式矩阵散点图更新到 v4 时出现问题

Issue with updating d3 interactive matrix scatterplot to v4

我正在尝试通过将我感兴趣的各种 v3 脚本更新为 v4 来提高 D3.js 的技能,但是我在尝试 "port" Mike 的交互式散点图矩阵时遇到了困难Bostok 发帖于此:https://bl.ocks.org/mbostock/4063663

虽然我已经毫无问题地移植了代码的静态版本(没有画笔),但当我尝试以与 v3 相同的方式实现画笔时,我发现了一个似乎是实际 D3 问题的问题不仅仅是与我的 D3 noob-ness 相关:刷子似乎粘在散点图矩阵上的错误单元格上! 特别是,如果我删除了 brushmove 部分并且我只是记录以控制数量 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"是错误的)

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

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

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>