簡體   English   中英

d3 selectall單擊不起作用

[英]d3 selectall on click is not working

在下面的代碼中,我正在繪制折線圖,​​並添加了過濾器以根據條件僅繪制某些坐標的圓。

我想做的是,如果用戶單擊這些圓,他們應該能夠看到帶有數據坐標的警報消息。

我添加了onclick函數,但是沒有被調用。

 <script src="http://cdnjs.cloudflare.com/ajax/libs/d3/3.2.2/d3.v3.min.js"></script> <script src="http://cdnjs.cloudflare.com/ajax/libs/nvd3/1.0.0-beta/nv.d3.js"></script> <script> var width = 900, height = 500; nv.addGraph(function () { var chart = nv.models.lineChart() .width(width) .height(height) .margin({ left: 75, right: 50 }); chart.xAxis.axisLabel('Time (ms)') .tickFormat(d3.format(',r')); chart.yAxis.axisLabel('Voltate (vt)') .tickFormat(d3.format('.2f')); var sampleSVG =d3.select('#chart svg'); sampleSVG .datum(myData()) .transition().attr('width', width).attr('height', height).duration(500) .call(chart) .each("end", function() { var data = myData(); d3.select('.nv-groups').selectAll("circle") .data(data[0].values.filter(function(d) { return dy > 3000; })) .enter().append("circle") .style("stroke", "gray") .style("fill", "blue") .attr("r", 5) .attr("cx", function(d) { return chart.xAxis.scale()(dx);}) .attr("cy", function(d) { return chart.yAxis.scale()(dy);}) //.on("click", function(d) { alert('on click called.'+chart.lines.x() ); } ); }); },function(){ var svg_circles = d3.selectAll("circle"); //alert('in function'+svg_circles); svg_circles.on('click', function(){ alert('on clk called '); console.log("test"); }); }); function myData() { return data = [{ "values": [{ "x": 0, "y": 3235, "isAlert" :'1', "alertInfo" : 'Alert generated for this trade' }, { "x": 173, "y": 2114 }, { "x": 347, "y": 1724 }, { "x": 526, "y": 2703 }, { "x": 700, "y": 2980 }, { "x": 931, "y": 3086 }, { "x": 1058, "y": 2881 }, { "x": 1220, "y": 2817 }, { "x": 1399, "y": 2242 }, { "x": 1584, "y": 2639 }, { "x": 1752, "y": 3122 }, { "x": 1983, "y": 3157 }, { "x": 2105, "y": 3391 }, { "x": 2284, "y": 3441 }, { "x": 2469, "y": 3356 }, { "x": 2637, "y": 3498 }, { "x": 2811, "y": 3753 }, { "x": 3042, "y": 3384 }, { "x": 3169, "y": 3399 }, { "x": 3331, "y": 3399 }, { "x": 3522, "y": 2164 }, { "x": 3690, "y": 2129 }, { "x": 3863, "y": 2200 }, { "x": 4095, "y": 2292 }], "key": "Stocks Data", "color": null }]} </script> 
 <link rel="stylesheet" type="text/css" href="http://cdnjs.cloudflare.com/ajax/libs/nvd3/1.0.0-beta/nv.d3.css"> 
 <div id="chart"> <svg></svg> </div> 

問題似乎是您的圈子不在SVG的前面。 一種快速的解決方法是將您的圈子添加到each()函數中的NVD3的交互層,並從addGraph()刪除回調。

var svg = d3.select(this);
svg.select('.nv-interactive').selectAll("circle")

但是,這部分阻止了將鼠標懸停/工具提示事件阻止到NVD3的點(在#nv-point-clips )。

編輯:這是一個小提琴: http : //jsfiddle.net/g6sp5p0f/9/ 這是使用NVD3的當前主版本,因此它可能不適用於較新/開發的版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM