gpt4 book ai didi

javascript - 将 JSON 加载到 Google 面积图中

转载 作者:行者123 更新时间:2023-12-03 08:24:21 25 4
gpt4 key购买 nike

我正在尝试将 json 数据加载到 Google Chart 中,就像他们那样 here 。为了简化事情,我首先在 html 文件中定义 json。我可以让这个示例正常工作(它是一个饼图),但是当我尝试使用面积图来实现它时,它不起作用。这是我正在做的事情:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var jsonData =
{
cols: [{id: 'A', label: 'NEW A', type: 'string'},
{id: 'B', label: 'B-label', type: 'number'},
{id: 'C', label: 'C-label', type: 'date'}
],
rows: [{c:[{v: 'a'},
{v: 1.0, f: 'One'},
{v: new Date(2008, 1, 28, 0, 31, 26), f: '2/28/08 12:31 AM'}
]},
{c:[{v: 'b'},
{v: 2.0, f: 'Two'},
{v: new Date(2008, 2, 30, 0, 31, 26), f: '3/30/08 12:31 AM'}
]},
{c:[{v: 'c'},
{v: 3.0, f: 'Three'},
{v: new Date(2008, 3, 30, 0, 31, 26), f: '4/30/08 12:31 AM'}
]}
]
};

var data = google.visualization.DataTable(jsonData);

var options = {
title: 'Company Performance',
hAxis: {title: 'Year', titleTextStyle: {color: '#333'}},
vAxis: {minValue: 0}
};

var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</script>
<div id="chart_div" style="width:900; height:500"></div>

上面的代码没有绘制任何内容。我确信这个问题确实微不足道且愚蠢,但有人能告诉我它是什么吗?

非常感谢,亚历克斯

最佳答案

第一个错误是未捕获错误:您在没有使用“new”关键字的情况下调用了 google.visualization.DataTable()。之后,检查数据。我不相信您可以将日期和数字合并到面积图中,它需要数字。

我插入一个示例并更改为数字,它可以工作:jsfiddle.net/L1tct3Lv/25

    var jsonData = 
{
cols: [{id: 'a', label: 'NEW A', type: 'string'},
{id: 'b', label: 'B-label', type: 'number'},
{id: 'c', label: 'C-label', type: 'number'}
],
rows: [{c:[{v: 'a'},
{v: 1.0, f: 'One'},
{v: 100}
]},
{c:[{v: 'b'},
{v: 2.0, f: 'Two'},
{v: 200}
]},
{c:[{v: 'c'},
{v: 3.0, f: 'Three'},
{v: 300}
]}
]
};

var data = new google.visualization.DataTable(jsonData);

关于javascript - 将 JSON 加载到 Google 面积图中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33617147/

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