admin管理员组文章数量:1296919
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=".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?
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/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?
Share Improve this question edited Apr 24, 2016 at 9:09 Gerardo Furtado 102k9 gold badges128 silver badges177 bronze badges asked Apr 24, 2016 at 3:02 ShwetaShweta 1371 gold badge2 silver badges11 bronze badges 1- Press F12. What errors do you see in the console? – J. Titus Commented Apr 24, 2016 at 3:06
1 Answer
Reset to default 8Problem:
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./mbostock/d3/wiki/Requests#d3_json
本文标签: javascriptImporting json file in d3jsStack Overflow
版权声明:本文标题:javascript - Importing json file in d3.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741643857a2390071.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论