ESPTrainer built with Meteor and React

ScoreChart.jsx 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import React, { Component, PropTypes} from 'react';
  2. import Chart from 'chart.js';
  3. export default class ScoreChart extends Component {
  4. constructor(props) {
  5. super(props);
  6. Chart.defaults.global.legend.labels.fontColor = '#fff';
  7. this.chart = null;
  8. this.scoreScales = {
  9. yAxes: [{
  10. ticks: {
  11. fontColor: "#fff",
  12. stepSize: 6,
  13. max: 24,
  14. }
  15. }],
  16. xAxes: [{
  17. ticks: {
  18. fontColor: "#fff",
  19. fontSize: 10,
  20. }
  21. }]
  22. };
  23. }
  24. shouldComponentUpdate(nextProps) {
  25. return this.props.data !== nextProps.data;
  26. }
  27. componentDidMount() {
  28. let labels = Object.keys(this.props.data);
  29. const data = labels.map(key => this.props.data[key]);
  30. labels = labels.map(ts => moment.unix(ts).format('DD/MM/YYYY H:ss'));
  31. this.chart = new Chart(this.canvas, {
  32. type: 'line',
  33. data: {
  34. labels: labels,
  35. datasets: [{
  36. label: 'Progression',
  37. data: data,
  38. backgroundColor: 'rgba(33, 150, 243, 0.2)',
  39. borderColor: 'rgba(33, 150, 243, 1)',
  40. borderWidth: 1
  41. }]
  42. },
  43. options: { scales: this.scoreScales }
  44. });
  45. }
  46. componentDidUpdate() {
  47. let labels = Object.keys(this.props.data);
  48. const data = labels.map(key => this.props.data[key]);
  49. labels = labels.map(ts => moment.unix(ts).format('DD/MM/YYYY H:ss'));
  50. this.chart.data.labels = labels;
  51. this.chart.data.datasets[0].data = data;
  52. this.chart.update();
  53. }
  54. render() {
  55. return <canvas ref={(node) => this.canvas = node} />;
  56. }
  57. }
  58. Chart.propTypes = {
  59. id: PropTypes.string.isRequired,
  60. data: PropTypes.object.isRequired
  61. };