GraphItem.jsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { h, render, Component } from 'preact'; // eslint-disable-line no-unused-vars
  2. import d3Linegraph from '../../d3/main';
  3. import * as d3 from 'd3';
  4. // displays the current Graph Item depending on mode
  5. export default class GraphItem extends Component {
  6. // update data and replace it depending on group
  7. updateData () {
  8. const options = {
  9. entries: this.props.dataSet.data,
  10. questionGroup: this.props.dataSet.gruppe,
  11. points: this.props.points,
  12. // TODO: define in database / "offline content" along with graph data
  13. xAxisUnit: 'Zeit (Quartal)',
  14. yAxisUnit: 'Gewinn in Millionen Euro'
  15. };
  16. if (this.props.dataSet.gruppe === 1 || this.props.dataSet.gruppe === 3) {
  17. options.konkurrent = this.props.dataSet.konkurrent.toUpperCase();
  18. }
  19. if (this.props.dataSet.gruppe === 3) {
  20. // Initial (selected) range
  21. // TODO: change api to reflect this (reference/selected/solution) structure *per graph*
  22. // TODO: add to documentation that reference range is hard coded here
  23. options.referenceRange = {
  24. lowerBound: 'Q1/2015',
  25. upperBound: 'Q4/2015'
  26. };
  27. // Solution range
  28. options.solutionRange = {
  29. lowerBound: this.props.dataSet.untereGrenze,
  30. upperBound: this.props.dataSet.obereGrenze,
  31. gradient: {
  32. min: this.props.dataSet.anstiegUntereGrenze,
  33. max: this.props.dataSet.anstiegObereGrenze
  34. }
  35. };
  36. options.setDataMethod = this.props.setData;
  37. }
  38. const draw = d3Linegraph().options(options); // create and initialise d3 base element
  39. this.container.innerHTML = ''; // clear container
  40. d3.select(this.container).call(draw); // call / run d3 base element
  41. }
  42. // LIFECYCLE methods
  43. componentDidMount () {
  44. this.updateData();
  45. }
  46. shouldComponentUpdate (nextProps) {
  47. const isQuestion = nextProps.chartMode === 'question';
  48. const isFirstGroup = nextProps.dataSet.gruppe === 1;
  49. const isNextQuestion = nextProps.currentQuestion !== this.props.currentQuestion;
  50. return (isQuestion && isFirstGroup) || isNextQuestion;
  51. }
  52. componentDidUpdate () {
  53. this.updateData();
  54. }
  55. // output entry point for d3js module
  56. render () {
  57. return (<div className="chart chart--centered" ref={ elem => (this.container = elem) }></div>);
  58. }
  59. }