我使用的是Mike Bostock的示例:(https://bl.ocks.org/mbostock/3887051)。
我的本地页面上没有任何显示,并且我不断收到错误消息
Fetch API cannot load file:///C:/Users/Sam/Desktop/Other/WebDev/test/data.csv. URL scheme must be "http" or "https" for CORS request.
jo @ d3.v5.min.js:2
(anonymous) @ d3.v5.min.js:2
(anonymous) @ groupedBar.html:38
我也有话要说
Uncaught (in promise) TypeError: Failed to fetch
at jo (d3.v5.min.js:2)
at Object.csv (d3.v5.min.js:2)
at groupedBar.html:38
jo @ d3.v5.min.js:2
(anonymous) @ d3.v5.min.js:2
(anonymous) @ groupedBar.html:38
Promise.then (async)
(anonymous) @ d3.v5.min.js:2
(anonymous) @ groupedBar.html:38
这是完整的代码:
<!DOCTYPE html>
<style>
.axis .domain {
display: none;
}
</style>
<svg width="960" height="500"></svg>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script>
var svg = d3.select("svg"),
margin = {top: 20, right: 20, bottom: 30, left: 40},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom,
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var x0 = d3.scaleBand()
.rangeRound([0, width])
.paddingInner(0.1);
var x1 = d3.scaleBand()
.padding(0.05);
var y = d3.scaleLinear()
.rangeRound([height, 0]);
var z = d3.scaleOrdinal()
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);
d3.csv("data.csv", function(d, i, columns) {
for (var i = 1, n = columns.length; i < n; ++i) d[columns[i]] = +d[columns[i]];
return d;
}, function(error, data) {
if (error) throw error;
var keys = data.columns.slice(1);
x0.domain(data.map(function(d) { return d.State; }));
x1.domain(keys).rangeRound([0, x0.bandwidth()]);
y.domain([0, d3.max(data, function(d) { return d3.max(keys, function(key) { return d[key]; }); })]).nice();
g.append("g")
.selectAll("g")
.data(data)
.enter().append("g")
.attr("transform", function(d) { return "translate(" + x0(d.State) + ",0)"; })
.selectAll("rect")
.data(function(d) { return keys.map(function(key) { return {key: key, value: d[key]}; }); })
.enter().append("rect")
.attr("x", function(d) { return x1(d.key); })
.attr("y", function(d) { return y(d.value); })
.attr("width", x1.bandwidth())
.attr("height", function(d) { return height - y(d.value); })
.attr("fill", function(d) { return z(d.key); });
g.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x0));
g.append("g")
.attr("class", "axis")
.call(d3.axisLeft(y).ticks(null, "s"))
.append("text")
.attr("x", 2)
.attr("y", y(y.ticks().pop()) + 0.5)
.attr("dy", "0.32em")
.attr("fill", "#000")
.attr("font-weight", "bold")
.attr("text-anchor", "start")
.text("Population");
var legend = g.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("text-anchor", "end")
.selectAll("g")
.data(keys.slice().reverse())
.enter().append("g")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", width - 19)
.attr("width", 19)
.attr("height", 19)
.attr("fill", z);
legend.append("text")
.attr("x", width - 24)
.attr("y", 9.5)
.attr("dy", "0.32em")
.text(function(d) { return d; });
});
</script>
有人可以帮助我了解我需要更改的内容吗?我可以从网页下载CSV,因此看起来上传正确。
答案 0 :(得分:-3)
您的控制台日志会准确告诉您您做错了什么。您正在通过本地目录URL访问该页面,而不是通过本地服务器作为HTTP / HTTPS进行访问。出于安全原因,您无法执行类似这样的对本地文件的获取请求和AJAX调用。
要解决您的问题,请在localhost上启动虚拟Web服务器,然后通过该服务器提供页面。