简体   繁体   English

如何在d3.js饼图中显示小值弧?

[英]how to display the small value arcs in the d3.js pie chart?

I am using d3.js to render my pie chart but for small values, it's not displaying the corresponding arc in the pie chart my input values are like this我正在使用 d3.js 来呈现我的饼图,但对于小值,它没有在饼图中显示相应的弧,我的输入值是这样的

$scope.exampleData = [{
    key: "zero",
    y: 11121
}, {
    key: "one",
    y: 2780
}, {
    key: "two",
    y: 2780
}, {
    key: "three",
    y: 11
}, {
    key: "four",
    y: 15689
}, {
    key: "five",
    y: 1111
}];

here input key three the value is 11, this arc is not appearing in my chart can anyone help how to display the small value arcs in the pie chart.这里输入键三的值是 11,这个弧没有出现在我的图表中,任何人都可以帮助如何在饼图中显示小值弧。

After couple hours debug the d3.js internal code, I came to know that we can display the arcs for smaller values.I achieve this functionality by changing the internal code in the d3.js file.经过几个小时调试 d3.js 内部代码后,我开始知道我们可以显示较小值的弧线。我通过更改 d3.js 文件中的内部代码来实现此功能。 actually, for smaller values, the arc is not displayed because of for small input values the start angle and end angles are less that's why it is not shown the chart.实际上,对于较小的值,圆弧不会显示,因为对于较小的输入值,起始角度和结束角度较小,这就是图表未显示的原因。 then I figure out where internally d3.js assigning that start angle and end angle.然后我找出内部 d3.js 分配起始角度和结束角度的位置。 I increased the end angle of small input values now my chart displayed arc for smaller input values in the chart.我增加了小输入值的结束角度,现在我的图表显示了图表中较小输入值的弧线。 this is the code where I internally change the code in d3.js这是我在内部更改 d3.js 中的代码的代码

d3.layout.pie = function() {
    var value = Number, sort = d3_layout_pieSortByValue, startAngle = 0, endAngle = τ;
    function pie(data) {
      var values = data.map(function(d, i) {
        return +value.call(pie, d, i);
      });
      var a = +(typeof startAngle === "function" ? startAngle.apply(this, arguments) : startAngle);
      var k = ((typeof endAngle === "function" ? endAngle.apply(this, arguments) : endAngle) - a) / d3.sum(values);
      var index = d3.range(data.length);
      if (sort != null) index.sort(sort === d3_layout_pieSortByValue ? function(i, j) {
        return values[j] - values[i];
      } : function(i, j) {
        return sort(data[i], data[j]);
      });
      var arcs = [];
      index.forEach(function(i) {
        var d;
if(1000>values[i])
{
arcs[i] = {
          data: data[i],
          value: d = values[i],
          startAngle: a,
          endAngle: a += 0.208246141
        }
}else{
        arcs[i] = {
          data: data[i],
          value: d = values[i],
          startAngle: a,
          endAngle: a += d * k
        };
}
      });

In the above just i added only在上面我只添加了

if(1000>values[i])
{
arcs[i] = {
          data: data[i],
          value: d = values[i],
          startAngle: a,
          endAngle: a += 0.208246141
        }
}else{
        arcs[i] = {
          data: data[i],
          value: d = values[i],
          startAngle: a,
          endAngle: a += d * k
        };
}

I write the if-else block to check the value is less than 1000 then I increased the end angle by 0.208246141, then my arc is shown perfectly in the chart.我写了 if-else 块来检查值是否小于 1000 然后我将结束角度增加了 0.208246141,然后我的弧线在图表中完美显示。

"Lying with data" above is an overstatement.上面的“用数据说谎”是夸大其词。 It's not uncommon to set a minimum to make small values visible - check out the default settings for RawGraphs.设置最小值以使小值可见的情况并不少见 - 查看 RawGraphs 的默认设置。 In this case, I'd use .padAngle() and the apply a stroke to the arc path to thicken.在这种情况下,我会使用.padAngle()并将笔触应用到圆弧路径以加厚。

Also I realize this was posted 2+yrs ago and has probably already fixed!我也意识到这是 2 多年前发布的,可能已经修复了!

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

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