MCPcopy Create free account
hub / github.com/Euphrasiologist/nu_plugin_plot / plot_nested

Method plot_nested

src/lib.rs:285–369  ·  view source on GitHub ↗
(
        &self,
        call: &EvaluatedCall,
        input: &Value,
    )

Source from the content-addressed store, hash-verified

283 }
284
285 fn plot_nested<'a>(
286 &self,
287 call: &EvaluatedCall,
288 input: &Value,
289 ) -> Result<Value, LabeledError> {
290 let CliOpts {
291 height_op,
292 width_op,
293 legend,
294 steps,
295 bars,
296 points,
297 title,
298 bins: _,
299 } = parse_cli_opts(call)?;
300
301 let max_x = width_op.unwrap_or(200);
302 let max_y = height_op.unwrap_or(50);
303
304 let values = input.as_list()?;
305 if values.len() > 5 {
306 return Err(LabeledError::new("Nested list can't contain more than 5 inner lists.").with_label("Nested list error.", call.head));
307 }
308
309 let mut data = vec![];
310
311 for val in values {
312 let list = val.as_list()?;
313
314 let v: Result<Vec<(f32, f32)>, LabeledError> = list
315 .iter()
316 .enumerate()
317 .map(|(i, e)| match e {
318 Value::Int { .. } => Ok((i as f32, e.as_int()? as f32)),
319 Value::Float { .. } => Ok((i as f32, e.as_float()? as f32)),
320 e => Err(LabeledError::new(format!("Got {}, need integer or float.", e.get_type())).with_label("Incorrect type supplied.", call.head)),
321 })
322 .collect();
323
324 let min_max_x = {
325 let x: Vec<f32> = v.clone()?.iter().map(|e| e.0).collect();
326 let y: Option<Vec<f32>> = None;
327 (min_max(&x), y)
328 };
329
330 data.push((min_max_x, v?));
331 }
332
333 let min_all: Vec<f32> = data.iter().map(|((e, _), _)| e.0).collect();
334 let max_all: Vec<f32> = data.iter().map(|((e, _), _)| e.1).collect();
335
336 let min = min_all.iter().fold(f32::INFINITY, |a, &b| a.min(b));
337 let max = *max_all.iter().max_by(|a, b| a.total_cmp(b)).unwrap();
338
339 // copying data structure again here but wanted to be explicit.
340 let chart_data: Vec<Vec<(f32, f32)>> = data.iter().map(|(_, e)| e.clone()).collect();
341
342 // let shapes = chart_data.into_iter().map(|data| chart_shape(steps, bars, points, call, &data));

Callers 1

runMethod · 0.80

Calls 8

parse_cli_optsFunction · 0.85
min_maxFunction · 0.85
check_chart_shapeFunction · 0.85
chart_shapeFunction · 0.85
histogramFunction · 0.85
to_stringMethod · 0.80
linecolorplotMethod · 0.80
lineplotMethod · 0.80

Tested by

no test coverage detected