数据的格式如下:
[
{y: 17.0, x: '2015-07-23 00:02:41'},
{y: 17.0, x: '2015-07-23 00:03:41'},
{y: 15.0, x: '2015-07-23 01:21:44'},
{y: 23.0, x: '2015-07-23 02:50:48'},
{y: 21.0, x: '2015-07-23 02:51:48'},
{y: 28.0, x: '2015-07-23 02:52:48'},
{y: 24.0, x: '2015-07-23 02:53:48'},
{y: 27.0, x: '2015-07-23 02:54:48'},
{y: 18.0, x: '2015-07-23 02:55:48'},
{y: 28.0, x: '2015-07-23 02:56:48'}
]
我试图根据这些数据绘制折线图;但每次我尝试它都是空白的;这就是我正在做的事情:
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 %H:%M:%S").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")
.ticks(5);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(5);
var vline = d3.svg.line()
.x(function(d, i) { return x(parseDate(d.x)); })
.y(function(d, i) { return y(d.y); });
// add canvas
var svg = d3.select(".swatch").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 + ")");
x.domain(d3.extent(data, function(d) { return d.x; }));
y.domain([0, d3.max(data, function(d) { return d.y; })]);
// draw line
svg.append("path")
.attr("class", "line")
.attr("d", vline(data));
// x axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// y axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
使用console.log
打印时,字段输出正确(来自var vline = d3.svg.line()...
,但该行永远不会被绘制。
知道为什么它没有画线?
答案 0 :(得分:3)
$(document).ready(function () {
data =[
{y: 17.0, x: '2015-07-23 00:02:41'},
{y: 17.0, x: '2015-07-23 00:03:41'},
{y: 15.0, x: '2015-07-23 01:21:44'},
{y: 23.0, x: '2015-07-23 02:50:48'},
{y: 21.0, x: '2015-07-23 02:51:48'},
{y: 28.0, x: '2015-07-23 02:52:48'},
{y: 24.0, x: '2015-07-23 02:53:48'},
{y: 27.0, x: '2015-07-23 02:54:48'},
{y: 18.0, x: '2015-07-23 02:55:48'},
{y: 28.0, x: '2015-07-23 02:56:48'}
];
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 %H:%M:%S").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")
.ticks(5);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(5);
var vline = d3.svg.line()
.x(function(d, i) { return x(parseDate(d.x)); })
.y(function(d, i) { return y(d.y); });
// add canvas
var svg = d3.select(".swatch").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 + ")");
x.domain(d3.extent(data, function(d) { return parseDate(d.x); }));
y.domain([0, d3.max(data, function(d) { return d.y; })]);
// draw line
svg.append("path")
.attr("class", "line")
.attr("d", vline(data));
// x axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// y axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
});
path {
stroke: black;
fill: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="dectree" class='swatch'></div>
这是工作.....