转换在首次创建图表时不起作用

时间:2018-10-10 09:24:02

标签: javascript d3.js

我像这样创建一个#!/usr/bin/python from scapy.all import * from settings import * #Create Layer-2 Frame. l2 = Ether( dst=dst, src=src, type=2048) #dst and src from settings pkt = IP(dst=dst)/ICMP(type=8)/Raw(load=("X"*10000)) #Create some big pings: for i in range(10000,10020): frags = fragment(pkt) #create Fragments for fragment in frags: sendp(l2/fragment, iface=iface) print("i: " + str(i))

http://jsfiddle.net/gs6rehnx/2042/

D3

单击<button type="button" onclick="createPie()">Click Me First!</button> <button type="button" onclick="updatePie()">Update Diagram!</button> <div class='foo'></div> const width = 260; const height = 260; const thickness = 40; const duration = 750; const radius = Math.min(width, height) / 2; const color = d3.scaleOrdinal(d3.schemeCategory10); const arc = d3 .arc() .innerRadius(radius - thickness) .outerRadius(radius); const pie = d3 .pie() .value(function(d) { return d.value; }) .sort(null); const create = function(data) { const svg = d3 .select('.foo') .append('svg') .attr('class', 'pie') .attr('width', width) .attr('height', height); svg .append('g') .attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')') .attr('id', 'bar'); draw(data); } const draw = function(data) { const path = d3.select('#bar') .selectAll('path') .data(pie(data)) path .enter() .append('g') .append('path') .attr('d', arc) .attr('fill', (d, i) => color(i)); path .transition() .duration(duration) .attrTween('d', function(d) { const interpolate = d3.interpolate({ startAngle: 0, endAngle: 0 }, d); return function(t) { return arc(interpolate(t)); }; }); }; const data = [{ name: 'USA', value: 50 }, { name: 'UK', value: 5 }, { name: 'Canada', value: 5 }, { name: 'Maxico', value: 40 } ] function createPie() { create(data) } const newData = [{ name: 'USA', value: 40 }, { name: 'UK', value: 20 }, { name: 'Canada', value: 30 }, { name: 'Maxico', value: 10 } ]; function updatePie() { draw(newData) } 按钮时,仅显示该图。当我单击Click Me First!按钮时,过渡有效。为什么单击第一个按钮时不起作用?

1 个答案:

答案 0 :(得分:0)

当您单击Click Me First时,在create函数中将svg命名为bar并调用draw

Update Diagram!单击第一个按钮,则svg不存在。 因此功能draw无法正常工作。

 const path = d3.select('#bar')
               .selectAll('path')
                .data(pie(data))