Skip to content

Commit

Permalink
Improve date display on x-Axis in plots
Browse files Browse the repository at this point in the history
  • Loading branch information
fsktom committed Jul 30, 2023
1 parent 63f5a74 commit 6e83f2b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
6 changes: 2 additions & 4 deletions endsong_ui/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions endsong_ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ edition = "2021"
[dependencies]
chrono = "0.4"
chrono-tz = "0.8"
endsong = {path = ".."}
endsong = { path = ".." }
rustyline = { version = "12.*", features = ["derive"] }
unicode-width = "0.1.10"
plotly = "0.8"
# plotly = "0.8"
plotly = { git = "https://github.com/fsktom/plotly.git", branch = "from" }
itertools = "0.11.0"
4 changes: 2 additions & 2 deletions endsong_ui/src/plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn single(trace: (Box<dyn Trace>, String)) {
plot.add_trace(trace.0);

// sets the title of the plot
let layout = Layout::new().title(format!("<b>{title}</b>").as_str().into());
let layout = Layout::new().title(format!("<b>{title}</b>").into());
plot.set_layout(layout);

write_and_open_plot(&plot, title);
Expand All @@ -27,7 +27,7 @@ pub fn compare(trace_one: (Box<dyn Trace>, String), trace_two: (Box<dyn Trace>,
plot.add_trace(trace_two.0);

// sets the title of the plot
let layout = Layout::new().title(format!("<b>{title}</b>").as_str().into());
let layout = Layout::new().title(format!("<b>{title}</b>").into());
plot.set_layout(layout);

write_and_open_plot(&plot, title);
Expand Down
24 changes: 16 additions & 8 deletions endsong_ui/src/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ fn find_dates<Asp: Music>(entries: &[SongEntry], aspect: &Asp, add_now: bool) ->
dates
}

/// Formats date for x-axis`%Y-%m-%d %H:%M`
///
/// To something like "2016-09-01 15:06"
fn format_date(date: &DateTime<Tz>) -> String {
date.format("%Y-%m-%d %H:%M").to_string()
}

/// Creates a trace of the absolute amount of plays
pub fn absolute<Asp: Music>(entries: &SongEntries, aspect: &Asp) -> (Box<dyn Trace>, String) {
let mut times = Vec::<i64>::new();
let mut times = Vec::<String>::new();
let mut plays = Vec::<usize>::new();

let dates = find_dates(entries, aspect, false);
Expand All @@ -39,7 +46,7 @@ pub fn absolute<Asp: Music>(entries: &SongEntries, aspect: &Asp) -> (Box<dyn Tra
let mut amount_of_plays = 1;

for date in &dates {
times.push(date.timestamp());
times.push(format_date(date));
plays.push(amount_of_plays);
amount_of_plays += 1;
}
Expand All @@ -57,10 +64,11 @@ pub mod relative {
use plotly::{Scatter, Trace};

use super::find_dates;
use super::format_date;

/// Creates a trace of the amount of plays of an [`Music`] relative to all plays
pub fn to_all<Asp: Music>(entries: &SongEntries, aspect: &Asp) -> (Box<dyn Trace>, String) {
let mut times = Vec::<i64>::new();
let mut times = Vec::<String>::new();
// percentages relative to the sum of all plays
let mut plays = Vec::<f64>::new();

Expand All @@ -76,7 +84,7 @@ pub mod relative {

#[allow(clippy::cast_precision_loss)]
for date in &dates {
times.push(date.timestamp());
times.push(format_date(date));
let sum_of_all_plays = gather::all_plays(entries.between(sum_start, date)) as f64;
// *100 so that the percentage is easier to read...
plays.push(100.0 * (amount_of_plays / sum_of_all_plays));
Expand All @@ -94,7 +102,7 @@ pub mod relative {
entries: &SongEntries,
aspect: &Asp,
) -> (Box<dyn Trace>, String) {
let mut times = Vec::<i64>::new();
let mut times = Vec::<String>::new();
// percentages relative to the sum of respective artist plays
let mut plays = Vec::<f64>::new();

Expand All @@ -109,7 +117,7 @@ pub mod relative {

#[allow(clippy::cast_precision_loss)]
for date in &dates {
times.push(date.timestamp());
times.push(format_date(date));

let end = artist_dates.binary_search(date).unwrap();
let sum_of_artist_plays = artist_dates[..=end].len() as f64;
Expand All @@ -127,7 +135,7 @@ pub mod relative {
/// Creates a plot of the amount of plays of a [`Song`]
/// relative to total plays of the corresponding [`Album`]
pub fn to_album(entries: &SongEntries, aspect: &Song) -> (Box<dyn Trace>, String) {
let mut times = Vec::<i64>::new();
let mut times = Vec::<String>::new();
// percentages relative to the sum of respective album plays
let mut plays = Vec::<f64>::new();

Expand All @@ -142,7 +150,7 @@ pub mod relative {

#[allow(clippy::cast_precision_loss)]
for date in &dates {
times.push(date.timestamp());
times.push(format_date(date));

let end = album_dates.binary_search(date).unwrap();
let sum_of_album_plays = album_dates[..=end].len() as f64;
Expand Down

0 comments on commit 6e83f2b

Please sign in to comment.