我已经阅读了所有与我的问题相关的堆叠溢出问题,但我仍然想不出如何解决这个问题。下面的代码读取data.JSON,然后以条形图的形式输出数据,由于某种原因,在Chrome或IE中没有出现。
我读到的一些答案是将html的所有引用都引用到txt或设置SVG的边界,但据我所知,就我有限的经验而言,这已经完成了 :
<!DOCTYPE html>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<head>
<style>
.bar{
fill: darkblue;
}
.bar:hover{
fill: darkred;
}
.axis {
font: 20px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
<h2>User Runtime by Hours</h2>
<h3>Generate on: </h3>
</head>
<body>
<script src="d3js.org/d3.min.js"></script>
<script src="js/jquery.min.js"></script>
<script>
// set the dimensions of the canvas
var margin = {top: 20, right: 20, bottom: 100, left: 80},
width = 1600 - margin.left - margin.right,
height = 700 - margin.top - margin.bottom;
// set the ranges
var x = d3.scale.ordinal().rangeRoundBands([0, width], .05);
var y = d3.scale.linear().range([height, 0]);
// define the axis
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(10);
// add the SVG element
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + (margin.left + 20) + "," + margin.top + ")");
// load data
d3.json("data.json", function(error, data) {
data.forEach(function(d) {
d.User = d.User;
d.Runtime = +d.Runtime;
});
// scale the range of the data
x.domain(data.map(function(d) { return d.User; }));
y.domain([0, d3.max(data, function(d) { return d.Runtime; })]);
// add axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.7em")
.attr("dy", "-.3em")
.attr("transform", "rotate(-90)" );
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("hour")
.attr("transform", "rotate(-90)")
.attr("y", 5)
.attr("dy", "-30em")
.style("text-anchor", "start")
.text("Hours");
// Add bar chart
svg.selectAll("bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.User); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.Runtime); })
.attr("height", function(d) { return height - y(d.Runtime); });
});
</script>
</body>
示例数据JSON:
[
{
"User" : "xxxx",
"Runtime" : 931.216111111
},
{
"User" : "yyyy",
"Runtime" : 600.404444444
}
]
Chrome错误代码:
d3.min.js:1 Failed to load file:///C:/EUV/logbook/data.json: Cross origin
requests are only supported for protocol schemes: http, data, chrome,
chrome-extension, https.
Cn.i.send @ d3.min.js:1
index.html:76 Uncaught TypeError: Cannot read property 'forEach' of
undefined
at Object.<anonymous> (index.html:76)
at Object.t (d3.min.js:1)
at XMLHttpRequest.u (d3.min.js:1)
at Object.Cn.i.send (d3.min.js:1)
at Object.i.(anonymous function) [as get] (file:///C:/EUV/logbook/d3js.org/d3.min.js:1:10862)
at Cn (d3.min.js:1)
at Object.oa.json (d3.min.js:5)
at index.html:74
发布于 2018-08-07 09:40:14
你正在使用file://
因为他们不是http://
在本地pc中安装一个web服务器,或者将模型上传到某个地方。
如果你使用的是ATOM或VS代码,则有本地服务器的扩展。
https://stackoverflow.com/questions/-100001969
复制相似问题