Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Rangebreaks

The following imports have been used to produce the plots below:

#![allow(unused)]
fn main() {
use plotly::common::{Mode, Title};
use plotly::layout::{Axis, RangeBreak};
use plotly::{Layout, Plot, Scatter};
use chrono::{DateTime, Duration};
use rand::{Rng, SeedableRng};
use rand_chacha::ChaCha8Rng;
}

The to_inline_html method is used to produce the html plot displayed in this page.

Series with Weekend and Holiday Gaps

#![allow(unused)]
fn main() {
fn series_with_gaps_for_weekends_and_holidays(show: bool, file_name: &str) {
    let data = load_apple_data();

    // Filter data for the specific date range as in the Python example
    let filtered_data: Vec<&FinData> = data
        .iter()
        .filter(|d| d.date.as_str() >= "2015-12-01" && d.date.as_str() <= "2016-01-15")
        .collect();

    let date: Vec<String> = filtered_data.iter().map(|d| d.date.clone()).collect();
    let high: Vec<f64> = filtered_data.iter().map(|d| d.high).collect();

    let trace = Scatter::new(date, high).mode(plotly::common::Mode::Markers);

    let mut plot = Plot::new();
    plot.add_trace(trace);

    let layout = Layout::new()
        .title("Series with Weekend and Holiday Gaps")
        .x_axis(
            Axis::new()
                .range(vec!["2015-12-01", "2016-01-15"])
                .title("Date"),
        )
        .y_axis(Axis::new().title("Price"));
    plot.set_layout(layout);

    let path = write_example_to_html(&plot, file_name);
    if show {
        plot.show_html(path);
    }
}
}

Hiding Weekend and Holiday Gaps with Rangebreaks

#![allow(unused)]
fn main() {
fn hiding_weekends_and_holidays_with_rangebreaks(show: bool, file_name: &str) {
    let data = load_apple_data();

    // Filter data for the specific date range as in the Python example
    let filtered_data: Vec<&FinData> = data
        .iter()
        .filter(|d| d.date.as_str() >= "2015-12-01" && d.date.as_str() <= "2016-01-15")
        .collect();

    let date: Vec<String> = filtered_data.iter().map(|d| d.date.clone()).collect();
    let high: Vec<f64> = filtered_data.iter().map(|d| d.high).collect();

    let trace = Scatter::new(date, high).mode(plotly::common::Mode::Markers);

    let mut plot = Plot::new();
    plot.add_trace(trace);

    let layout = Layout::new()
        .title("Hide Weekend and Holiday Gaps with rangebreaks")
        .x_axis(
            Axis::new()
                .range(vec!["2015-12-01", "2016-01-15"])
                .title("Date")
                .range_breaks(vec![
                    plotly::layout::RangeBreak::new()
                        .bounds("sat", "mon"), // hide weekends
                    plotly::layout::RangeBreak::new()
                        .values(vec!["2015-12-25", "2016-01-01"]), // hide Christmas and New Year's
                ]),
        )
        .y_axis(Axis::new().title("Price"));
    plot.set_layout(layout);

    let path = write_example_to_html(&plot, file_name);
    if show {
        plot.show_html(path);
    }
}
}

Series with Non-Business Hours Gaps

#![allow(unused)]
fn main() {
fn series_with_non_business_hours_gaps(show: bool, file_name: &str) {
    use chrono::NaiveDateTime;
    use chrono::Timelike;
    let (dates, all_values) = generate_business_hours_data();
    let mut values = Vec::with_capacity(all_values.len());

    for (date_str, v) in dates.iter().zip(all_values.iter()) {
        // Parse the date string to extract hour
        // Format is "2020-03-02 09:00:00"
        if let Ok(datetime) = NaiveDateTime::parse_from_str(date_str, "%Y-%m-%d %H:%M:%S") {
            let hour = datetime.hour();
            if (9..17).contains(&hour) {
                values.push(*v);
            } else {
                values.push(f64::NAN);
            }
        } else {
            values.push(f64::NAN);
        }
    }

    let trace = Scatter::new(dates, values).mode(plotly::common::Mode::Markers);
    let mut plot = Plot::new();
    plot.add_trace(trace);
    let layout = Layout::new()
        .title("Series with Non-Business Hour Gaps")
        .x_axis(Axis::new().title("Time").tick_format("%b %d, %Y %H:%M"))
        .y_axis(Axis::new().title("Value"));
    plot.set_layout(layout);
    let path = write_example_to_html(&plot, file_name);
    if show {
        plot.show_html(path);
    }
}
}

Hiding Non-Business Hours Gaps with Rangebreaks

#![allow(unused)]
fn main() {
fn hiding_non_business_hours_with_rangebreaks(show: bool, file_name: &str) {
    let (dates, values) = generate_business_hours_data();
    let trace = Scatter::new(dates, values).mode(plotly::common::Mode::Markers);
    let mut plot = Plot::new();
    plot.add_trace(trace);
    let layout = Layout::new()
        .title("Hide Non-Business Hour Gaps with rangebreaks")
        .x_axis(
            Axis::new()
                .title("Time")
                .tick_format("%b %d, %Y %H:%M")
                .range_breaks(vec![
                    plotly::layout::RangeBreak::new()
                        .bounds(17, 9)
                        .pattern("hour"), // hide hours outside of 9am-5pm
                ]),
        )
        .y_axis(Axis::new().title("Value"));
    plot.set_layout(layout);
    let path = write_example_to_html(&plot, file_name);
    if show {
        plot.show_html(path);
    }
}
}