如何用d3.js绘制折线图?

时间:2016-05-16 08:38:21

标签: javascript php d3.js

我第一次使用d3.js

所以我提到http://bl.ocks.org/mbostock/3883245

但是,在从ajax获取数据后,只显示了y轴。

这是JS代码:

var margin = {top: 20, right: 20, bottom: 30, left: 50},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var parseDate = d3.time.format("%d-%b-%y").parse;

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");

var yAxis = d3.svg.axis()
.scale(y)
.orient("left");

var line = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.close); });

var svg = d3.select("#wishingListMsg").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 + ")");

d3.json("MYURL", function(error, data) {
if (error) throw error;console.log(data);

data.forEach(function(d) {
    d.date = parseDate(d.date);
    d.close = +d.close;
});

x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.close; })]);

svg.append("g")
  .attr("class", "x axis")
  .attr("transform", "translate(0," + height + ")")
  .call(xAxis);

svg.append("g")
  .attr("class", "y axis")
  .call(yAxis)
  .append("text")
  .attr("transform", "rotate(-90)")
  .attr("y", 6)
  .attr("dy", ".71em")
  .style("text-anchor", "end")
  .text("Price ($)");

svg.append("path")
  .datum(data)
  .attr("class", "line")
  .attr("d", line);
});

和浏览器返回的JSON数据:

[{"date":"2016-05-03","close":"500"},{"date":"2016-05-04","close":"399"},{"date":"2016-05-11","close":"1"},{"date":"2016-05-13","close":"20"}]

HTML:

<div id="wishingListMsg"></div>

如何修改代码?

1 个答案:

答案 0 :(得分:2)

您的数据中的日期如下:

2016-05-03 //year-month-day

在示例中:

24-Apr-07 //day-month-year

所以你的日期解析错了。所以不要这样:

var parseDate = d3.time.format("%d-%b-%y").parse;
//which is trying to parse : day, abbreviated month name (which you dont have) and year

请改用此解析:

var parseDate = d3.time.format("%Y-%m-%d").parse;
//which will parse : year, month and day (all in integers)

时间解析的文档:https://github.com/d3/d3/wiki/Time-Formatting

我还添加了这个CSS:

.line{
  fill:none;
  stroke:black;
  stroke-width:2px;
}

如果删除它,你会发现原因:)基本上,它默认为填充黑色而没有笔划,所以你必须删除填充并且只需要路径的笔划。

更新小提琴:https://jsfiddle.net/thatOneGuy/au1h6mvr/

var data = [{
  "date": "2016-05-03",
  "close": "500"
}, {
  "date": "2016-05-04",
  "close": "399"
}, {
  "date": "2016-05-11",
  "close": "1"
}, {
  "date": "2016-05-13",
  "close": "20"
}]




var margin = {
    top: 20,
    right: 20,
    bottom: 30,
    left: 50
  },
  width = 960 - margin.left - margin.right,
  height = 500 - margin.top - margin.bottom;

var parseDate = d3.time.format("%Y-%m-%d").parse;

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");

var yAxis = d3.svg.axis()
  .scale(y)
  .orient("left");

var line = d3.svg.line()
  .x(function(d) {
    return x(d.date);
  })
  .y(function(d) {
    return y(d.close);
  });

var svg = d3.select("#wishingListMsg").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 + ")");


data.forEach(function(d) {
console.log('date',d.date)
  d.date = parseDate(d.date);
  
  d.close = +d.close;
});

x.domain(d3.extent(data, function(d) {
console.log('d',d)
  return d.date;
}));
y.domain([0, d3.max(data, function(d) {
  return d.close;
})]);

svg.append("g")
  .attr("class", "x axis")
  .attr("transform", "translate(0," + height + ")")
  .call(xAxis);

svg.append("g")
  .attr("class", "y axis")
  .call(yAxis)
  .append("text")
  .attr("transform", "rotate(-90)")
  .attr("y", 6)
  .attr("dy", ".71em")
  .style("text-anchor", "end")
  .text("Price ($)");

svg.append("path")
  .datum(data)
  .attr("class", "line")
  .attr("d", line);
.line{
  fill:none;
  stroke:black;
  stroke-width:2px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="wishingListMsg"></div>