【问题标题】:Sunburst Chart - Adding an extra layer of dataSunburst Chart - 添加额外的数据层
【发布时间】:2018-04-15 21:12:17
【问题描述】:

我正在学习本教程:

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 格式,但我无法理解它。

【问题讨论】:

    标签: javascript json d3.js graph


    【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-21
      • 1970-01-01
      • 2012-10-25
      • 2015-07-03
      • 2017-10-27
      • 2019-01-17
      • 1970-01-01
      相关资源
      最近更新 更多