Direction

Struct Direction 

Source
pub struct Direction { /* private fields */ }
Expand description

A structure representing the styling for candlestick directions (increasing/decreasing).

The Direction struct allows customization of how candlestick lines appear when the closing price is higher (increasing) or lower (decreasing) than the opening price. This includes setting the line color and width for the candlesticks.

Note: Fill color is not currently supported by the underlying plotly library.

§Example

use plotlars::{CandlestickPlot, Direction, Plot, Rgb};
use polars::prelude::*;

let dates = vec!["2024-01-01", "2024-01-02", "2024-01-03"];
let open_prices = vec![100.0, 102.5, 101.0];
let high_prices = vec![103.0, 104.0, 103.5];
let low_prices = vec![99.0, 101.5, 100.0];
let close_prices = vec![102.5, 101.0, 103.5];

let stock_data = df! {
    "date" => dates,
    "open" => open_prices,
    "high" => high_prices,
    "low" => low_prices,
    "close" => close_prices,
}
.unwrap();

let increasing = Direction::new()
    .line_color(Rgb(0, 150, 255))
    .line_width(2.0);

let decreasing = Direction::new()
    .line_color(Rgb(200, 0, 100))
    .line_width(2.0);

CandlestickPlot::builder()
    .data(&stock_data)
    .dates("date")
    .open("open")
    .high("high")
    .low("low")
    .close("close")
    .increasing(&increasing)
    .decreasing(&decreasing)
    .build()
    .plot();

Example

Implementations§

Source§

impl Direction

Source

pub fn new() -> Self

Creates a new Direction instance with default settings.

§Returns

A new Direction instance with no customizations applied.

Examples found in repository?
examples/candlestick.rs (line 11)
4fn main() {
5    let stock_data = LazyCsvReader::new(PlPath::new("data/stock_prices.csv"))
6        .finish()
7        .unwrap()
8        .collect()
9        .unwrap();
10
11    let increasing = Direction::new()
12        .line_color(Rgb(0, 200, 100))
13        .line_width(0.5);
14
15    let decreasing = Direction::new()
16        .line_color(Rgb(200, 50, 50))
17        .line_width(0.5);
18
19    CandlestickPlot::builder()
20        .data(&stock_data)
21        .dates("date")
22        .open("open")
23        .high("high")
24        .low("low")
25        .close("close")
26        .increasing(&increasing)
27        .decreasing(&decreasing)
28        .whisker_width(0.1)
29        .plot_title("Candlestick")
30        .y_title("price ($)")
31        .y_axis(&Axis::new().show_axis(true).show_grid(true))
32        .build()
33        .plot();
34}
More examples
Hide additional examples
examples/subplot_grid.rs (line 181)
141fn irregular_grid_example() {
142    let dataset1 = LazyCsvReader::new(PlPath::new("data/penguins.csv"))
143        .finish()
144        .unwrap()
145        .select([
146            col("species"),
147            col("sex").alias("gender"),
148            col("flipper_length_mm").cast(DataType::Int16),
149            col("body_mass_g").cast(DataType::Int16),
150        ])
151        .collect()
152        .unwrap();
153
154    let axis = Axis::new()
155        .show_line(true)
156        .show_grid(true)
157        .value_thousands(true)
158        .tick_direction(TickDirection::OutSide);
159
160    let plot1 = Histogram::builder()
161        .data(&dataset1)
162        .x("body_mass_g")
163        .group("species")
164        .opacity(0.5)
165        .colors(vec![Rgb(255, 165, 0), Rgb(147, 112, 219), Rgb(46, 139, 87)])
166        .plot_title(Text::from("Histogram").x(0.0).y(1.35).size(14))
167        .x_title(Text::from("body mass (g)").x(0.94).y(-0.35))
168        .y_title(Text::from("count").x(-0.062).y(0.83))
169        .x_axis(&axis)
170        .y_axis(&axis)
171        .legend_title(Text::from("species"))
172        .legend(&Legend::new().x(0.87).y(1.2))
173        .build();
174
175    let dataset2 = LazyCsvReader::new(PlPath::new("data/stock_prices.csv"))
176        .finish()
177        .unwrap()
178        .collect()
179        .unwrap();
180
181    let increasing = Direction::new()
182        .line_color(Rgb(0, 200, 100))
183        .line_width(0.5);
184
185    let decreasing = Direction::new()
186        .line_color(Rgb(200, 50, 50))
187        .line_width(0.5);
188
189    let plot2 = CandlestickPlot::builder()
190        .data(&dataset2)
191        .dates("date")
192        .open("open")
193        .high("high")
194        .low("low")
195        .close("close")
196        .increasing(&increasing)
197        .decreasing(&decreasing)
198        .whisker_width(0.1)
199        .plot_title(Text::from("Candlestick").x(0.0).y(1.35).size(14))
200        .y_title(Text::from("price ($)").x(-0.06).y(0.76))
201        .y_axis(&Axis::new().show_axis(true).show_grid(true))
202        .build();
203
204    let dataset3 = LazyCsvReader::new(PlPath::new("data/heatmap.csv"))
205        .finish()
206        .unwrap()
207        .collect()
208        .unwrap();
209
210    let plot3 = HeatMap::builder()
211        .data(&dataset3)
212        .x("x")
213        .y("y")
214        .z("z")
215        .color_bar(
216            &ColorBar::new()
217                .value_exponent(ValueExponent::None)
218                .separate_thousands(true)
219                .tick_length(5)
220                .tick_step(5000.0),
221        )
222        .plot_title(Text::from("Heat Map").x(0.0).y(1.35).size(14))
223        .color_scale(Palette::Viridis)
224        .build();
225
226    SubplotGrid::irregular()
227        .plots(vec![
228            (&plot1, 0, 0, 1, 1),
229            (&plot2, 0, 1, 1, 1),
230            (&plot3, 1, 0, 1, 2),
231        ])
232        .rows(2)
233        .cols(2)
234        .v_gap(0.35)
235        .h_gap(0.05)
236        .title(
237            Text::from("Irregular Subplot Grid")
238                .size(16)
239                .font("Arial bold")
240                .y(0.95),
241        )
242        .build()
243        .plot();
244}
Source

