Newer
Older
* Hydrograph charting module.
*/
import {DateTime} from 'luxon';
Bucknell, Mary S.
committed
Bucknell, Mary S.
committed
import {drawInfoAlert} from 'd3render/alerts';
import {renderTimeSeriesUrlParams} from 'ml/url-params';
import {retrieveHydrographData} from 'ml/store/hydrograph-data';
Bucknell, Mary S.
committed
import {retrieveHydrographParameters} from 'ml/store/hydrograph-parameters';
import {setSelectedParameterCode, setCompareDataVisibility, setSelectedCustomDateRange, setSelectedDateRange,
setSelectedIVMethodID
} from 'ml/store/hydrograph-state';
Bucknell, Mary S.
committed
import {Actions as floodDataActions} from 'ml/store/flood-inundation';
import {getPreferredIVMethodID} from './selectors/time-series-data';
Bucknell, Mary S.
committed
import {showDataIndicators} from './data-indicator';
import {drawDataTables} from './data-table';
Bucknell, Mary S.
committed
import {drawGraphBrush} from './graph-brush';
import {drawGraphControls} from './graph-controls';
Bucknell, Mary S.
committed
import {drawTimeSeriesLegend} from './legend';
import {drawMethodPicker} from './method-picker';
Bucknell, Mary S.
committed
import {drawSelectionTable} from './parameters';
import {drawSelectActions} from './select-actions';
import {drawTimeSeriesGraph} from './time-series-graph';
Bucknell, Mary S.
committed
import {drawTooltipCursorSlider} from './tooltip';
Bucknell, Mary S.
committed
/*
* Renders the hydrograph on the node element using the Redux store for state information. The siteno, latitude, and
* longitude are required parameters. All others are optional and are used to set the initial state of the hydrograph.
Bucknell, Mary S.
committed
* @param {Redux store} store
* @param {DOM node} node
* @param {Object} - string properties to set initial state information. The property siteno is required
* @param {Promise} loadPromise - will resolve when any data needed by this module
* that is fetched by the caller has been fetched
* */
Bucknell, Mary S.
committed
sitename,
parameterCode,
compare,
period,
startDT,
endDT,
showOnlyGraph = false,
showMLName = false
Bucknell, Mary S.
committed
const nodeElem = select(node);
if (!config.ivPeriodOfRecord && !config.gwPeriodOfRecord) {
select(node).select('.graph-container').call(drawInfoAlert, {title: 'Hydrograph Alert', body: 'No IV or field visit data is available.'});
Bucknell, Mary S.
committed
}
Bucknell, Mary S.
committed
showDataIndicators(true);
const initialPeriod = startDT && endDT ? 'custom' : period || 'P7D';
const initialStartTime = startDT ?
DateTime.fromISO(startDT, {zone: config.locationTimeZone}).toISO() : null;
const initialEndTime = endDT ?
DateTime.fromISO(endDT, {zone: config.locationTimeZone}).endOf('day').toISO() : null;
Bucknell, Mary S.
committed
const initialLoadCompare = compare === 'true' || compare === true ? true : false;
const thisShowOnlyGraph = showOnlyGraph === 'true' || showOnlyGraph === true ? true : false;
const thisShowMLName = showMLName === 'true' || showMLName === true ? true : false;
Bucknell, Mary S.
committed
const fetchHydrographDataPromise = store.dispatch(retrieveHydrographData(siteno, {
Bucknell, Mary S.
committed
parameterCode: parameterCode,
period: initialPeriod === 'custom' ? null : initialPeriod,
startTime: initialStartTime,
endTime: initialEndTime,
Bucknell, Mary S.
committed
loadCompare: initialLoadCompare,
loadMedian: false
}));
Bucknell, Mary S.
committed
Bucknell, Mary S.
committed
// if showing the controls, fetch the parameters
Bucknell, Mary S.
committed
let fetchParametersPromise;
if (!thisShowOnlyGraph) {
Bucknell, Mary S.
committed
fetchParametersPromise = store.dispatch(retrieveHydrographParameters(siteno));
}
// Initialize all hydrograph state variables if showing the control
store.dispatch(setSelectedParameterCode(parameterCode));
store.dispatch(setCompareDataVisibility(initialLoadCompare));
if (period) {
store.dispatch(setSelectedDateRange(period));
} else if (startDT && endDT) {
store.dispatch(setSelectedDateRange('custom'));
store.dispatch(setSelectedCustomDateRange(startDT, endDT));
} else {
store.dispatch(setSelectedDateRange('P7D'));
Bucknell, Mary S.
committed
}
Bucknell, Mary S.
committed
Bucknell, Mary S.
committed
// Fetch waterwatch flood levels
const fetchFloodLevelsPromise = store.dispatch(floodDataActions.retrieveWaterwatchData(siteno));
Bucknell, Mary S.
committed
Bucknell, Mary S.
committed
let fetchDataPromises = [fetchHydrographDataPromise];
// If flood levels are to be shown then wait to render the hydrograph until those have been fetched.
Bucknell, Mary S.
committed
if (parameterCode === config.GAGE_HEIGHT_PARAMETER_CODE) {
fetchDataPromises.push(fetchFloodLevelsPromise);
}
Promise.all(fetchDataPromises).then(() => {
Bucknell, Mary S.
committed
// selectedIVMethodID should be set regardless of whether we are showing only the graph but the preferred method ID
// can not be determined until the data is fetched so that is done here.
const initialIVMethodID = timeSeriesId || getPreferredIVMethodID(store.getState());
store.dispatch(setSelectedIVMethodID(initialIVMethodID));
Bucknell, Mary S.
committed
showDataIndicators(false, store);
Bucknell, Mary S.
committed
let graphContainer = nodeElem.select('.graph-container');
graphContainer.call(drawTimeSeriesGraph, store, siteno, agencyCd, sitename, thisShowMLName, !thisShowOnlyGraph);
Bucknell, Mary S.
committed
if (!thisShowOnlyGraph) {
Bucknell, Mary S.
committed
graphContainer
.call(drawTooltipCursorSlider, store)
.call(drawGraphBrush, store);
}
const legendControlsContainer = graphContainer.append('div')
.classed('ts-legend-controls-container', true)
.call(drawTimeSeriesLegend, store);
if (!thisShowOnlyGraph) {
nodeElem.select('#hydrograph-method-picker-container')
.call(drawMethodPicker, store);
legendControlsContainer.call(drawGraphControls, store, siteno);
nodeElem.select('.select-actions-container').call(drawSelectActions, store, siteno);
Bucknell, Mary S.
committed
nodeElem.select('#iv-data-table-container')
.call(drawDataTables, store);
Bucknell, Mary S.
committed
fetchParametersPromise.then(() => {
Bucknell, Mary S.
committed
nodeElem.select('.select-time-series-container')
.call(drawSelectionTable, store, siteno);
});
Bucknell, Mary S.
committed
renderTimeSeriesUrlParams(store);
}
Bucknell, Mary S.
committed
})
.catch(reason => {
console.error(reason);
throw reason;
Bucknell, Mary S.
committed
});