我有一个使用TSV文件绘制的D3.js线图。现在我有一个JSON文件,时间戳和值作为键。这是TSV文件的代码:
<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文件有密钥:“date”和“close”。 我的JSON对象有以下键:“timestamp”,我需要的“value”和我不需要的“valid”,“channelid”,所以它几乎相同。 示例JSON文件如下所示:
Object { timestamp: "2017-10-15 19:45", ChannelID: 13, value: 65469, valid: false }
为什么D3不再显示图表?我需要改变什么?
答案 0 :(得分:2)
与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
});