Newer
Older
/**
* Hydrograph charting module.
*/
import { select } from 'd3-selection';
Bucknell, Mary S.
committed
import { createStructuredSelector } from 'reselect';
Bucknell, Mary S.
committed
import { link } from '../../lib/d3-redux';
Bucknell, Mary S.
committed
import {isLoadingTS, hasAnyTimeSeries } from '../../selectors/time-series-selector';
import { Actions } from '../../store';
Bucknell, Mary S.
committed
import { cursorSlider } from './cursor';
Soper, Samuel Alexander
committed
import { drawDateRangeControls } from './date-controls';
import { lineSegmentsByParmCdSelector } from './drawing-data';
import { drawGraphControls } from './graph-controls';
import { SPARK_LINE_DIM } from './layout';
import { drawTimeSeriesLegend } from './legend';
import { drawLoadingIndicator } from '../loading-indicator';
import { drawMethodPicker } from './method-picker';
import { plotSeriesSelectTable, availableTimeSeriesSelector } from './parameters';
import { timeSeriesScalesByParmCdSelector } from './scales';
import { allTimeSeriesSelector } from './time-series';
import { drawTimeSeriesGraph } from './time-series-graph';
// 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')
Bucknell, Mary S.
committed
/**
Bucknell, Mary S.
committed
* Modify styling to hide or display the elem.
Bucknell, Mary S.
committed
*
* @param elem
Bucknell, Mary S.
committed
* @param {Boolean} showElem
Bucknell, Mary S.
committed
*/
const controlDisplay = function(elem, showElem) {
Bucknell, Mary S.
committed
elem.attr('hidden', showElem ? null : true);
Bucknell, Mary S.
committed
};
Bucknell, Mary S.
committed
const dataLoadingAlert = function(elem, message) {
elem.select('#no-data-message').remove();
Bucknell, Mary S.
committed
if (message) {
elem.append('div')
.attr('id', 'no-data-message')
.attr('class', 'usa-alert usa-alert-info')
.append('div')
Bucknell, Mary S.
committed
.attr('class', 'usa-alert-body')
.append('p')
.attr('class', 'usa-alert-text')
.text(message);
Bucknell, Mary S.
committed
}
Bucknell, Mary S.
committed
export const attachToNode = function (store,
node,
{
siteno,
parameter,
compare,
period,
cursorOffset,
showOnlyGraph = false,
showMLName = false
} = {}) {
Bucknell, Mary S.
committed
const nodeElem = select(node);
if (!siteno) {
select(node).call(drawMessage, 'No data is available.');
return;
Bucknell, Mary S.
committed
}
// Initialize hydrograph with the store and show the loading indicator
Bucknell, Mary S.
committed
store.dispatch(Actions.resizeUI(window.innerWidth, node.offsetWidth));
Bucknell, Mary S.
committed
nodeElem
Naab, Daniel James
committed
.select('.loading-indicator-container')
Bucknell, Mary S.
committed
.call(link(store, drawLoadingIndicator, createStructuredSelector({
Naab, Daniel James
committed
showLoadingIndicator: isLoadingTS('current', 'P7D'),
sizeClass: () => 'fa-3x'
})));
Naab, Daniel James
committed
// If specified, turn the visibility of the comparison time series on.
if (compare) {
store.dispatch(Actions.toggleTimeSeries('compare', true));
}
// If specified, initialize the cursorOffset
if (cursorOffset !== undefined) {
store.dispatch(Actions.setCursorOffset(cursorOffset));
}
Bucknell, Mary S.
committed
// Fetch the time series data
if (period) {
store.dispatch(Actions.retrieveCustomTimePeriodTimeSeries(siteno, parameter ? parameter : '00060', period))
Bucknell, Mary S.
committed
.catch((message) => dataLoadingAlert(nodeElem, message ? message : 'No data returned'));
Bucknell, Mary S.
committed
} else {
store.dispatch(Actions.retrieveTimeSeries(siteno, parameter ? [parameter] : null))
Bucknell, Mary S.
committed
.catch(() => dataLoadingAlert((nodeElem, 'No current time series data available for this site')));
Bucknell, Mary S.
committed
}
store.dispatch(Actions.retrieveMedianStatistics(siteno));
// Set up rendering functions for the graph-container
let graphContainer = nodeElem.select('.graph-container')
Bucknell, Mary S.
committed
.call(link(store, controlDisplay, hasAnyTimeSeries))
.call(drawTimeSeriesGraph, store, siteno, showMLName);
if (!showOnlyGraph) {
graphContainer.call(cursorSlider, store);
}
graphContainer.append('div')
.classed('ts-legend-controls-container', true)
.call(drawTimeSeriesLegend, store);
// Add UI interactive elements and the provisional data alert.
Bucknell, Mary S.
committed
if (!showOnlyGraph) {
Bucknell, Mary S.
committed
nodeElem
Bucknell, Mary S.
committed
.call(drawMethodPicker, store)
.call(drawDateRangeControls, store, siteno);
Bucknell, Mary S.
committed
nodeElem.select('.ts-legend-controls-container')
Bucknell, Mary S.
committed
.call(drawGraphControls, store);
Bucknell, Mary S.
committed
nodeElem.select('.select-time-series-container')
Bucknell, Mary S.
committed
.call(link(store, plotSeriesSelectTable, createStructuredSelector({
siteno: () => siteno,
availableTimeSeries: availableTimeSeriesSelector,
Naab, Daniel James
committed
lineSegmentsByParmCd: lineSegmentsByParmCdSelector('current', 'P7D'),
Bucknell, Mary S.
committed
timeSeriesScalesByParmCd: timeSeriesScalesByParmCdSelector('current', 'P7D', SPARK_LINE_DIM),
}), store));
Bucknell, Mary S.
committed
nodeElem.select('.provisional-data-alert')
Bucknell, Mary S.
committed
.call(link(store, function(elem, allTimeSeries) {
elem.attr('hidden', Object.keys(allTimeSeries).length ? null : true);
}, allTimeSeriesSelector));
}
Bucknell, Mary S.
committed
window.onresize = function() {
store.dispatch(Actions.resizeUI(window.innerWidth, node.offsetWidth));
Bucknell, Mary S.
committed
};