- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想创建一个动态 Google 折线图,其中填充了 JSONArray 中的数据。我有以下代码:
<div id="curve_chart" style="margin-bottom: -12px; margin-left: -28px;"></div>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', { 'packages': ['corechart'] });
google.charts.setOnLoadCallback(drawChart0);
function drawChart0() {
var data = new google.visualization.DataTable(JSONArray);
// assumes "day" is a date and "totalfeedings" is a number
data.addColumn('string', 'Day');
data.addColumn('number', 'TotalFeedings');
for (var i = 0; i < JSONArray.length; i++) {
data.addRow([JSONArray[i].Day, JSONArray[i].TotalFeedings]);
}
var options0 = {
curveType: 'function',
legend: 'none',
lineWidth: 6,
width: 440,
height: 180,
backgroundColor: 'transparent',
axisTitlesPosition: 'in',
color: '#286090',
trendlines: {
type: 'linear',
color: 'green',
lineWidth: 3,
opacity: 0.3,
showR2: true,
visibleInLegend: true
},
vAxis: {
gridlines: {
count: 8
}
}
};
var chart0 = new google.visualization.LineChart(document.getElementById('curve_chart'));
chart0.draw(data, options0);
}
</script>
JSONArray 包含以下数据:
{
"data": [
{ "Day": "2019-09-13T00:00:00", "TotalFeedings": 3 },
{ "Day": "2019-09-14T00:00:00", "TotalFeedings": 10 },
{ "Day": "2019-09-15T00:00:00", "TotalFeedings": 14 },
{ "Day": "2019-09-16T00:00:00", "TotalFeedings": 19 },
{ "Day": "2019-09-17T00:00:00", "TotalFeedings": 14 },
{ "Day": "2019-09-18T00:00:00", "TotalFeedings": 24 },
{ "Day": "2019-09-19T00:00:00", "TotalFeedings": 15 },
{ "Day": "2019-09-20T00:00:00", "TotalFeedings": 18 },
{ "Day": "2019-09-21T00:00:00", "TotalFeedings": 14 },
{ "Day": "2019-09-22T00:00:00", "TotalFeedings": 10 },
{ "Day": "2019-09-23T00:00:00", "TotalFeedings": 13 },
{ "Day": "2019-09-24T00:00:00", "TotalFeedings": 9 },
{ "Day": "2019-09-25T00:00:00", "TotalFeedings": 11 },
{ "Day": "2019-09-26T00:00:00", "TotalFeedings": 8 },
{ "Day": "2019-09-27T00:00:00", "TotalFeedings": 7 },
{ "Day": "2019-09-28T00:00:00", "TotalFeedings": 2 }
]
}
图表没有出现。
我想知道我在这里做错了什么。
最佳答案
创建一个像我下面所做的那样的数组
您需要执行 JSONArray.data[i].Day
和 JSONArray.data[i].TotalFeedings
才能访问 JSON 数据。
JSONArray = {
"data": [
{
"Day": "2019-09-13T00:00:00",
"TotalFeedings": 3
},
{
"Day": "2019-09-14T00:00:00",
"TotalFeedings": 10
},
{
"Day": "2019-09-15T00:00:00",
"TotalFeedings": 14
}
//rest of your data
]
}
// change from this
data.addRow([JSONArray[i].Day, JSONArray[i].TotalFeedings]);
// to this
data.addRow([JSONArray.data[i].Day, JSONArray.data[i].TotalFeedings]);
关于javascript - 将 JSONArray 解析为 Google 折线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58154032/
我正在使用网络服务请求直方图数据。数据是数组中的一组数组: [[1375056000000,23.284713745117],[1375142400000,3.809531211853], [1375
这是我的 json 文件的相关部分: "categories": [ [ "Belgian Restaurant", "belgian" ],
我目前很困惑,为什么我不能在我的 Android 应用程序中从 JSONArray 中提取 JSONArray。下面给出了一个示例和我的源代码片段。 //The JSON { "cur
我正在尝试解析 JSONObject。 这个 JSONObject 中有一个 JSONArray,它在 JSONArray 中还有另一个 JSONArray。 我试图解析的 json 形式如下。 {
我正在尝试解析对象数据列表中的 JSON 数组 payment_details。 在这里,我已经成功解析了回收站 View 中调用的数据数组,但我无法调用 payment_details 数组。 适配
我正在寻找一个选项,可以将多个值添加到 JSONArray 并将其添加到另一个 JSONArray 中,而无需创建多个变量。例如: JSONArray array1 = new JSONArr
我遇到过一种情况,org.json.JSONArray 对象非常大,这最终会导致延迟和其他问题。因此,我们决定将 JSONArray 分割成更小的 block 。例如,如果 JSONArray 是这样
您好,我正在尝试在另一个 json 数组中解析一个 json 数组。帮我解决这个问题。 我的 Json 数组是 "results":[ { "percentcompleted"
我正在使用 Gson用于解析 json 响应。我需要在 JsonArray 中解析 JSONArry.. 我的回答是.. { "message": "Retreive sucessfully", "f
@GET @Produces("application/json") public Response GetAll() throws JSONException{ tblCategoryDao
我从服务器获取以下 json: { "Data": [ { "Record": [ " d11", "d12"
我正在使用 ajax 将 json 对象从 javascript 传递到 java servlet。 var jsonObj = JSON.stringify(objArray); //Then I
我需要获取作为“结果”内部数组的“示例”数据 JSON FILE 并将其附加到字符串。最简单的方法是什么? 这就是我得到“定义”的方式 private JSONObject queryResults;
我需要从 JSONArray 获取 JSONArray: JSONParser parser = new JSONParser(); JSONObject jObject=(J
我有一个 jsonArray 响应。我需要阅读此响应,其中包含 3 个 jsonArray。 这是 json 响应。这是一个 GET 请求并通过 Volley 请求发送。 [ "0x9000",
在此 Json 响应中,如何使用 Java 访问“smallImageUrls”并获取图像 url? 似乎“smallImageUrls”是“匹配”jsonarray 中的一个数组。如果我错了,请有人
这个问题在这里已经有了答案: Convert string to JSON array (10 个答案) 关闭 5 年前。 如何正确地将此字符串转换为 jsonArray? { "myArr
是否可以从较新的 EJB JSON 库转换为较旧的 org.json 库,而无需依赖 org.json.simple 或 GSON? 在下面的例子中,“buttons”是一个填充的 JsonArr
如何从jsonarray获取jsonarray? 我试过的代码。 for(loop) List stringList = new ArrayLis
我目前是 Json 新手,遇到了一个问题。我搜索了很多但找不到答案! 我从 json url 获取名称列表。名称可以在此 json 文件中重复,但我只想将它们的一条记录保留到我称为“arr”的新数组中
我是一名优秀的程序员,十分优秀!