Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript } //添加数据时追加切片。 this.path.enter() .append('svg:path') .attr('class','slice') .style('stroke','f3f5f6') .attr('stroke-width',2) .attr('fill',函数(d,i){ 返回此参考颜色(d.data.label); }) .attr('d',此.arc(方向)) //存储初始值。 .每个功能(d){ 此参数为当前值={ 数据:d.data, 价值:d.价值, 星际缠结:方向,星际缠结, endAngle:direction.endAngle }; }) .on('click',此.updateSelection); //删除数据时删除切片。 this.path.exit() .transition() .持续时间(450) .attrween('d',this.arcTweenOut) //现在移除退出的弧。 .remove(); //重新绘制弧。 this.path.transition() .持续时间(450) .attrTween('d',this.arcTween); //添加图例 这个.addLegend(); //现在一切都画好了, //我们只从现在开始更新。 如果(!这个。画的){ 这是真的; } } PieChart.prototype.addLegend=函数addLegend(){ //更新切片时,不需要重新绘制图例。 如果(this.painted | | |!this.legend){ 返回; } const thisRef=this; const rect=this.radius*0.04; 常数间距=此半径*0.02; const legend=this.svg.selectAll(“.legend”) .data(this.color.domain()); 图例。输入() .append('g') .attr('类','图例') .attr('fill-opacity',0) .attr('transform',函数(d,i){ 常数高度=矩形+间距*2; 常量偏移=高度*thisRef.color.domain().length/2; 常量水平=-4*rect; 常量垂直=i*高度-偏移; 返回` translate(${horizontal},${vertical})`; }); 图例.append('rect') .attr('width',rect) .attr('height',rect) .style('fill',此.color); 图例.append('text') .attr('x',矩形+间距) .attr('y',rect) .文本(功能(d){ 返回d; }); 图例.transition() .持续时间(450) .attr('fill-opacity',1); }; //演示/使用 const pieChart=新pieChart({ 目标:“#图表”, 多重:对, 传说:没错, 宽度:400, 身高:400 }); 控制台日志(pieChart); pieChart.selection.subscribe(函数(选择){ 控制台日志(选择); }); //画出情节。 更新(dataset.apples); //这里仅用于演示目的 d3.选择全部(“输入”) .关于(“变更”,更新); var timeout=setTimeout(函数(){ d3.选择(“输入[值=\“橙子\”]”)。属性(“选中”,为真)。每个(更新); }, 2000); 函数更新(){ clearTimeout(timeout);//此处仅用于演示目的 //更新数据。 更新(数据集[this.value]); } 正文{ 字体系列:“Helvetica Neue”,Helvetica,Arial,无衬线; } .集装箱{ 位置:相对位置; } 形式{ 位置:绝对位置; 右:10px; 顶部:10px; } //图表 .切片{ 光标:指针; } .传奇{ 字体大小:12px; } 苹果 橘子_Javascript_D3.js - Fatal编程技术网

Javascript } //添加数据时追加切片。 this.path.enter() .append('svg:path') .attr('class','slice') .style('stroke','f3f5f6') .attr('stroke-width',2) .attr('fill',函数(d,i){ 返回此参考颜色(d.data.label); }) .attr('d',此.arc(方向)) //存储初始值。 .每个功能(d){ 此参数为当前值={ 数据:d.data, 价值:d.价值, 星际缠结:方向,星际缠结, endAngle:direction.endAngle }; }) .on('click',此.updateSelection); //删除数据时删除切片。 this.path.exit() .transition() .持续时间(450) .attrween('d',this.arcTweenOut) //现在移除退出的弧。 .remove(); //重新绘制弧。 this.path.transition() .持续时间(450) .attrTween('d',this.arcTween); //添加图例 这个.addLegend(); //现在一切都画好了, //我们只从现在开始更新。 如果(!这个。画的){ 这是真的; } } PieChart.prototype.addLegend=函数addLegend(){ //更新切片时,不需要重新绘制图例。 如果(this.painted | | |!this.legend){ 返回; } const thisRef=this; const rect=this.radius*0.04; 常数间距=此半径*0.02; const legend=this.svg.selectAll(“.legend”) .data(this.color.domain()); 图例。输入() .append('g') .attr('类','图例') .attr('fill-opacity',0) .attr('transform',函数(d,i){ 常数高度=矩形+间距*2; 常量偏移=高度*thisRef.color.domain().length/2; 常量水平=-4*rect; 常量垂直=i*高度-偏移; 返回` translate(${horizontal},${vertical})`; }); 图例.append('rect') .attr('width',rect) .attr('height',rect) .style('fill',此.color); 图例.append('text') .attr('x',矩形+间距) .attr('y',rect) .文本(功能(d){ 返回d; }); 图例.transition() .持续时间(450) .attr('fill-opacity',1); }; //演示/使用 const pieChart=新pieChart({ 目标:“#图表”, 多重:对, 传说:没错, 宽度:400, 身高:400 }); 控制台日志(pieChart); pieChart.selection.subscribe(函数(选择){ 控制台日志(选择); }); //画出情节。 更新(dataset.apples); //这里仅用于演示目的 d3.选择全部(“输入”) .关于(“变更”,更新); var timeout=setTimeout(函数(){ d3.选择(“输入[值=\“橙子\”]”)。属性(“选中”,为真)。每个(更新); }, 2000); 函数更新(){ clearTimeout(timeout);//此处仅用于演示目的 //更新数据。 更新(数据集[this.value]); } 正文{ 字体系列:“Helvetica Neue”,Helvetica,Arial,无衬线; } .集装箱{ 位置:相对位置; } 形式{ 位置:绝对位置; 右:10px; 顶部:10px; } //图表 .切片{ 光标:指针; } .传奇{ 字体大小:12px; } 苹果 橘子

