关于javascript中的标题字段

时间:2015-07-04 05:44:11

标签: javascript json

实际上我在那里写的js期待这种格式的json

[{
  "type": "My bharath",
  "Q1": 1230,
  "Q2": 571,
  "Q3": 166,
  "Q4": 2
}]

                                                                                                                          但是我们以这种格式发送json

[{
  "data": {
    "type": "My Bharath",
    "Q4' 2014": 4,
    "Q1' 2015": 4,
    "Q2' 2015": 508,
    "Q3' 2015": 0
  },
  "legends": {
    "Q1": "Q4' 2014",
    "Q2": "Q1' 2015",
    "Q3": "Q2' 2015",
    "Q4": "Q3' 2015"
  }
}]
"graphs" : [{
        "balloonText" : "[[category]] <br>[[title]]:[[value]]",
        "fillAlphas" : 0.8,
        "id" : "4",
        "lineAlpha" : 0.2,
        "title" : "9-12 Months",
        "type" : "column",
        "valueField" : "Q4"
    },{
        "balloonText" : "[[category]] <br>[[title]]:[[value]]",
        "fillAlphas" : 0.8,
        "id" : "3",
        "lineAlpha" : 0.2,
        "title" : "6-9 Months",
        "type" : "column",
        "valueField" : "Q3"
    }, {
        "balloonText" : "[[category]] <br>[[title]]:[[value]]",
        "fillAlphas" : 0.8,
        "id" : "2",
        "lineAlpha" : 0.2,
        "title" : "3-6 Months",
        "type" : "column",
        "valueField" : "Q2"
    }, {
        "balloonText" : "[[category]] <br>[[title]]:[[value]]",
        "fillAlphas" : 0.8,
        "id" : "1",
        "lineAlpha" : 0.2,
        "title" : "0-3 Months",
        "type" : "column",
        "valueField" : "Q1"
    } ],

第一个是我得到的json格式。 第二个是我正在试图根据获得的数据显示图表。

我的问题是每当“valueField”:“Q4”然后它必须将标题赋予“Q3'2015”。 所以基本上它必须检查值字段并通过将其与json(Legends)匹配来给我标题字段。

如果有任何查询评论。

1 个答案:

答案 0 :(得分:0)

var dataLegends = [
{
    "data": {
        "type": "My Bharath",
        "Q4' 2014": 0,
        "Q1' 2015": 0,
        "Q2' 2015": 283,
        "Q3' 2015": 0
    },
    "legends": {
        "Q1": "Q4' 2014",
        "Q2": "Q1' 2015",
        "Q3": "Q2' 2015",
        "Q4": "Q3' 2015"
    }
}
];

var graphs = {"graphs" : [{
        "balloonText" : "[[category]] <br>[[title]]:[[value]]",
        "fillAlphas" : 0.8,
        "id" : "4",
        "lineAlpha" : 0.2,
        "title" : "9-12 Months",
        "type" : "column",
        "valueField" : "Q4"
    },{
        "balloonText" : "[[category]] <br>[[title]]:[[value]]",
        "fillAlphas" : 0.8,
        "id" : "3",
        "lineAlpha" : 0.2,
        "title" : "6-9 Months",
        "type" : "column",
        "valueField" : "Q3"
    }, {
        "balloonText" : "[[category]] <br>[[title]]:[[value]]",
        "fillAlphas" : 0.8,
        "id" : "2",
        "lineAlpha" : 0.2,
        "title" : "3-6 Months",
        "type" : "column",
        "valueField" : "Q2"
    }, {
        "balloonText" : "[[category]] <br>[[title]]:[[value]]",
        "fillAlphas" : 0.8,
        "id" : "1",
        "lineAlpha" : 0.2,
        "title" : "0-3 Months",
        "type" : "column",
        "valueField" : "Q1"
    } ]
};

window.log = function(msg) { document.getElementById('log').innerHTML += msg; };

// overwrite title field with lookup by valueField in legend hash
graphs.graphs.map(function(g) { g.title = dataLegends[0].legends[g.valueField]; });

// prove that we changed the title field
log(graphs.graphs.map(function(g) { return g.title; }));
<div id="log"></div>