0

I am using JIT Infovis stacked or mono-valued piechart to do some data visualization. The stacked pie chart takes an object "obj" specifically with the following structure:

This is the hard coded version of jsonObj:

  var obj = {
        // labelID is unique in each case and may be multiple or just one
        'labelID': ['pVal'],
        'values': [{ //labelName is unique in each case and will only be one
            'labelName': 'name1',
            //there may be multiple or just one "values"
            //ex, 'values': [80, 40, 15, 10]
            'values': 80
        }, {
            'labelName': 'name2',
            'values': [20]
        }, {
            'labelName': 'name3',
            'values': [38]
        }, {
            'labelName': 'name4',
            'values': [58]
        }]
    };

I'm trying to dynamically populate "obj" with searched data returned to user. But I can not create an empty "obj" with the above specific structure to dynamically populate the data. I tried several times but don't think I'm creating them correctly. I have three values that I'm trying to dynamically populate into this "obj" but can't get my arms around it. chartID[m], chartArrName[m], chartVal[m].

I need a correct empty "obj" that correspond to the structure defined above.

 var "obj" = {
        label: ['pVal'],
        values: [{
            label: [],
            values: []
        }]
    };
    for (m = 0; m <= chartArrID.length - 1; m++) {
        obj.values[m].label += chartArrName[m];
        obj.values[m].values += parseFloat(chartArrVal[m]);
        //json.push({label: chartArrName[m], values: parseFloat(chartArrVal[m])});
    }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
FongYu
  • 767
  • 4
  • 15
  • 24

1 Answers1

1

This is not a JSON object. JSON is a lightweight data interchange format. You are just using a object initializer syntax to create an object. You can add what you want of the fly.

var myObj = {};
myObj.name = "foo";
myObj.books = [ "one", "two" ];
myObj.emptyField = null;
myObj.emptyArray = [];
myObj["anotherField"] = "bar";   // this works too

myObj.anotherArray = [];

myObj.anotherArray[0] = {};  // you need to insert something in the first position!
myObj.anotherArray[0].label = "aaa";
myObj.anotherArray[0].value = "bbb";
myObj.anotherArray[0].xyz = "ccc";

myObj.anotherArray[1] = {};  // in the second too, of course!
myObj.anotherArray[1].label = "ddd";
myObj.anotherArray[1].value = "eee";
myObj.anotherArray[1].xyz = "fff";
davidbuzatto
  • 9,207
  • 1
  • 43
  • 50
  • How come what you suggested doesn't work when you have array in the middle of the structue. How can I create an object and dynamically populate data on the fly then? var json = {}; json.label = 'pval'; json.values[0].label = chartArrName[0]; Uncaught TypeError: Cannot read property '0' of undefined json.values[0].values = parseFloat(chartArrVal[0]); json.values[1].label = chartArrName[1]; json.values[1].values = parseFloat(chartArrVal[1]); json.values[2].label = chartArrName[2]; json.values[2].values = parseFloat(chartArrVal[2]); – FongYu Jul 23 '12 at 18:56
  • Can you update your question? Can you explain what is going wrong? You are trying to insert a field in and element that is undefined... I inserted more examples in my code... Take a look. – davidbuzatto Jul 23 '12 at 18:57
  • updated, but running into more questions which I will address in another thread. Thanks davidbuzatto! – FongYu Jul 23 '12 at 19:17