简体   繁体   中英

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 .

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 ).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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