我有这种格式的data.json:
{
"Users": [
{
"userName": "Herbie",
"weigh-in data": [
{ "date": "2016.01.04", "weight": "114.3" },
{ "date": "2016.01.05", "weight": "114.6" },
{ "date": "2016.01.06", "weight": "114.9" }
]
},
{
"userName": "Wayne",
"weigh-in data": [
{ "date": "2016.02.01", "weight": "120.3" },
{ "date": "2016.02.05", "weight": "123.6" },
{ "date": "2016.02.06", "weight": "123.9" }
]
}
]
}
//等,更多用户对象
在我的应用程序中:选择了一个用户,ajax调用获取了这些数据,我通过它循环只获取所选用户的称重数据,并且我成功地渲染了这个数据数据到表格。
现在我试图在Dimple图表中使用相同的结果但是Dimple显然不喜欢我的chartData数组:
var dataObj = JSON.parse(jsonData);
var usersArray = dataObj.Users;
var chartData = [];
// etc. SNIP
for (var obj of usersArray) {
if (obj.userName === selUser) { // weigh-ins of the selected user only
dataRows.innerHTML = "";
for (var i = 0, j = selUserData.length; i < j; i++) {
var dataDate = selUserData[i].date;
var dataWeight = selUserData[i].weight;
chartData.push('{ "User"' + ': ' + '"'+selUser+'", ' + '"Date":' + ' ' + '"'+dataDate+'", ' + '"Weight":' + ' ' + '"'+dataWeight+'" }');
// SNIP: build rows from the data, load up the table, no problem
dataRows.innerHTML += row;
} // selUserData loop
var svg = dimple.newSvg("#chartContainer", 800, 600);
var chart = new dimple.chart(svg, chartData);
chart.setBounds(60, 30, 505, 305);
var x = chart.addCategoryAxis("x", "Date");
x.addOrderRule("Dates");
var y = chart.addCategoryAxis("y", "Weight");
y.addOrderRule("Weights");
var s = chart.addSeries("weigh-ins", dimple.plot.line);
chart.draw();
...导致非图表。但是,如果我是console.log或alert(chartData)并将结果设置为chartData,即:
var chartData = [
{ "User": "Wayne", "Date": "2016.02.01", "Weight": "180.3" },
{ "User": "Wayne", "Date": "2016.02.05", "Weight": "123.6" },
{ "User": "Wayne", "Date": "2016.02.06", "Weight": "153.9" }
]
...然后我得到一张图表,这让我很困惑。
非常感谢任何见解,
威士忌
答案 0 :(得分:1)
您将JSON作为字符串而不是对象推送到数组中,它应该是:
<?php
$used_boxes = array();
$valid_data = array();
foreach($data as $row){
if(!in_array($used_boxes)){
//Box not used
$valid_data[$row['customer']] = $row['box'];
$used_boxes[] = $row['box']
}else{
//Box already used
}
}
var_dump($valid_data);
其中还有一个更容易阅读的好处!