gpt4 book ai didi

javascript - 为什么这个折线图没有绘制? (d3js)

转载 作者:行者123 更新时间:2023-11-30 12:19:10 24 4
gpt4 key购买 nike

数据的格式如下:

[
{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()... 但永远不会绘制线条。

知道为什么它不划清界线吗?

最佳答案

$(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>

这里正在工作......

关于javascript - 为什么这个折线图没有绘制? (d3js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31579384/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com