heatmaps.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset='utf-8'>
  5. <title>Heatmaps - Handsontable</title>
  6. <!--
  7. Loading Handsontable (full distribution that includes all dependencies
  8. -->
  9. <link data-jsfiddle="common" rel="stylesheet" media="screen" href="../dist/handsontable.css">
  10. <link data-jsfiddle="common" rel="stylesheet" media="screen" href="../dist/pikaday/pikaday.css">
  11. <script data-jsfiddle="common" src="../dist/pikaday/pikaday.js"></script>
  12. <script data-jsfiddle="common" src="../dist/moment/moment.js"></script>
  13. <script data-jsfiddle="common" src="../dist/zeroclipboard/ZeroClipboard.js"></script>
  14. <script data-jsfiddle="common" src="../dist/numbro/numbro.js"></script>
  15. <script data-jsfiddle="common" src="../dist/numbro/languages.js"></script>
  16. <script data-jsfiddle="common" src="../dist/handsontable.js"></script>
  17. <!--
  18. Loading demo dependencies. They are used here only to enhance the examples on this page
  19. -->
  20. <link data-jsfiddle="common" rel="stylesheet" media="screen" href="css/samples.css?20140331">
  21. <script src="js/samples.js"></script>
  22. <script src="js/highlight/highlight.pack.js"></script>
  23. <link rel="stylesheet" media="screen" href="js/highlight/styles/github.css">
  24. <link rel="stylesheet" href="css/font-awesome/css/font-awesome.min.css">
  25. <script data-jsfiddle="common" src="bower_components/chroma-js/chroma.min.js"></script>
  26. <!--
  27. Facebook open graph. Don't copy this to your project :)
  28. -->
  29. <meta property="og:title" content="Heatmaps for values in a column">
  30. <meta property="og:description"
  31. content="">
  32. <meta property="og:url" content="http://handsontable.com/demo/heatmaps.html">
  33. <meta property="og:image" content="http://handsontable.com/demo/image/og-image.png">
  34. <meta property="og:image:type" content="image/png">
  35. <meta property="og:image:width" content="409">
  36. <meta property="og:image:height" content="164">
  37. <link rel="canonical" href="http://handsontable.com/demo/heatmaps.html">
  38. <!--
  39. Google Analytics for GitHub Page. Don't copy this to your project :)
  40. -->
  41. <script src="js/ga.js"></script>
  42. </head>
  43. <body>
  44. <div class="wrapper">
  45. <div class="wrapper-row">
  46. <div id="global-menu-clone">
  47. <h1><a href="../index.html">Handsontable</a></h1>
  48. </div>
  49. <div id="container">
  50. <div class="columnLayout">
  51. <div class="rowLayout">
  52. <div class="descLayout">
  53. <div class="pad" data-jsfiddle="example1">
  54. <a name="lazy"></a>
  55. <h2>Heatmaps for values in a column</h2>
  56. <p>
  57. The following demo shows an example of using heatmaps for the values in tha grid's columns.
  58. </p>
  59. <p>
  60. Changing the values in the grid automatically recalculates the font color for the whole column, thereby
  61. updating a heatmap.
  62. </p>
  63. <p>
  64. The dynamic color scale calculation is done using the excellent <a href="http://driven-by-data.net/about/chromajs">Chroma.js</a>.
  65. </p>
  66. <div id="example1"></div>
  67. </div>
  68. </div>
  69. <div class="codeLayout">
  70. <div class="pad">
  71. <div class="jsFiddle">
  72. <button class="jsFiddleLink" data-runfiddle="example1">Edit in jsFiddle</button>
  73. </div>
  74. <script data-jsfiddle="example1">
  75. var data = [
  76. [2002, 190251, 5090, 195341],
  77. [2003, 224495, 6486, 230981],
  78. [2004, 254044, 6765, 260809],
  79. [2005, 254099, 7521, 261620],
  80. [2006, 271108, 9449, 280557],
  81. [2007, 280565, 11714, 292279],
  82. [2008, 284120, 11292, 295412],
  83. [2009, 279742, 11468, 291210],
  84. [2010, 290411, 11806, 302217],
  85. [2011, 290652, 10891, 301543],
  86. [2012, 283863, 10402, 294265],
  87. [2013, 267646, 10104, 255850]
  88. ],
  89. container = document.getElementById('example1'),
  90. lastChange = null,
  91. heatmap,
  92. heatmapScale,
  93. hot;
  94. heatmapScale = chroma.scale(['#17F556', '#ED6D47']);
  95. hot = new Handsontable(container,{
  96. data: data,
  97. colHeaders: ["Year", "Domestic Flights", "International Flights", "Total Flights"],
  98. columns: [
  99. {
  100. type: 'numeric'
  101. },
  102. {
  103. type: 'numeric',
  104. format: '0,0',
  105. renderer: heatmapRenderer
  106. },
  107. {
  108. type: 'numeric',
  109. format: '0,0',
  110. renderer: heatmapRenderer
  111. },
  112. {
  113. type: 'numeric',
  114. format: '0,0',
  115. renderer: heatmapRenderer
  116. },
  117. ],
  118. afterLoadData: updateHeatmap,
  119. beforeChangeRender: updateHeatmap
  120. });
  121. function updateHeatmap(change, source) {
  122. if (change) {
  123. heatmap[change[0][1]] = generateHeatmapData.call(this, change[0][1]);
  124. } else {
  125. heatmap = [];
  126. for(var i = 1, colCount = this.countCols(); i < colCount ; i++) {
  127. heatmap[i] = generateHeatmapData.call(this, i);
  128. }
  129. }
  130. }
  131. function point(min, max, value) {
  132. return (value - min) / (max - min);
  133. }
  134. function generateHeatmapData(colId) {
  135. var values = this.getDataAtCol(colId);
  136. return {
  137. min: Math.min.apply(null, values),
  138. max: Math.max.apply(null, values)
  139. };
  140. }
  141. function heatmapRenderer(instance, td, row, col, prop, value, cellProperties) {
  142. Handsontable.renderers.TextRenderer.apply(this, arguments);
  143. if (heatmap[col]) {
  144. td.style.backgroundColor = heatmapScale(point(heatmap[col].min, heatmap[col].max, parseInt(value, 10))).hex();
  145. td.style.textAlign = 'right';
  146. td.style.fontWeight = 'bold';
  147. }
  148. }
  149. </script>
  150. </div>
  151. </div>
  152. </div>
  153. <div class="footer-text">
  154. </div>
  155. </div>
  156. </div>
  157. </div>
  158. </div>
  159. <div id="outside-links-wrapper"></div>
  160. </body>
  161. </html>