Newer
Older
import { extent } from 'd3-array';
import { line as d3Line, curveStepAfter } from 'd3-shape';
import {link} from '../../lib/redux';
import {addSVGAccessibility} from '../../accessibility';
import {appendAxes, axesSelector} from './axes';
import {
currentVariableLineSegmentsSelector,
getCurrentVariableMedianStatPoints,
HASH_ID,
MASK_DESC
} from './drawing-data';
import {CIRCLE_RADIUS_SINGLE_PT, layoutSelector} from './layout';
import {createStructuredSelector} from 'reselect';
import {xScaleSelector, yScaleSelector} from './scales';
import {descriptionSelector, isVisibleSelector, titleSelector} from './time-series';
import {getAgencyCode, getMonitoringLocationName} from '../../selectors/time-series-selector';
import {createTooltipFocus, createTooltipText} from './tooltip';
import {mediaQuery} from '../../utils';
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
const plotDataLine = function(elem, {visible, lines, tsKey, xScale, yScale}) {
if (!visible) {
return;
}
const tsKeyClass = `ts-${tsKey}`;
for (let line of lines) {
if (line.classes.dataMask === null) {
// If this is a single point line, then represent it as a circle.
// Otherwise, render as a line.
if (line.points.length === 1) {
elem.append('circle')
.data(line.points)
.classed('line-segment', true)
.classed('approved', line.classes.approved)
.classed('estimated', line.classes.estimated)
.classed('not-current-method', !line.classes.currentMethod)
.classed(tsKeyClass, true)
.attr('r', CIRCLE_RADIUS_SINGLE_PT)
.attr('cx', d => xScale(d.dateTime))
.attr('cy', d => yScale(d.value));
} else {
const tsLine = d3Line()
.x(d => xScale(d.dateTime))
.y(d => yScale(d.value));
elem.append('path')
.datum(line.points)
.classed('line-segment', true)
.classed('approved', line.classes.approved)
.classed('estimated', line.classes.estimated)
.classed('not-current-method', !line.classes.currentMethod)
.classed(`ts-${tsKey}`, true)
.attr('d', tsLine);
}
} else {
const maskCode = line.classes.dataMask.toLowerCase();
const maskDisplayName = MASK_DESC[maskCode].replace(' ', '-').toLowerCase();
const [xDomainStart, xDomainEnd] = extent(line.points, d => d.dateTime);
const [yRangeStart, yRangeEnd] = yScale.domain();
let maskGroup = elem.append('g')
.attr('class', `${tsKey}-mask-group`);
const xSpan = xScale(xDomainEnd) - xScale(xDomainStart);
const rectWidth = xSpan > 1 ? xSpan : 1;
maskGroup.append('rect')
.attr('x', xScale(xDomainStart))
.attr('y', yScale(yRangeEnd))
.attr('width', rectWidth)
.attr('height', Math.abs(yScale(yRangeEnd) - yScale(yRangeStart)))
.attr('class', `mask ${maskDisplayName}-mask`)
.classed(`ts-${tsKey}`, true);
const patternId = HASH_ID[tsKey] ? `url(#${HASH_ID[tsKey]})` : '';
maskGroup.append('rect')
.attr('x', xScale(xDomainStart))
.attr('y', yScale(yRangeEnd))
.attr('width', rectWidth)
.attr('height', Math.abs(yScale(yRangeEnd) - yScale(yRangeStart)))
.attr('fill', patternId);
}
}
};
const plotDataLines = function(elem, {visible, tsLinesMap, tsKey, xScale, yScale}, container) {
container = container || elem.append('g');
const elemId = `ts-${tsKey}-group`;
container.selectAll(`#${elemId}`).remove();
const tsLineGroup = container
.append('g')
.attr('id', elemId)
.classed('tsKey', true);
for (const lines of Object.values(tsLinesMap)) {
plotDataLine(tsLineGroup, {visible, lines, tsKey, xScale, yScale});
}
return container;
};
const plotSvgDefs = function(elem) {
let defs = elem.append('defs');
defs.append('mask')
.attr('id', 'display-mask')
.attr('maskUnits', 'userSpaceOnUse')
.append('rect')
.attr('x', '0')
.attr('y', '0')
.attr('width', '100%')
.attr('height', '100%')
.attr('fill', '#0000ff');
defs.append('pattern')
.attr('id', HASH_ID.current)
.attr('width', '8')
.attr('height', '8')
.attr('patternUnits', 'userSpaceOnUse')
.attr('patternTransform', 'rotate(45)')
.append('rect')
.attr('width', '4')
.attr('height', '8')
.attr('transform', 'translate(0, 0)')
.attr('mask', 'url(#display-mask)');
defs.append('pattern')
.attr('id', HASH_ID.compare)
.attr('width', '8')
.attr('height', '8')
.attr('patternUnits', 'userSpaceOnUse')
.attr('patternTransform', 'rotate(135)')
.append('rect')
.attr('width', '4')
.attr('height', '8')
.attr('transform', 'translate(0, 0)')
.attr('mask', 'url(#display-mask)');
};
/**
* Plots the median points for a single median time series.
* @param {Object} elem
* @param {Function} xscale
* @param {Function} yscale
* @param {Number} modulo
* @param {Array} points
*/
const plotMedianPoints = function(elem, {xscale, yscale, modulo, points}) {
const stepFunction = d3Line()
.curve(curveStepAfter)
.x(function(d) {
return xscale(d.date);
})
.y(function(d) {
return yscale(d.value);
});
let medianGrp = elem.append('g');
medianGrp.append('path')
.datum(points)
.classed('median-data-series', true)
.classed('median-step', true)
.classed(`median-step-${modulo}`, true)
.attr('d', stepFunction);
};
/**
* Plots the median points for all median time series for the current variable.
* @param {Object} elem
* @param {Boolean} visible
* @param {Function} xscale
* @param {Function} yscale
* @param {Array} pointsList
*/
const plotAllMedianPoints = function (elem, {visible, xscale, yscale, seriesPoints}) {
elem.select('#median-points').remove();
if (!visible) {
return;
}
const container = elem
.append('g')
.attr('id', 'median-points');
seriesPoints.forEach((points, index) => {
plotMedianPoints(container, {xscale, yscale, modulo: index % 6, points: points});
});
};
const createTitle = function(elem, siteNo, showMLName) {
let titleDiv = elem.append('div')
.classed('time-series-graph-title', true);
if (showMLName) {
titleDiv.append('div')
.call(link((elem, {mlName, agencyCode}) => {
elem.html(`${mlName}, ${agencyCode} ${siteNo}`);
}, createStructuredSelector({
mlName: getMonitoringLocationName(siteNo),
agencyCode: getAgencyCode(siteNo)
})));
}
titleDiv.append('div')
.call(link((elem, title) => {
elem.html(title);
}, titleSelector));
};
const watermark = function (elem) {
// These constants will need to change if the watermark svg is updated
const watermarkHalfHeight = 87 / 2;
const watermarkHalfWidth = 235 / 2;
elem.append('img')
.classed('watermark', true)
.attr('alt', 'USGS - science for a changing world')
.attr('src', config.STATIC_URL + '/img/USGS_green_logo.svg')
.call(link(function(elem, layout) {
const centerX = layout.margin.left + (layout.width - layout.margin.right - layout.margin.left) / 2;
const centerY = layout.margin.top + (layout.height - layout.margin.bottom - layout.margin.top) / 2;
const scale = !mediaQuery(config.USWDS_MEDIUM_SCREEN) ? 0.5 : 1;
const translateX = centerX - watermarkHalfWidth;
const translateY = centerY - watermarkHalfHeight;
const transform = `matrix(${scale}, 0, 0, ${scale}, ${translateX}, ${translateY})`;
elem.style('transform', transform);
// for Safari browser
elem.style('-webkit-transform', transform);
}, layoutSelector));
};
export const drawTimeSeriesGraph = function(elem, siteNo, showMLName) {
elem.append('div')
.attr('class', 'hydrograph-container')
.call(watermark)
.call(createTitle, siteNo, showMLName)
.call(createTooltipText)
.append('svg')
.attr('xmlns', 'http://www.w3.org/2000/svg')
.classed('hydrograph-svg', true)
.call(link((elem, layout) => {
elem.attr('viewBox', `0 0 ${layout.width + layout.margin.left + layout.margin.right} ${layout.height + layout.margin.top + layout.margin.bottom}`);
elem.attr('width', layout.width);
elem.attr('height', layout.height);
}, layoutSelector))
.call(link(addSVGAccessibility, createStructuredSelector({
title: titleSelector,
description: descriptionSelector,
isInteractive: () => true,
idPrefix: () => 'hydrograph'
})))
.call(plotSvgDefs)
.call(svg => {
svg.append('g')
.call(link((elem, layout) => elem.attr('transform', `translate(${layout.margin.left},${layout.margin.top})`), layoutSelector))
.call(link(appendAxes, axesSelector))
.call(link(plotDataLines, createStructuredSelector({
visible: isVisibleSelector('current'),
tsLinesMap: currentVariableLineSegmentsSelector('current'),
xScale: xScaleSelector('current'),
yScale: yScaleSelector,
tsKey: () => 'current'
})))
.call(link(plotDataLines, createStructuredSelector({
visible: isVisibleSelector('compare'),
tsLinesMap: currentVariableLineSegmentsSelector('compare'),
xScale: xScaleSelector('compare'),
yScale: yScaleSelector,
tsKey: () => 'compare'
})))
.call(createTooltipFocus)
.call(link(plotAllMedianPoints, createStructuredSelector({
visible: isVisibleSelector('median'),
xscale: xScaleSelector('current'),
yscale: yScaleSelector,
seriesPoints: getCurrentVariableMedianStatPoints
})));
});
};