javascript - How to display d3 elements on a gridster box? -


i trying create area chart using d3, using array:

var jsonresobj = {                initial_hours: [                   1800, 1700, 1030, 1130, 950, 1249, 1225, 1821, 1250,                   1505, 38, 130, 1520, 1600, 1330, 1930, 1806, 1535                 ]               }; 

the code axis , chart here:

var margin = {top: 20, right: 20, bottom: 40, left: 50},         width = 350 - margin.left - margin.right,         height = 400 - margin.top - margin.bottom,         days = jsonresobj.initial_hours.length;      var x = d3.scale.linear()     .domain([0, days])     .range([0, width]);      var y = d3.scale.linear()     .domain([0, d3.max(jsonresobj.initial_hours)])     .range([height, 0]);      var xaxis = d3.svg.axis()     .scale(x)     .orient("bottom");      var yaxis = d3.svg.axis()     .scale(y)     .orient("left");      var area = d3.svg.area()     .x(function(d) { return x(days); })     .y0(height)     .y1(function(d) { return y(jsonresobj.initial_hours); });      var svg = d3.select("#box1").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 + ")");      svg.append("path")     .datum(jsonresobj)     .attr("class", "area")     .attr("d", area);      svg.append("g")     .attr("class", "x axis")     .attr("transform", "translate(0," + height + ")")     .call(xaxis);      svg.append("g")     .attr("class", "y axis")     .call(yaxis); 

my gridster box:

<li data-sizey="3" data-sizex="3" data-col="3" data-row="1">     <div class="gridster-box" id="box1">         <div class="handle-resize"></div>     </div> </li> 

when load page, axes show , area selector 0px * 0px

how draw these area charts in gridster box?

you need transform data each data point object containing x , y properties , can accessed area function.

the way have currently, there no way d3 know day or hour area data point referring to.

see code below showing changes area function refer day , hour properties of data object passed via function(d). data transformation right after svg variable declaration. here's working fiddle of solution.

var jsonresobj = {            initial_hours: [               1800, 1700, 1030, 1130, 950, 1249, 1225, 1821, 1250,1505, 38, 130, 1520, 1600, 1330, 1930, 1806, 1535             ]           };  var margin = {top: 20, right: 20, bottom: 40, left: 50},     width = 350 - margin.left - margin.right,     height = 400 - margin.top - margin.bottom,     days = jsonresobj.initial_hours.length;  var x = d3.scale.linear() .domain([0, days]) .range([0, width]);  var y = d3.scale.linear() .domain([0, d3.max(jsonresobj.initial_hours)]) .range([height, 0]);  var xaxis = d3.svg.axis() .scale(x) .orient("bottom");  var yaxis = d3.svg.axis() .scale(y) .orient("left");  var area = d3.svg.area() .x(function(d) { return x(d.day); }) .y0(height) .y1(function(d) { return y(d.hour); });  var svg = d3.select("#box1").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 = []; jsonresobj.initial_hours.foreach(function(d,i){         data.push({day: i, hour: d}) });  svg.append("path") .datum(data) .attr("class", "area") .attr("d", area);  svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xaxis);  svg.append("g")   .attr("class", "y axis")   .attr("transform", "translate(0,0)")   .call(yaxis); 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -