index.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import React, { Component } from 'react';
  2. import { MiniArea } from '../Charts';
  3. import NumberInfo from '../NumberInfo';
  4. import styles from './index.less';
  5. function fixedZero(val) {
  6. return val * 1 < 10 ? `0${val}` : val;
  7. }
  8. function getActiveData() {
  9. const activeData = [];
  10. for (let i = 0; i < 24; i += 1) {
  11. activeData.push({
  12. x: `${fixedZero(i)}:00`,
  13. y: Math.floor(Math.random() * 200) + (i * 50),
  14. });
  15. }
  16. return activeData;
  17. }
  18. export default class ActiveChart extends Component {
  19. state = {
  20. activeData: getActiveData(),
  21. };
  22. componentDidMount() {
  23. this.timer = setInterval(() => {
  24. this.setState({
  25. activeData: getActiveData(),
  26. });
  27. }, 1000);
  28. }
  29. componentWillUnmount() {
  30. clearInterval(this.timer);
  31. }
  32. render() {
  33. const { activeData = [] } = this.state;
  34. return (
  35. <div className={styles.activeChart}>
  36. <NumberInfo subTitle="目标评估" total="有望达到预期" />
  37. <div style={{ marginTop: 32 }}>
  38. <MiniArea
  39. animate={false}
  40. line
  41. borderWidth={2}
  42. height={84}
  43. scale={{
  44. y: {
  45. tickCount: 3,
  46. },
  47. }}
  48. yAxis={{
  49. tickLine: false,
  50. label: false,
  51. title: false,
  52. line: false,
  53. }}
  54. data={activeData}
  55. />
  56. </div>
  57. {activeData && (
  58. <div className={styles.activeChartGrid}>
  59. <p>{[...activeData].sort()[activeData.length - 1].y + 200} 亿元</p>
  60. <p>{[...activeData].sort()[Math.floor(activeData.length / 2)].y} 亿元</p>
  61. </div>
  62. )}
  63. {activeData && (
  64. <div className={styles.activeChartLegend}>
  65. <span>00:00</span>
  66. <span>{activeData[Math.floor(activeData.length / 2)].x}</span>
  67. <span>{activeData[activeData.length - 1].x}</span>
  68. </div>
  69. )}
  70. </div>
  71. );
  72. }
  73. }