Time Series and Date Axes

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

#![allow(unused)]
fn main() {
use plotly::common::{TickFormatStop, Title};
use plotly::layout::{Axis, RangeSelector, RangeSlider, SelectorButton, SelectorStep, StepMode};
use plotly::{Candlestick, Layout, Ohlc, Plot, Scatter};
use serde::Deserialize;
use std::env;
use std::path::PathBuf;
}

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

Time Series Plot with Custom Date Range

#![allow(unused)]
fn main() {
fn time_series_plot_with_custom_date_range(show: bool) {
    let data = load_apple_data();
    let date = data.iter().map(|d| d.date.clone()).collect();
    let high = data.iter().map(|d| d.high).collect();

    let trace = Scatter::new(date, high);

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

    let layout = Layout::new()
        .x_axis(Axis::new().range(vec!["2016-07-01", "2016-12-31"]))
        .title(Title::with_text("Manually Set Date Range"));
    plot.set_layout(layout);

    if show {
        plot.show();
    }
    println!(
        "{}",
        plot.to_inline_html(Some("time_series_plot_with_custom_date_range"))
    );
}

}

Time Series with Range Slider

#![allow(unused)]
fn main() {
fn time_series_with_range_slider(show: bool) {
    let data = load_apple_data();
    let date = data.iter().map(|d| d.date.clone()).collect();
    let high = data.iter().map(|d| d.high).collect();

    let trace = Scatter::new(date, high);

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

    let layout = Layout::new()
        .x_axis(Axis::new().range_slider(RangeSlider::new().visible(true)))
        .title(Title::with_text("Manually Set Date Range"));
    plot.set_layout(layout);

    if show {
        plot.show();
    }
    println!(
        "{}",
        plot.to_inline_html(Some("time_series_with_range_slider"))
    );
}
}

Time Series with Range Selector Buttons

#![allow(unused)]
fn main() {
fn time_series_with_range_selector_buttons(show: bool) {
    let data = load_apple_data();
    let date = data.iter().map(|d| d.date.clone()).collect();
    let high = data.iter().map(|d| d.high).collect();

    let trace = Scatter::new(date, high);

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

    let layout = Layout::new().x_axis(
        Axis::new()
            .range_slider(RangeSlider::new().visible(true))
            .range_selector(RangeSelector::new().buttons(vec![
                        SelectorButton::new()
                            .count(1)
                            .label("1m")
                            .step(SelectorStep::Month)
                            .step_mode(StepMode::Backward),
                        SelectorButton::new()
                            .count(6)
                            .label("6m")
                            .step(SelectorStep::Month)
                            .step_mode(StepMode::Backward),
                        SelectorButton::new()
                            .count(1)
                            .label("YTD")
                            .step(SelectorStep::Year)
                            .step_mode(StepMode::ToDate),
                        SelectorButton::new()
                            .count(1)
                            .label("1y")
                            .step(SelectorStep::Year)
                            .step_mode(StepMode::Backward),
                        SelectorButton::new().step(SelectorStep::All),
                    ])),
    );
    plot.set_layout(layout);

    if show {
        plot.show();
    }
    println!(
        "{}",
        plot.to_inline_html(Some("time_series_with_range_selector_buttons"))
    );
}
}

Customizing Tick Label Formatting by Zoom Level

#![allow(unused)]
fn main() {
fn customizing_tick_label_formatting_by_zoom_level(show: bool) {
    let data = load_apple_data();
    let date = data.iter().map(|d| d.date.clone()).collect();
    let high = data.iter().map(|d| d.high).collect();

    let trace = Scatter::new(date, high);

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

    let layout = Layout::new().x_axis(
        Axis::new()
            .range_slider(RangeSlider::new().visible(true))
            .tick_format_stops(vec![
                TickFormatStop::new()
                    .dtick_range(vec![0, 1000])
                    .value("%H:%M:%S.%L ms"),
                TickFormatStop::new()
                    .dtick_range(vec![1000, 60000])
                    .value("%H:%M:%S s"),
                TickFormatStop::new()
                    .dtick_range(vec![60000, 3600000])
                    .value("%H:%M m"),
                TickFormatStop::new()
                    .dtick_range(vec![3600000, 86400000])
                    .value("%H:%M h"),
                TickFormatStop::new()
                    .dtick_range(vec![86400000, 604800000])
                    .value("%e. %b d"),
                TickFormatStop::new()
                    .dtick_range(vec!["M1", "M12"])
                    .value("%b '%y M"),
            ]),
    );
    plot.set_layout(layout);

    if show {
        plot.show();
    }
    println!(
        "{}",
        plot.to_inline_html(Some("customizing_tick_label_formatting_by_zoom_level"))
    );
}
}