简体   繁体   中英

Importing json file in d3.js

I am really new to d3.js and am creating a line graph from json data in d3.js. The code works well when json data is inside the html file but I want to pull the data out of html and refer the data as a json file. The sample index.html looks like this:

<!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>

I want to pull out the json data and refer it in index.html as the actual json data is in MBs. So, I saved the data and referred it inside the InitChart() function as

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

with both the html and json files being in the same directory. When I open index.html in brower(firefox), I get a blank page. What could be the error?

Problem:

Using d3.json in a variable. d3.json don't return anything (actually, it returns an object associated with the request, which has nothing to do with the file content itself).

Solution:

Using d3.json(url[, callback]) instead.

Instructions:

D3 provides a way to load JSON files. You'll have to do this:

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

This way, the data in the JSON file is associated to the parameter data and can be used inside the callback function.

Here is the documentation: https://github.com/mbostock/d3/wiki/Requests#d3_json

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