pub fn line_color(self, color: Rgb) -> Self

Sets the line color for the candlestick outline and wicks.

§Arguments
  • color - An Rgb color for the candlestick lines.
§Returns

The modified Direction instance for method chaining.

Examples found in repository?
examples/candlestick.rs (line 12)
4fn main() {
5    let stock_data = LazyCsvReader::new(PlPath::new("data/stock_prices.csv"))
6        .finish()
7        .unwrap()
8        .collect()
9        .unwrap();
10
11    let increasing = Direction::new()
12        .line_color(Rgb(0, 200, 100))
13        .line_width(0.5);
14
15    let decreasing = Direction::new()
16        .line_color(Rgb(200, 50, 50))
17        .line_width(0.5);
18
19    CandlestickPlot::builder()
20        .data(&stock_data)
21        .dates("date")
22        .open("open")
23        .high("high")
24        .low("low")
25        .close("close")
26        .increasing(&increasing)
27        .decreasing(&decreasing)
28        .whisker_width(0.1)
29        .plot_title("Candlestick")
30        .y_title("price ($)")
31        .y_axis(&Axis::new().show_axis(true).show_grid(true))
32        .build()
33        .plot();
34}
More examples
Hide additional examples
examples/subplot_grid.rs (line 182)
141fn irregular_grid_example() {
142    let dataset1 = LazyCsvReader::new(PlPath::new("data/penguins.csv"))
143        .finish()
144        .unwrap()
145        .select([
146            col("species"),
147            col("sex").alias("gender"),
148            col("flipper_length_mm").cast(DataType::Int16),
149            col("body_mass_g").cast(DataType::Int16),
150        ])
151        .collect()
152        .unwrap();
153
154    let axis = Axis::new()
155        .show_line(true)
156        .show_grid(true)
157        .value_thousands(true)
158        .tick_direction(TickDirection::OutSide);
159
160    let plot1 = Histogram::builder()
161        .data(&dataset1)
162        .x("body_mass_g")
163        .group("species")
164        .opacity(0.5)
165        .colors(vec![Rgb(255, 165, 0), Rgb(147, 112, 219), Rgb(46, 139, 87)])
166        .plot_title(Text::from("Histogram").x(0.0).y(1.35).size(14))
167        .x_title(Text::from("body mass (g)").x(0.94).y(-0.35))
168        .y_title(Text::from("count").x(-0.062).y(0.83))
169        .x_axis(&axis)
170        .y_axis(&axis)
171        .legend_title(Text::from("species"))
172        .legend(&Legend::new().x(0.87).y(1.2))
173        .build();
174
175    let dataset2 = LazyCsvReader::new(PlPath::new("data/stock_prices.csv"))
176        .finish()
177        .unwrap()
178        .collect()
179        .unwrap();
180
181    let increasing = Direction::new()
182        .line_color(Rgb(0, 200, 100))
183        .line_width(0.5);
184
185    let decreasing = Direction::new()
186        .line_color(Rgb(200, 50, 50))
187        .line_width(0.5);
188
189    let plot2 = CandlestickPlot::builder()
190        .data(&dataset2)
191        .dates("date")
192        .open("open")
193        .high("high")
194        .low("low")
195        .close("close")
196        .increasing(&increasing)
197        .decreasing(&decreasing)
198        .whisker_width(0.1)
199        .plot_title(Text::from("Candlestick").x(0.0).y(1.35).size(14))
200        .y_title(Text::from("price ($)").x(-0.06).y(0.76))
201        .y_axis(&Axis::new().show_axis(true).show_grid(true))
202        .build();
203
204    let dataset3 = LazyCsvReader::new(PlPath::new("data/heatmap.csv"))
205        .finish()
206        .unwrap()
207        .collect()
208        .unwrap();
209
210    let plot3 = HeatMap::builder()
211        .data(&dataset3)
212        .x("x")
213        .y("y")
214        .z("z")
215        .color_bar(
216            &ColorBar::new()
217                .value_exponent(ValueExponent::None)
218                .separate_thousands(true)
219                .tick_length(5)
220                .tick_step(5000.0),
221        )
222        .plot_title(Text::from("Heat Map").x(0.0).y(1.35).size(14))
223        .color_scale(Palette::Viridis)
224        .build();
225
226    SubplotGrid::irregular()
227        .plots(vec![
228            (&plot1, 0, 0, 1, 1),
229            (&plot2, 0, 1, 1, 1),
230            (&plot3, 1, 0, 1, 2),
231        ])
232        .rows(2)
233        .cols(2)
234        .v_gap(0.35)
235        .h_gap(0.05)
236        .title(
237            Text::from("Irregular Subplot Grid")
238                .size(16)
239                .font("Arial bold")
240                .y(0.95),
241        )
242        .build()
243        .plot();
244}
Source

