KendoUI chart doesn't accept numbers in keys for JSON data

If I provide this simple data to a KendoUI chart, the chart data shows up correctly.

var data = [
                {"state":"NY","abc":12312},
                {"state":"AZ","abc":12312},
                {"state":"CA","abc":12312},
                {"state":"VT","abc":12312}
            ];

Please note that the above JSON has STRINGS for the keys.

However, if I provide a number as a key, I get an "Unexpected number at line number 2156" error in kendo.all.js.

var data = [
                {"state":"NY","1":12312},
                {"state":"AZ","1":12312},
                {"state":"CA","1":12312},
                {"state":"VT","1":12312}
            ];

Any help?

Upvotes: 2

Views: 211

Answers (2)

user3733845
user3733845

Reputation: 1

There is a way out for this problem but it is not a genuine fix. What you can do is if you are making the chart data by array. For example in my case:

var arrData = new Array();
for (i = 0; i < data.d.length - 1; i++) {
    var tnt = new customData();
    tnt.displayField = data.d[i + 1][1].descriptionField.trim();

    for (var j = 1; j < data.d[0].length; j++) {
        var prop = 'a' +j.toString()
        var value = '';
        switch (displayName) {
            case 'DI_RESP':
                value = parseInt(data.d[i + 1][j].sampleField);
                tnt[prop] = value
                break;
            default:
                value = data.d[i + 1][j].sampleField;
                tnt[prop] = value;
                break;
        }
    }
    arrData.push(tnt);
}

As you can see customData model contains two properties descriptionField and sampleField. descriptionField is the category name sampleField is a float value. I appended a letter 'a' just to make it a string. Error will go away.

Upvotes: 0

Derick Bailey
Derick Bailey

Reputation: 72868

This is a known issue / by design in Kendo's DataViz charts: http://www.kendoui.com/forums/ui/general-discussions/kendo-datasource-field-name.aspx

The gist of it is that the keys from the JSON doc are used as variable names within the chart's code, which means the keys must be valid JavaScript variable names.

Upvotes: 3

Related Questions