Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. /**
  2. *
  3.  
  4.  
  5.  
  6.  
  7. usage:
  8. <div data-sparkline="71, 78, 39, 66 "></div>
  9.  
  10. */
  11.  
  12.  
  13.  
  14. $(function () {
  15. /**
  16. * Create a constructor for sparklines that takes some sensible defaults and merges in the individual
  17. * chart options. This function is also available from the jQuery plugin as $(element).highcharts('SparkLine').
  18. */
  19. Highcharts.SparkLine = function (a, b, c) {
  20. var hasRenderToArg = typeof a === 'string' || a.nodeName,
  21. options = arguments[hasRenderToArg ? 1 : 0],
  22. defaultOptions = {
  23. chart: {
  24. renderTo: (options.chart && options.chart.renderTo) || this,
  25. backgroundColor: null,
  26. borderWidth: 0,
  27. type: 'area',
  28. margin: [2, 0, 2, 0],
  29. width: 120,
  30. height: 20,
  31. style: {
  32. overflow: 'visible'
  33. },
  34.  
  35. // small optimalization, saves 1-2 ms each sparkline
  36. skipClone: true
  37. },
  38. title: {
  39. text: ''
  40. },
  41. credits: {
  42. enabled: false
  43. },
  44. xAxis: {
  45. labels: {
  46. enabled: false
  47. },
  48. title: {
  49. text: null
  50. },
  51. startOnTick: false,
  52. endOnTick: false,
  53. tickPositions: []
  54. },
  55. yAxis: {
  56. endOnTick: false,
  57. startOnTick: false,
  58. labels: {
  59. enabled: false
  60. },
  61. title: {
  62. text: null
  63. },
  64. tickPositions: [0]
  65. },
  66. legend: {
  67. enabled: false
  68. },
  69. tooltip: {
  70. backgroundColor: null,
  71. borderWidth: 0,
  72. shadow: false,
  73. useHTML: true,
  74. hideDelay: 0,
  75. shared: true,
  76. padding: 0,
  77. positioner: function (w, h, point) {
  78. return { x: point.plotX - w / 2, y: point.plotY - h };
  79. }
  80. },
  81. plotOptions: {
  82. series: {
  83. animation: false,
  84. lineWidth: 1,
  85. shadow: false,
  86. states: {
  87. hover: {
  88. lineWidth: 1
  89. }
  90. },
  91. marker: {
  92. radius: 1,
  93. states: {
  94. hover: {
  95. radius: 2
  96. }
  97. }
  98. },
  99. fillOpacity: 0.25
  100. },
  101. column: {
  102. negativeColor: '#910000',
  103. borderColor: 'silver'
  104. }
  105. }
  106. };
  107.  
  108. options = Highcharts.merge(defaultOptions, options);
  109.  
  110. return hasRenderToArg ?
  111. new Highcharts.Chart(a, options, c) :
  112. new Highcharts.Chart(options, b);
  113. };
  114.  
  115. var start = +new Date(),
  116. $tds = $('td[data-sparkline]'),
  117. fullLen = $tds.length,
  118. n = 0;
  119.  
  120. // Creating 153 sparkline charts is quite fast in modern browsers, but IE8 and mobile
  121. // can take some seconds, so we split the input into chunks and apply them in timeouts
  122. // in order avoid locking up the browser process and allow interaction.
  123. function doChunk() {
  124. var time = +new Date(),
  125. i,
  126. len = $tds.length,
  127. $td,
  128. stringdata,
  129. arr,
  130. data,
  131. chart;
  132.  
  133. for (i = 0; i < len; i += 1) {
  134. $td = $($tds[i]);
  135. stringdata = $td.data('sparkline');
  136. arr = stringdata.split('; ');
  137. data = $.map(arr[0].split(', '), parseFloat);
  138. chart = {};
  139.  
  140. if (arr[1]) {
  141. chart.type = arr[1];
  142. }
  143. $td.highcharts('SparkLine', {
  144. series: [{
  145. data: data,
  146. pointStart: 1
  147. }],
  148. tooltip: {
  149. headerFormat: '<span style="font-size: 10px">' + $td.parent().find('th').html() + ', Q{point.x}:</span><br/>',
  150. pointFormat: '<b>{point.y}.000</b> USD'
  151. },
  152. chart: chart
  153. });
  154.  
  155. n += 1;
  156.  
  157. // If the process takes too much time, run a timeout to allow interaction with the browser
  158. if (new Date() - time > 500) {
  159. $tds.splice(0, i + 1);
  160. setTimeout(doChunk, 0);
  161. break;
  162. }
  163.  
  164. // Print a feedback on the performance
  165. if (n === fullLen) {
  166. $('#result').html('Generated ' + fullLen + ' sparklines in ' + (new Date() - start) + ' ms');
  167. }
  168. }
  169. }
  170. doChunk();
  171.  
  172. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement