gpt4 book ai didi

javascript - 在谷歌可视化线上绘制 x 轴日期

转载 作者:行者123 更新时间:2023-11-30 20:29:35 25 4
gpt4 key购买 nike

我正在尝试使用谷歌可视化创建折线图。所以我从他们的网页上复制了这个例子chart example并且有效。

我想做的唯一更改是将 x 轴设为日期而不是数字。所以下面的第一部分代码工作正常,来自他们的网站。

<!DOCTYPE html>
<html lang="en-US">
<body>

<div id="linechart_material"></div>

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

<script type="text/javascript">
// Load google charts
google.charts.load('current', {'packages':['line']});
google.charts.setOnLoadCallback(drawChart);

function drawChart() {

var data = new google.visualization.DataTable();
data.addColumn('number', 'Day');
data.addColumn('number', 'Guardians of the Galaxy');
data.addColumn('number', 'The Avengers');
data.addColumn('number', 'Transformers: Age of Extinction');

data.addRows([
[1, 37.8, 80.8, 41.8],
[2, 30.9, 69.5, 32.4],
[3, 25.4, 57, 25.7],
[4, 11.7, 18.8, 10.5],
[5, 11.9, 17.6, 10.4],
[6, 8.8, 13.6, 7.7],
[7, 7.6, 12.3, 9.6],
[8, 12.3, 29.2, 10.6],
[9, 16.9, 42.9, 14.8],
[10, 12.8, 30.9, 11.6],
[11, 5.3, 7.9, 4.7],
[12, 6.6, 8.4, 5.2],
[13, 4.8, 6.3, 3.6],
[14, 4.2, 6.2, 3.4]
]);

var options = {
chart: {
title: 'Box Office Earnings in First Two Weeks of Opening',
subtitle: 'in millions of dollars (USD)'
},
width: 900,
height: 500
};

var chart = new google.charts.Line(document.getElementById('linechart_material'));

chart.draw(data, google.charts.Line.convertOptions(options));
}
</script>

</body>
</html>

然而,当我进行以下更改时,没有任何显示,

data.addColumn('number', 'Day'); to data.addColumn('date', 'Day');

并将 data.addRows 位更改为,

data.addRows([
[new date(2018,1, 1), 37.8, 80.8, 41.8],
[new date(2018,1, 2), 30.9, 69.5, 32.4],
[new date(2018,1, 3), 25.4, 57, 25.7],
[new date(2018,1, 4), 11.7, 18.8, 10.5],
[new date(2018,1, 5), 11.9, 17.6, 10.4],
[new date(2018,1, 6), 8.8, 13.6, 7.7],
[new date(2018,1, 7), 7.6, 12.3, 9.6],
[new date(2018,1, 8), 12.3, 29.2, 10.6],
[new date(2018,1, 9), 16.9, 42.9, 14.8],
[new date(2018,1, 10), 12.8, 30.9, 11.6],
[new date(2018,1, 11), 5.3, 7.9, 4.7],
[new date(2018,1, 12), 6.6, 8.4, 5.2],
[new date(2018,1, 13), 4.8, 6.3, 3.6],
[new date(2018,1, 14), 4.2, 6.2, 3.4]
]);

最佳答案

new Date 中的D 应该大写...

data.addRows([
[new Date(2018,1, 1), 37.8, 80.8, 41.8], // <-- be sure Date is capitalized

关于javascript - 在谷歌可视化线上绘制 x 轴日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50505566/

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