Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import XML data using d3.js?

Tags:

xml

d3.js

can someone provide a basic example how to import data from an XML file using d3?

My XML file looks like this:

    <data>
        <value>71</value>
        <value>12</value>
        <value>44</value>
        <value>88</value>
    </data>

How can I add these values to a data array? Here is what I tried so far:

    d3.xml("values.xml", function(xml) {
     d3.select(xml).selectAll("data").each(function(data) {
        d3.select(data).selectAll("value");
        //add data to array?;
        };
});
    //use Array
like image 712
Olga Avatar asked May 21 '12 09:05

Olga


People also ask

Which is the correct way to use XML file for D3?

The correct way to use XML file for d3 is d3. xml(url[mimeType][,callback]) . This command is used to create a request for the XML file at the specified url.

How do I import D3 into JavaScript?

Install D3 by running npm install d3 --save . Import D3 to App. js by adding import * as d3 from d3 . You need to use import * (“import everything”) since D3 has no default exported module.

Can you use XML with JavaScript?

With a few lines of JavaScript code, you can read an XML file and update the data content of any HTML page.

What does D3 CSV return?

d3. csv() returns the data as an object. This object is an array of objects loaded from your csv file where each object represents one row of your csv file.


1 Answers

The XML object that is passed into the callback is the root element of the XML DOM (see https://github.com/mbostock/d3/wiki/Requests#wiki-d3_xml ), and therefore you need to process it using the JavaScript XML/DOM access facilities.

I have written a small example that shows how to use d3.xml to create a bar chart (based on the original d3 barchart example http://mbostock.github.com/d3/tutorial/bar-1.html ):

Link to see example: http://bl.ocks.org/2772585

Link with XML code: https://gist.github.com/lgrammel/2772585

like image 119
Lars Grammel Avatar answered Oct 18 '22 08:10

Lars Grammel