繁体   English   中英

Dc.js和D3.js图表​​更新

[英]Dc.js and D3.js chart update

我正在使用dc.js绘制一些图表。

在d3代码中,我动态地计算了几列的总和,然后将它们添加到用d3.js绘制的饼图中。

这是计算列总和的代码:

var pieChart = [];
classesJson.forEach(function(classJson){
    var memDegree = ndx.groupAll().reduceSum(function(d){
        return d[classJson.name];
    }).value();
    //console.log(memDegree);
    pieChart.push({name:classJson.name, memDegree:memDegree});
});

第一次绘图工作正常。 但是,当我单击dc.js条形图上的元素时,d3.js饼图没有更新。 如何才能完成上述代码中的GroupAll值在d3.js饼图中也进行更新?

这是饼图的总d3代码:

radius = Math.min(300, 234) / 2;

var color = d3.scale.ordinal()
    .range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);

var arc = d3.svg.arc()
    .outerRadius(radius - 10)
    .innerRadius(0);

var pie = d3.layout.pie()
    .sort(null)
    .value(function(d) { return d.memDegree; });

var svg = d3.select("#membership-degree-pie-chart").append("svg")
    .attr("width", 300)
    .attr("height", 234)
  .append("g")
    .attr("transform", "translate(" + 300 / 2 + "," + 234 / 2 + ")");

var pieChart = [];
classesJson.forEach(function(classJson){
    var memDegree = ndx.groupAll().reduceSum(function(d){
        return d[classJson.name];
    }).value();
    //console.log(memDegree);
    pieChart.push({name:classJson.name, memDegree:memDegree});
});

pieChart.forEach(function(d) {
    d.memDegree = +d.memDegree;
  });

  var g = svg.selectAll(".arc")
      .data(pie(pieChart))
    .enter().append("g")
      .attr("class", "arc");

  g.append("path")
      .attr("d", arc)
      .style("fill", function(d) { return color(d.data.name); });

  g.append("text")
      .attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
      .attr("dy", ".35em")
      .style("text-anchor", "middle")
      .text(function(d) { return d.data.name; });

没有小提琴或plnkr,很难分辨。 但是我未经测试就编辑了您的代码。 请检查是否有帮助,我已经创建了更改功能来更新图形。 您可以在要更新图形的地方调用更改函数。 希望能帮助到你。

      var g = svg.selectAll(".arc")
          .data(pie(pieChart))
        .enter().append("g")
          .attr("class", "arc")
        .append("path")
        .attr("d", arc)
        .style("fill", function (d) { return color(d.data.name); })
.each(function(d) { this._current = d; }); // store the initial angles;

    g.append("text")
        .attr("transform", function (d) { return "translate(" + arc.centroid(d) + ")"; })
        .attr("dy", ".35em")
        .style("text-anchor", "middle")
        .text(function (d) { return d.data.name; });

    //For updating change in data
     function change() {


        pie.value(function(d) { return d.memDegree; }); // change the value function
        g = g.data(pie); // compute the new angles
        g.transition().duration(750).attrTween("d", function (a) {
            var i = d3.interpolate(this._current, a);
            this._current = i(0);
            return function (t) {
                return arc(i(t));
            };
        }); // redraw the arcs
    }

您可以在DC图表上使用侦听器来检测已被过滤,然后调用d3图表的更新函数。

yourDCChart.on("filtered", function (chart, filter) {
    // update function for d3
    updateD3Chart();
});

我将自定义可视化效果的D3绘制功能附加到DC图表,每次更新/渲染该图表时,都会再次绘制D3图表:

dcTable
.on("renderlet.<renderletKey>", function (d3ChartData) {
  drawD3(d3ChartData)
}

暂无
暂无

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

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