简体   繁体   English

传入JSON对象的D3JS返回错误

[英]D3JS passing in a JSON object returns an error

This is my code: 这是我的代码:

d3.json('../../dev/json/ajson.php', function (error, json) {
  if (error) return console.warn(error);

  megavalues = ["Meetings", "Development", "Management"];
  megadata = [213, 891, 121];

  var categoryGraph = d3.select('#graph-container').append('svg')
      .attr('width', graphWidth)
      .attr('height', graphHeight)
      .data(megadata)
      .append('svg:g')
      .attr('transform', 'translate(' + graphWidth / 2.085 + ',' + graphHeight / 2.085 + ')');

  var arc = d3.svg.arc()
      .outerRadius(graphRadius - 10)
      .innerRadius(graphRadius - 130);

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

  var categoryDataGraph = categoryGraph.selectAll('g.slice')
      .data(pie(megadata))
      .enter().append('svg:g')
      .attr('class', 'slice');

  categoryDataGraph.append('svg:path')
      .attr('fill', function(d, i){ return color(i); })
      .attr('d', arc);

  categoryDataGraph.append('text')
      .attr('transform', function(d) { return 'translate(' + arc.centroid(d) + ')' })
      .attr('dy', '.35em')
      .attr('text-anchor', 'middle')
      .text(function(d, i) { return megavalues[i]; });

This is the result: 结果如下:

http://cloud.erikhellman.com/eQHJ/5TmsqKWu http://cloud.erikhellman.com/eQHJ/5TmsqKWu

This is my problem: 这是我的问题:

I am currently drawing the graphs using 2 arrays (megavalues & megadata), but what I WANT to do is to draw it onto categoryGraph using this json object: 我目前正在使用2个数组(兆值和兆数据)绘制图形,但是我想做的是使用此json对象将其绘制到categoryGraph上:

json.projectData.timeDistribution

which looks like this: 看起来像这样:

Object {Meetings: 12, Planning: 1, Development: 21} 

However, when I pass the object as the data the graph isn't drawn and/or returns this error: 但是,当我将对象作为数据传递时,未绘制图形和/或返回此错误:

Uncaught TypeError: Object #<Object> has no method 'map'

I have spent the past 5 hours reading through the documentation and googling for help. 在过去的5个小时中,我已经通读了文档并仔细搜索以寻求帮助。 This is the first time I am using D3.js and this far surpasses my JavaScript knowledge I think. 这是我第一次使用D3.js,远远超出了我的JavaScript知识。 On all the examples I have found there has either been arrays passed that only consisted of numbers or if there has been text included, the JSON has looked different. 在我发现的所有示例中,要么传递了仅由数字组成的数组,要么如果包含文本,则JSON看起来就不同了。 But I am sure it has to work even with my JSON object looking like this, right? 但是我确信即使我的JSON对象看起来像这样,它也必须工作,对吗?

This is my first question here, if I can improve the question or improve anything here in any way, please tell me. 这是我的第一个问题,如果我可以以任何方式改善这个问题或改善这里的任何内容,请告诉我。 :) :)

The "secret sauce" here is how you access the values in what's passed in. The difference between the two structures that you've tried is that one is an array (and works), whereas the other one is an object (and doesn't). 这里的“秘密秘诀”是您如何访问传入的值。您尝试过的两种结构之间的区别是,一个结构是一个数组(并且可以工作),而另一个结构是一个对象(而不会) t)。 This is because D3 won't iterate over an object (as suggested by the error message). 这是因为D3不会迭代对象(如错误消息所建议)。

The easiest way to fix this is to transform your object into an array using d3.entries and changing the accessor function accordingly. 解决此问题的最简单方法是使用d3.entries将对象转换为数组,并相应地更改访问器函数。 That is, your code would look something like 也就是说,您的代码看起来像

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

var categoryDataGraph = categoryGraph.selectAll('g.slice')
  .data(pie(d3.entries(json.projectData.timeDistribution))
  .enter().append('svg:g')
  .attr('class', 'slice');

You then also need to change the way the text is created: 然后,您还需要更改文本的创建方式:

categoryDataGraph.append('text')
  .attr('transform', function(d) { return 'translate(' + arc.centroid(d) + ')' })
  .attr('dy', '.35em')
  .attr('text-anchor', 'middle')
  .text(function(d, i) { return d3.keys(json.projectData.timeDistribution)[i]; });

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

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