I have a JSON in this format:
[{"Wed Jan 23 00:00:00 IST 2013":3383387},
{"Thu Jan 24 00:00:00 IST 2013":3388022},
{"Fri Jan 25 00:00:00 IST 2013":3393322},
{"Sat Jan 26 00:00:00 IST 2013":3401241},
{"Sun Jan 27 00:00:00 IST 2013":3406134},
{"Mon Jan 28 00:00:00 IST 2013":3410703},
{"Tue Jan 29 00:00:00 IST 2013":3415120}]
I am trying to create a simple line chart using this JSON and D3 sample code mentioned at http://bl.ocks.org/3883195. Instead of using d3.tsv I am using d3.json. But for some reason it’s not taking JSON in this format. I believe I can make it work if this JSON can be converted to this format:
[{"Date":
["Wed Jan 23 00:00:00 IST 2013",
"Thu Jan 24 00:00:00 IST 2013",
"Fri Jan 25 00:00:00 IST 2013",
"Sat Jan 26 00:00:00 IST 2013",
"Sun Jan 27 00:00:00 IST 2013",
"Mon Jan 28 00:00:00 IST 2013",
"Tue Jan 29 00:00:00 IST 2013"],
"Values":[3415120,2343244,454545,4564566,56334534,34543554,34543555]}]
Question is, is there any easy way to convert my JSON to this format. I am using php function json-encode to encode an array to JSON.
The only problem with the foramt your JSON is in is that the keys denote data that you want to use to plot the graph. You can easily convert it to a format that is more ameniable to that with
d3.entries()however — there’s no need to give different JSON to D3.