Passed
Push — master ( e3086e...7c86e8 )
by Guangyu
04:24
created

src/components/MyEMS/Meter/RealtimeChart.js   A

Complexity

Total Complexity 3
Complexity/F 1

Size

Lines of Code 157
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 103
mnd 0
bc 0
fnc 3
dl 0
loc 157
rs 10
bpm 0
cpm 1
noi 0
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A RealtimeChart.componentWillUnmount 0 4 1
B RealtimeChart.render 0 45 1
A RealtimeChart.componentDidMount 0 15 1
1
import React, { Component } from 'react';
2
import { Line } from 'react-chartjs-2';
3
import range from 'lodash/range';
4
import { Card, CardHeader, CardBody, ListGroup, ListGroupItem } from 'reactstrap';
5
import { rgbaColor } from '../../../helpers/utils';
6
import { withTranslation } from 'react-i18next';
7
8
9
const energyTrendLog = [
10
  11183,
11
  12116,
12
  13176,
13
  14172,
14
  15166,
15
  16161,
16
  17164,
17
  18159,
18
  19172,
19
  20173,
20
  21184,
21
  22163,
22
  23099,
23
  24173,
24
  25183,
25
  26167,
26
  27160,
27
  28183,
28
  29163,
29
  30176,
30
  31172,
31
  32166,
32
  33173,
33
  34188,
34
  35175
35
];
36
const dividerBorder = '1px solid rgba(255, 255, 255, 0.15)';
37
const listItemBorderColor = 'rgba(255, 255, 255, 0.05)';
38
39
const chartOptions = {
40
  legend: { display: false },
41
  scales: {
42
    yAxes: [
43
      {
44
        display: true,
45
        stacked: false
46
      }
47
    ],
48
    xAxes: [
49
      {
50
        stacked: false,
51
        ticks: { display: false },
52
        categoryPercentage: 1.0,
53
        gridLines: {
54
          color: rgbaColor('#fff', 0.1),
55
          display: true
56
        }
57
      }
58
    ]
59
  }
60
};
61
62
class RealtimeChart extends Component {
63
  refreshInterval;
64
  refreshTimeout;
65
  state = {
66
    energyTrendLog,
67
    currentEnergyValue: energyTrendLog[energyTrendLog.length - 1],
68
    chartData: {
69
      labels: range(1, 26),
70
      datasets: [
71
        {
72
          label: 'Users',
73
          backgroundColor: rgbaColor('#fff', 0.3),
74
          //data: []
75
        }
76
      ]
77
    },
78
    pointList: [{'id': 1231, 'name': '电流a (A)', 'value': 18.098},
79
        {'id': 1232, 'name': '电流b (A)', 'value': 15.001},
80
        {'id': 1233, 'name': '电流c (A)', 'value': 16.257},
81
        {'id': 1234, 'name': '有功瞬时功率 a (kW)', 'value': 25.98},
82
        {'id': 1235, 'name': '有功瞬时功率 b (kW)', 'value': 21.76},
83
        {'id': 1236, 'name': '有功瞬时功率 c (kW)', 'value': 29.12},
84
      ],
85
  };
86
87
  componentWillUnmount() {
88
    clearInterval(this.refreshInterval);
89
    clearTimeout(this.refreshTimeout);
90
  }
91
92
  componentDidMount() {
93
    this.refreshInterval = setInterval(() => {
94
      const currentEnergyValue = this.state.currentEnergyValue + Math.floor(Math.random() * (120 - 60) + 60);
95
      
96
      const energyTrendLog = [...this.state.energyTrendLog];
97
      energyTrendLog.shift();
98
      this.setState({ energyTrendLog }, () => {
99
        this.refreshTimeout = setTimeout(() => {
100
          const energyTrendLog = [...this.state.energyTrendLog];
101
          energyTrendLog.push(currentEnergyValue);
102
          this.setState({ energyTrendLog, currentEnergyValue });
103
        }, 500);
104
      });
105
    }, 1 * 1000);
106
  }
107
108
  render() {
109
    const { t } = this.props;
110
    const chartData = {
111
      ...this.state.chartData,
112
      datasets: [
113
        {
114
          ...this.state.chartData.datasets[0],
115
          data: this.state.energyTrendLog
116
        }
117
      ]
118
    };
119
120
    return (
121
      <Card className="h-100 bg-gradient">
122
        <CardHeader className="bg-transparent">
123
          <h5 className="text-white">{this.props.title}</h5>
124
          <div className="real-time-user display-1 font-weight-normal text-white">{this.state.currentEnergyValue}</div>
125
        </CardHeader>
126
        <CardBody className="text-white fs--1">
127
          <p className="pb-2" style={{ borderBottom: dividerBorder }}>
128
          {t('Realtime Value of Energy Value Point UNIT', {'UNIT': 'kWh'})}
129
          </p>
130
          <Line data={chartData} options={chartOptions} width={10} height={4} />
131
          <ListGroup flush className="mt-4">
132
          
133
            <ListGroupItem
134
              className="bg-transparent d-flex justify-content-between px-0 py-1 font-weight-semi-bold border-top-0"
135
              style={{ borderColor: listItemBorderColor }}
136
            >
137
              <p className="mb-0">{t('Related Parameters')}</p>
138
              <p className="mb-0">{t('Realtime Value')}</p>
139
            </ListGroupItem>
140
            {this.state.pointList.map(pointItem => (
141
              <ListGroupItem key={pointItem['id']}
142
                className="bg-transparent d-flex justify-content-between px-0 py-1"
143
                style={{ borderColor: listItemBorderColor }}
144
              >
145
                <p className="mb-0">{pointItem['name']}</p>
146
                <p className="mb-0">{pointItem['value']}</p>
147
              </ListGroupItem>
148
            ))}
149
          </ListGroup>
150
        </CardBody>
151
      </Card>
152
    );
153
  }
154
}
155
156
export default  withTranslation()(RealtimeChart);
157