Pergunta

I am trying to use Chart.js. If I pass the chart creator the example code:

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

It works fine. However, when I try and pass it this object:

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

Where labels and datasets are arrays and arrays of arrays, respectively. Both labels and datasets both show:

[object Element], [object Element], etc

When I alert them as follows:

alert(labels);
alert(datasets);

I know that they are filled with string values, and the number of elements in them is correct. Is there something that I am missing in order for Chart.js to see them the same way it sees the data object? I should note, that the Chart.js does generate, however the labels all read [object Element] and the data does not show (Because it thinks that it is [object Element] instead of a number I think).

How do I convert the coded arrays to work the same as the literal created arrays?

EDIT:

dataset Object:

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];
}
Foi útil?

Solução

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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top