首先:感谢您的阅读,但其中有很多内容,在底部链接的CodePen示例中可能更容易看到它。

我最近问了一个有关如何为树图动态创建数组的问题,并指出了正确的方向。我已经能够对哈希表进行一些简单的操作,但是无法弄清楚如何完成最后一步。

当前,树形图具有2个“级别”,其操作如下:

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)), []);


现在,我要尝试的是带来一个附加条件并添加另一个级别。例如,在“共和党”和“民主”下,您将有“像”,“倾斜”和“倾斜”的子类别,但对于“折腾”则没有任何子类别。

这是我要做出的最终结果:

  series: [{
      text: "Democratic",
      style: { "backgroundColor": "blue" },
      children: [{
        text: "Likely",
        style: { "backgroundColor": "darkblue" },
        children: [{
          text: "Alabama",
          value: 8,
          style: { "backgroundColor": "darkblue" },
        }, {
          text: "New Mexico",
          value: 14,
          style: { "backgroundColor": "darkblue" },
        }]
      }, {
        text: "Leaning",
        style: { "backgroundColor": "blue" },
        children: [{
          text: "Florida",
          value: 9,
          style: { "backgroundColor": "blue" },
        }, {
          text: "North Carolina",
          value: 6,
          style: { "backgroundColor": "blue" },
        }]
      }, {
        text: "Tipping",
        style: { "backgroundColor": "lightblue" },
        children: [{
          text: "Utah",
          value: 4,
          style: { "backgroundColor": "lightblue" },
        }, {
          text: "Idaho",
          value: 5,
          style: { "backgroundColor": "lightblue" },
        }]
      }]
    },

    { text: "Republican",
      style: { "backgroundColor": "red" },
      children: [{
        text: "Likely",
        style: { "backgroundColor": "darkred" },
        children: [{
          text: "alabama",
          value: 13,
          style: { "backgroundColor": "darkred" },
        }, {
          text: "New Mexico",
          value: 14,
          style: { "backgroundColor": "darkred" },
        }]
      }, {
        text: "Leaning",
        style: { "backgroundColor": "red" },
        children: [{
          text: "Florida",
          value: 9,
          style: { "backgroundColor": "red" },
        }, {
          text: "North Carolina",
          value: 6,
          style: { "backgroundColor": "red" },
        }]
      }, {
        text: "Tipping",
        style: { "backgroundColor": "lightred" },
        children: [{
          text: "Utah",
          value: 27,
          style: { "backgroundColor": "lightred" },
        }, {
          text: "Idaho",
          value: 12,
          style: { "backgroundColor": "lightred" },
        }]
      }]
    }, {
      text: "Toss Up",
      style: { "backgroundColor": "grey" },
      children: [{
          text: "alabama",
          value: 10,
          style: { "backgroundColor": "grey" },
        }, {
          text: "New Mexico",
          value: 14,
          style: { "backgroundColor": "grey" },
      }]
    },
  ]
};


我一直在尝试找出如何修改给出的示例,但是已经失败了几个小时。我不确定应该查询什么。我认为应该在“ prev.push”(第2040 here行)中的其他语句中进行调整,但是我不知道如何实现该调整,我还怀疑我需要更改整个方法。

var result = data.reduce(function(hash) {
  return function(prev, curr) {
    if (hash[curr.party]) {
      console.log("if: " + curr.party + " " + curr.category);
      hash[curr.party].children.push({
        text: curr.text,
        value: curr.value,
        style: {
          backgroundColor: curr.color
        }
      });
    }

    else {
      console.log("else: " + curr.party + " " + curr.category);
      hash[curr.party] = {};
      hash[curr.party].children = hash[curr.party].children || [];

      hash[curr.party].children.push({
        text: curr.text,
        value: curr.value,
        style: {
          backgroundColor: curr.color
        }
      });

      prev.push({
        text: curr.party,
        style: {
          backgroundColor: curr.color
        },
        children: hash[curr.party].children // children: [{ text: curr.category, children: [{ }] }]
      });

      hash[curr.category] = {};
      hash[curr.category].children = hash[curr.category].children || [];
      console.log("category");
      prev.push({
        text: curr.category,
        style: {
          backgroundColor: curr.color
        },
        children: hash[curr.category].children
      });
    }
    return prev;
  };
}(Object.create(null)), []);


总结一下:


这是一个CodePen of the basic structure of the goal
这是我的current code

最佳答案

之前的分类是基于color进行的,看到您的数据我想我们现在可以基于party进行分类。由于在树中添加了另一个级别,因此我们可以基于category对其进行分类。

因此,有很多更改-请参见下面的演示:



var data = [{"text":"California","value":55,"color":"#000066","party":"Democratic","compare":1,"category":"Likely"},{"text":"Connecticut","value":7,"color":"#000066","party":"Democratic","compare":1,"category":"Likely"},{"text":"Colorado","value":9,"color":"#3333CC","party":"Democratic","compare":2,"category":"Leaning"},{"text":"Florida","value":29,"color":"#9999FF","party":"Democratic","compare":3,"category":"Tipping"},{"text":"Iowa","value":6,"color":"red","party":"Republican","compare":4,"category":"Likely"},{"text":"Alabama","value":9,"color":"#CC3333","party":"Republican","compare":5,"category":"Leaning"},{"text":"Alaska","value":3,"color":"#FF9999","party":"Republican","compare":6,"category":"Tipping"},{"text":"Arizona","value":11,"color":"#666666","party":"Toss-Up","compare":7,"category":"Toss Up"},{"text":"Texas","value":11,"color":"#666666","party":"Toss-Up","compare":7,"category":"Toss Up"}];

var result = data.reduce(function(hash) {
  return function(prev, curr) {
    if (!hash[curr.party]) {
      hash[curr.party] = {};
      hash[curr.party].children = hash[curr.party].children || [];
      prev.push({
        text: curr.party,
        style: {
          backgroundColor: curr.color
        },
        children: hash[curr.party].children
      });
    } else if (hash[curr.party] && hash[curr.party][curr.category]) {
      hash[curr.party][curr.category].children.push({
        text: curr.text,
        value: curr.value,
        style: {
          backgroundColor: curr.color
        }
      });
    }

    if (hash[curr.party] && !hash[curr.party][curr.category]) {
      if (curr.category == "Toss Up") {
        hash[curr.party].children.push({
          text: curr.text,
          value: curr.value,
          style: {
            backgroundColor: curr.color
          }
        });
      } else {
        hash[curr.party][curr.category] = {};
        hash[curr.party][curr.category].children = hash[curr.party][curr.category].children || [];
        hash[curr.party].children.push({
          text: curr.category,
          style: {
            backgroundColor: curr.color
          },
          children: hash[curr.party][curr.category].children
        });
        hash[curr.party][curr.category].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;}

关于javascript - 微调哈希值以动态创建ZingCharts树图数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40368831/

10-09 03:59