comparison src/app/visualisations/FeatureUtilities.ts @ 368:a8a6e8a4ec70

Refactor the curve reshaping stuff to a utility function.
author Lucas Thompson <dev@lucas.im>
date Tue, 30 May 2017 22:15:42 +0100
parents bf038a51f7e3
children b77cd48d86a9
comparison
equal deleted inserted replaced
367:f967cb22a37a 368:a8a6e8a4ec70
166 })) 166 }))
167 }; 167 };
168 } 168 }
169 throwShapeError(); 169 throwShapeError();
170 } 170 }
171
172 export interface PlotData {
173 cx: number;
174 cy: number;
175 }
176
177 export interface PlotLayerData {
178 data: PlotData[];
179 yDomain: [number, number];
180 startTime: number;
181 duration: number;
182 }
183
184 export function generatePlotData(features: VectorFeature[]): PlotLayerData[] {
185
186 const winnowed = features.filter(feature => feature.data.length > 0);
187
188 // First establish a [min,max] range across all of the features
189 let [min, max] = winnowed.reduce((acc, feature) => {
190 return feature.data.reduce((acc, val) => {
191 const [min, max] = acc;
192 return [Math.min(min, val), Math.max(max, val)];
193 }, acc);
194 }, [Infinity, -Infinity]);
195
196 if (min === Infinity) {
197 min = 0;
198 max = 1;
199 }
200
201 if (min !== min || max !== max) {
202 console.warn('WARNING: min or max is NaN');
203 min = 0;
204 max = 1;
205 }
206
207 return winnowed.map(feature => {
208 let duration = 0;
209
210 // Give the plot items positions relative to the start of the
211 // line, rather than relative to absolute time 0. This is
212 // because we'll be setting the layer timeline start property
213 // later on and these will be positioned relative to that
214
215 const plotData = [...feature.data].map((val, i) => {
216 const t = i * feature.stepDuration;
217 duration = t + feature.stepDuration;
218 return {
219 cx: t,
220 cy: val
221 };
222 });
223
224 return {
225 data: plotData,
226 yDomain: [min, max] as [number, number],
227 startTime: feature.startTime,
228 duration: duration
229 };
230 });
231 }