Newer
Older
/**
* Hydrograph charting module.
*/
const { bisector } = require('d3-array');
Naab, Daniel James
committed
const { mouse, select } = require('d3-selection');
const { line } = require('d3-shape');
Naab, Daniel James
committed
const { createSelector, createStructuredSelector } = require('reselect');
const { addSVGAccessibility, addSROnlyTable } = require('../../accessibility');
Naab, Daniel James
committed
const { dispatch, link, provide } = require('../../lib/redux');
Naab, Daniel James
committed
const { appendAxes, axesSelector } = require('./axes');
Bucknell, Mary S.
committed
const { MARGIN, updateLayoutVariables } = require('./layout');
const { pointsSelector, validPointsSelector, isVisibleSelector } = require('./points');
const { xScaleSelector, yScaleSelector } = require('./scales');
const { Actions, configureStore } = require('./store');
Bucknell, Mary S.
committed
let WIDTH;
let HEIGHT;
let ASPECT_RATIO_PERCENT;
// Function that returns the left bounding point for a given chart point.
const bisectDate = bisector(d => d.time).left;
const drawMessage = function (elem, message) {
// Set up parent element and SVG
elem.innerHTML = '';
const alertBox = elem
.append('div')
.attr('class', 'usa-alert usa-alert-warning')
.append('div')
.attr('class', 'usa-alert-body');
alertBox
.append('h3')
.attr('class', 'usa-alert-heading')
.html('Hydrograph Alert');
alertBox
.append('p')
Naab, Daniel James
committed
const plotDataLine = function (elem, {visible, points, tsDataKey, xScale, yScale}) {
const elemId = 'ts-' + tsDataKey;
elem.selectAll(`#${elemId}`).remove();
Naab, Daniel James
committed
if (!visible) {
Naab, Daniel James
committed
elem.datum(points)
.append('path')
.classed('line', true)
.attr('id', elemId)
Naab, Daniel James
committed
.attr('d', line().x(d => xScale(d.time))
.y(d => yScale(d.value)));
const getNearestTime = function (data, time) {
let index = bisectDate(data, time, 1);
let datum;
let d0 = data[index - 1];
let d1 = data[index];
if (d0 && d1) {
datum = time - d0.time > d1.time - time ? d1 : d0;
} else {
datum = d0 || d1;
// Return the nearest data point and its index.
return {
datum,
index: datum === d0 ? index - 1 : index
};
};
Naab, Daniel James
committed
const plotTooltips = function (elem, {xScale, yScale, data}) {
// Create a node to hightlight the currently selected date/time.
let focus = elem.append('g')
.attr('class', 'focus')
.style('display', 'none');
focus.append('circle')
.attr('r', 7.5);
focus.append('text');
elem.append('rect')
.attr('class', 'overlay')
.attr('width', WIDTH)
.attr('height', HEIGHT)
.on('mouseover', () => focus.style('display', null))
.on('mouseout', () => focus.style('display', 'none'))
.on('mousemove', function () {
// Get the nearest data point for the current mouse position.
const time = xScale.invert(mouse(this)[0]);
const {datum, index} = getNearestTime(data, time);
if (!datum) {
return;
}
Naab, Daniel James
committed
// Move the focus node to this date/time.
focus.attr('transform', `translate(${xScale(datum.time)}, ${yScale(datum.value)})`);
// Draw text, anchored to the left or right, depending on
// which side of the graph the point is on.
const isFirstHalf = index < data.length / 2;
focus.select('text')
.text(() => datum.label)
.attr('text-anchor', isFirstHalf ? 'start' : 'end')
.attr('x', isFirstHalf ? 15 : -15)
.attr('dy', isFirstHalf ? '.31em' : '-.31em');
});
};
Naab, Daniel James
committed
Naab, Daniel James
committed
const plotMedianPoints = function (elem, {visible, xscale, yscale, medianStatsData}) {
elem.select('#median-points').remove();
Naab, Daniel James
committed
if (!visible) {
return;
}
const container = elem
.append('g')
.attr('id', 'median-points');
container.selectAll('medianPoint')
.data(medianStatsData)
.enter()
.append('circle')
.attr('cx', function(d) {
})
.attr('cy', function(d) {
container.selectAll('medianPointText')
.data(medianStatsData)
.enter()
.append('text')
.text(function(d) {
return d.label;
})
Naab, Daniel James
committed
const timeSeriesGraph = function (elem) {
elem.append('div')
.attr('class', 'hydrograph-container')
.style('padding-bottom', ASPECT_RATIO_PERCENT)
.append('svg')
Bucknell, Mary S.
committed
//.attr('preserveAspectRatio', 'xMinYMin meet')
.attr('viewBox', `0 0 ${WIDTH} ${HEIGHT}`)
Naab, Daniel James
committed
.call(link(addSVGAccessibility, createStructuredSelector({
title: state => state.title,
description: state => state.desc,
isInteractive: () => true
})))
.append('g')
.attr('transform', `translate(${MARGIN.left},${MARGIN.top})`)
Naab, Daniel James
committed
.call(link(appendAxes, axesSelector))
.call(link(plotDataLine, createStructuredSelector({
Naab, Daniel James
committed
visible: isVisibleSelector('current'),
points: validPointsSelector('current'),
xScale: xScaleSelector('current'),
yScale: yScaleSelector,
Naab, Daniel James
committed
tsDataKey: () => 'current'
Naab, Daniel James
committed
})))
Naab, Daniel James
committed
.call(link(plotDataLine, createStructuredSelector({
Naab, Daniel James
committed
visible: isVisibleSelector('compare'),
points: validPointsSelector('compare'),
xScale: xScaleSelector('compare'),
yScale: yScaleSelector,
Naab, Daniel James
committed
tsDataKey: () => 'compare'
Naab, Daniel James
committed
})))
Naab, Daniel James
committed
.call(link(plotTooltips, createStructuredSelector({
Naab, Daniel James
committed
xScale: xScaleSelector('current'),
yScale: yScaleSelector,
data: pointsSelector('current')
Naab, Daniel James
committed
})))
.call(link(plotMedianPoints, createStructuredSelector({
Naab, Daniel James
committed
visible: isVisibleSelector('medianStatistics'),
xscale: xScaleSelector('current'),
yscale: yScaleSelector,
medianStatsData: pointsSelector('medianStatistics')
Naab, Daniel James
committed
})));
elem.call(link(addSROnlyTable, createStructuredSelector({
columnNames: createSelector(
(state) => state.title,
(title) => [title, 'Time']
),
data: createSelector(
Naab, Daniel James
committed
pointsSelector('current'),
Naab, Daniel James
committed
points => points.map((value) => {
return [value.value, value.time];
Bucknell, Mary S.
committed
})
Naab, Daniel James
committed
)
})));
};
const attachToNode = function (node, {siteno} = {}) {
if (!siteno) {
select(node).call(drawMessage, 'No data is available.');
return;
Bucknell, Mary S.
committed
}
let store = configureStore();
Bucknell, Mary S.
committed
// Set layout variables
let newLayout = updateLayoutVariables(node.offsetWidth);
WIDTH = newLayout.width;
HEIGHT = newLayout.height;
ASPECT_RATIO_PERCENT = newLayout.aspect_ratio_percent;
select(node)
.call(provide(store))
Naab, Daniel James
committed
.call(timeSeriesGraph)
.select('.hydrograph-last-year-input')
.on('change', dispatch(function () {
return Actions.toggleTimeseries('compare', this.checked);
}));
store.dispatch(Actions.retrieveTimeseries(siteno));
};
module.exports = {attachToNode, getNearestTime, timeSeriesGraph};