0
votes

In my JS, I am trying to add a for loop in amCharts guides to have 3 guides labels and their values passed from a views file.

I am currently working with this:

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

It is working fine, but if I have to scale it I need a loop. I am looking for something like this:

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

But this is not working. Help me out please.

2

2 Answers

1
votes

I suppose you can create a function that would generate those guides for you:

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

Then just use that function call in your chart config:

"guides": generateGuides( graphConfig ),
0
votes

hey that idea worked and i did something like this

"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
            }()),