-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
179 lines (145 loc) · 5.75 KB
/
index.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import Measure from 'react-measure';
import React, { Component } from 'react';
import autobind from 'react-autobind';
import moment from 'moment';
import { connect } from 'react-redux';
import { histogram } from 'd3-array';
import { line } from 'd3-shape';
import { nest } from 'd3-collection';
import { scaleLinear, scaleTime } from 'd3-scale';
import { timeDay } from 'd3-time';
import { prop, stringifyDateShort } from '../../utils';
import { GridY, GridX, AxisY, AxisX } from '../chart-utils';
const stringifyPercent = value => `${parseInt(value * 100)}%`;
const avgProp = (arr = [], key) => {
if (!arr.length) {
return 0;
}
return arr.map(prop(key)).reduce((a, b) => a + b, 0) / arr.length;
};
const FocusChart = ({ width, height, scaleX, scaleY, histogram }) => {
const calculatePath = key => line().x(d => scaleX(d.start)).y(d => scaleY(d[key]));
const pathFocusPercent = calculatePath('focusPercent')(histogram);
const pathTotalTime = calculatePath('durationPercent')(histogram);
return (
<svg width={width} height={height} className="focus__chart">
<path d={pathFocusPercent} className="focus__chart-path focus__chart-focus-duration-path" />
<path d={pathTotalTime} className="focus__chart-path focus__chart-total-time-path" />
<GridX scale={scaleX} ticks={scaleX.ticks()} height={height} className="focus__chart-grid-x-tick" />
<GridY scale={scaleY} ticks={scaleY.ticks()} width={width} className="focus__chart-grid-y-tick" />
<g transform={`translate(0, ${height - 6})`}>
<AxisX
scale={scaleX}
ticks={scaleX.ticks(width / 80)}
className="focus__chart-axis-x-tick"
stringifyFn={stringifyDateShort}
/>
</g>
<AxisY
scale={scaleY}
ticks={scaleY.ticks()}
className="focus__chart-axis-y-tick"
stringifyFn={stringifyPercent}
/>
</svg>
);
};
class Focus extends Component {
constructor() {
super();
autobind(this);
this.state = {
dimensions: {
width: 0,
height: 0
}
};
}
onMeasure(dimensions) {
this.setState({ dimensions });
}
render() {
const { dimensions } = this.state;
const { events } = this.props;
const chartMargin = 20;
const width = Math.floor(dimensions.width) - chartMargin;
const height = Math.floor(dimensions.height) - chartMargin;
const startDate = events[0].start;
const endDate = events[events.length - 1].end;
const margin = 26;
const scaleX = scaleTime().domain([startDate, endDate]).range([margin, width - margin]).nice();
const scaleY = scaleLinear().domain([0, 1]).range([height - margin, margin]).nice();
const histogramScale = scaleTime().domain([startDate, endDate]).nice(timeDay);
const histogramTicks = histogramScale.ticks(timeDay, 1);
const calculateHistogram = histogram().value(prop('start')).domain(scaleX.domain()).thresholds(histogramTicks);
const eventsHistogram = calculateHistogram(events);
const focusHistogram = eventsHistogram
// .slice(1, eventsHistogram.length - 1)
.slice(1)
.map(bin => {
const durationTotal = bin.reduce((acc, { duration }) => acc + duration, 0);
const durationBin = bin.length > 0 ? bin[bin.length - 1].end.getTime() - bin[0].start.getTime() : 0;
return {
start: bin.x0,
end: bin.x1,
durationTotal,
durationPercent: durationTotal / (8 * 60 * 60 * 1000),
durationBin,
focusPercent: durationTotal > 0 && durationBin > 0 ? durationTotal / durationBin : 0,
bin
};
});
const nestedHistogram = nest()
.key(d => moment(d.start).format('YYYY-MM'))
.rollup(d => ({
start: d[0].start,
bins: d,
durationPercent: avgProp(d, 'durationPercent'),
focusPercent: avgProp(d, 'focusPercent')
}))
.entries(focusHistogram.filter(d => d.focusPercent > 0))
.map(prop('value'));
const calcAverageProp = key => {
return focusHistogram.map(prop(key)).filter(_ => _ > 0).reduce((a, b) => a + b, 0) / focusHistogram.length;
};
const averageFocusPercent = calcAverageProp('focusPercent');
const averageDurationPercent = calcAverageProp('durationPercent');
return (
<div className="focus__content">
<div className="focus__label">
<div className="focus__label-wrapper">
<svg width={40} height={10} className="focus__label-chart-label">
<path d="M0 5 L40 5" className="focus__chart-path focus__chart-focus-duration-path" />
</svg>
<span className="focus__label-text">average daily focus</span>
<span className="focus__label-value">
{stringifyPercent(averageFocusPercent)}
</span>
</div>
<div className="focus__label-wrapper">
<svg width={40} height={10} className="focus__label-chart-label">
<path d="M0 5 L40 5" className="focus__chart-path focus__chart-total-time-path" />
</svg>
<span className="focus__label-text">average daily time</span>
<span className="focus__label-value">
{stringifyPercent(averageDurationPercent)}
</span>
</div>
</div>
<Measure onMeasure={this.onMeasure}>
<div className="focus__chart-wrapper">
{width > 0 &&
<FocusChart width={width} height={height} scaleX={scaleX} scaleY={scaleY} histogram={nestedHistogram} />}
</div>
</Measure>
</div>
);
}
}
const mapStateToProps = state => {
const events = state.get('events').valueSeq().toJS().sort((a, b) => {
return a.start.getTime() - b.start.getTime();
});
return { events };
};
export default connect(mapStateToProps)(Focus);