0
我想通过点击弧线并拖动来做出选择。下面的例子显示了一个非常相似的概念,但是选择从一个随机的位置开始,而我想从深蓝色的弧上的位置开始,方法是单击鼠标并拖动。 http://jsfiddle.net/bno009s5/ 选择弧上的区域
var dataset = {
apples: [532, 284],
};
var degree = Math.PI/180;
var width = 460,
height = 300,
radius = Math.min(width, height)/2;
var color = d3.scale.category20();
var pie = d3.layout.pie().startAngle(-90 * degree).endAngle(90 * degree)
.sort(null);
var arc = d3.svg.arc()
.innerRadius(50)
.outerRadius(100);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width/2 + "," + height/2 + ")");
var path = svg.selectAll("path")
.data(pie(dataset.apples))
.enter().append("path")
.attr("fill", function(d, i) {
return color(i);
})
.attr("d", arc)
.each(function(d) {
this._current = d;
}); // store the initial values;
window.setInterval(dummyData, 2000);
// Store the displayed angles in _current.
// Then, interpolate from _current to the new angles.
// During the transition, _current is updated in-place by d3.interpolate.
function arcTween(a) {
var i = d3.interpolate(this._current, a);
this._current = i(0);
return function(t) {
return arc(i(t));
};
}
function dummyData() {
var num = Math.floor(Math.random() * 100);
var key = Math.floor(Math.random() * dataset.apples.length);
dataset.apples[key] = num;
draw();
};
function draw() {
svg.selectAll("path")
.data(pie(dataset.apples))
.transition()
.attrTween("d", arcTween);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.0.4/d3.min.js"></script>
阿尔文您好,下面是.classed,我曾使用arcs.on( “鼠标按下”,函数(d代码){ VAR DIV = d3.select(本)(”活性”,TRUE); currentSelected =此; 的console.log(d3.mouse(arcs.node())); d3.select(窗口) 。对( '鼠标移动',的mouseMove) 。对(” mouseup',mouseUp); var point = d3.mouse(arcs.node()); startAngle =(Math.atan2(point [1],point [0])+ Math.PI/2); console.log(startAngle); });我已经转换了x,y角度,并且我在mousemove事件上计算了结束角度。我感谢您的帮助。 – user3202499 2014-10-28 05:59:02
@ user3202499:你有这个编码的小提琴的链接吗? – 2014-11-02 18:52:03