javascript - D3 Update Graph -
really stucked on issue days. trying update d3 graph show how long takes run function on calculator. info get, show time taken , display on graph drew. issue here can't graph update.
codes graph:
var margin = {top: 20, right: 20, bottom: 30, left: 50}, width = 400 - margin.left - margin.right, height = 300 - margin.top - margin.bottom; var x = d3.scale.linear() .range([0, width]) var y = d3.scale.linear() .range([height, 0]); var xaxis = d3.svg.axis() .scale(x) .orient("bottom"); var yaxis = d3.svg.axis() .scale(y) .orient("left"); var line = d3.svg.line() .x(function(d) { return x(d.date); }) .y(function(d) { return y(d.close); }); var svg = d3.select("body").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); var data = dataone.map(function(d) { return { date:d[0], close: d[1] }; }); console.log(data); x.domain(d3.extent(data, function(d) { return d.date; })); y.domain(d3.extent(data, function(d) { return d.close; })); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xaxis); svg.append("g") .attr("class", "y axis") .call(yaxis) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("price ($)"); svg.append("path") .datum(data) .attr("class", "line") .attr("d", line);
code update function , update of array:
function updatearray(){ dataone.push(clickss-0.5,clickss); updatedata(); } function updatedata() { var data = dataone.map(function(d) { return { date:d[0], close: d[1] }; }); // scale range of data again x.domain(d3.extent(data, function(d) { return d.date; })); y.domain([0, d3.max(data, function(d) { return d.close; })]); // select section want apply our changes var svg = d3.select("body").transition(); // make changes svg.select(".line") // change line .duration(750) .attr("d", valueline(data)); svg.select(".x.axis") // change x axis .duration(750) .call(xaxis); svg.select(".y.axis") // change y axis .duration(750) .call(yaxis);
}
please advise on how can work. read quite few guides
couple things:
1.) in update function, line generator function line
not valueline
.
2.) looking @ accessor functions, don't think meant dataone.push(clickss-0.5,clickss);
rather dataone.push([clickss-0.5,clickss]);
. need array of arrays.
in addition, don't need map dataone
array structure. change accessors:
x.domain(d3.extent(data, function(d) { return d[0]; })); y.domain(d3.extent(data, function(d) { return d[1]; })); ... var line = d3.svg.line() .x(function(d) { return x(d[0]); }) .y(function(d) { return y(d[1]); });
here's code working , cleaned bit:
<!doctype html> <html> <head> <script data-require="d3@3.5.3" data-semver="3.5.3" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.js"></script> </head> <body> <script> var dataone = []; (var = 0; < 10; i++){ dataone.push([i,math.random()]); } var margin = { top: 20, right: 20, bottom: 30, left: 50 }, width = 400 - margin.left - margin.right, height = 300 - margin.top - margin.bottom; var x = d3.scale.linear() .range([0, width]) var y = d3.scale.linear() .range([height, 0]); var xaxis = d3.svg.axis() .scale(x) .orient("bottom"); var yaxis = d3.svg.axis() .scale(y) .orient("left"); var line = d3.svg.line() .x(function(d) { return x(d[0]); }) .y(function(d) { return y(d[1]); }); var svg = d3.select("body").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); // scale range of data again x.domain(d3.extent(dataone, function(d) { return d[0]; })); y.domain([0, d3.max(dataone, function(d) { return d[1]; })]); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xaxis); svg.append("g") .attr("class", "y axis") .call(yaxis) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("price ($)"); svg.append("path") .datum(dataone) .attr("class", "line") .attr("d", line) .style("fill","none") .style("stroke","steelblue"); function updatedata() { // scale range of data again x.domain(d3.extent(dataone, function(d) { return d[0]; })); y.domain([0, d3.max(dataone, function(d) { return d[1]; })]); // select section want apply our changes var svg = d3.select("body").transition(); // make changes svg.select(".line") // change line .duration(750) .attr("d", line(dataone)); svg.select(".x.axis") // change x axis .duration(750) .call(xaxis); svg.select(".y.axis") // change y axis .duration(750) .call(yaxis); } setinterval(function(){ (var = 0; < 5; i++){ dataone.push([dataone.length + i, math.random()]); } updatedata(); },1000); </script> </body> </html>
Comments
Post a Comment