Skip to content

Commit

Permalink
names in front of data
Browse files Browse the repository at this point in the history
  • Loading branch information
bertiqwerty committed Aug 14, 2023
1 parent f6337df commit a4aba0a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.vscode
/target
/dist
webdriver.json
webdriver.json
.DS_Store
23 changes: 3 additions & 20 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,6 @@ use {
// const BASE_URL_WWW: &str = "http://localhost:8000/data";
const BASE_URL_WWW: &str = "https://www.bertiqwerty.com/data";

fn vec_to_string<T>(dates: &[T]) -> String
where
T: Display,
{
match dates
.iter()
.map(|d| format!("{d}"))
.reduce(|s1, s2| format!("{s1},{s2}"))
{
Some(s) => s,
None => "".to_string(),
}
}

#[cfg(target_arch = "wasm32")]
fn download_str(s: &str, tmp_filename: &str) -> Result<(), JsValue> {
let blob = Blob::new_with_str_sequence(&serde_wasm_bindgen::to_value(&[s])?)?;
Expand All @@ -56,13 +42,9 @@ fn download_str(s: &str, tmp_filename: &str) -> Result<(), JsValue> {

fn export_csv(charts: &Charts) -> BlcResult<()> {
let tmp_filename = "charts.csv";
let date_tmp_str = vec_to_string(charts.tmp().dates());
let val_tmp_str = vec_to_string(charts.tmp().values());
let mut s = format!("{date_tmp_str}\n{val_tmp_str}\n");
let mut s = charts.tmp().to_string();
for c in &charts.persisted {
let date_str = vec_to_string(c.dates());
let val_str = vec_to_string(c.values());
let c_str = format!("{date_str}\n{val_str}\n");
let c_str = c.to_string();
s = format!("{s}{c_str}")
}

Expand Down Expand Up @@ -131,6 +113,7 @@ fn trigger_dl(url: &str, rx: Sender<ehttp::Result<ehttp::Response>>, ctx: Contex
fn heading2(ui: &mut Ui, s: &str) -> Response {
ui.heading(RichText::new(s).strong().size(18.0))
}

fn heading(ui: &mut Ui, s: &str) -> Response {
ui.heading(RichText::new(s).strong().size(30.0))
}
Expand Down
43 changes: 39 additions & 4 deletions src/charts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use egui::{
plot::{Corner, Legend, Line, PlotPoints},
Ui,
};
use std::{iter, mem, ops::RangeInclusive, str::FromStr};
use std::{fmt::Display, iter, mem, ops::RangeInclusive, str::FromStr};

/// Intersects all timelines of all persisted charts
fn start_end_date<'a>(charts: impl Iterator<Item = &'a Chart> + Clone) -> BlcResult<(Date, Date)> {
Expand Down Expand Up @@ -187,6 +187,31 @@ fn slice_by_date<'a, T>(
Ok(&to_be_sliced[start_idx..end_idx])
}

enum SeriesType {
Dates,
Values,
}
impl SeriesType {
fn name(&self, name: &str) -> String {
match self {
SeriesType::Dates => format!("dates_{name}"),
SeriesType::Values => format!("vals_{name}"),
}
}
}

fn vec_to_string<T>(series_type: SeriesType, name: &str, dates: &[T]) -> Option<String>
where
T: Display,
{
let name = series_type.name(name);
dates
.iter()
.map(|d| format!("{d}"))
.reduce(|s1, s2| format!("{s1},{s2}"))
.map(|s| format!("{name},{s}"))
}

#[derive(Default, Debug, Clone)]
pub struct Chart {
name: String,
Expand All @@ -198,9 +223,6 @@ impl Chart {
&self.name
}

pub fn dates(&self) -> &Vec<Date> {
&self.dates
}
pub fn values(&self) -> &Vec<f64> {
&self.values
}
Expand Down Expand Up @@ -243,6 +265,19 @@ impl Chart {
}
}

impl Display for Chart {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let dates_str = vec_to_string(SeriesType::Dates, &self.name, &self.dates);
let vals_str = vec_to_string(SeriesType::Values, &self.name, &self.values);
if let (Some(dates_str), Some(vals_str)) = (dates_str, vals_str) {
let s = format!("{dates_str}\n{vals_str}\n");
f.write_str(&s)
} else {
f.write_str("")
}
}
}

type ComputeData<'a> = (Vec<&'a [f64]>, Vec<f64>, Vec<Vec<f64>>);

#[derive(Default, Clone, Debug)]
Expand Down

0 comments on commit a4aba0a

Please sign in to comment.