sparkline.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. $(function () {
  2. $(".sparkline").each(function () {
  3. var $this = $(this);
  4. $this.sparkline('html', $this.data());
  5. });
  6. $('.sparkline-pie').sparkline('html', {
  7. type: 'pie',
  8. offset: 90,
  9. width: '150px',
  10. height: '150px',
  11. sliceColors: ['#E91E63', '#00BCD4', '#FFC107']
  12. })
  13. drawDocSparklines();
  14. drawMouseSpeedDemo();
  15. });
  16. //Taken from http://omnipotent.net/jquery.sparkline ================
  17. function drawDocSparklines() {
  18. // Bar + line composite charts
  19. $('#compositebar').sparkline('html', { type: 'bar', barColor: '#aaf' });
  20. $('#compositebar').sparkline([4, 1, 5, 7, 9, 9, 8, 7, 6, 6, 4, 7, 8, 4, 3, 2, 2, 5, 6, 7],
  21. { composite: true, fillColor: false, lineColor: 'red' });
  22. // Line charts taking their values from the tag
  23. $('.sparkline-1').sparkline();
  24. // Larger line charts for the docs
  25. $('.largeline').sparkline('html',
  26. { type: 'line', height: '2.5em', width: '4em' });
  27. // Customized line chart
  28. $('#linecustom').sparkline('html',
  29. {
  30. height: '1.5em', width: '8em', lineColor: '#f00', fillColor: '#ffa',
  31. minSpotColor: false, maxSpotColor: false, spotColor: '#77f', spotRadius: 3
  32. });
  33. // Bar charts using inline values
  34. $('.sparkbar').sparkline('html', { type: 'bar' });
  35. $('.barformat').sparkline([1, 3, 5, 3, 8], {
  36. type: 'bar',
  37. tooltipFormat: '{{value:levels}} - {{value}}',
  38. tooltipValueLookups: {
  39. levels: $.range_map({ ':2': 'Low', '3:6': 'Medium', '7:': 'High' })
  40. }
  41. });
  42. // Tri-state charts using inline values
  43. $('.sparktristate').sparkline('html', { type: 'tristate' });
  44. $('.sparktristatecols').sparkline('html',
  45. { type: 'tristate', colorMap: { '-2': '#fa7', '2': '#44f' } });
  46. // Composite line charts, the second using values supplied via javascript
  47. $('#compositeline').sparkline('html', { fillColor: false, changeRangeMin: 0, chartRangeMax: 10 });
  48. $('#compositeline').sparkline([4, 1, 5, 7, 9, 9, 8, 7, 6, 6, 4, 7, 8, 4, 3, 2, 2, 5, 6, 7],
  49. { composite: true, fillColor: false, lineColor: 'red', changeRangeMin: 0, chartRangeMax: 10 });
  50. // Line charts with normal range marker
  51. $('#normalline').sparkline('html',
  52. { fillColor: false, normalRangeMin: -1, normalRangeMax: 8 });
  53. $('#normalExample').sparkline('html',
  54. { fillColor: false, normalRangeMin: 80, normalRangeMax: 95, normalRangeColor: '#4f4' });
  55. // Discrete charts
  56. $('.discrete1').sparkline('html',
  57. { type: 'discrete', lineColor: 'blue', xwidth: 18 });
  58. $('#discrete2').sparkline('html',
  59. { type: 'discrete', lineColor: 'blue', thresholdColor: 'red', thresholdValue: 4 });
  60. // Bullet charts
  61. $('.sparkbullet').sparkline('html', { type: 'bullet' });
  62. // Pie charts
  63. $('.sparkpie').sparkline('html', { type: 'pie', height: '1.0em' });
  64. // Box plots
  65. $('.sparkboxplot').sparkline('html', { type: 'box' });
  66. $('.sparkboxplotraw').sparkline([1, 3, 5, 8, 10, 15, 18],
  67. { type: 'box', raw: true, showOutliers: true, target: 6 });
  68. // Box plot with specific field order
  69. $('.boxfieldorder').sparkline('html', {
  70. type: 'box',
  71. tooltipFormatFieldlist: ['med', 'lq', 'uq'],
  72. tooltipFormatFieldlistKey: 'field'
  73. });
  74. // click event demo sparkline
  75. $('.clickdemo').sparkline();
  76. $('.clickdemo').bind('sparklineClick', function (ev) {
  77. var sparkline = ev.sparklines[0],
  78. region = sparkline.getCurrentRegionFields();
  79. value = region.y;
  80. alert("Clicked on x=" + region.x + " y=" + region.y);
  81. });
  82. // mouseover event demo sparkline
  83. $('.mouseoverdemo').sparkline();
  84. $('.mouseoverdemo').bind('sparklineRegionChange', function (ev) {
  85. var sparkline = ev.sparklines[0],
  86. region = sparkline.getCurrentRegionFields();
  87. value = region.y;
  88. $('.mouseoverregion').text("x=" + region.x + " y=" + region.y);
  89. }).bind('mouseleave', function () {
  90. $('.mouseoverregion').text('');
  91. });
  92. }
  93. /**
  94. ** Draw the little mouse speed animated graph
  95. ** This just attaches a handler to the mousemove event to see
  96. ** (roughly) how far the mouse has moved
  97. ** and then updates the display a couple of times a second via
  98. ** setTimeout()
  99. **/
  100. function drawMouseSpeedDemo() {
  101. var mrefreshinterval = 500; // update display every 500ms
  102. var lastmousex = -1;
  103. var lastmousey = -1;
  104. var lastmousetime;
  105. var mousetravel = 0;
  106. var mpoints = [];
  107. var mpoints_max = 30;
  108. $('html').mousemove(function (e) {
  109. var mousex = e.pageX;
  110. var mousey = e.pageY;
  111. if (lastmousex > -1) {
  112. mousetravel += Math.max(Math.abs(mousex - lastmousex), Math.abs(mousey - lastmousey));
  113. }
  114. lastmousex = mousex;
  115. lastmousey = mousey;
  116. });
  117. var mdraw = function () {
  118. var md = new Date();
  119. var timenow = md.getTime();
  120. if (lastmousetime && lastmousetime != timenow) {
  121. var pps = Math.round(mousetravel / (timenow - lastmousetime) * 1000);
  122. mpoints.push(pps);
  123. if (mpoints.length > mpoints_max)
  124. mpoints.splice(0, 1);
  125. mousetravel = 0;
  126. $('#mousespeed').sparkline(mpoints, { width: mpoints.length * 2, tooltipSuffix: ' pixels per second' });
  127. }
  128. lastmousetime = timenow;
  129. setTimeout(mdraw, mrefreshinterval);
  130. };
  131. // We could use setInterval instead, but I prefer to do it this way
  132. setTimeout(mdraw, mrefreshinterval);
  133. }
  134. //=================================================================