D3.js在将源文件从tsv更改为JSON之后不绘制图形
问题描述:
我有使用TSV文件绘制的D3.js线条图。现在我有一个带有时间戳和值作为键的JSON文件。这与TSV文件中的代码:D3.js在将源文件从tsv更改为JSON之后不绘制图形
<script language="javascript" type="text/javascript">
window.onload = function(){
var svg = d3.select("svg"),
margin = {top: 20, right: 20, bottom: 30, left: 50},
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 parseTime = d3.timeParse("%d-%b-%y");
var x = d3.scaleTime()
.rangeRound([0, width]);
var y = d3.scaleLinear()
.rangeRound([height, 0]);
var line = d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.close); });
d3.tsv(" /data.tsv ", function(d) {
d.date = parseTime(d.date);
d.close = +d.close;
return d;
}, function(error, data) {
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain(d3.extent(data, function(d) { return d.close; }));
g.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x))
.append("text")
.attr("fill", "#000")
.attr("transform", "rotate(0)")
.attr("x", 6)
.attr("dx", "1em")
.attr("text-anchor", "begin ")
.text("time")
.select(".domain")
.remove();
g.append("g")
.call(d3.axisLeft(y))
.append("text")
.attr("fill", "#000")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("text-anchor", "end")
.text("Price ($)");
g.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("stroke-width", 1.5)
.attr("d", line);
});
}
</script>
这是JSON文件的代码:
var svg = d3.select("svg"),
margin = {top: 20, right: 20, bottom: 30, left: 50},
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 parseTime = d3.timeParse("%y-%m-%d");
@* x-Axis goes from 0 to width*@
var x = d3.scaleTime()
.rangeRound([0, width]);
@* y-Axis goes from 0 to height*@
var y = d3.scaleLinear()
.rangeRound([height, 0]);
@* line generator, returns timestamp on x-axis and value on y-axis *@
var line = d3.line()
.x(function(d) { return x(d.timestamp); })
.y(function(d) { return y(d.value); });
d3.json(" /all.json ", function(d) {
d.forEach(function(d){
d.timestamp = d.timestamp;
d.value = d.value;
console.log(d.timestamp);
console.log(d.value);
});
return d;
}, function(error, data) {
@* returns maximum and minimum of array d (data) *@
x.domain(d3.extent(data, function(d) { return d.timestamp; }));
y.domain(d3.extent(data, function(d) { return d.value; }));
g.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x))
.append("text")
.attr("fill", "#000")
.attr("transform", "rotate(0)")
.attr("x", 6)
.attr("dx", "1em")
.attr("text-anchor", "begin ")
.text("time")
.select(".domain")
.remove();
g.append("g")
.call(d3.axisLeft(y))
.append("text")
.attr("fill", "#000")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("text-anchor", "end")
.text("Price ($)");
g.append("path")
@* https://github.com/d3/d3-selection/blob/master/README.md#selection_datum *@
.data(data)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("stroke-width", 1.5)
.attr("d", line);
});
}
的TSV文件拿钥匙:“日期”和“关闭”。 我的JSON对象具有我需要的“timestamp”,“value”和我不需要的“valid”,“channelid”键,所以它几乎是相同的。 一个例子JSON文件是这样的:
Object { timestamp: "2017-10-15 19:45", ChannelID: 13, value: 65469, valid: false }
为什么doesn't D3不再显示图形?我需要改变什么?
答
不像d3.tsv
,d3.json
确实不是接受一个行转换函数。
在你的代码...
d3.json("/all.json ", function(d) {
d.forEach(function(d){
d.timestamp = d.timestamp;
d.value = d.value;
console.log(d.timestamp);
console.log(d.value);
});
return d;
}, function(error, data) {
...一切"/all.json "
和function(error, data)
之间是行转换功能。
解决方案:移动的行功能回调里面...
d3.json("/all.json ", function(error, data) {
//this is "inside the callback"
});
,或者,因为它没有做任何事情,只是将其删除。
而且,你必须分析你的时间(这你是在TSV版本做):
d3.json("/all.json ", function(error, data) {
data.forEach(function(d){
d.timestamp = parseTime(d.timestamp);
});
//the rest of the code
});
抱歉愚蠢的问题,但究竟在何处,我需要把它?我是一个d3.js noob –
加号实际上做了什么?我想显式显示值,所以如果值是5,我希望它在y轴上是5。 –
你的意思是“功能(错误,数据)”? –