【问题标题】:Muliple pie charts are not loading多个饼图未加载
【发布时间】:2019-08-01 20:03:09
【问题描述】:

我正在尝试使用 d3 js 在单个页面中添加多个饼图。 饼图为 2 时正在加载,但是当我添加第三个饼图时,该图未附加到 SVG。 [这是我的完整代码。] 我的朋友:http://www.jsfiddle.net/SampathPerOxide/xt0L1scu/10/

  var h = 600;
  var r = h / 2;
  var arc = d3.svg.arc().outerRadius(r);

  var data = [{
        "label": "a",
        "value": 74
     },
     {
        "label": "b",
        "value": 7
     },
     {
        "label": "b",
        "value": 7
     },
     {
        "label": "d",
        "value": 12
     }
  ];
  var data1 = [{
        "label": "e",
        "value": 74
     },
     {
        "label": "f",
        "value": 7
     },
     {
        "label": "g",
        "value": 7
     },
     {
        "label": "h",
        "value": 12
     }
  ];

  var data2 = [{
        "label": "i",
        "value": 74
     },
     {
        "label": "j",
        "value": 7
     },
     {
        "label": "k",
        "value": 7
     },
     {
        "label": "l",
        "value": 12
     }
  ];
  var colors = [
     'rgb(178, 55, 56)',
     'rgb(213, 69, 70)',
     'rgb(230, 125, 126)',
     'rgb(239, 183, 182)'
  ]
  nv.addGraph(function() {
     var chart = nv.models.pieChart()
        .x(function(d) {
           return d.label
        })
        .y(function(d) {
           return d.value
        })
        .color(colors)
        .showLabels(true)
        .labelType("percent");

     d3.select("#chart svg")
        .datum(data)
        .transition().duration(1200)
        .call(chart);

     d3.selectAll(".nv-label text")
        .attr("transform", function(d) {
           d.innerRadius = -450;
           d.outerRadius = r;
           return "translate(" + arc.centroid(d) + ")";
        })
        .attr("text-anchor", "middle")
        /* Alter CSS attributes */
        .style({
           "font-size": "1em"
        });

     d3.selectAll('.nv-series').each(function(d, i) {
        var group = d3.select(this),
           circle = group.select('circle');
        var color = circle.style('fill');
        circle.remove();
        var symbol = group.append('path')
           .attr('d', d3.svg.symbol().type('square'))
           .style('stroke', color)
           .style('fill', color)
           .attr('transform', 'scale(1.5) translate(-2,0)')
     });
     return chart;
  });
  nv.addGraph(function() {
     var chartnew = nv.models.pieChart()
        .x(function(d) {
           return d.label
        })
        .y(function(d) {
           return d.value
        })
        .color(colors)
        .showLabels(true)
        .labelType("percent");

     d3.select("#chartnew svg")
        .datum(data1)
        .transition().duration(1200)
        .call(chartnew);

     d3.selectAll(".nv-label text")
        /* Alter SVG attribute (not CSS attributes) */
        .attr("transform", function(d) {
           d.innerRadius = -450;
           d.outerRadius = r;
           return "translate(" + arc.centroid(d) + ")";
        })
        .attr("text-anchor", "middle")
        .style({
           "font-size": "1em"
        });

     d3.selectAll('.nv-series').each(function(d, i) {
        var group = d3.select(this),
           circle = group.select('circle');
        var color = circle.style('fill');
        circle.remove();
        var symbol = group.append('path')
           .attr('d', d3.svg.symbol().type('square'))
           .style('stroke', color)
           .style('fill', color)
           .attr('transform', 'scale(1.5) translate(-2,0)')
     });
     return chartnew;
  });
  nv.addGraph(function() {
     var chartnewagains = nv.models.pieChart()
        .x(function(d) {
           return d.label
        })
        .y(function(d) {
           return d.value
        })
        .color(colors)
        .showLabels(true)
        .labelType("percent");

     d3.select("#chartnewagain svg")
        .datum(data2)
        .transition().duration(1200)
        .call(chartnewagains);

     d3.selectAll(".nv-label text")
        .attr("transform", function(d) {
           d.innerRadius = -450;
           d.outerRadius = r;
           return "translate(" + arc.centroid(d) + ")";
        })
        .attr("text-anchor", "middle")
        .style({
           "font-size": "1em"
        });

     d3.selectAll('.nv-series').each(function(d, i) {
        var group1 = d3.select(this),
           circle = group.select('circle');
        var color = circle.style('fill');
        circle.remove();
        var symbol = group.append('path')
           .attr('d', d3.svg.symbol().type('square'))
           .style('stroke', color)
           .style('fill', color)
           // ADJUST SIZE AND POSITION
           .attr('transform', 'scale(1.5) translate(-2,0)')
     });
     return chartnewagains;
  });

如何使用上述代码在同一页面上添加五个饼图?

【问题讨论】:

  • 是否可以改进数据结构,使所有五个饼图的数据都在一个数组中?
  • 要添加到上面的注释并只是为了改进您的代码,您正在以一种非常 WET(将所有内容编写两次)的方式使用 d3 函数。为什么不创建一个绘制饼图的函数,并通过传入 div 名称和数据的参数来调用它,并使用数组中的 data、data1、data2 创建一个 for 循环?

标签: javascript jquery angularjs d3.js nvd3.js


【解决方案1】:

您需要将您的选择集中在每个 SVG 上。 所以,首先选择当前的 SVG:

const svg = d3.select("#chartnewagain svg")

那么,

svg.selectAll(".nv-label text")
svg.selectAll('.nv-series')

因为,当您使用 d3.selectAll(".something") 时,您选择的是类 .something 的所有内容(也在上面的 SVG 中),而您不希望这样。

希望对你有帮助:)

编辑: 问题是

 d3.selectAll('.nv-series').each(function(d, i) {
  var group = d3.select(this),
    circle = group.select('circle');
  console.log(circle)  
  var color = circle.style('fill');  // error
...

因为当你第一次这样做时,'.nv-series' 是从第一个 svg 中选择的,一切都很好。第二次 '.nv-series' 是从第一个和第二个 svg 中选择的,并且因为您已经首先删除了该圆圈,所以当您从 circle = group.select('circle') 找不到的元素中获取样式时,它会引发错误

所以,解决办法是:

const svg = d3.select("#div_id svg")
...
svg.selectAll(".nv-label text")
...
svg.selectAll('.nv-series')
...

对于每个实例

【讨论】:

    【解决方案2】:

    我认为 Donat 建议的解决方案不会奏效,因为问题不仅仅是选择。我简化了代码以将datadata1data2 推送到一个名为alldata 的数组中。这使我可以为每个 alldata 循环创建一个forEach 循环,并创建一个统一的绘制图表函数,该函数可以为数据绘制饼图。 经过多次故障排除后,我发现代码中的问题是附加了导致问题的数据图例。看来circle.remove(); 是造成问题的原因。我编辑了下面小提琴中的代码,通过使用更好的选择删除圆圈来解决问题。

    这是一个完整的工作小提琴,改进了代码以使其更易于阅读: https://jsfiddle.net/coolakul/z1b2p7x3/

    我希望这会有所帮助。如果您有任何需要澄清的地方,请告诉我。

    【讨论】:

    • 完美,使用循环进行迭代。谢谢你的小提琴。
    猜你喜欢
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-27
    • 1970-01-01
    相关资源
    最近更新 更多