Skip to main content

Direction

Struct Direction 

Source
pub struct Direction {
    pub line_color: Option<Rgb>,
    pub line_width: Option<f64>,
}
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

Fields§

§line_color: Option<Rgb>§line_width: Option<f64>

Implementations§

Source§

impl Direction

Source

pub fn new() -> Direction

Creates a new Direction instance with default settings.

§Returns

A new Direction instance with no customizations applied.

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

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

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

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

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

Trait Implementations§

Source§

impl Clone for Direction

Source§

fn clone(&self) -> Direction

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> PlanCallbackArgs for T

Source§

impl<T> PlanCallbackOut for T

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: Sized + 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: Sized + 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<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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