1

我正在尝试使用 Chart.js。如果我通过图表创建者示例代码:

var data = {
    labels : ["January","February","March","April","May","June","July"],
    datasets : [
        {
            fillColor : "rgba(220,220,220,0.5)",
            strokeColor : "rgba(220,220,220,1)",
            pointColor : "rgba(220,220,220,1)",
            pointStrokeColor : "#fff",
            data : [65,59,90,81,56,55,40]
        },
        {
            fillColor : "rgba(151,187,205,0.5)",
            strokeColor : "rgba(151,187,205,1)",
            pointColor : "rgba(151,187,205,1)",
            pointStrokeColor : "#fff",
            data : [28,48,40,19,96,27,100]
        }
    ]
}

它工作正常。但是,当我尝试传递这个对象时:

function ChartDataObject(labels, datasets)
{
    this.labels = Array();
    this.datasets = Array();
    this.datasets = datasets;
    this.labels = labels;
}

其中labelsdatasets分别是数组和数组的数组。两者labelsdatasets显示:

[object Element], [object Element], etc

当我如下提醒他们时:

alert(labels);
alert(datasets);

我知道它们填充了字符串值,并且其中的元素数量是正确的。为了让 Chart.js 以与查看对象相同的方式查看它们,我是否缺少某些东西data?我应该注意,Chart.js 确实会生成,但是所有标签都已读取[object Element]并且数据未显示(因为它认为它[object Element]不是我认为的数字)。

如何将编码数组转换为与文字创建的数组相同的工作方式?

编辑:

dataset目的:

function Dataset(webrequest, fillColor, strokeColor)
{
    this.webrequest = webrequest;
    this.fillColor = fillColor;
    this.strokeColor = strokeColor;
    this.data = Array();
}

dataset.data filling:

var valuesArray = xmlDoc.getElementsByTagName("value");
for (var i = 0; i < valuesArray.length; i++)
{
    that.datasets[a].data[i] = valuesArray[i];
}
4

1 回答 1

0

It seems likely that whatever you are passing to your constructor for labels and data is not in the correct form. For us to help identify exactly what your issue is, you will have to show us the code you are using to create those data structures.

If you do it like this, it will reproduce what was in the sample code:

// array of names
var names = ["January","February","March","April","May","June","July"];

// array of objects
var items = [
        {
            fillColor : "rgba(220,220,220,0.5)",
            strokeColor : "rgba(220,220,220,1)",
            pointColor : "rgba(220,220,220,1)",
            pointStrokeColor : "#fff",
            data : [65,59,90,81,56,55,40]
        },
        {
            fillColor : "rgba(151,187,205,0.5)",
            strokeColor : "rgba(151,187,205,1)",
            pointColor : "rgba(151,187,205,1)",
            pointStrokeColor : "#fff",
            data : [28,48,40,19,96,27,100]
        }
    ];


function ChartDataObject(labels, datasets)
{
    this.datasets = datasets;
    this.labels = labels;
}

var obj = new ChartDataObject(names, items);
// now obj should be in the same form as the data variable in the sample code
于 2013-08-21T12:53:39.977 回答