File size: 1,852 Bytes
f2bee8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import React from 'react';
import PropTypes from 'prop-types';
import styles from './meter.css';

const Meter = props => {
    const {
        level,
        width,
        height
    } = props;

    const nGreen = 11;
    const nYellow = 5;
    const nRed = 3;

    const nBars = nGreen + nYellow + nRed;

    const barSpacing = 2.5;
    const barRounding = 3;
    const barHeight = (height - (barSpacing * (nBars + 1))) / nBars;

    const nBarsToMask = nBars - Math.floor(level * nBars);
    const scale = ((nBarsToMask * (barHeight + barSpacing)) + (barSpacing / 2)) / height;

    return (
        <div
            className={styles.maskContainer}
            style={{height: `${height}px`}}
        >
            <svg
                className={styles.container}
                height={height}
                width={width}
            >
                {Array(nBars).fill(0)
                    .map((value, index) => (
                        <rect
                            className={index < nGreen ? styles.green :
                                (index < nGreen + nYellow ? styles.yellow : styles.red)}
                            height={barHeight}
                            key={index}
                            rx={barRounding}
                            ry={barRounding}
                            width={width - 2}
                            x={1}
                            y={height - ((barSpacing + barHeight) * (index + 1))}
                        />
                    ))}
            </svg>
            <div
                className={styles.mask}
                style={{
                    transform: `scaleY(${scale})`
                }}
            />
        </div>
    );
};

Meter.propTypes = {
    height: PropTypes.number,
    level: PropTypes.number,
    width: PropTypes.number
};

export default Meter;