Initialize data from json variable d3js (javascript) -
to create dynamic graphic, json variable rest server. found way initialize graphic variable, have apply function on each data inside before.
here part of current code :
d3.json("test.json", function(data) { var parse = d3.time.format("%y-%m-%d %h").parse; types = d3.nest() .key(function(d) { return d.type; }) .entries(stocks = data); types.foreach(function(s) { s.values.foreach(function(d) { d.jour = parse(d.jour); d.count = +d.count; }); s.maxcount = d3.max(s.values, function(d) { return d.count; }); s.sumcount = d3.sum(s.values, function(d) { return d.count; }); }); var g = svg.selectall("g") .data(types) .enter().append("g") .attr("class", "type"); settimeout(lines, duration); });
as can see , code use json file, it's tests. can't use method because data come rest server. have idea replace utilisation of json file variable containing json.
here, example of content json :
[ {"type":"cad","jour":"2016-03-29 00","count":28697}, {"type":"cad","jour":"2016-03-29 01","count":29156}, {"type":"cad","jour":"2016-03-29 02","count":29799}, {"type":"cad","jour":"2016-03-29 03","count":30095}, {"type":"cad","jour":"2016-03-29 04","count":28430}, {"type":"cad","jour":"2016-03-29 05","count":29124}, {"type":"cad","jour":"2016-03-29 06","count":28073}, {"type":"cad","jour":"2016-03-29 07","count":29398}, {"type":"cad","jour":"2016-03-29 08","count":28522}, {"type":"cad","jour":"2016-03-29 09","count":15685}, {"type":"ism","jour":"2016-03-29 00","count":39170}, {"type":"ism","jour":"2016-03-29 01","count":39143}, {"type":"ism","jour":"2016-03-29 02","count":39061}, {"type":"ism","jour":"2016-03-29 03","count":39066}, {"type":"ism","jour":"2016-03-29 04","count":38529}, {"type":"ism","jour":"2016-03-29 05","count":39019}, {"type":"ism","jour":"2016-03-29 06","count":38354}, {"type":"ism","jour":"2016-03-29 07","count":38195}, {"type":"ism","jour":"2016-03-29 08","count":37907}, {"type":"ism","jour":"2016-03-29 09","count":20015} ]
thanks in advance.
just make server call , initialize d3 graphic when received data it. put code in success callback:
$.getjson('/getgraphicdata', function(data) { var parse = d3.time.format("%y-%m-%d %h").parse; types = d3.nest() .key(function(d) { return d.type; }) .entries(stocks = data); types.foreach(function(s) { s.values.foreach(function(d) { d.jour = parse(d.jour); d.count = +d.count; }); s.maxcount = d3.max(s.values, function(d) { return d.count; }); s.sumcount = d3.sum(s.values, function(d) { return d.count; }); }); var g = svg.selectall("g") .data(types) .enter().append("g") .attr("class", "type"); settimeout(lines, duration); });
edit
as @florianbury mentioned in comments, can like:
d3.json('/getgraphicdata', function(data) { ... });
Comments
Post a Comment