d3.js TSV to Javascript variable

2019-07-11 13:54发布

问题:

I'm working on a graph with d3.js library. The 3d.js tsv parser creates the 'd' variable as such:

TSV:

day Value
01-01   50
01-02   45
01-03   60

code:

d3.tsv("data.tsv", function(error, data){...}

and now you use 'd.day' and 'd.Value' to build your graph - scaterplots, linegraph, etc...

I won't be using a tsv file but a javascript variable instead. Which is the most correct (if possible) javascript variable representation in order to use this d.day and d.Value in 3d.js and parsing it conventionally like 'svg.data(data)'?

回答1:

If you're not reading from a file, you can still use the JSON format (which is what you're talking about). The format for a json variable is as follows:

var myVariable = {
"indexA" : "valueA",
"indexB" : "valueB"
}

You can even have an array as the value (even though you didn't ask) and that would look very similar:

var myVariable = {
"indexA" : [ "indexA1": "valueA1", "indexA2": "valueA2", ...],
"indexB" : "valueB"
}

You can find out more information about the JSON format here: http://www.json.org/