简体   繁体   English

highchart最大限度地减少代码冗余

[英]highchart minimize code redundancy

I am using HighCharts to draw graph and there are some code redundancys that i want to minimize 我正在使用HighCharts绘制图形,并且我想尽量减少一些代码冗余

the code looks like this : 代码看起来像这样:

data = [{
        y: pageViews[0],
        color: colors[0],
        drilldown: {
            name: date[0],
            categories: ['a','b','c'],
            data: [photosR[0], ratingsR[0], searchesR[0], socialR[0]],
            color: colors[0]
        }
    }, {
        y: pageViews[1],
        color: colors[1],
        drilldown: {
            name: date[1],
            categories: ['a','b','c'],
            data: [photosR[1], ratingsR[1], searchesR[1], socialR[1]],
            color: colors[1]
        }
    }]

this is a drill down bar chart in highchart. 这是海图中的明细条形图。 I want to create a loop instead of writing the below code 2 times 我想创建一个循环,而不是编写下面的代码两次

y: pageViews[0],
color: colors[0],
drilldown: {...}

I feel that it has to do with object, but haven't learn a lot about it yet 我觉得它与对象有关,但还没有学到很多

I assume, that all your arrays have the same length, so it is safe to loop any of them. 我假设所有数组的长度都相同,因此可以安全地循环其中的任何一个。
This should work for you: 这应该为您工作:

var data = [];
for (var i = 0; i < pageViews.length; i++) {
   data.push({
      y: pageViews[i],
      color: colors[i],
      drilldown: {
         name: date[i],
         categories: ['a','b','c'],
         data: [photosR[i], ratingsR[i], searchesR[i], socialR[i]],
         color: colors[i]
      }
   });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM