From c28d1a2fdf4ed0ffa5d7549ea57aeb697c61f433 Mon Sep 17 00:00:00 2001
From: Andrew Yan <ayan@usgs.gov>
Date: Mon, 29 Jan 2018 11:30:01 -0600
Subject: [PATCH] WIP: median stats sort of working again

---
 .../scripts/components/hydrograph/index.js    | 45 +++++++++++++------
 assets/src/scripts/models.js                  | 11 ++++-
 2 files changed, 42 insertions(+), 14 deletions(-)

diff --git a/assets/src/scripts/components/hydrograph/index.js b/assets/src/scripts/components/hydrograph/index.js
index ed71aecc9..316a76b9f 100644
--- a/assets/src/scripts/components/hydrograph/index.js
+++ b/assets/src/scripts/components/hydrograph/index.js
@@ -7,7 +7,7 @@ const { line } = require('d3-shape');
 const { timeFormat } = require('d3-time-format');
 
 const { addSVGAccessibility, addSROnlyTable } = require('../../accessibility');
-const { getTimeseries, getPreviousYearTimeseries, getSiteStatistics, parseRDB, readTS, parseMedianData } = require('../../models');
+const { getTimeseries, getPreviousYearTimeseries, getMedianStatistics } = require('../../models');
 
 const { appendAxes, updateYAxis, createAxes } = require('./axes');
 const { createScales, createXScale, updateYScale } = require('./scales');
@@ -191,38 +191,47 @@ class Hydrograph {
         return tsLine;
     }
 
-    _plotMedianPoints(plot, xScale, yScale) {
-        plot.selectAll('medianPoint')
-            .data(this._medianStats)
+    plotMedianPoints(data) {
+        const currentYExtent = extent(this._tsData.current, d => d.value);
+        const yExtent = extent(data, d => d.value);
+        const yDataExtent = [min([yExtent[0], currentYExtent[0]]), max([yExtent[1], currentYExtent[1]])];
+        let xscale = this.scale.xScale;
+        let yscale = this.scale.yScale;
+        updateYScale(yscale, yDataExtent);
+        updateYAxis(this.axis.yAxis, yscale);
+        this.svg.select('.y-axis')
+            .call(this.axis.yAxis);
+        this.plot.selectAll('medianPoint')
+            .data(data)
             .enter()
             .append('circle')
             .attr('r', '4px')
             .attr('fill', 'orange')
             .attr('x', function(d) {
-                return xScale(d.time);
+                return xscale(d.time);
             })
             .attr('y', function(d) {
-                return yScale(d.value);
+                return yscale(d.value);
             })
             .attr('cx', function(d) {
-                return xScale(d.time);
+                return xscale(d.time);
             })
             .attr('cy', function(d) {
-                return yScale(d.value);
+                return yscale(d.value);
             });
 
-        plot.selectAll('medianPointText')
-            .data(this._medianStats)
+        this.plot.selectAll('medianPointText')
+            .data(data)
             .enter()
             .append('text')
             .text(function(d) {
                 return d.label;
             })
             .attr('x', function(d) {
-                return xScale(d.time) + 5;
+                return xscale(d.time) + 5;
             })
             .attr('y', function(d) {
-                return yScale(d.value);
+                return yscale(d.value);
             });
     }
 
@@ -290,7 +299,9 @@ class Hydrograph {
 function attachToNode(node, {siteno}) {
     let hydrograph;
     let getLastYearTS;
-    getTimeseries({sites: [siteno]}).then((series) => {
+    let medianStatistics;
+    let ts = getTimeseries({sites: [siteno]});
+    ts.then((series) => {
             let dataIsValid = series && series[0] &&
                 !series[0].values.some(d => d.value === -999999);
             hydrograph = new Hydrograph({
@@ -308,6 +319,14 @@ function attachToNode(node, {siteno}) {
                     startTime: series[0].seriesStartDate,
                     endTime: series[0].seriesEndDate
                 });
+                medianStatistics = getMedianStatistics({
+                   sites: [node.dataset.siteno],
+                   timeseries: series[0].values
+                });
+                medianStatistics.then(
+                    (data) => {
+                        hydrograph.plotMedianPoints(data)
+                    });
             }
         }, () =>
             hydrograph = new Hydrograph({
diff --git a/assets/src/scripts/models.js b/assets/src/scripts/models.js
index c9abd61c3..11757c709 100644
--- a/assets/src/scripts/models.js
+++ b/assets/src/scripts/models.js
@@ -70,7 +70,7 @@ export function getTimeseries({sites, params=['00060'], startDate=null, endDate=
                 return error;
             });
 }
-export function getSiteStatistics({sites, params=['00060'], statType='median'}) {
+export function getSiteStatistics({sites, statType='median', params=['00060']}) {
     let url = `${SERVICE_ROOT}/stat/?format=rdb&sites=${sites.join(',')}&statReportType=daily&statTypeCd=${statType}&parameterCd=${params.join(',')}`;
     return get(url);
 }
@@ -197,3 +197,12 @@ export function getPreviousYearTimeseries({site, startTime, endTime}) {
 
     return getTimeseries({sites: [site], startDate: lastYearStartTime, endDate: lastYearEndTime});
 }
+
+export function getMedianStatistics({sites, timeseries, params=['00060']}) {
+    let medianRDB = getSiteStatistics({sites: sites, statType: 'median', params: params});
+    return medianRDB.then((response) => {
+        return parseMedianData(parseRDB(response), timeseries);
+    }, (error) => {
+        return error;
+    });
+}
-- 
GitLab