在for循环中获取变量的先前值并将其用于当前选择

时间:2016-03-01 14:52:58

标签: javascript for-loop math

我正在尝试在一组SVG上设置转换属性,以便它们全部堆叠在一起。根据chartType,SVG可以是20px或100px高。基本上,为for循环中前一项设置的transform属性(对于Y)应该能够用作for循环中下一项的基线。

到目前为止我所拥有的:

childrenCharts(10)
[{childName:"VitalsChart"},
{childName:"VitalsChart"},
{childName:"VitalsChart"},
{childName:"StepChart"},
{childName:"StepChart"},
{childName:"StepChart"},
{childName:"StepChart"},
{childName:"StepChart"},
{childName:"StepChart"},
{childName:"StepChart"},]

for (var i=0; i<childrenCharts.length; ++i) {
      currentName = childrenCharts[i].chartName;
      var sibHeight;
      console.log(sibHeight);
      var subtractor = i-1
      if (currentName === "StepChart") {
        var baseline = (childrenCharts.length-subtractor)*childHeight;
            sibHeight = baseline - 40;
      }
      else {
        childrenCharts[i].selection
             .attr("transform", "translate(" + 0 + "," + (childrenCharts.length-i)*childHeight + ")"); 
        sibHeight = (childrenCharts.length-subtractor)*childHeight;  

      }         
    }

StepChart是20px和childHeight = 100px

的图表

目前,sibHeight登录控制台:

1000 900 800 660 560 460 360 260

我需要实际的sibHeights:

1000 900 800 660 620 580 540 500

0 个答案:

没有答案