Loading D3.js data from a simple JSON string

后端 未结 6 790
礼貌的吻别
礼貌的吻别 2020-11-28 06:29

Most of the examples in gallery load data from TSV files.

How can I convert the following to use a local json variable instead of TSV data?

d3.tsv(&quo         


        
6条回答
  •  再見小時候
    2020-11-28 07:17

    For D3js v2 or v3 (not sure which one).

    Declare your dataset

    var dataset = { 
        "first-name": "Stack",
        "last-name": "Overflow",
    }; // JSON object
    var dataset = [ 5, 10, 15, 20, 25 ]; // or array
    

    As stated by the doc, you can use either:

    an array of numbers or objects, or a function that returns an array of values

    Bind it

    d3.select("body").selectAll("p")
        .data(dataset)
        .enter()
        .append("p")
        .text("New paragraph!");
    

    More explanation at Scott Murray's D3's tutorial#Binding data.

    The data() function apply to a selection, more information can be found in the official documentation: selection.data([values[, key]]).

提交回复
热议问题