chart-dash3-int.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //[Dashboard Javascript]
  2. //Project: Riday Admin - Responsive Admin Template
  3. //Primary use: Used only for the main dashboard (index.html)
  4. // ------------------------------
  5. var Widgetschart = function() {
  6. // Simple sparklines
  7. var _sparklinesWidget = function(element, chartType, qty, chartHeight, interpolation, duration, interval, color) {
  8. if (typeof d3 == 'undefined') {
  9. console.warn('Warning - d3.min.js is not loaded.');
  10. return;
  11. }
  12. // Initialize chart only if element exsists in the DOM
  13. if(element) {
  14. // Basic setup
  15. // ------------------------------
  16. // Define main variables
  17. var d3Container = d3.select(element),
  18. margin = {top: 0, right: 0, bottom: 0, left: 0},
  19. width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right,
  20. height = chartHeight - margin.top - margin.bottom;
  21. // Generate random data (for demo only)
  22. var data = [];
  23. for (var i=0; i < qty; i++) {
  24. data.push(Math.floor(Math.random() * qty) + 5);
  25. }
  26. // Construct scales
  27. // ------------------------------
  28. // Horizontal
  29. var x = d3.scale.linear().range([0, width]);
  30. // Vertical
  31. var y = d3.scale.linear().range([height - 5, 5]);
  32. // Set input domains
  33. // ------------------------------
  34. // Horizontal
  35. x.domain([1, qty - 3]);
  36. // Vertical
  37. y.domain([0, qty]);
  38. // Construct chart layout
  39. // ------------------------------
  40. // Line
  41. var line = d3.svg.line()
  42. .interpolate(interpolation)
  43. .x(function(d, i) { return x(i); })
  44. .y(function(d, i) { return y(d); });
  45. // Area
  46. var area = d3.svg.area()
  47. .interpolate(interpolation)
  48. .x(function(d,i) {
  49. return x(i);
  50. })
  51. .y0(height)
  52. .y1(function(d) {
  53. return y(d);
  54. });
  55. // Create SVG
  56. // ------------------------------
  57. // Container
  58. var container = d3Container.append('svg');
  59. // SVG element
  60. var svg = container
  61. .attr('width', width + margin.left + margin.right)
  62. .attr('height', height + margin.top + margin.bottom)
  63. .append("g")
  64. .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  65. // Add mask for animation
  66. // ------------------------------
  67. // Add clip path
  68. var clip = svg.append("defs")
  69. .append("clipPath")
  70. .attr('id', function(d, i) { return "load-clip-" + element.substring(1); });
  71. // Add clip shape
  72. var clips = clip.append("rect")
  73. .attr('class', 'load-clip')
  74. .attr("width", 0)
  75. .attr("height", height);
  76. // Animate mask
  77. clips
  78. .transition()
  79. .duration(1000)
  80. .ease('linear')
  81. .attr("width", width);
  82. //
  83. // Append chart elements
  84. //
  85. // Main path
  86. var path = svg.append("g")
  87. .attr("clip-path", function(d, i) { return "url(#load-clip-" + element.substring(1) + ")"; })
  88. .append("path")
  89. .datum(data)
  90. .attr("transform", "translate(" + x(0) + ",0)");
  91. // Add path based on chart type
  92. if(chartType == "area") {
  93. path.attr("d", area).attr('class', 'd3-area').style("fill", color); // area
  94. }
  95. else {
  96. path.attr("d", line).attr("class", "d3-line d3-line-medium").style('stroke', color); // line
  97. }
  98. // Animate path
  99. path
  100. .style('opacity', 0)
  101. .transition()
  102. .duration(500)
  103. .style('opacity', 1);
  104. // Set update interval. For demo only
  105. // ------------------------------
  106. setInterval(function() {
  107. // push a new data point onto the back
  108. data.push(Math.floor(Math.random() * qty) + 5);
  109. // pop the old data point off the front
  110. data.shift();
  111. update();
  112. }, interval);
  113. // Update random data. For demo only
  114. // ------------------------------
  115. function update() {
  116. // Redraw the path and slide it to the left
  117. path
  118. .attr("transform", null)
  119. .transition()
  120. .duration(duration)
  121. .ease("linear")
  122. .attr("transform", "translate(" + x(0) + ",0)");
  123. // Update path type
  124. if(chartType == "area") {
  125. path.attr("d", area).attr('class', 'd3-area').style("fill", color);
  126. }
  127. else {
  128. path.attr("d", line).attr("class", "d3-line d3-line-medium").style('stroke', color);
  129. }
  130. }
  131. // Resize chart
  132. // ------------------------------
  133. // Call function on window resize
  134. $(window).on('resize', resizeSparklines);
  135. // Call function on sidebar width change
  136. $(document).on('click', '.sidebar-control', resizeSparklines);
  137. // Resize function
  138. //
  139. // Since D3 doesn't support SVG resize by default,
  140. // we need to manually specify parts of the graph that need to
  141. // be updated on window resize
  142. function resizeSparklines() {
  143. // Layout variables
  144. width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right;
  145. // Layout
  146. // -------------------------
  147. // Main svg width
  148. container.attr("width", width + margin.left + margin.right);
  149. // Width of appended group
  150. svg.attr("width", width + margin.left + margin.right);
  151. // Horizontal range
  152. x.range([0, width]);
  153. // Chart elements
  154. // -------------------------
  155. // Clip mask
  156. clips.attr("width", width);
  157. // Line
  158. svg.select(".d3-line").attr("d", line);
  159. // Area
  160. svg.select(".d3-area").attr("d", area);
  161. }
  162. }
  163. };
  164. //
  165. // Return objects assigned to module
  166. //
  167. return {
  168. init: function() {
  169. _sparklinesWidget("#sparklines_color", "line", 30, 200, "basis", 750, 2000, "rgba(255,255,255,0.75)");
  170. }
  171. }
  172. }();
  173. // Initialize module
  174. // ------------------------------
  175. // When content loaded
  176. document.addEventListener('DOMContentLoaded', function() {
  177. Widgetschart.init();
  178. });