简体   繁体   English

用“零”数据破解d3饼图

[英]hack for d3 pie chart with 'zero' data

I want to display a d3 pie chart that will be visible even if there is no data yet (similarly to this quesiton: Is it possible to start a D3JS pie chart with all values being 0? ) - for example, a pie chart showing the total number of asked versus answered questions on a user's profile - when the user has just signed up, you'd still expect to see the pie chart (perhaps greyed out in a single colour). 我想显示即使没有数据也可以看到的d3饼图(类似于此问题: 是否可以启动所有值为0的D3JS饼图? )-例如,显示用户个人资料上的已回答问题和已回答问题的总数-当用户刚注册时,您仍然希望看到饼图(可能以单色显示为灰色)。

The solution I want to implement is to check during creation of the pie chart whether the data, which has 2 rows (one for the correct count and one for the incorrect count) consists only of zeros, and if so I'd simply change one of those numbers to 1 in order to draw the chart and then intervene later to ensure there's no text saying something like "1 correct". 我要实现的解决方案是在创建饼图时检查是否有2行(正确计数为1行,错误计数为1行)的数据仅由零组成,如果是,则只需更改一个将这些数字中的1改为1,以绘制图表,然后稍后进行干预,以确保没有文字说“ 1正确”。

So my question is whether anyone can see a reasonable way to change the following code during pie chart creation to change one of the pie chart values from 0 to 1 only if BOTH rows are 0... 所以我的问题是,只有当两行都为0时,是否有人可以看到在饼图创建期间更改以下代码的合理方法,以将饼图值之一从0更改为1 ...

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

I've experimented with numerous solutions looping through the data (eg using the .each() function) to get a sum total of all values of d.someCount, but they so far haven't had the desired effect and so I suspect I'm not visualising the problem correctly. 我已经尝试了多种解决方案来遍历数据(例如使用.each()函数)以获取d.someCount的所有值的总和,但是到目前为止它们还没有达到预期的效果,所以我怀疑我无法正确显示问题。

--EDIT-- - 编辑 -

Example as requested - try changing both values (top row in Javascript) to zero and the chart disappears (understandably based on the feedback on the other SO post I linked to)... 根据要求的示例-尝试将两个值(JavaScript的第一行)都更改为零,图表消失(基于我链接到的其他SO帖子的反馈,这是可以理解的)...

http://codepen.io/d3wannabe/pen/oZbWEv http://codepen.io/d3wannabe/pen/oZbWEv

You could add an additional placeholder category, "empty" , whose count is set dynamically based on the other two categories. 您可以添加一个额外的占位符类别"empty" ,其数量是根据其他两个类别动态设置的。 And you could give it its own color to distinguish it from the other two. 您可以给它自己的颜色以区别于其他两个。 Does that accomplish your goal? 这样可以实现您的目标吗?

var pieChartData = [ { "type": "asked", "theCount": "0" }
                   , { "type": "answered", "theCount": "0" }
                   , { "type": "empty", "theCount": "0"}];

if(pieChartData[0].theCount === "0" &&
  pieChartData[1].theCount === "0"){
  pieChartData[2].theCount = "1"
} else {
  pieChartData[2].theCount = "0"
}

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

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