Skip to content

Commit f63481f

Browse files
authored
Merge pull request #1001 from cmu-delphi/sgratzl/neighboring
fix neighbors and disable in survey view
2 parents 403f519 + b5d7a28 commit f63481f

File tree

5 files changed

+54
-35
lines changed

5 files changed

+54
-35
lines changed

src/blocks/HistoryLineChart.svelte

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
6060
export let showAnnotations = true;
6161
62+
export let showNeighbors = true;
63+
6264
let showFull = expandableWindow === 'full';
6365
6466
/**
@@ -104,7 +106,7 @@
104106
region,
105107
date,
106108
timeFrame,
107-
{ height, zero, raw, isMobile, singleRegionOnly, domain, cumulative },
109+
{ height, zero, raw, isMobile, singleRegionOnly, domain, cumulative, showNeighbors },
108110
) {
109111
const isWeekly = sensor.value.isWeeklySignal;
110112
const options = {
@@ -134,10 +136,11 @@
134136
if (region.level === 'county') {
135137
// county vs related vs state vs nation
136138
const state = getStateOfCounty(region);
137-
return generateCompareLineSpec(
138-
[region.displayName, neighboringInfo.displayName, state.displayName, nationInfo.displayName],
139-
options,
140-
);
139+
const regions = [region.displayName, state.displayName, nationInfo.displayName];
140+
if (showNeighbors) {
141+
regions.splice(1, 0, neighboringInfo.displayName);
142+
}
143+
return generateCompareLineSpec(regions, options);
141144
}
142145
if (region.level !== 'nation') {
143146
return generateCompareLineSpec([region.displayName, nationInfo.displayName], options);
@@ -150,7 +153,7 @@
150153
* @param {import("../stores/params").DateParam} date
151154
* @param {import("../stores/params").RegionParam} region
152155
*/
153-
function loadData(sensor, region, timeFrame, singleRegionOnly) {
156+
function loadData(sensor, region, timeFrame, singleRegionOnly, showNeighbors) {
154157
if (!region.value) {
155158
return null;
156159
}
@@ -163,19 +166,24 @@
163166
const data = [selfData];
164167
165168
if (region.level === 'county') {
169+
if (showNeighbors) {
170+
const relatedCounties = getRelatedCounties(region.value);
171+
const relatedData = fetcher
172+
.fetch1SensorNRegionsNDates(sensor, relatedCounties, timeFrame)
173+
.then((r) => averageByDate(r, neighboringInfo))
174+
.then((r) => addMissing(r, sensor.isWeeklySignal ? 'week' : 'day'));
175+
data.push(relatedData);
176+
}
166177
const state = getStateOfCounty(region);
167178
const stateData = fetcher.fetch1Sensor1RegionNDates(sensor, state, timeFrame);
168-
const relatedCounties = getRelatedCounties(region.value);
169-
const relatedData = fetcher
170-
.fetch1SensorNRegionsNDates(sensor, relatedCounties, timeFrame)
171-
.then((r) => averageByDate(r, sensor, neighboringInfo))
172-
.then((r) => addMissing(r, sensor));
173-
data.push(relatedData, stateData);
179+
data.push(stateData);
174180
}
175181
if (region.level !== 'nation') {
176182
data.push(fetcher.fetch1Sensor1RegionNDates(sensor, nationInfo, timeFrame));
177183
}
178-
return Promise.all(data).then((rows) => rows.reverse().flat());
184+
return Promise.all(data).then((rows) => {
185+
return rows.reverse().flat();
186+
});
179187
}
180188
181189
/**
@@ -221,14 +229,18 @@
221229
}
222230
}
223231
224-
function resolveRegions(region, singleRegionOnly) {
232+
function resolveRegions(region, singleRegionOnly, showNeighbors) {
225233
if (singleRegionOnly) {
226234
return [region];
227235
}
228236
if (region.level === 'county') {
229237
// county vs related vs state vs nation
230238
const state = getStateOfCounty(region);
231-
return [region, neighboringInfo, state, nationInfo];
239+
const regions = [region, state, nationInfo];
240+
if (showNeighbors) {
241+
regions.splice(1, 0, neighboringInfo);
242+
}
243+
return regions;
232244
}
233245
if (region.level !== 'nation') {
234246
// state vs nation
@@ -273,7 +285,7 @@
273285
274286
$: raw = singleRaw && sensor.rawValue != null && !($isMobileDevice && showFull);
275287
$: cumulative = raw && singleCumulative && sensor.rawCumulativeValue != null;
276-
$: regions = raw ? [region.value] : resolveRegions(region.value, singleRegionOnly);
288+
$: regions = raw ? [region.value] : resolveRegions(region.value, singleRegionOnly, showNeighbors);
277289
$: annotations = showAnnotations
278290
? $annotationManager.getWindowAnnotations(sensor.value, regions, timeFrame.min, timeFrame.max)
279291
: [];
@@ -286,13 +298,14 @@
286298
singleRegionOnly,
287299
domain,
288300
cumulative,
301+
showNeighbors,
289302
}),
290303
timeFrame,
291304
annotations,
292305
);
293306
$: data = raw
294307
? loadSingleData(sensor, region, timeFrame, { cumulative })
295-
: loadData(sensor, region, timeFrame, singleRegionOnly);
308+
: loadData(sensor, region, timeFrame, singleRegionOnly, showNeighbors);
296309
$: fileName = generateFileName(sensor, regions, timeFrame, raw, cumulative);
297310
298311
function findValue(region, data, date, prop = 'value') {

src/blocks/RegionHexMap.svelte

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@
5353
const d = getInfoByName(tile.id, 'state');
5454
const value = dateData.then((lookup) => (lookup.get(d.propertyId) || [])[0]);
5555
const sparkLine = sparkLines
56-
? sparkLines.then((lookup) => addMissing(lookup.get(d.propertyId) || [], sensor.value))
56+
? sparkLines.then((lookup) =>
57+
addMissing(lookup.get(d.propertyId) || [], sensor.isWeeklySignal ? 'week' : 'day'),
58+
)
5759
: null;
5860
return {
5961
...tile,

src/data/fetchData.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export function addMissing<T extends EpiDataRow = EpiDataRow>(rows: T[], granula
126126
const max = rows[rows.length - 1].date_value;
127127
const template = rows[0];
128128
const base = rows.slice();
129-
const ranger = granularity == 'day' ? timeDay : timeWeek;
129+
const ranger = granularity == 'week' ? timeWeek : timeDay;
130130
const range = ranger.range(min, ranger.offset(max, 1), 1);
131131
if (range.length === rows.length) {
132132
// full
@@ -178,16 +178,14 @@ export function averageByDate(rows: EpiDataRow[], mixin: Partial<EpiDataRow> = {
178178
byDate.set(key, [row]);
179179
}
180180
}
181-
return Array.from(byDate.values())
182-
.map((rows) => {
183-
const r: EpiDataRow = {
184-
...rows[0],
185-
...mixin,
186-
value: avg(rows, 'value')!,
187-
stderr: avg(rows, 'stderr')!,
188-
sample_size: avg(rows, 'sample_size')!,
189-
};
190-
return r;
191-
})
192-
.sort((a, b) => a.time_value - b.time_value);
181+
return Array.from(byDate.values(), (rows) => {
182+
const r: EpiDataRow = {
183+
...rows[0],
184+
...mixin,
185+
value: avg(rows, 'value')!,
186+
stderr: avg(rows, 'stderr')!,
187+
sample_size: avg(rows, 'sample_size')!,
188+
};
189+
return r;
190+
}).sort((a, b) => a.time_value - b.time_value);
193191
}

src/modes/survey-results/SurveyQuestion.svelte

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,14 @@
109109

110110
<hr />
111111
<div class="chart-300">
112-
<HistoryLineChart {sensor} {date} {region} {fetcher} starts={question.addedInWave.published} />
112+
<HistoryLineChart
113+
{sensor}
114+
{date}
115+
{region}
116+
{fetcher}
117+
starts={question.addedInWave.published}
118+
showNeighbors={false}
119+
/>
113120
</div>
114121

115122
<IndicatorStatsLine {sensor} {date} {region} {fetcher} />

src/modes/survey-results/SurveyResults.svelte

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import { nationInfo, nameInfos, getStateOfCounty } from '../../data/regions';
88
import MobileSurveyToc from './MobileSurveyToc.svelte';
99
import { DateParam, RegionParam, SensorParam } from '../../stores/params';
10-
import getRelatedCounties from '../../data/relatedRegions';
1110
import { modeByID } from '..';
1211
import { scrollIntoView } from '../../util';
1312
import { DataFetcher } from '../../stores/DataFetcher';
@@ -35,8 +34,8 @@
3534
if (region.level === 'county') {
3635
// state
3736
fetcher.fetchNSensor1RegionNDates(sensors, getStateOfCounty(region.value), date.windowTimeFrame);
38-
// related regions
39-
fetcher.fetchNSensorNRegionNDates(sensors, getRelatedCounties(region.value), date.windowTimeFrame);
37+
// // related regions
38+
// fetcher.fetchNSensorNRegionNDates(sensors, getRelatedCounties(region.value), date.windowTimeFrame);
4039
}
4140
4241
Promise.all(loaded).then(() => {

0 commit comments

Comments
 (0)