src/components/MyEMS/common/LineChart.js   A
last analyzed

Complexity

Total Complexity 5
Complexity/F 5

Size

Lines of Code 112
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 86
mnd 4
bc 4
fnc 1
dl 0
loc 112
rs 10
bpm 4
cpm 5
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A LineChart.js ➔ data 0 16 5
1
import React, { useState, useContext } from 'react';
2
import { Row, Col, Card, CardBody, CustomInput } from 'reactstrap';
3
import { Line } from 'react-chartjs-2';
4
import { rgbaColor, themeColors, isIterableArray } from '../../../helpers/utils';
5
import AppContext from '../../../context/Context';
6
7
const LineChart = ({
8
  reportingTitle,
9
  baseTitle,
10
  labels,
11
  data,
12
  options
13
}) => {
14
  const [selectedLabel, setSelectedLabel] = useState('a0');
15
  const [option, setOption] = useState('a0');
16
  const { isDark } = useContext(AppContext);
17
18
  const config = {
19
    data(canvas) {
20
      const ctx = canvas.getContext('2d');
21
      const gradientFill = isDark
22
        ? ctx.createLinearGradient(0, 0, 0, ctx.canvas.height)
23
        : ctx.createLinearGradient(0, 0, 0, 250);
24
      gradientFill.addColorStop(0, isDark ? 'rgba(44,123,229, 0.5)' : 'rgba(255, 255, 255, 0.3)');
25
      gradientFill.addColorStop(1, isDark ? 'transparent' : 'rgba(255, 255, 255, 0)');
26
27
      return {
28
        labels: labels[selectedLabel],
29
        datasets: [
30
          {
31
            borderWidth: 2,
32
            data: data[option],
33
            borderColor: rgbaColor(isDark ? themeColors.primary : '#fff', 0.8),
34
            backgroundColor: gradientFill
35
          }
36
        ]
37
      };
38
    },
39
    options: {
40
      legend: { display: false },
41
      tooltips: {
42
        mode: 'x-axis',
43
        xPadding: 20,
44
        yPadding: 10,
45
        displayColors: false,
46
        callbacks: {
47
          label: tooltipItem => `${labels[selectedLabel][tooltipItem.index]} - ${tooltipItem.yLabel}`,
48
          title: () => null
49
        }
50
      },
51
      hover: { mode: 'label' },
52
      scales: {
53
        xAxes: [
54
          {
55
            ticks: {
56
              fontColor: rgbaColor('#fff', 0.7),
57
              fontStyle: 600
58
            },
59
            gridLines: {
60
              color: rgbaColor('#fff', 0.1),
61
              zeroLineColor: rgbaColor('#fff', 0.1),
62
              lineWidth: 1
63
            }
64
          }
65
        ],
66
        yAxes: [
67
          {
68
            display: true,
69
            gridLines: {
70
              color: rgbaColor('#fff', 0.1)
71
            }
72
          }
73
        ]
74
      }
75
    }
76
  };
77
78
  return (
79
    <Card className="mb-3">
80
      <CardBody className="rounded-soft bg-gradient">
81
        <Row className="text-white align-items-center no-gutters">
82
          <Col>
83
            <h4 className="text-white mb-0">{reportingTitle}</h4>
84
            <p className="fs--1 font-weight-semi-bold">
85
              {baseTitle}
86
            </p>
87
          </Col>
88
          {isIterableArray(options) &&
89
            <Col xs="auto" className="d-none d-sm-block">
90
              <CustomInput
91
                id="ddd"
92
                type="select"
93
                bsSize="sm"
94
                className="mb-3 shadow"
95
                value={option}
96
                onChange={({ target }) => {setOption(target.value); setSelectedLabel(target.value);}}
97
              >
98
                {options.map(({ value, label }) => (
99
                    <option key={value} value={value}>{label}</option>
100
                  ))}
101
              </CustomInput>
102
            </Col>
103
          }   
104
        </Row>
105
        <Line data={config.data} options={config.options} width={1618} height={375} />
106
      </CardBody>
107
    </Card>
108
  );
109
};
110
111
export default LineChart;
112