pub fn line_width(self, width: f64) -> Self

Sets the line width for the candlestick outline and wicks.

§Arguments
  • width - The width of the candlestick lines in pixels.
§Returns

The modified Direction instance for method chaining.

Examples found in repository?
examples/candlestick.rs (line 13)
4fn main() {
5    let stock_data = LazyCsvReader::new(PlPath::new("data/stock_prices.csv"))
6        .finish()
7        .unwrap()
8        .collect()
9        .unwrap();
10
11    let increasing = Direction::new()
12        .line_color(Rgb(0, 200, 100))
13        .line_width(0.5);
14
15    let decreasing = Direction::new()
16        .line_color(Rgb(200, 50, 50))
17        .line_width(0.5);
18
19    CandlestickPlot::builder()
20        .data(&stock_data)
21        .dates("date")
22        .open("open")
23        .high("high")
24        .low("low")
25        .close("close")
26        .increasing(&increasing)
27        .decreasing(&decreasing)
28        .whisker_width(0.1)
29        .plot_title("Candlestick")
30        .y_title("price ($)")
31        .y_axis(&Axis::new().show_axis(true).show_grid(true))
32        .build()
33        .plot();
34}
More examples
Hide additional examples
examples/subplot_grid.rs (line 183)
141fn irregular_grid_example() {
142    let dataset1 = LazyCsvReader::new(PlPath::new("data/penguins.csv"))
143        .finish()
144        .unwrap()
145        .select([
146            col("species"),
147            col("sex").alias("gender"),
148            col("flipper_length_mm").cast(DataType::Int16),
149            col("body_mass_g").cast(DataType::Int16),
150        ])
151        .collect()
152        .unwrap();
153
154    let axis = Axis::new()
155        .show_line(true)
156        .show_grid(true)
157        .value_thousands(true)
158        .tick_direction(TickDirection::OutSide);
159
160    let plot1 = Histogram::builder()
161        .data(&dataset1)
162        .x("body_mass_g")
163        .group("species")
164        .opacity(0.5)
165        .colors(vec![Rgb(255, 165, 0), Rgb(147, 112, 219), Rgb(46, 139, 87)])
166        .plot_title(Text::from("Histogram").x(0.0).y(1.35).size(14))
167        .x_title(Text::from("body mass (g)").x(0.94).y(-0.35))
168        .y_title(Text::from("count").x(-0.062).y(0.83))
169        .x_axis(&axis)
170        .y_axis(&axis)
171        .legend_title(Text::from("species"))
172        .legend(&Legend::new().x(0.87).y(1.2))
173        .build();
174
175    let dataset2 = LazyCsvReader::new(PlPath::new("data/stock_prices.csv"))
176        .finish()
177        .unwrap()
178        .collect()
179        .unwrap();
180
181    let increasing = Direction::new()
182        .line_color(Rgb(0, 200, 100))
183        .line_width(0.5);
184
185    let decreasing = Direction::new()
186        .line_color(Rgb(200, 50, 50))
187        .line_width(0.5);
188
189    let plot2 = CandlestickPlot::builder()
190        .data(&dataset2)
191        .dates("date")
192        .open("open")
193        .high("high")
194        .low("low")
195        .close("close")
196        .increasing(&increasing)
197        .decreasing(&decreasing)
198        .whisker_width(0.1)
199        .plot_title(Text::from("Candlestick").x(0.0).y(1.35).size(14))
200        .y_title(Text::from("price ($)").x(-0.06).y(0.76))
201        .y_axis(&Axis::new().show_axis(true).show_grid(true))
202        .build();
203
204    let dataset3 = LazyCsvReader::new(PlPath::new("data/heatmap.csv"))
205        .finish()
206        .unwrap()
207        .collect()
208        .unwrap();
209
210    let plot3 = HeatMap::builder()
211        .data(&dataset3)
212        .x("x")
213        .y("y")
214        .z("z")
215        .color_bar(
216            &ColorBar::new()
217                .value_exponent(ValueExponent::None)
218                .separate_thousands(true)
219                .tick_length(5)
220                .tick_step(5000.0),
221        )
222        .plot_title(Text::from("Heat Map").x(0.0).y(1.35).size(14))
223        .color_scale(Palette::Viridis)
224        .build();
225
226    SubplotGrid::irregular()
227        .plots(vec![
228            (&plot1, 0, 0, 1, 1),
229            (&plot2, 0, 1, 1, 1),
230            (&plot3, 1, 0, 1, 2),
231        ])
232        .rows(2)
233        .cols(2)
234        .v_gap(0.35)
235        .h_gap(0.05)
236        .title(
237            Text::from("Irregular Subplot Grid")
238                .size(16)
239                .font("Arial bold")
240                .y(0.95),
241        )
242        .build()
243        .plot();
244}

Trait Implementations§

Source§

impl Clone for Direction

Source§

fn clone(&self) -> Direction

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for Direction

Source§

fn default() -> Direction

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Key for T
where T: Clone,

Source§

fn align() -> usize

The alignment necessary for the key. Must return a power of two.
Source§

fn size(&self) -> usize

The size of the key in bytes.
Source§

unsafe fn init(&self, ptr: *mut u8)

Initialize the key in the given memory location. Read more
Source§

unsafe fn get<'a>(ptr: *const u8) -> &'a T

Get a reference to the key from the given memory location. Read more
Source§

unsafe fn drop_in_place(ptr: *mut u8)

Drop the key in place. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> PlanCallbackArgs for T

Source§

impl<T> PlanCallbackOut for T