ESPTrainer built with Meteor and React

ScoreChart.jsx 1.6KB

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