index.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import React from 'react';
  2. import styles from './styles.module.css';
  3. // import PropTypes from 'prop-types';
  4. /**
  5. * Stock Heatmap
  6. * @author Rounak Saha
  7. * © Copyright 2020, Rounak Saha
  8. */
  9. export default class StockHeatmap extends React.Component {
  10. canvasRef = React.createRef();
  11. data = [];
  12. shouldComponentUpdate(nextProps, nextState) {
  13. const shouldUpdate = this.props.width !== nextProps.width
  14. || this.props.height !== nextProps.height;
  15. if (shouldUpdate) this.updateHeatmapDimensions();
  16. return shouldUpdate;
  17. }
  18. /**
  19. * This function will be called if there is any dimension change on heatmap
  20. */
  21. updateHeatmapDimensions() {
  22. }
  23. /**
  24. * This method will be called after an update of internal data is performed.
  25. */
  26. updateHeatmap() {
  27. }
  28. /**
  29. * Set Data for the Heatmap to generate
  30. * @param {any[]} data The data to set
  31. */
  32. setData(data) {
  33. console.log('setdata called=', data);
  34. if (data && data.length > 0) {
  35. this.data = data;
  36. this.updateHeatmap();
  37. }
  38. }
  39. /**
  40. * Add as extra data to existing data array.
  41. * @param {any} data
  42. */
  43. addData(data) {
  44. if (typeof (data) === 'object') {
  45. this.data.push(data);
  46. this.updateHeatmap();
  47. }
  48. }
  49. /**
  50. * Render Function
  51. */
  52. render() {
  53. const { width, height } = this.props;
  54. console.log('heatmap rendered', width, height, this.data);
  55. return (
  56. <canvas ref={this.canvasRef} width={width || 300} height={height || 150} className={styles.mapCanvas}></canvas>
  57. );
  58. }
  59. }