在amCharts指南中循环

时间:2015-11-04 00:27:43

标签: javascript model-view-controller amcharts

在我的JS中,我试图在amCharts指南中添加一个for循环,以便从视图文件中传递3个参考标签及其值。

我目前正在处理这个问题:

"guides": [{
                "dashLength": 6,
                "inside": true,
                "label": graphConfig.dataProvider[0].label[0],
                "labelRotation":90,
                "lineAlpha": 1,
                "category": graphConfig.dataProvider[0].val[0],
            }, {
                "dashLength": 6,
                "inside": true,
                "label": graphConfig.dataProvider[0].label[1],
                "labelRotation":90,
                "lineAlpha": 1,
                "category": graphConfig.dataProvider[0].val[1],
            }, {
                "dashLength": 6,
                "inside": true,
                "label": graphConfig.dataProvider[0].label[2],
                "labelRotation":90,
                "lineAlpha": 1,
                "category": graphConfig.dataProvider[0].val[2],
            }],

它工作正常,但如果我必须缩放它,我需要一个循环。我正在寻找这样的东西:

"guides": [
    for (var i=0; i<3; i++)
     {
        {
                    "dashLength": 6,
                    "inside": true,
                    "label": graphConfig.dataProvider[0].label[i],
                    "labelRotation":90,
                    "lineAlpha": 1,
                    "category": graphConfig.dataProvider[0].val[i],
                },
      }

但这不起作用。请帮帮我。

2 个答案:

答案 0 :(得分:1)

我想你可以创建一个能为你生成这些指南的函数:

function generateGuides( graphConfig ) {
  var guides = [];
  for ( var i = 0; i < 3; i++ ) {
    guides.push( {
      "dashLength": 6,
      "inside": true,
      "label": graphConfig.dataProvider[ 0 ].label[ i ],
      "labelRotation": 90,
      "lineAlpha": 1,
      "category": graphConfig.dataProvider[ 0 ].val[ i ],
    } );
  }
  return guides;
}

然后在图表配置中使用该函数调用:

"guides": generateGuides( graphConfig ),

答案 1 :(得分:0)

嘿,这个想法奏效了,我做了类似的事情

"guides": (function(){
            var guideArray = []
            for (var i = 0; i < 3; i++) {

                guideArray.push({
                    "dashLength": 6,
                    "inside": true,
                    "label": graphConfig.dataProvider[0].label[i],
                    "labelRotation":90,
                    "lineAlpha": 1,
                    "category": graphConfig.dataProvider[0].val[i], 
                  })
                }
            return guideArray
            }()),