src/components/dashboard/PaymentsLineChart.js   A
last analyzed

Complexity

Total Complexity 5
Complexity/F 5

Size

Lines of Code 108
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

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