Last active
November 11, 2024 08:47
-
-
Save cristiandley/46157c292a97c99986f8bd98fcf89d0d to your computer and use it in GitHub Desktop.
Heatmapjs React integration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import _ from 'lodash'; | |
import h337 from 'heatmap.js'; | |
import ReactDOM, { render } from 'react-dom'; | |
import React, {Component, PropTypes} from 'react'; | |
class ReactHeatmap extends Component { | |
constructor(props, context) { | |
super(props, context); | |
this.state = { cfg: null }; | |
} | |
componentDidMount(){ | |
const { style, data, config } = this.props; | |
let c = config || {}; | |
let _container = ReactDOM.findDOMNode(this); | |
let defaultCfg = { | |
width: style.width.replace('px','') || _container.offsetWidth, | |
height: style.height.replace('px','') || _container.offsetHeight, | |
}; | |
let _cfg = _.merge( defaultCfg, c ); | |
_cfg.container = _container; | |
this.heatmapInstance = h337.create( _cfg ); | |
this.setState({ cfg: _cfg }); | |
this.heatmapInstance.setData( data ); | |
} | |
componentWillReceiveProps(nextProps){ | |
return nextProps != this.props; | |
} | |
shouldComponentUpdate(nextProps){ | |
return nextProps != this.props; | |
} | |
render(){ | |
return ( | |
<div ref="react-heatmap"></div> | |
); | |
} | |
} | |
export default ReactHeatmap; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment