在d3.js中导入json文件

Shweta

我真的是d3.js的新手,正在使用d3.js中的json数据创建折线图。当json数据位于html文件中时,该代码运行良好,但我想将数据从html中拉出,并将数据作为json文件引用。示例index.html如下所示:

<!DOCTYPE html>
<html lang="en">

<body>
            <svg id="visualisation" width="1000" height="500"></svg>
            <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
            <script>
                function InitChart() {
                    var data = [{
                        "sale": "202",
                        "year": "2000"
                    }, {
                        "sale": "215",
                        "year": "2002"
                    }, {
                        "sale": "179",
                        "year": "2004"
                    }, {
                        "sale": "199",
                        "year": "2006"
                    }, {
                        "sale": "134",
                        "year": "2008"
                    }, {
                        "sale": "176",
                        "year": "2010"
                    }];


                    var vis = d3.select("#visualisation"),
                        WIDTH = 1000,
                        HEIGHT = 500,
                        MARGINS = {
                            top: 20,
                            right: 20,
                            bottom: 20,
                            left: 50
                        },
                        xScale = d3.scale.linear().range([MARGINS.left, WIDTH - MARGINS.right]).domain([2000, 2010]),
                        yScale = d3.scale.linear().range([HEIGHT - MARGINS.top, MARGINS.bottom]).domain([134, 215]),
                        xAxis = d3.svg.axis()
                        .scale(xScale),
                        yAxis = d3.svg.axis()
                        .scale(yScale)
                        .orient("left");

                    vis.append("svg:g")
                        .attr("class", "x axis")
                        .attr("transform", "translate(0," + (HEIGHT - MARGINS.bottom) + ")")
                        .call(xAxis);
                    vis.append("svg:g")
                        .attr("class", "y axis")
                        .attr("transform", "translate(" + (MARGINS.left) + ",0)")
                        .call(yAxis);
                    var lineGen = d3.svg.line()
                        .x(function(d) {
                            return xScale(d.year);
                        })
                        .y(function(d) {
                            return yScale(d.sale);
                        })
                        .interpolate("basis");
                    vis.append('svg:path')
                        .attr('d', lineGen(data))
                        .attr('stroke', 'green')
                        .attr('stroke-width', 2)
                        .attr('fill', 'none');
                }
                InitChart();
            </script>
</body>

</html>

我想提取json数据并在index.html中引用它,因为实际的json数据以MB为单位。因此,我保存了数据并在InitChart()函数中将其引用为

var data = d3.json("linedata.json");

html和json文件都位于同一目录中。当我在brower(firefox)中打开index.html时,出现空白页。可能是什么错误?

杰拉尔多·富塔多

问题:

d3.json在变量中使用d3.json不返回任何内容(实际上,它返回与请求关联的对象,该对象与文件内容本身无关)。

解:

使用d3.json(url[, callback])代替。

说明:

D3提供了一种加载JSON文件的方法。您必须这样做:

d3.json("linedata.json", function(data){
    function initChart(){
        //all your code for initChart here
    };
    initChart();
});

这样,JSON文件中的数据将与参数关联data,并可在回调函数中使用。

这是文档:https : //github.com/mbostock/d3/wiki/Requests#d3_json

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章