The first thing you’ll need to do is figure out how to write JSON that conforms to the format which that notebook can read. For reference, here’s the actual file that the notebook you’re looking at is parsing. https://unpkg.com/@observablehq/flare@0.0.0/index.js
Basically you want to create a JSON data which imitates the structure of everything in the curly braces after “return
” in that JS file. Here’s one very basic example which you can try out by replacing the data cell in the online Observable version with this:
data = ({name:"big",children:[{name:"medium2",children:[{name:"small200",size:"200"},{name:"small100",size:"100"}]},
{name:"medium3",children:[{name:"small50",size:"50"},{name:"small150",size:"150"},
{name:"small75",size:"75"}]}]})
If you were to export the notebook, the cell should translate into something like this:
{
name: "data",
inputs: [],
value: (function(){return({name:"big",children:[{name:"medium2",children:[{name:"small200",size:"200"},{name:"small100",size:"100"}]},
{name:"medium3",children:[{name:"small50",size:"50"},{name:"small150",size:"150"},
{name:"small75",size:"75"}]}]}
)})
},
This should work for testing, but you may eventually want an approach which more cleanly separates the data from the visualization so that you won’t have to keep editing this JS file to change the data.
One way to do this is as follows. First save your JSON file somewhere in the local directory where your html / js files are, say in my_data.json
. Note that to be valid JSON, the property names like “name”, “size”, and “children” have to be in double quotes like this:
{"name":"big","children":[{"name":"medium2","children":[{"name":"small200","size":"200"},{"name":"small100","size":"100"}]},
{"name":"medium3","children":[{"name":"small50","size":"50"},{"name":"small150","size":"150"},
{"name":"small75","size":"75"}]}]}
Then in the JS file corresponding to your notebook, replace the data cell with the following, which uses d3.json
to request and parse the file:
{
name: "data",
inputs: ["d3"],
value: (function(d3){return(
d3.json('my_data.json')
)})
},
Now you can just edit my_data.json
and refresh the page to see the changes.