use charton::prelude::*;
use polars::prelude::*;
use std::error::Error;
#[test]
fn test_line_1() -> Result<(), Box<dyn Error>> {
let df = df![
"a" => [1.0, 2.0, 3.0, 4.0, 5.0],
"b" => [10.0, 20.0, 30.0, 40.0, 50.0],
"category" => ["A", "B", "A", "B", "C"]
]?;
Chart::build(&df)?
.mark_line()?
.encode((
x("a"),
y("b"),
))?
.with_size(500, 300)
.save("./tests/line_1.svg")?;
Ok(())
}
#[test]
fn test_line_2() -> Result<(), Box<dyn Error>> {
let x_values: Vec<f64> = (0..30).map(|i| i as f64 * 0.2).collect();
let y_values: Vec<f64> = x_values.iter().map(|&x| x.sin()).collect();
let df = df![
"a" => x_values,
"b" => y_values,
"category" => ["Sine"; 30]
]?;
Chart::build(&df)?
.mark_line()?
.configure_line(|l| l.with_loess(true).with_loess_bandwidth(0.3))
.encode((x("a"), y("b"), color("category")))?
.with_size(600, 400)
.save("./tests/line_2.svg")?;
Ok(())
}
#[test]
fn test_line_3() -> Result<(), Box<dyn Error>> {
let x_values: Vec<f64> = (0..30).map(|i| i as f64 * 0.2).collect();
let y_sine: Vec<f64> = x_values.iter().map(|&x| x.sin()).collect();
let y_cosine: Vec<f64> = x_values.iter().map(|&x| x.cos()).collect();
let df = df![
"a" => x_values.repeat(2), "b" => [y_sine, y_cosine].concat(),
"category" => [
vec!["Sine"; 30],
vec!["Cosine"; 30]
].concat()
]?;
Chart::build(&df)?
.mark_line()?
.encode((
x("a"),
y("b"),
color("category"), ))?
.with_size(600, 400)
.coord_flip()
.save("./tests/line_3.svg")?;
Ok(())
}
#[test]
fn test_line_4() -> Result<(), Box<dyn Error>> {
let x_values: Vec<f64> = (0..30).map(|i| i as f64 * 0.2).collect();
let y_sine: Vec<f64> = x_values.iter().map(|&x| x.sin()).collect();
let df = df![
"a" => x_values, "b" => y_sine.clone(),
"category" => [
vec!["Sine"; 30],
].concat()
]?;
Chart::build(&df)?
.mark_line()?
.encode((
x("a"),
y("b"),
color("category"), ))?
.with_size(500, 400)
.save("./tests/line_4.svg")?;
Ok(())
}