-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathindex.js
191 lines (161 loc) · 5.5 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
180
181
182
183
184
185
186
187
188
189
190
191
import defaultsDeep from 'lodash.defaultsdeep';
import axis from './axis';
import { getBreakpointLabel } from './breakpoint';
import bounds from './bounds';
import defaultConfiguration from './config';
import dropLine from './dropLine';
import zoom from './zoom';
import { getDomainTransform } from './zoom';
import { addMetaballsDefs } from './metaballs';
import './style.css';
import { withinRange } from './withinRange';
// do not export anything else here to keep window.eventDrops as a function
export default ({
d3 = window.d3,
global = window,
...customConfiguration
}) => {
const initChart = selection => {
selection.selectAll('svg').remove();
const root = selection.selectAll('svg').data(selection.data());
root.exit().remove();
const config = defaultsDeep(
customConfiguration || {},
defaultConfiguration(d3)
);
const {
id,
drops,
zoom: zoomConfig,
drop: { onClick, onMouseOut, onMouseOver },
metaballs,
label: { width: labelWidth },
line: { height: lineHeight },
range: { start: rangeStart, end: rangeEnd },
margin,
breakpoints,
} = config;
const getEvent = () => d3.event; // keep d3.event mutable see https://github.com/d3/d3/issues/2733
// Follow margins conventions (https://bl.ocks.org/mbostock/3019563)
const width = selection.node().clientWidth - margin.left - margin.right;
const xScale = d3
.scaleTime()
.domain([rangeStart, rangeEnd])
.range([0, width - labelWidth]);
chart._scale = xScale;
chart.currentBreakpointLabel = getBreakpointLabel(
breakpoints,
global.innerWidth
);
const svg = root
.enter()
.append('svg')
.attr('width', width)
.classed('event-drop-chart', true);
const height = parseFloat(svg.style('height'));
if (id) {
svg.attr('id', id);
}
if (zoomConfig) {
const zoomObject = d3.zoom();
svg.call(
zoom(
d3,
svg,
config,
zoomObject,
xScale,
draw,
getEvent,
width,
height
)
);
chart._zoomToDomain = (domain, duration, delay, ease) => {
const zoomIdentity = getDomainTransform(
d3,
config,
zoomObject,
domain,
xScale,
width
);
svg
.transition()
.ease(ease)
.delay(delay)
.duration(duration)
.call(zoomObject.transform, zoomIdentity);
};
}
if (metaballs) {
svg.call(addMetaballsDefs(config));
}
svg
.merge(root)
.attr(
'height',
d => (d.length + 1) * lineHeight + margin.top + margin.bottom
);
svg
.append('g')
.classed('viewport', true)
.attr('transform', `translate(${margin.left},${margin.top})`)
.call(draw(config, xScale));
};
const chart = selection => {
chart._initialize = () => initChart(selection);
chart._initialize();
global.addEventListener('resize', chart._initialize, true);
};
chart.scale = () => chart._scale;
chart.filteredData = () => chart._filteredData;
chart.zoomToDomain = (
domain,
duration = 0,
delay = 0,
ease = d3.easeLinear
) => {
if (chart._zoomToDomain) {
chart._zoomToDomain(domain, duration, delay, ease);
}
};
chart.destroy = (callback = () => {}) => {
global.removeEventListener('resize', chart._initialize, true);
callback();
};
const draw = (config, scale) => selection => {
const { drop: { date: dropDate } } = config;
const dateBounds = scale.domain().map(d => new Date(d));
const filteredData = selection.data().map(dataSet => {
if (!Array.isArray(dataSet)) {
throw new Error(
'Selection data is not an array. Are you sure you provided an array of arrays to `data` function?'
);
}
return dataSet.map(row => {
if (!row.fullData) {
row.fullData = config.drops(row);
if (!row.fullData) {
throw new Error(
'No drops data has been found. It looks by default in the `data` property. You can use the `drops` configuration parameter to tune it.'
);
}
}
row.data = row.fullData.filter(d =>
withinRange(dropDate(d), dateBounds)
);
return row;
});
});
chart._scale = scale;
chart._filteredData = filteredData[0];
selection
.data(filteredData)
.call(dropLine(config, scale))
.call(bounds(config, scale))
.call(axis(d3, config, scale, chart.currentBreakpointLabel));
};
chart.draw = draw;
return chart;
};