/
index.jsx
150 lines (124 loc) · 3.7 KB
/
index.jsx
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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* External dependencies
*/
const React = require( 'react' ),
PropTypes = require( 'prop-types' ),
noop = require( 'lodash/noop' ),
throttle = require( 'lodash/throttle' );
/**
* Internal dependencies
*/
const BarContainer = require( './bar-container' ),
touchDetect = require( 'lib/touch-detect' );
require( './style.scss' );
export default class ModuleChart extends React.Component {
static displayName = 'ModuleChart';
static propTypes = {
loading: PropTypes.bool,
data: PropTypes.array,
minTouchBarWidth: PropTypes.number,
minBarWidth: PropTypes.number,
barClick: PropTypes.func
};
static defaultProps = {
minTouchBarWidth: 42,
minBarWidth: 15,
barClick: noop
};
state = {
maxBars: 100, // arbitrarily high number. This will be calculated by resize method
width: 650
};
// Add listener for window resize
componentDidMount() {
this.resize = throttle( this.resize, 400 );
window.addEventListener( 'resize', this.resize );
this.resize();
}
// Remove listener
componentWillUnmount() {
window.removeEventListener( 'resize', this.resize );
}
UNSAFE_componentWillReceiveProps( nextProps ) {
if ( this.props.loading && ! nextProps.loading ) {
this.resize();
}
}
resize = () => {
const node = this.refs.chart;
let width = node.clientWidth - 82,
maxBars;
if ( touchDetect.hasTouch() ) {
width = ( width <= 0 ) ? 350 : width; // mobile safari bug with zero width
maxBars = Math.floor( width / this.props.minTouchBarWidth );
} else {
maxBars = Math.floor( width / this.props.minBarWidth );
}
this.setState( {
maxBars: maxBars,
width: width
} );
};
getYAxisMax = ( values ) => {
const max = Math.max.apply( null, values ),
operand = Math.pow( 10, ( max.toString().length - 1 ) );
let rounded = ( Math.ceil( ( max + 1 ) / operand ) * operand );
if ( rounded < 10 ) {
rounded = 10;
}
return rounded;
};
getData = () => {
let data = this.props.data;
data = data.slice( 0 - this.state.maxBars );
return data;
};
getValues = () => {
let data = this.getData();
data = data.map( function( item ) {
return item.value;
}, this );
return data;
};
isEmptyChart = ( values ) => {
values = values.filter( function( value ) {
return value > 0;
}, this );
return values.length === 0;
};
render() {
const values = this.getValues(),
yAxisMax = this.getYAxisMax( values ),
data = this.getData();
let emptyChart;
// If we have an empty chart, show a message
// @todo this message needs to either use a <Notice> or make a custom "chart__notice" class
if ( values.length && this.isEmptyChart( values ) ) {
emptyChart = (
<div className="dops-chart__empty">
<span className="dops-chart__empty_notice">
{ /* Translate this. */ }
No activity this period
</span>
</div>
);
}
return (
<div ref="chart" className="dops-chart">
<div className="dops-chart__y-axis-markers">
<div className="dops-chart__y-axis-marker is-hundred"></div>
<div className="dops-chart__y-axis-marker is-fifty"></div>
<div className="dops-chart__y-axis-marker is-zero"></div>
</div>
<div className="dops-chart__y-axis">
<div className="dops-chart__y-axis-width-fix">{ new Number( 100000 ).toLocaleString() }</div>
<div className="dops-chart__y-axis-label is-hundred">{ yAxisMax.toLocaleString() }</div>
<div className="dops-chart__y-axis-label is-fifty">{ ( yAxisMax / 2 ).toLocaleString() }</div>
<div className="dops-chart__y-axis-label is-zero">{ 0 }</div>
</div>
<BarContainer barClick={ this.props.barClick } data={ data } yAxisMax={ yAxisMax } chartWidth={ this.state.width } isTouch={ touchDetect.hasTouch() } />
{ emptyChart }
</div>
);
}
}