heatContainer.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import LineHeatmap from "./lineHeatmap";
  2. import "nouislider";
  3. const template = `<div>
  4. <div class="line-heatmap-controls">
  5. <p>Cade o tempo no log?</p>
  6. <div id="line-heatmap-slider"></div>
  7. </div>
  8. <div id="line-heatmap-canvas">
  9. </div>
  10. </div>
  11. `;
  12. const stateObject = {};
  13. function init (elementId, logString) {
  14. const el = document.getElementById(elementId);
  15. el.innerHTML = template;
  16. const logData = logTransform(logString);
  17. // create container
  18. const heatmap = new LineHeatmap('line-heatmap-canvas');
  19. heatmap.create();
  20. heatmap.setTrackData(logData);
  21. const html5Slider = document.getElementById('line-heatmap-slider');
  22. noUiSlider.create(html5Slider, {
  23. start: [1, logData.length],
  24. connect: true,
  25. margin: 1,
  26. step: 1,
  27. range: {
  28. 'min': 1,
  29. 'max': logData.length
  30. }
  31. });
  32. stateObject['container'] = el;
  33. stateObject['heatmap'] = heatmap;
  34. stateObject['data'] = logData;
  35. stateObject['slider'] = html5Slider;
  36. html5Slider.noUiSlider.on('update', function (values, handle) {
  37. const logValues = this.get();
  38. console.log(logValues);
  39. const newData = stateObject['data'].slice(+logValues[0]-1, +logValues[1] + 1);
  40. console.log(newData);
  41. stateObject['heatmap'].setTrackData(newData);
  42. });
  43. }
  44. function updateLogString (logString) {
  45. const logData = logTransform(logString);
  46. stateObject['data'] = logData;
  47. stateObject['heatmap'].noUiSlider.updateOptions({
  48. range: {
  49. 'min': 1,
  50. 'max': logData.length
  51. }
  52. });
  53. }
  54. function logTransform (logString) {
  55. const data = logString.split("\n")
  56. .filter(v => v.split(',').length > 3)
  57. .map(v => {
  58. const o = JSON.parse('[' + v + ']');
  59. return {
  60. x: o[0],
  61. y: o[1],
  62. value: 1
  63. };
  64. });
  65. return data;
  66. }
  67. export default {
  68. init,
  69. updateLogString
  70. }