【发布时间】:2015-02-15 13:41:42
【问题描述】:
我是 d3.js 的新手,我需要在矩形的右上角创建一个关闭按钮 [x]。单击按钮后,矩形应从图表中删除。
我正在考虑按照链接内容中的建议使用svg remove。
我目前使用字母 X 作为关闭按钮。然而,这个实现似乎不起作用,字母不可见(尽管出现在 DOM 上)。
bl.ocks 链接在这里:http://bl.ocks.org/bhandarisumit/b48e134e358f94a290a2
代码:
var margin = {top: 15, right: 40, bottom: 20, left: 50},
width = 960 - margin.left - margin.right,
height = 240 - margin.top - margin.bottom;
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var clip = svg.append("defs").append("svg:clipPath")
.attr("id", "clip")
.append("svg:rect")
.attr("id", "clip-rect")
.attr("x", "0")
.attr("y", "0")
.attr("width", width)
.attr("height", height);
var chartBody = svg.append("g")
.attr("clip-path", "url(#clip)");
var rect = chartBody.append('svg:rect')
.attr('width', width-20)
.attr('height', height-20)
.attr('fill', 'white');
var labeledRect = chartBody.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("height", height-20)
.attr("width", width-20)
.style("stroke", "red")
.attr("class", "labelbox")
.style("fill", "none");
var closeArea = chartBody.append('text')
.text('X')
.attr("font-family", "sans-serif")
.attr("font-size", "20px")
.attr("fill", "red");
labeledRect.append('text').attr('color','black').text('X');
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 2px;
}
rect {
fill: #fff;
}
<script src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.2/d3.js"></script>
【问题讨论】:
标签: javascript svg d3.js