class Sparkline extends React.Component {
constructor(props) {
super(props)
this.xScale = d3.scaleLinear()
this.yScale = d3.scaleLinear()
this.line = d3.line()
this._updateDataTransforms(props)
}
componentDidMount() {
const self = this
d3.select(ReactDOM.findDOMNode(this.refs.svg))
.on('mousemove', function() {
self._onMouseMove(d3.mouse(this)[0])
})
.on('mouseleave', function() {
self._onMouseMove(null)
})
}
componentWillReceiveNewProps(newProps) {
this._updateDataTransforms(newProps)
}
_updateDataTransforms(props) {
const { xAccessor, yAccessor, width, height, data } = props
this.xScale.domain([0, data.length]).range([0, width])
this.yScale.domain([0, 10]).range([height, 0])
this.line
.x((d, i) => this.xScale(xAccessor(d, i)))
.y((d, i) => this.yScale(yAccessor(d, i)))
this.bisectByX = d3.bisector(xAccessor).left
}
_onMouseMove(xPixelPos) {
const { data, onHover } = this.props
if (xPixelPos === null) {
onHover(null, null)
} else {
const xValue = this.xScale.invert(xPixelPos)
const i = this.bisectByX(data, xValue, 1)
onHover(data[i], i)
}
}
render() {
const { data, width, height, xAccessor, hovered } = this.props
const hoveredRender = hovered ? (