graph.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. function meCoffeeGraph () {
  2. this.n = 243;
  3. this.data = d3.range(this.n).map(function() { return 18; });
  4. this.data2 = d3.range(this.n).map(function() { return 18; });
  5. this.data_sp = d3.range(this.n).map(function() { return 18; });
  6. }
  7. // superclass method
  8. meCoffeeGraph.prototype.draw = function( ) {
  9. this.duration = 750;
  10. this.now = new Date(Date.now() - this.duration);
  11. var
  12. count = 0;
  13. var margin = {top: 6, right: 0, bottom: 20, left: 30};
  14. var rect = $('#graph').parent()[0].getBoundingClientRect();
  15. this.width = rect.width - margin.right - margin.left;
  16. this.height = rect.height - margin.top - margin.bottom;
  17. this.x = d3.time.scale()
  18. .domain([this.now - (this.n - 2) * this.duration, this.now - this.duration])
  19. .range([0, this.width]);
  20. this.y = d3.scale.linear()
  21. .range([this.height, 0]);
  22. this.line = d3.svg.line()
  23. .interpolate("basis")
  24. .y(function(d, i) { return thiss.y(d); })
  25. .x(function(d, i) { return thiss.x(thiss.now - (thiss.n - 1 - i) * thiss.duration); });
  26. //.x(function(d, i) { return this.y(i); });
  27. this.line_tmp2 = d3.svg.line()
  28. .interpolate("basis")
  29. .y(function(d, i) { return thiss.y(d); })
  30. .x(function(d, i) { return thiss.x(thiss.now - (thiss.n - 1 - i) * thiss.duration); });
  31. //.x(function(d, i) { return this.y(i); });
  32. this.line2 = d3.svg.line()
  33. .interpolate("basis")
  34. .x(function(d, i) { return thiss.x(thiss.now - (thiss.n - 1 - i) * thiss.duration); })
  35. .y(function(d, i) { return thiss.y(d); });
  36. $('#graph svg').remove( );
  37. this.svg = d3.select("#graph").append("svg")
  38. .attr( "class", "svg-content" )
  39. .attr( "viewBox", "0 0 " + rect.width + " " + rect.height )
  40. .attr( "preserveAspectRatio", "none" )
  41. .attr("id", "svg-g")
  42. //.attr("width", width + margin.left + margin.right)
  43. //.attr("height", height + margin.top + margin.bottom)
  44. //.style("margin-left", -margin.left + "px")
  45. .append("g")
  46. .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  47. this.svg.append("defs").append("clipPath")
  48. .attr("id", "clip")
  49. .append("rect")
  50. .attr("width", this.width)
  51. .attr("height", this.height);
  52. this.axis = this.svg.append("g")
  53. .attr("class", "x axis")
  54. .attr("transform", "translate(0," + this.height + ")")
  55. .call( this.x.axis = d3.svg.axis().scale(this.x).orient("bottom"));
  56. this.yaxis = this.svg.append("g")
  57. .attr("class", "y axis")
  58. .call( this.y.axis = d3.svg.axis().scale(this.y).ticks( 5 ).orient("left"));
  59. this.path = this.svg.append("g")
  60. .attr("clip-path", "url(#clip)")
  61. .style("stroke", "blue")
  62. .append("path")
  63. .datum(this.data)
  64. .attr("id", "line_tmp");
  65. this.path = this.svg.append("g")
  66. .attr("clip-path", "url(#clip)")
  67. .style("stroke", "red")
  68. .append("path")
  69. .datum(this.data2)
  70. .attr("id", "line_tmp2");
  71. this.path_sp = this.svg.append("g")
  72. .attr("clip-path", "url(#clip)")
  73. .style("stroke", "green")
  74. .append("path")
  75. .datum(this.data_sp)
  76. .attr("id", "line_sp");
  77. this.transition = d3.select({}).transition()
  78. .duration(100)
  79. .ease("linear");
  80. }
  81. meCoffeeGraph.prototype.tick = function( tmp, tmp2, sp, timestamp ) {
  82. thiss = this
  83. if( !this.started )
  84. this.started = new Date();
  85. //thiss.transition = thiss.transition.each(function() {
  86. // update the domains
  87. if( timestamp )
  88. thiss.now = new Date( this.started.getTime() + timestamp * 1000 );
  89. else
  90. thiss.now = new Date();
  91. thiss.x.domain([thiss.now - (thiss.n - 2) * thiss.duration, thiss.now - thiss.duration]);
  92. var data2_min = d3.min(thiss.data2),
  93. data2_max = d3.max(thiss.data2),
  94. data_min = d3.min(thiss.data),
  95. data_max = d3.max(thiss.data);
  96. if( data2_min > 5 && data2_max < 190 ) {
  97. data_min = Math.min( data2_min, data_min );
  98. data_max = Math.max( data2_max, data_max );
  99. }
  100. thiss.y.domain( [ data_min * 0.90, data_max * 1.1 ] );
  101. // push the accumulated count onto the back, and reset the count
  102. thiss.data.push( tmp );
  103. thiss.data2.push( tmp2 );
  104. thiss.data_sp.push( sp );
  105. count = 0;
  106. // redraw the line
  107. //thiss = this
  108. thiss.svg.select("#line_tmp")
  109. .attr("d", thiss.line )
  110. .attr("transform", null);
  111. thiss.svg.select("#line_tmp2")
  112. .attr("d", thiss.line_tmp2 )
  113. .attr("transform", null);
  114. thiss.svg.select("#line_sp")
  115. .attr("d", thiss.line2 )
  116. .attr("transform", null);
  117. // slide the x-axis left
  118. thiss.axis.call( thiss.x.axis);
  119. thiss.yaxis.call( thiss.y.axis);
  120. // slide the line left
  121. thiss.path.transition()
  122. .attr("transform", "translate(" + thiss.x(thiss.now - (thiss.n - 1) * thiss.duration) + ")");
  123. thiss.path_sp.transition()
  124. .attr("transform", "translate(" + thiss.x(thiss.now - (thiss.n - 1) * thiss.duration) + ")");
  125. // pop the old data point off the front
  126. thiss.data.shift();
  127. thiss.data2.shift();
  128. thiss.data_sp.shift();
  129. //}).transition(); //.each("start", tick);
  130. }
  131. meCoffeeGraph.prototype.redraw = function() {
  132. thiss = this;
  133. thiss.svg.select("#line_tmp")
  134. .attr("d", thiss.line )
  135. .attr("transform", null);
  136. thiss.svg.select("#line_sp")
  137. .attr("d", thiss.line2 )
  138. .attr("transform", null);
  139. }