我正在尝试用 d3 做一个简单的情节。当我一次只绘制一行时它工作正常,但我想让我的代码更通用。
我的数据采用这种格式(请注意,2 组数据可能包含不同数量的点,并且我的 2 组度量之间的时间不完全同步):
var data = [{key: "kmm03", value:[{"time":1364108443000,"mesure":"1.6299999952316284"},{"time":1364108503000,"mesure":"1.100000023841858"},{"time" :1364108563000,"mesure":"1.159999966621399"}}, {key:"kmm04", value:[{"time":1364108416000,"mesure":"2.690000057220459"},{"time":1364108476000,"mesure": "3.319999933242798"},{"time":1364108536000,"mesure":"3.140000104904175"},{"time":1364108596000,"mesure":"2.9800000190734863"}}]
现在我尝试像这样绘制它,但我无法让 svg 行显示:
var margin = {top: 20, right: 40, bottom: 20, left: 40};
var width = 780 - margin.left - margin.right,
height = 250 - margin.top - margin.bottom;
var time_scale = d3.time.scale()
//.domain(time_extent)
.domain([1364108443000, 1364112559000])
.range([0, width]);
var mesure_scale = d3.scale.linear()
.domain([0,10])
.range([height, 0]);
var vis = d3.select("#box1")
.append("svg:svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var time_axis = d3.svg.axis()
.scale(time_scale)
.orient("bottom");
var mesure_axis = d3.svg.axis()
.scale(mesure_scale)
.orient("left");
var set_axis_t = vis.append("svg:g")
.attr("transform", "translate(0, " + height +")")
.call(time_axis);
var set_axis_m = vis.append("svg:g")
.attr("transform", "translate(0, 0)")
.call(mesure_axis);
var line = d3.svg.line()
.x(function(d){return time_scale(d.time);})
.y(function(d){return mesure_scale(d.mesure);});
var group = vis.selectAll('path')
.data(data)
.enter().append('path')
.attr("d", line);