【问题标题】:How to dynamically create array to update ZingChart treemap如何动态创建数组以更新 ZingChart 树图
【发布时间】:2016-10-28 21:52:19
【问题描述】:

我正在尝试为 ZingCharts 动态创建此数据数组,但我很困惑。

基本上,我需要做这个结构:

var series = [{
  text: "Democratic", 
  style: {
      backgroundColor: "blue"
    },

  children: [{ 
    text: "California",
    value: 55,
    style: {
      backgroundColor: "blue"
    },

  }, {
    text: "Oregon",
    value: 7,
    style: {
      backgroundColor: "blue"
    },
  }]
}, 
{
  text: "Republican", 
  style: {
      backgroundColor: "red"
    },
  children: [{ 
    text: "Texas",
    value: 38,
    style: {
      backgroundColor: "red"
    },
  }, {
    text: "Georgia",
    value: 16,
    style: {
      backgroundColor: "red"
    },
  }]
}, {
  text: "TossUp", 
  style: {
      backgroundColor: "grey"
    },
  children: [{ 
    text: "Arizona",
    value: 22,
    style: {
      backgroundColor: "grey"
    },
  }]
}, ]
}]
};

我不知道这样做的正确方法是什么。我很想做这样的事情:

result.push("text: '" + data[0].party + "'")

我需要对所有 50 个状态执行此操作,并且数据的结构如下:

var data = [{
  "color": "blue",
  "party": "Democratic",
  "text": "California",
  "value": 55,
}, {
  "color": "blue",
  "party": "Democratic",
  "text": "Oregon",
  "value": 7,
}, {
  "color": "red",
  "party": "Republican",
  "text": "Texas",
  "value": 38,
}, {
  "color": "red",
  "party": "Republican",
  "text": "Georgia",
  "value": 16,
}, {
  "color": "grey",
  "party": "Democratic",
  "text": "Arizona",
  "value": 11,
}];

任何关于我应该搜索什么的提示将不胜感激。谢谢!

【问题讨论】:

    标签: javascript arrays charts zingchart


    【解决方案1】:

    使用reduce 函数和hash table 创建您想要的数据结构:

    var data=[{color:"blue",party:"Democratic",text:"California",value:55},{color:"blue",party:"Democratic",text:"Oregon",value:7},{color:"red",party:"Republican",text:"Texas",value:38},{color:"red",party:"Republican",text:"Georgia",value:16},{color:"grey",party:"Democratic",text:"Arizona",value:11}];
    
    var result = data.reduce(function(hash) {
      return function(prev, curr) {
        if (hash[curr.color]) {
          hash[curr.color].children.push({
            text: curr.text,
            value: curr.value,
            style: {
              backgroundColor: curr.color
            }
          });
        } else {
          hash[curr.color] = {};
          hash[curr.color].children = hash[curr.color].children || [];
          prev.push({
            text: curr.party,
            style: {
              backgroundColor: curr.color
            },
            children: hash[curr.color].children
          });
          hash[curr.color].children.push({
            text: curr.text,
            value: curr.value,
            style: {
              backgroundColor: curr.color
            }
          });
        }
        return prev;
      };
    }(Object.create(null)), []);
    
    console.log(result);
    .as-console-wrapper{top: 0;max-height: 100%!important;}

    请告诉我您对此的看法,谢谢!

    【讨论】:

    • 非常干净!感谢您的回答
    • @kukkuz - 这是一个很大的帮助。如果您愿意看一下,我实际上是想弄清楚与此相关的事情。谢谢! stackoverflow.com/questions/40368831/…
    猜你喜欢
    • 2019-05-10
    • 2012-02-24
    • 2017-04-28
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    相关资源
    最近更新 更多