简体   繁体   English

D3饼图工具提示和动画问题

[英]D3 Pie chart tooltip and animation issue

Below is my code that initialises a pie chart. 以下是我的初始化饼图的代码。 The problem I'm having is adding a tooltip and animation. 我遇到的问题是添加工具提示和动画。 I want to be able to hover over a slice on my pie chart and that slice should enlarge with a brief description (tooltip) of the data represented: 我希望能够将鼠标悬停在饼图上的一个切片上,并且该切片应该放大并带有所代表数据的简要说明(工具提示):

Code: 码:

 <!-- contains the data that will be plotted on my pie chart --> var data = [5, 6, 7, 8, 9, 10, 11, 12]; var radius = 500; var color = d3.scale.ordinal(.range(['#00FFFF', '#32CD32', '#1E90FF', '#FF4500', '#FFD700', '#FF7F50', '#DC143C', '#0000FF']); <!-- base container that will hold the pie chart. --> <!-- allows you to add function to the graph --> var container = d3.select("body").append("svg") .attr("width", 1000) .attr("height", 1000) <!-- making proper adjustments to the container --> var group = container.append("g") .attr("transform", "translate(500, 500)"); <!-- arc is the base and shape of the pie chart--> <!-- it contains an inner.radius, outer.radius--> var arc = d3.svg.arc() .innerRadius(0) .outerRadius(radius - 100); var pie = d3.layout.pie() .value(function(d) { return d; }); <!-- grouping and binding my data to the pie chart --> var arcs = group.selectAll(".arc") .data(pie(data)) <!-- passes data to pie layout--> .enter() .append("g") .attr("class", "arc"); <!-- each color represents a number from the data--> <!-- creating a path arcs .append("path") .attr("d", arc) .attr("fill", function(d) { return color(d.data); }); container.selectAll("slice") .data(pie(data)) .enter() .attr("class", "slice") .attr("d", path) var slices = container.selectAll(".slice") .data(pie(data)) .enter(); slices .append("path") .attr("class", function(d) { return "slice" + color(d.data); }) .attr("d", path) .on("mousemove", function(d) { var slicecolor = this; d3.select(this).style("opacity", 1); .on("mouseout", function(d) { d3.selectAll("path") .style({ "opacity": 0.5 }); }); 
 .slice { stroke: black; stroke-width: 1px; } .slice:hover { fill: aliceblue; } .hidden { display: none; } div.tooltip { border-width: 1px; border-radius: 25px; padding: 50px; color: #549CFF; position: absolute; text-align: left; left: 80px; opacity: 0.5; } 
 <script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script> 

This looks like an error 这看起来像一个错误

d3.select(this).style("opacity", 1); // <= errant semi-colon
        .on("mouseout", function(d) {
            d3.selectAll("path")
                .style({"opacity":0.5});
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM