Installation
Start server
Steps for visualizations
Loading data
- Download the file from the website and then include
d3js
in the.html
in thescript
section:<script src="d3.js"> </script>
A server is needed to upload data and files when using d3js. Put data and index.html in the same folder.
- Using python:
- On terminal, start a server:
and get the server address
cd to folder python -m http.server
- On chrome, type:
and I can see
localhost:server_address
index.html
rendered (ifindex.html
not there, I see the folder content)
- On terminal, start a server:
- Atom:
- Transform data (e.g.
d3.cross
,d3.max
) - Map data to image space (e.g. dollars to n. of pixels) (e.g.
d3.scaleLinear
,d3.scaleTime
) - Compute the layout (e.g.
d3.path
,d3.treemap
) - Draw the chart (e.g.
d3.select
,d3.append
)
- Data are in
.json
or.csv
- Loading requires a server
- The process is asynchronous. If we want to do something with the data, we need to provide a callback:
d3.csv(dataUrl) #for json: d3.json(dataUrl) .then(callback) # once the data is ready, we can run the method (not before) .catch(errorHandler) # to check if there are loading errors
- Example:
<body> <div id="container"></div> # data will be given to container </body> <script src"d3.js"></script> # import d3js <script> let container = d3.select("#container") # connects to the html container d3.csv("file_name.csv") .then(function (data)){ #using anonimous function, i.e. without name + providing data to the function for (let client of data){ # for loop thought data write (client.Name) } }) function write(text) { # this function is written below the line where it is called container.append("div").text(text) # provide text to the container; no need to pass container as a variable } </script>
References
- Information Visualization: Programming with D3.js on Coursera