gpt4 book ai didi

javascript - C3 条形图,条数未知

转载 作者:行者123 更新时间:2023-12-03 11:29:49 24 4
gpt4 key购买 nike

我正在使用 C3.js 创建条形图。

我的情况是这样的:

我想收集 X 数量的数据并将它们显示在我的图表中,为此我创建了以下脚本:

    $.ajax({
type: 'POST',
url: '/AcademyStat/academy_module_user_report',
dataType: 'json',
data: {
request: 'ajax',
team_id: team_id,
module_id: module_id
},
success: function (data)
{
if(data != null)
{
for (var i = 0; i < data.length; i++)
{
var add_data = [data[i]['name'], data[i]['score']]
char_data.push(add_data);
}
var chart = c3.generate({
bindto: '#score_chart',
data: {
columns: [
char_data

],
type: 'bar'
}
});
}
}
});

正如您从上面的示例中看到的,我循环遍历数据,然后将其添加到数组中,直到我有一个数组的数组(未知数量的数据)。

但是,当我在浏览器中运行此命令时,我在控制台中收到以下错误:

    Error: Invalid value for <path> attribute d="M 286.6,NaN L286.6,NaN L531.4,NaN L531.4,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 1102.6,NaN L1102.6,NaN L1347.3999999999999,NaN L1347.3999999999999,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 286.6,NaN L286.6,NaN L531.4,NaN L531.4,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 1102.6,NaN L1102.6,NaN L1347.3999999999999,NaN L1347.3999999999999,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 286.6,NaN L286.6,NaN L531.4,NaN L531.4,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 1102.6,NaN L1102.6,NaN L1347.3999999999999,NaN L1347.3999999999999,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 286.6,NaN L286.6,NaN L531.4,NaN L531.4,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 1102.6,NaN L1102.6,NaN L1347.3999999999999,NaN L1347.3999999999999,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 286.6,NaN L286.6,NaN L531.4,NaN L531.4,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 1102.6,NaN L1102.6,NaN L1347.3999999999999,NaN L1347.3999999999999,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 286.6,NaN L286.6,NaN L531.4,NaN L531.4,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 1102.6,NaN L1102.6,NaN L1347.3999999999999,NaN L1347.3999999999999,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 286.6,NaN L286.6,NaN L531.4,NaN L531.4,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 1102.6,NaN L1102.6,NaN L1347.3999999999999,NaN L1347.3999999999999,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 286.6,NaN L286.6,NaN L531.4,NaN L531.4,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 1102.6,NaN L1102.6,NaN L1347.3999999999999,NaN L1347.3999999999999,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 286.6,NaN L286.6,NaN L531.4,NaN L531.4,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 1102.6,NaN L1102.6,NaN L1347.3999999999999,NaN L1347.3999999999999,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 286.6,NaN L286.6,NaN L531.4,NaN L531.4,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 1102.6,NaN L1102.6,NaN L1347.3999999999999,NaN L1347.3999999999999,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 286.6,NaN L286.6,NaN L531.4,NaN L531.4,NaN z" d3.min.js:5
Error: Invalid value for <path> attribute d="M 1102.6,NaN L1102.6,NaN L1347.3999999999999,NaN L1347.3999999999999,NaN z

但是,如果我改为使用方法 pop 数据显示正常。 pop(); 的唯一问题是我必须知道我的 char_data 数组有多少个数组,但我不可能知道。

有人尝试过类似的方法或者知道我如何解决这个问题吗?

最佳答案

看起来您正在将图表columns设置为数组内的数组columns: [ [ ['name', 'score'] ] ]

列:[ char_data ]更改为列:char_data

$.ajax({
type: 'POST',
url: '/AcademyStat/academy_module_user_report',
dataType: 'json',
data: {
request: 'ajax',
team_id: team_id,
module_id: module_id
},
success: function (data) {
if (data != null) {
for (var i = 0; i < data.length; i++) {
var add_data = [data[i]['name'], data[i]['score']]
char_data.push(add_data);
}
var chart = c3.generate({
bindto: '#score_chart',
data: {
columns: char_data,
type: 'bar'
}
});
}
}
});

关于javascript - C3 条形图,条数未知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26782482/

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