我无法获取图表上的数据点。两个轴都是可缩放的,并且都是日期范围。更具体地说,x 轴是日期,y 轴是小时。我想知道这是否与我的 JSON 文件格式化时间有关,但我仍然不确定为什么它没有绘制任何内容。我的 JSON 文件的示例是
data = [
{ "date" : "2013-21-02T00:00:00",
"vertical" : "2013-20-02T11:00:00"
},...]
下面是我的代码:
//Define the min and max date
var mindate = new Date(2013,02,20), // TODO: clip date
maxdate = new Date(2013,02,26);
//Define the range of hours for the yaxis. Uses military time.
var ymindate = new Date(2013,02,20, 7), // TODO: clip date
ymaxdate = new Date(2013,02,20, 17);
margin = { top: 20, right: 20, bottom: 20, left: 45};
width = 400 - margin.left - margin.right;
height = 200 - margin.top - margin.bottom;
var x = d3.time.scale()
.domain([mindate, maxdate])
.range([0, width]);
var y = d3.time.scale()
.domain([ymindate, ymaxdate])
.range([height, 0]);
var line = d3.svg.line()
.x(function (d) { return d.date; })
.y(function (d) { return d.vertical; });
var zoom = d3.behavior.zoom()
.x(x)
.y(y)
.on("zoom", zoomed);
svg = d3.select('#chart')
.append("svg:svg")
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.call(zoom)
.append("svg:g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var make_x_axis = function () {
return d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(5);
};
var make_y_axis = function () {
return d3.svg.axis()
.scale(y)
.orient("left")
.ticks(5);
};
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(5);
svg.append("svg:g")
.attr("class", "x axis")
.attr("transform", "translate(0, " + height + ")")
.call(xAxis);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(5);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
svg.append("g")
.attr("class", "x grid")
.attr("transform", "translate(0," + height + ")")
.call(make_x_axis()
.tickSize(-height, 0, 0)
.tickFormat(""));
svg.append("g")
.attr("class", "y grid")
.call(make_y_axis()
.tickSize(-width, 0, 0)
.tickFormat(""));
var clip = svg.append("svg:clipPath")
.attr("id", "clip")
.append("svg:rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", width)
.attr("height", height);
var chartBody = svg.append("g")
.attr("clip-path", "url(#clip)");
chartBody.append("svg:path")
.datum(data)
.attr("class", "line")
.attr("d", line);
function zoomed() {
svg.select(".x.axis").call(xAxis);
svg.select(".y.axis").call(yAxis);
svg.select(".x.grid")
.call(make_x_axis()
.tickSize(-height, 0, 0)
.tickFormat(""));
svg.select(".y.grid")
.call(make_y_axis()
.tickSize(-width, 0, 0)
.tickFormat(""));
svg.select(".line")
.attr("class", "line")
.attr("d", line);
}
}
任何帮助将不胜感激!谢谢!
最佳答案
您需要做的第一件事是将数据解析为 D3 可以识别的格式。所以类似:
var parseDate = d3.time.format("%Y-%d-%mT%H:%M:%S").parse; //Note the T in the middle
data.forEach(function (d,i) {
d.date = parseDate(d.date);
d.vertical= parseDate(d.vertical);
})
从那里,只需将它扔到行函数中,我认为应该一切顺利。哦,API 引用对此非常有帮助,here 在哪里? .
我刚刚编辑了上面的代码块,将值更改为垂直并包含最后的右括号(哎呀)。
接下来是将行生成器设置为仅返回日期或垂直值,因此需要将其更改为:
var line = d3.svg.line()
.x(function (d) { return x(d.date); })
.y(function (d) { return y(d.vertical); });
请注意包含 x(...)
和 y(...)
。
这是一个工作 fiddle 的链接
关于javascript - X 和 Y 轴绘图日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19612454/