Sunburst Chart - 添加额外的数据层

时间:2018-04-15 21:12:18

标签: javascript json d3.js graph

我正在学习本教程:

https://bl.ocks.org/denjn5/3b74baf5edc4ac93d5e487136481c601

它工作正常,但只有两个“数据环”。我想补充一点。它使用以下数据从data.json文件中提取数据:

{

    "name": "TOPICS", "children": [{
        "name": "Topic A",
        "children": [{"name": "Sub A1", "size": 5, "text": "A story", "sentiment": 1, "source": "dictionary"},
            {"name": "Sub A2", "size": 5, "text": "A note", "sentiment": 0.5, "source": "dictionary"}]
    }, {
        "name": "Topic B",
        "children": [{"name": "Sub B1", "size": 5, "text": "A vignette", "sentiment": 1, "source": "newspaper"},
            {"name": "Sub B3", "size": 5, "text": "A joke", "sentiment": 0.5, "source": "email"}]
    }, {
        "name": "Topic C",
        "children": [{"name": "Sub A1", "size": 5, "text": "A narrative", "sentiment": 1, "source": "newspaper"},
            {"name": "Sub A2", "size": 5, "text": "A chronology", "sentiment": 0.5, "source": "email"}]
    }]
}

我的问题是,如何在外面添加第三个数据环?看起来这是JSON格式,但我无法理解它。

1 个答案:

答案 0 :(得分:1)

鉴于您共享的特定bl.ocks,您需要做的就是在您希望成为新图层的父项目的项目中创建一个children数组,并删除size属性父母:

例如,在Sub A1中创建一个新图层:

{
    "name": "TOPICS",
    "children": [{
        "name": "Topic A",
        "children": [{
            "name": "Sub A1",
            "text": "A story",
            "sentiment": 0.8,
            "source": "dictionary",
            "children": [{
                "name": "New Layer 1",
                "size": 5,
                "text": "A story",
                "sentiment": 0.8,
                "source": "dictionary"
            },{ 
            ...etc
            }]
        }, {
            "name": "Sub A2",
            "size": 5,
            "text": "A note",
            "sentiment": 0.3,
            "source": "dictionary"
        }]
    },{
    ...etc
    }]
}

我只是在这里复制/粘贴......当然,你必须相应地调整值。

以下是更新的bl.ocks:https://bl.ocks.org/GerardoFurtado/7c30efbc20232abda294cd71a959c79d/f211ca864503860dfa181c5e9e142e38897abb56