VoteItem.jsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { h, render, Component } from 'preact'; // eslint-disable-line no-unused-vars
  2. import { generateKey } from '../../utilities/randomizer';
  3. import animatedBar from '../../d3/bar';
  4. import * as d3 from 'd3';
  5. // vote component
  6. export default class VoteItem extends Component {
  7. constructor (props) {
  8. super(props);
  9. this.key = generateKey('graph');
  10. }
  11. static get defaultProps () {
  12. return { width: 845 };
  13. }
  14. setData (props) {
  15. if (props.votesFraction !== undefined) { // eslint-disable-line
  16. const countup = animatedBar().options({
  17. startval: 0,
  18. endval: parseFloat(props.votesFraction),
  19. duration: 750,
  20. round: 100,
  21. width: props.width,
  22. classname: 'd3-increment',
  23. colors: [ '#E3E3E3', props.activeColor ],
  24. tag: 'p'
  25. });
  26. this.container.innerHTML = '';
  27. d3.select(this.container).call(countup);
  28. }
  29. }
  30. componentDidMount () {
  31. this.setData(this.props);
  32. }
  33. componentWillReceiveProps (nextProps) {
  34. if (JSON.stringify(nextProps) !== JSON.stringify(this.props)) {
  35. this.setData(nextProps);
  36. }
  37. }
  38. render () {
  39. return (
  40. <div className="vote__item" key={this.key}>
  41. <div className="vote__item__label">{this.props.label}</div>
  42. <div className="vote__item__graph" ref={ elem => (this.container = elem) }></div>
  43. </div>
  44. );
  45. }
  46. }