index.tsx 3.25 KB
Newer Older
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
1
2
3
4
5
6
import * as React from 'react';
import withStyles from '@material-ui/core/styles/withStyles';
import {Bar} from './Bar';
import BarGrid from './BarGrid';

const chartHeight = 200;
Oleksii Savran's avatar
Oleksii Savran committed
7
const measureMargin = chartHeight / 4;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
8
9
10
11
const defaultChartColor = '#88ba42';
const spacingQuota = 7;

const style = (theme) => ({
12
  flex: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
13
    display: 'flex' as 'flex',
14
    direction: 'ltr' as 'ltr',
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
15
16
17
18
  },
  /*tslint:disable*/
  measureSection: {
    '& > div': {
Oleksii Savran's avatar
Oleksii Savran committed
19
      marginBottom: `calc(${measureMargin}px - 1em)`,
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    },
    position: 'relative' as 'relative',
  },
  withAxes: {
    paddingLeft: '25px',
  },
  yLabel: {
    transform: 'rotate(270deg)',
    transformOrigin: `${chartHeight / 2}px ${chartHeight / 2}px`,
    position: 'absolute' as 'absolute',
    whiteSpace: 'pre' as 'pre',
    width: `${chartHeight}px`,
    textAlign: 'center' as 'center',
    left: 0,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
34
  },
35
36
37
38
  xLabel: {
    width: '100%',
    textAlign: 'center' as 'center',
  }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
39
40
41
  /*tslint:enable*/
});

42
43
44
45
46
47
48
49
interface IBarChartProps extends React.ClassAttributes<any> {
  classes: any;
  data: any[];
  xLabel?: string;
  yLabel?: string;
}

class BarChart extends React.Component<IBarChartProps, any> {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

  public state = {
    measures: [],
    scale: 0,
  };
  private getMeasures = () => {
    const {data} = this.props;
    const max = Math.max(...data.map((item) => item.value));

    const topMeasure = Math.round((max / 0.75) / Math.pow(10, `${max}`.length - 1)) * Math.pow(10, `${max}`.length - 1);

    return {measures: [topMeasure, topMeasure * 0.75, topMeasure * 0.5, topMeasure * 0.25], scale: chartHeight / topMeasure};
  }

  private renderBar = (item, index, dataLength) => {
    const {scale} = this.state;

    const barHeight = Math.round(item.value * scale);
    const width = 90 / dataLength;


    return (
      <Bar
        key={ `bar_${index}` }
        value={ item.key }
        color={ defaultChartColor }
        height={ barHeight }
        width={ width }
        x={ (width + spacingQuota / dataLength) * index + 1.5 }
        y={ chartHeight - barHeight }
      />
    );
  }

  public componentWillMount() {
    if (this.props.data) {
      this.setState(this.getMeasures());
    }
  }

  public componentWillReceiveProps(nextProps) {
    if (this.state.measures.length === 0 && this.props.data) {
      this.setState(this.getMeasures());
    }
  }

  public render() {
    this.getMeasures();

99
    const {data, classes, xLabel, yLabel} = this.props;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
100
101
102
103
104
105
106
    const {measures} = this.state;

    if (!data || data.length === 0) {
      return null;
    }

    return (
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
      <div>
        <div className={ classes.flex }>
          <div className={ `${classes.measureSection} ${yLabel ? classes.withAxes : ''}` }>
            { yLabel &&
              <div className={ classes.yLabel }>
                { yLabel }
              </div>
            }
            <div>{ measures[0] }</div>
            <div>{ measures[1] }</div>
            <div>{ measures[2] }</div>
            <div>{ measures[3] }</div>
          </div>
          <BarGrid>
            { data.map((item, i) => (this.renderBar(item, i, data.length))) }
          </BarGrid>
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
123
        </div>
124
125
126
127
128
        { xLabel &&
          <div className={ classes.xLabel }>
            { xLabel }
          </div>
        }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
129
130
131
132
133
134
135
136
      </div>
    );

  }
}


export default withStyles(style)(BarChart);