简体   繁体   English

如何为d3图形构建准备JSON对象?

[英]How to prepare JSON object for d3 graph building?

I am writing a small application that would parse xml file and then create a graph out of it using d3.js . 我正在编写一个小型应用程序,它将解析xml文件,然后使用d3.js从中创建一个图形。

So far I have build the following script: 到目前为止,我已经构建了以下脚本:

<script>
$(function() {
  var mydata = new Array(1);
  $.get("testcase.xml", function(xml) {
      $(xml).find('measurement').each(function(){
        var meas = $(this); 
        var type = meas.find('type').text();
        var interval = meas.find('interval').text();

        mydata.push({'type': type, 'interval': interval});

      });

      var ch = d3.select("body")
          .append("ul")
          .selectAll("li")
          .data(mydata)
          .enter()
          .append("li")
          .text(function(d){
              return d.type + ": " + d.interval;
              });
    });

});
</script>

I am pretty sure I am building the mydata array in a wrong way but I am getting 5 bullet points (that many measurement tags I have in my testcase.xml but they do not contain the data that I have build up in mydata variable ). 我很确定我以错误的方式构建了mydata数组,但我得到了5个要点(我的testcase.xml有很多measurement标签,但是它们不包含我在mydata变量中积累的数据 )。

I have finally solved it ( as posted in the comment ) : 我终于解决了它(如评论中所述):

The problem for this was that I have been creating new Array(1) and then after appending data, I was trying to read .type property of undefined (which was the first element in the array. 问题是,我一直在创建new Array(1) ,然后追加数据后,我试图读取undefined .type property (这是数组中的第一个元素。

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

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