你可以在這裏看看http://www.d3noob.org/2012/12/starting-with-basic-d3-line-graph.html
你的情況,因爲你已經在列表中有您的數據更加簡單。不要將它分成兩個數組,最好有一個列表[{date:...,value:...},{date:...,value:...} ...]
您必須初始化d3(如您所見,x軸有一個「時間」範圍,如果使用它,則可以使用d3.time.format(「%d-%b-%y」)解析日期。 ).parse功能):
var margin = {top: 30, right: 20, bottom: 30, left: 50},
width = 600 - margin.left - margin.right,
height = 270 - margin.top - margin.bottom;
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
var xAxis = d3.svg.axis().scale(x)
.orient("bottom").ticks(5);
var yAxis = d3.svg.axis().scale(y)
.orient("left").ticks(5);
/* ---------------------- This is your line --------------------- */
var valueline = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.value); });
/* -------------------------------------------------------------- */
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 + ")");
然後追加行(其中,數據是列表{日期:...,值:...}:
svg.append("path") // Add the valueline path.
.attr("d", valueline(data));
svg.append("g") // Add the X Axis
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g") // Add the Y Axis
.attr("class", "y axis")
.call(yAxis);
PS:我用「價值」在這裏,但在你使用「價值」的問題。
查看[Mike Bostock提供的示例](http://bl.ocks.org/mbostock/raw/3883245/)。 – 2014-08-28 10:25:30