此代碼由js編程以使用D3 Libarary繪製折線圖。我面對控制檯顯示與d3.v3.min.js:1相關的錯誤(TypeError:e未定義)),並且當我點擊firefox的Debbuger tap時,它顯示此行數據無法讀取我的Json文件以使用D3.js代碼繪製折線圖
!function(){function n(n,t){return t> n?-1:n> t?1:n> = t?0:0/0} function t(n){return null!= n & !& isNaN(N)}函數E(N){返回{左:函數(T,E,R,U){對於(<的arguments.length 3 & &(R = 0),的arguments.length & (u = t.length); u> r;){var i = r + u >>> 1; n(t [i],e)< 0?r = i + 1:u = i}返回 這條線更長!
這是我的JSON文件
{
"Id": 2,
"Name": "ukraine",
"Occurrences": [
{
"OccurrenceDate": "Jan 2000",
"OccurrenceFrequency": 136
},
{
"OccurrenceDate": "Feb 2000",
"OccurrenceFrequency": 2
},
{
"OccurrenceDate": "Mar 2000",
"OccurrenceFrequency": 89
}
]}
這是我的代碼,我的代碼是使基於
<script src="http://d3js.org/d3.v3.min.js"></script>``
<script>
var margin = { top: 80, right: 80, bottom: 80, left: 80 },
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var parse = d3.time.format("%b %Y").parse;
// Scales and axes. Note the inverted domain for the y-scale: bigger is up!
var x = d3.time.scale().range([0, width]),
y = d3.scale.linear().range([height, 0]),
xAxis = d3.svg.axis().scale(x).tickSize(-height).tickSubdivide(true),
yAxis = d3.svg.axis().scale(y).ticks(4).orient("right");
// An area generator, for the light fill.
var area = d3.svg.area()
.interpolate("monotone")
.x(function (d) { return x(d.OccurrenceDate); })
.y0(height)
.y1(function (d) { return y(d.OccurrenceFrequency); });
// A line generator, for the dark stroke.
var line = d3.svg.line()
.interpolate("monotone")
.x(function (d) { return x(d.OccurrenceDate); })
.y(function (d) { return y(d.OccurrenceFrequency); });
d3.json("readme.json", type, function (error, data) {
// Filter to one Name; ukraine.
var values = data.filter(function (d) {
return d.Name == "ukraine";
});
// Compute the minimum and maximum date, and the maximum OccurrenceFrequency.
x.domain([values[0].OccurrenceDate, values[values.length - 1].OccurrenceDate]);
y.domain([0, d3.max(values, function (d) { return d.OccurrenceFrequency; })]).nice();
// Add an SVG element with the desired dimensions and margin.
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 + "," + margin.top + ")")
.on("click", click);
// Add the clip path.
svg.append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
// Add the area path.
svg.append("path")
.attr("class", "area")
.attr("clip-path", "url(#clip)")
.attr("d", area(values));
// Add the x-axis.
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Add the y-axis.
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + width + ",0)")
.call(yAxis);
// Add the line path.
svg.append("path")
.attr("class", "line")
.attr("clip-path", "url(#clip)")
.attr("d", line(values));
// Add a small label for the name.
svg.append("text")
.attr("x", width - 6)
.attr("y", height - 6)
.style("text-anchor", "end")
.text(values[0].Name);
// On click, update the x-axis.
function click() {
var n = values.length - 1,
i = Math.floor(Math.random() * n/2),
j = i + Math.floor(Math.random() * n/2) + 1;
x.domain([values[i].OccurrenceDate, values[j].OccurrenceDate]);
var t = svg.transition().duration(750);
t.select(".x.axis").call(xAxis);
t.select(".area").attr("d", area(values));
t.select(".line").attr("d", line(values));
}
});
// Parse dates and numbers. We assume values are sorted by date.
function type(d) {
d.OccurrenceDate = parse(d.OccurrenceDate);
d.OccurrenceFrequency = +d.OccurrenceFrequency;
return d;
}
</script>
爲什麼你在(...)中包裝你的JSON;而不是隻保留在{...}中? – dsuess
消息「e未定義」在哪一行發生?我在代碼中找不到名爲'e'的變量。 – dsuess
它在conslo中說沒有任何特定行((TypeError:e未定義)) – Sudgey