admin管理员组文章数量:1356120
I am using NVD3 to create a pie chart. code to generate pie chart:
nv.addGraph(function() {
var chart = nv.models.pieChart().x(function(d) { return d.label }).y(function(d) { return d.value }).showLabels(true);
d3.select("#chart svg").datum(exampleData()).transition().duration(350).call(chart);
return chart;
function exampleData() {
return vm.chartData.userData;
}
});
Now I have two slices in the pie chart as shown.
I have used the following approach 1)use js on method
var svg = d3.selectAll("#chart svg");
svg.select(".nv-pie").selectAll(".nv-slice")
.on('mouseover',function(d){
console.log(d);
});
But no click event is happening.
Please correct me where I am wrong.
I am using NVD3 to create a pie chart. code to generate pie chart:
nv.addGraph(function() {
var chart = nv.models.pieChart().x(function(d) { return d.label }).y(function(d) { return d.value }).showLabels(true);
d3.select("#chart svg").datum(exampleData()).transition().duration(350).call(chart);
return chart;
function exampleData() {
return vm.chartData.userData;
}
});
Now I have two slices in the pie chart as shown.
I have used the following approach 1)use js on method
var svg = d3.selectAll("#chart svg");
svg.select(".nv-pie").selectAll(".nv-slice")
.on('mouseover',function(d){
console.log(d);
});
But no click event is happening.
Please correct me where I am wrong.
Share Improve this question asked Apr 12, 2016 at 11:15 rahulrahul 3661 gold badge5 silver badges22 bronze badges 1- I have tried dispatch, but its not working too. – rahul Commented Apr 12, 2016 at 11:50
2 Answers
Reset to default 8Use this:
chart.pie.dispatch.on("elementClick", function(e) {
console.log(e);
});
Demo
var chartElement = d3.select("#chart svg");
var chart;
nv.addGraph(function() {
chart = nv.models.pieChart()
.x(function(d) {
return d.label
})
.y(function(d) {
return d.value
})
.showLabels(true);
var chartData = [{
label: "Foo",
value: 67
}, {
label: "Bar",
value: 33
}];
chartElement
.datum(chartData)
.call(chart);
chart.pie.dispatch.on("elementClick", function(e) {
alert("You've clicked " + e.data.label);
});
return chart;
});
#chart {
height: 500px;
}
<script src="https://cdnjs.cloudflare./ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/nvd3/1.8.2/nv.d3.min.js"></script>
<link href="https://cdnjs.cloudflare./ajax/libs/nvd3/1.8.2/nv.d3.min.css" rel="stylesheet" />
<div id="chart">
<svg></svg>
</div>
I think it could be a simple approach, i also got stuck in same, so after some search i got this solution. Need to define both different property for lagend and pie chart click element. for more details explore the chart : click here
legend : {
margin : {
top : 5,
right : 40,
bottom : 50,
left : 0
},
dispatch : {
legendClick : function(e) {
$rootScope.clickedLagend = e
//in case send it to another page
$location.path('/url');
$scope.$apply();
},
}
},
pie : {
dispatch : {
elementClick : function(e) {
$rootScope.clickedLagend = e
$location.path('/url');
$scope.$apply();
},
}
}
本文标签: javascriptNVD3 Create click event for individual slice of a pie chartStack Overflow
版权声明:本文标题:javascript - NVD3 Create click event for individual slice of a pie chart - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744049864a2582224.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论