1

我想复制 Mike Bostock 的可折叠树图形 ( http://bl.ocks.org/mbostock/4339083 )。

我已经nest使用d3.js. 现在,数据如下所示:

 [
  {
    "key": "Bayer HealthCare",
    "values": [
      {
        "key": "Animal Health",
        "values": [
          {
            "key": "Companion Animal",
            "values": [
              {
                "brand": "Drontal",
                "product": "Companion Animal",
                "subgroup": "Bayer HealthCare",
                "division": "Animal Health"
              },
              {
                "brand": "Profender",
                "product": "Companion Animal",
                "subgroup": "Bayer HealthCare",
                "division": "Animal Health"
              },
              {
                "brand": "Rompun",
                "product": "Companion Animal",
                "subgroup": "Bayer HealthCare",
                "division": "Animal Health"
              }
            ]
          },
          {
            "key": "Companion Animal - Food Animal Product",
            "values": [
              {
                "brand": "Baytril",
                "product": "Companion Animal - Food Animal Product",
                "subgroup": "Bayer HealthCare",
                "division": "Animal Health"
              }
            ]
          },
          {
            "key": "Companion animals",
            "values": [
              {
                "brand": "Advantage",
                "product": "Companion animals",
                "subgroup": "Bayer HealthCare",
                "division": "Animal Health"
              },
              {
                "brand": "Advantix",
                "product": "Companion animals",
                "subgroup": "Bayer HealthCare",
                "division": "Animal Health"
              },
              {
                "brand": "Advocate",
                "product": "Companion animals",
                "subgroup": "Bayer HealthCare",
                "division": "Animal Health"
              }
            ]
          },
          {
            "key": "Food Animal Product",
            "values": [
              {
                "brand": "Baycox",
                "product": "Food Animal Product",
                "subgroup": "Bayer HealthCare",
                "division": "Animal Health"
              },
              {
                "brand": "Catosal",
                "product": "Food Animal Product",
                "subgroup": "Bayer HealthCare",
                "division": "Animal Health"
              }
            ]
          }
        ]
      }
.....

(完整的数据和我到目前为止所做的可以在这里找到:http: //jsfiddle.net/L3phF/9/

不幸的是,我不知道如何将数据转换为像https://gist.github.com/mbostock/1093025这样的“FLARE JSON”格式。如果是对的,我必须将“key”重命名为“name”,将“values”重命名为“children”,并且我必须在最低级别的每个对象前面插入“”name:“”。

我很感激任何帮助!

4

1 回答 1

3

这会将“key”重命名为“name”,将“values”重命名为“children”:

var renameKeys=function(obj){
    /*Recursively renames "key" keys to "name" and
    renames "values" keys to "children" */
    obj.name=obj.key;
    delete obj['key'];
    if(obj.values){
        obj.children=obj.values;
        delete obj.values;
        obj.children.forEach(renameKeys);
    }
    return obj;
};

您还需要为 JSON 提供一个“根”节点,以便使用您链接到的可折叠树可视化。这应该这样做:

var nest = d3.nest()
    .key(function(d) { return d.subgroup; })
    .key(function(d) { return d.division; })
    .key(function(d) { return d.product; })
    .entries(parsed);

/*The visualization requires there to be a root node*/
var flareData={key:'root',values:[]};
flareData.values=nest;
renameKeys(flareData);

这是获取所需 JSON 的 JSFiddle:http: //jsfiddle.net/X2fxC/3/

这是一个带有结果可视化的 JSFiddle:http: //jsfiddle.net/X2fxC/5/

使用扩展树可视化进行绘制时,您的数据如下所示:

在此处输入图像描述

于 2014-04-01T01:57:12.680 回答