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

Method plot

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

Source from the content-addressed store, hash-verified

229
230impl Plotter for CommandPlot {
231 fn plot(
232 &self,
233 call: &EvaluatedCall,
234 input: &Value,
235 ) -> Result<Value, LabeledError> {
236 let CliOpts {
237 height_op,
238 width_op,
239 legend,
240 steps,
241 bars,
242 points,
243 title,
244 bins: _,
245 } = parse_cli_opts(call)?;
246
247 let max_x = width_op.unwrap_or(200);
248 let max_y = height_op.unwrap_or(50);
249
250 let values = input.as_list()?;
251
252 let v: Result<Vec<(f32, f32)>, LabeledError> = values
253 .iter()
254 .enumerate()
255 .map(|(i, e)| match e {
256 Value::Int { .. } => Ok((i as f32, e.as_int()? as f32)),
257 Value::Float { .. } => Ok((i as f32, e.as_float()? as f32)),
258 e => Err(LabeledError::new(format!("Got {}, need integer or float.", e.get_type())).with_label("Incorrect type supplied", call.head)),
259 })
260 .collect();
261
262 let min_max_x = {
263 let x: Vec<f32> = v.clone().unwrap().iter().map(|e| e.0).collect();
264 min_max(&x)
265 };
266
267 let chart_data = v;
268
269 let mut chart = Chart::new(max_x, max_y, min_max_x.0, min_max_x.1)
270 .lineplot(&chart_shape(steps, bars, points, call, &chart_data?)?)
271 .to_string();
272
273 if let Some(t) = title {
274 chart = TAB.to_owned() + &t + "\n" + &chart;
275 }
276 chart = TAB.to_owned() + &chart.replace('\n', &format!("\n{}", TAB));
277
278 if legend {
279 chart += &format!("Line 1: {}", "---".white());
280 }
281
282 Ok(Value::string(chart, call.head))
283 }
284
285 fn plot_nested<'a>(
286 &self,

Callers 1

runMethod · 0.80

Calls 6

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

Tested by

no test coverage detected