3

I need replace d3.csv that read data from a file, and replace it with hardcoded data. How to do it in d3 ?

var width,height
var chartWidth, chartHeight
var margin
var svg = d3.select("#graph").append("svg")
var chartLayer = svg.append("g").classed("chartLayer", true)

d3.csv("data.csv", cast,  main)

file (data.csv):

name,value
hoge,100
hellow, 312
world, 222
fuga, 254

I need to not depend on the CSV file.

1 Answer 1

1

When you load a CSV with d3.csv, the file is parsed in an array of objects, using the header row to define the key names and the subsequent rows to define the values.

This is what you'll have with your CSV:

var csv = `name,value
hoge,100
hellow, 312
world, 222
fuga, 254`;

var data = d3.csvParse(csv);

console.log(data);
<script src="https://d3js.org/d3.v4.min.js"></script>

Therefore, this is the array that you have to hardcode:

[{
    "name": "hoge",
    "value": "100"
}, {
    "name": "hellow",
    "value": " 312"
}, {
    "name": "world",
    "value": " 222"
}, {
    "name": "fuga",
    "value": " 254"
}]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.