gpt4 book ai didi

javascript - Dimple.js 不喜欢我的数组

转载 作者:行者123 更新时间:2023-12-03 05:37:38 24 4
gpt4 key购买 nike

我有以下格式的 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 显然不喜欢我的图表数据数组:

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" }
]

...然后我得到一张图表,消除了我的困惑。

非常感谢任何见解,

威士忌

最佳答案

您将 JSON 作为字符串而不是对象推送到数组中,它应该是:

chartData.push({ "User": selUser, "Date": dataDate, "Weight": dataWeight });

这还有一个额外的好处,那就是更容易阅读!

关于javascript - Dimple.js 不喜欢我的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40665767/

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