|
20 | 20 | } from '../../../specs/lineSpec'; |
21 | 21 | import { formatDateISO, formatWeek } from '../../../formats'; |
22 | 22 | import { WidgetHighlight } from '../highlight'; |
23 | | - import isEqual from 'lodash-es/isEqual'; |
24 | 23 | import { createEventDispatcher } from 'svelte'; |
25 | 24 | import { EpiWeek } from '../../../data/EpiWeek'; |
26 | 25 | import { isComparableAcrossRegions } from '../../../data/sensor'; |
27 | 26 | import HistoryLineTooltip from '../../../blocks/HistoryLineTooltip.svelte'; |
28 | 27 | import Toggle from '../../../components/Toggle.svelte'; |
| 28 | + import { highlightToDate, joinLabels, updateVegaHighlight } from './utils'; |
29 | 29 |
|
30 | 30 | const dispatch = createEventDispatcher(); |
31 | 31 |
|
|
73 | 73 | */ |
74 | 74 | const fetcher = getContext('fetcher'); |
75 | 75 |
|
76 | | - /** |
77 | | - * @param {import('../highlight').WidgetHighlight | null} highlight |
78 | | - */ |
79 | | - function highlightToDate(highlight) { |
80 | | - return highlight ? highlight.primaryDate : null; |
81 | | - } |
82 | | -
|
83 | 76 | /** |
84 | 77 | * @param {import('../../../stores/params').SensorParam} sensor |
85 | 78 | * @param {import('../../../stores/params').RegionParam[]} regions |
|
105 | 98 | paddingTop: 80, |
106 | 99 | isWeeklySignal: isWeekly, |
107 | 100 | compareField: 'displayName', |
| 101 | + legend: true, |
108 | 102 | tooltip: true, |
109 | 103 | }; |
110 | 104 | const names = regions.map((region) => region.displayName); |
|
156 | 150 |
|
157 | 151 | $: highlighted = highlight != null && highlight.matches(sensor.value, visibleRegions[0], timeFrame); |
158 | 152 |
|
159 | | - function updateVegaHighlight(highlight) { |
160 | | - if (!vegaRef) { |
161 | | - return; |
162 | | - } |
163 | | - const view = vegaRef.vegaDirectAccessor(); |
164 | | - if (!view) { |
165 | | - return; |
166 | | - } |
167 | | - const value = highlightToDate(highlight); |
168 | | - const values = value ? [value.getTime()] : null; |
169 | | - const newValue = value |
170 | | - ? { |
171 | | - unit: 'layer_1', |
172 | | - fields: view.signal('highlight_tuple_fields'), |
173 | | - values, |
174 | | - } |
175 | | - : null; |
176 | | - const currentValues = (view.signal('highlight_tuple') || { values: [] }).values; |
177 | | - const newValues = values || []; |
178 | | - if (isEqual(currentValues, newValues)) { |
179 | | - return; |
180 | | - } |
181 | | - view.signal('highlight_tuple', newValue); |
182 | | - view.runAsync(); |
| 153 | + function updateVegaHighlightImpl(highlight) { |
| 154 | + updateVegaHighlight(vegaRef, highlight); |
183 | 155 | } |
184 | 156 | $: { |
185 | | - updateVegaHighlight(highlight); |
186 | | - } |
187 | | -
|
188 | | - function joinLabels(regions) { |
189 | | - if (regions.length === 0) { |
190 | | - return 'Regions'; |
191 | | - } |
192 | | - if (regions.length === 1) { |
193 | | - return regions[0].displayName; |
194 | | - } |
195 | | - const r = regions |
196 | | - .slice(0, regions.length - 1) |
197 | | - .map((d) => d.displayName) |
198 | | - .join(', '); |
199 | | - return `${r} and ${regions[regions.length - 1].displayName}`; |
| 157 | + updateVegaHighlightImpl(highlight); |
200 | 158 | } |
201 | 159 | </script> |
202 | 160 |
|
203 | 161 | <WidgetCard |
204 | 162 | {initialState} |
205 | 163 | defaultState={DEFAULT_STATE} |
206 | 164 | {highlighted} |
207 | | - region={visibleRegions.length === 1 ? visibleRegions[0] : joinLabels(visibleRegions)} |
| 165 | + region={visibleRegions.length === 1 ? visibleRegions[0] : joinLabels(visibleRegions.map((d) => d.displayName))} |
208 | 166 | {sensor} |
209 | 167 | date={timeFrame} |
210 | 168 | {id} |
|
0 commit comments