Javascript } //添加数据时追加切片。 this.path.enter() .append('svg:path') .attr('class','slice') .style('stroke','f3f5f6') .attr('stroke-width',2) .attr('fill',函数(d,i){ 返回此参考颜色(d.data.label); }) .attr('d',此.arc(方向)) //存储初始值。 .每个功能(d){ 此参数为当前值={ 数据:d.data, 价值:d.价值, 星际缠结:方向,星际缠结, endAngle:direction.endAngle }; }) .on('click',此.updateSelection); //删除数据时删除切片。 this.path.exit() .transition() .持续时间(450) .attrween('d',this.arcTweenOut) //现在移除退出的弧。 .remove(); //重新绘制弧。 this.path.transition() .持续时间(450) .attrTween('d',this.arcTween); //添加图例 这个.addLegend(); //现在一切都画好了, //我们只从现在开始更新。 如果(!这个。画的){ 这是真的; } } PieChart.prototype.addLegend=函数addLegend(){ //更新切片时,不需要重新绘制图例。 如果(this.painted | | |!this.legend){ 返回; } const thisRef=this; const rect=this.radius*0.04; 常数间距=此半径*0.02; const legend=this.svg.selectAll(“.legend”) .data(this.color.domain()); 图例。输入() .append('g') .attr('类','图例') .attr('fill-opacity',0) .attr('transform',函数(d,i){ 常数高度=矩形+间距*2; 常量偏移=高度*thisRef.color.domain().length/2; 常量水平=-4*rect; 常量垂直=i*高度-偏移; 返回` translate(${horizontal},${vertical})`; }); 图例.append('rect') .attr('width',rect) .attr('height',rect) .style('fill',此.color); 图例.append('text') .attr('x',矩形+间距) .attr('y',rect) .文本(功能(d){ 返回d; }); 图例.transition() .持续时间(450) .attr('fill-opacity',1); }; //演示/使用 const pieChart=新pieChart({ 目标:“#图表”, 多重:对, 传说:没错, 宽度:400, 身高:400 }); 控制台日志(pieChart); pieChart.selection.subscribe(函数(选择){ 控制台日志(选择); }); //画出情节。 更新(dataset.apples); //这里仅用于演示目的 d3.选择全部(“输入”) .关于(“变更”,更新); var timeout=setTimeout(函数(){ d3.选择(“输入[值=\“橙子\”]”)。属性(“选中”,为真)。每个(更新); }, 2000); 函数更新(){ clearTimeout(timeout);//此处仅用于演示目的 //更新数据。 更新(数据集[this.value]); } 正文{ 字体系列:“Helvetica Neue”,Helvetica,Arial,无衬线; } .集装箱{ 位置:相对位置; } 形式{ 位置:绝对位置; 右:10px; 顶部:10px; } //图表 .切片{ 光标:指针; } .传奇{ 字体大小:12px; } 苹果 橘子,javascript,d3.js,Javascript,D3.js,下面是一个JSFIDLE示例,展示了如何实现这一点: 我已经将其用作基础,但您可以根据PieChart实现调整代码 其基本思想是,只要切换类别(oranges/apples),将重新计算饼图圆弧以匹配新数据。动画通过change功能中的转换完成: function change() { // ... calculate arcs path.transition().duration(750).attrTween("d", arcTween(selected)); } 然后,

下面是一个JSFIDLE示例,展示了如何实现这一点:

我已经将其用作基础,但您可以根据PieChart实现调整代码

其基本思想是,只要切换类别(
oranges/apples
),将重新计算饼图圆弧以匹配新数据。动画通过
change
功能中的转换完成:

function change() {
    // ... calculate arcs
    path.transition().duration(750).attrTween("d", arcTween(selected)); 
}
然后,真正的魔法发生在
arcTween
函数中。在原始示例中,仅更新
开始角度
结束角度
。您可以存储目标
outerRadius
,然后在转换的每个步骤上更新
arc
生成器:

function arcTween(selected) {
    return function(target, i) {
      target.outerRadius = radius - (i === selected ? 0 : 20);
      var arcInterpolator = d3.interpolate(this._current, target);
      this._current = arcInterpolator(0);
      return function(t) {
        var interpolatedArc = arcInterpolator(t);
        arc.outerRadius(interpolatedArc.outerRadius);
        return arc(interpolatedArc);
      };
    }
}

下面是一个JSFIDLE示例,展示了如何实现这一点:

我已经将其用作基础,但您可以根据PieChart实现调整代码

其基本思想是,只要切换类别(
oranges/apples
),将重新计算饼图圆弧以匹配新数据。动画通过
change
功能中的转换完成:

function change() {
    // ... calculate arcs
    path.transition().duration(750).attrTween("d", arcTween(selected)); 
}
然后,真正的魔法发生在
arcTween
函数中。在原始示例中,仅更新
开始角度
结束角度
。您可以存储目标
outerRadius
,然后在转换的每个步骤上更新
arc
生成器:

function arcTween(selected) {
    return function(target, i) {
      target.outerRadius = radius - (i === selected ? 0 : 20);
      var arcInterpolator = d3.interpolate(this._current, target);
      this._current = arcInterpolator(0);
      return function(t) {
        var interpolatedArc = arcInterpolator(t);
        arc.outerRadius(interpolatedArc.outerRadius);
        return arc(interpolatedArc);
      };
    }
}

是否可以添加自定义事件来更新,如“鼠标悬停”?在我的图表上,如果我更新了PieChart,那么就没有了,可以添加自定义事件来更新,比如“mouseover”?在我的图表上,如果我更新图表,就会消失