- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 d3 V4 中自定义了矩阵散点图,以查找特定单元格刷过区域下的点。这是那个 plunkr
http://plnkr.co/edit/fL0C3XqvLtQO9SKWoV5c?p=preview
现在,例如,如果我在任何单元格中刷了几个点,高光很好,但我在 console.log“选定点”中得到了很多数据点。 (使用 brushend 函数中的 brushSelection 范围逻辑,我尝试过滤数据点并在控制台记录它们)
理想情况下,我只想获取控制台中位于任何单元格刷亮区域下的那些数据点/对象。
谢谢。任何帮助将不胜感激。
<!DOCTYPE html>
<meta charset="utf-8">
<style>
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;
fill: black;
}
.frame {
fill: none;
stroke: #aaa;
}
circle {
fill-opacity: .7;
}
circle.hidden {
fill: #ccc !important;
}
.extent {
fill: #000;
fill-opacity: .125;
stroke: #fff;
}
</style>
<body>
<script src="https://d3js.org/d3.v4.js"></script>
<script>
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;
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);
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);
brushCell = this;
x.domain(domainByTrait[p.x]);
y.domain(domainByTrait[p.y]);
}
}
// Highlight the selected circles.
function brushmove(p) {
var e = d3.brushSelection(this);
//svg.selectAll("circle").classed("hidden", function(d) {
d3.select(this).selectAll("circle").classed("hidden", function(d) {
return !e
? false
: (
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]
);
});
}
// If the brush is empty, select all circles.
function brushend(p) {
var e = d3.brushSelection(this);
if (e === null) svg.selectAll(".hidden").classed("hidden", false);
var points = [];
d3.select(this).selectAll("circle").filter(function(d){
if (
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]
)
points.push(d);
})
console.log('selected points', points);
}
});
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;
}
</script>
最佳答案
这里有两个问题。
首先,p.x
和p.y
在brushend
函数中是不可访问的。但是您可以通过以下方式轻松获得圆圈的位置:
d3.select(this).attr("cx")
d3.select(this).attr("cy")
除此之外,if
中的逻辑不正确,您应该使用 AND
来获取 4 个点内的圆圈:
if (
e[0][0] < d3.select(this).attr("cx") && d3.select(this).attr("cx") < e[1][0]
&& e[0][1] < d3.select(this).attr("cy") && d3.select(this).attr("cy") < e[1][1]
)
这是更新的 Plunker:http://plnkr.co/edit/dA7qcvAhjZBsyqTsheYl?p=preview
关于javascript - 在刷过的区域下找到点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51539991/
我有一个带有 UIPageViewController 的应用程序是这样设置的: UINavigationController *navController = (UINavigationContro
我有一个用于边框背景的 MVVM 绑定(bind),其中我从字符串格式的文件中读取颜色名称,并使用代码将它们转换为画笔: Brush b1 = new SolidColorBrush((Color)C
我有一个 D3 堆积面积图,我正在尝试重新计算刷牙时的 Y 轴。问题是,当我重新计算 Y 轴的最大值时,它不是所有不同层的组合,它只是最后一层。例如,如果我同时刷了 1/17 和 1/20 日期,我将
当程序上传到STM32F4时,有没有办法用计算机信息更新RTC?例如,电脑上的日期和小时信息是:12h40 11/09/2018,当我用IAR/AC6刷微 Controller 时,RTC是用这些信息
我是一名优秀的程序员,十分优秀!