如何使用d3.js导入XML数据? [英] How to import XML data using d3.js?

查看:1553
本文介绍了如何使用d3.js导入XML数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以提供一个基本示例,如何使用d3从XML文件导入数据?

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

我的XML文件如下所示:

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


推荐答案

传递到回调的XML对象是XML DOM的根元素(参见 https://github.com/mbostock/d3/wiki/Requests#wiki-d3_xml ),因此您需要使用JavaScript XML / DOM进行处理访问设施(请参见 http://www.hiteshagrawal.com/javascript/javascript -parsing-xml-in-javascript )。

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 (see http://www.hiteshagrawal.com/javascript/javascript-parsing-xml-in-javascript ).

我写了一个小例子,说明如何使用d3.xml创建一个条形图在原始的d3 barchart示例 http://mbostock.github.com/d3/tutorial /bar-1.html ):

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

链接查看示例: http://bl.ocks.org/2772585

使用XML代码链接: https://gist.github.com/lgrammel/2772585

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

这篇关于如何使用d3.js导入XML数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