【发布时间】:2016-05-17 23:48:31
【问题描述】:
我有这个代码:
var sets = [
{sets: ['A'], size: 10},
{sets: ['B'], size: 10},
{sets: ['A','B'], size: 5}
];
var chart = venn.VennDiagram();
var div = d3.select("#venn").datum(sets).call(chart);
使用优秀的venn.js 库,我的维恩图绘制得很好。
使用此代码:
div.selectAll("g")
.on("mouseover", function (d, i) {
// sort all the areas relative to the current item
venn.sortAreas(div, d);
// Display a tooltip with the current size
tooltip.transition().duration(400).style("opacity", .9);
tooltip.text(d.size + " items");
// highlight the current path
var selection = d3.select(this).transition("tooltip").duration(400);
selection.select("path")
.style("stroke-width", 3)
.style("fill-opacity", d.sets.length == 1 ? .4 : .1)
.style("stroke-opacity", 1)
.style("cursor", "pointer");
})
.on("mousemove", function () {
tooltip.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("click", function (d, i) {
window.location.href = "/somepage"
})
.on("mouseout", function (d, i) {
tooltip.transition().duration(400).style("opacity", 0);
var selection = d3.select(this).transition("tooltip").duration(400);
selection.select("path")
.style("stroke-width", 1)
.style("fill-opacity", d.sets.length == 1 ? .25 : .0)
.style("stroke-opacity", 0);
});
我可以向我的 venn 添加点击、鼠标悬停...功能。
问题出在这里:
向圈子(集合 A 或 B)添加功能可以正常工作。
向交集(Set A intersect Set B)添加功能可以正常工作。
我需要在除区域之外添加一些功能(设置 A 除了设置 B)
这个问题有点帮助:2D Polygon Boolean Operations with D3.js SVG
但我没有成功完成这项工作。
尝试使用 clipperjs 或 Greiner-Hormann polygon clipping algorithm 找出除外区域,但无法正常工作。
更新 1:
本题中的代码复制自 venn.js 示例:http://benfred.github.io/venn.js/examples/intersection_tooltip.html
【问题讨论】:
-
你能做 Fiddle 或 Plunker 吗?
-
@GerardoFurtado 我的代码是从 venn.js 示例中复制的:benfred.github.io/venn.js/examples/intersection_tooltip.html
标签: javascript d3.js svg venn.js