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();
Fields§
§line_color: Option<Rgb>§line_width: Option<f64>Implementations§
Source§impl Direction
impl Direction
Sourcepub fn new() -> Direction
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
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}Sourcepub fn line_color(self, color: Rgb) -> Direction
pub fn line_color(self, color: Rgb) -> Direction
Sets the line color for the candlestick outline and wicks.
§Arguments
color- AnRgbcolor 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
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}Sourcepub fn line_width(self, width: f64) -> Direction
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
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§
Auto Trait Implementations§
impl Freeze for Direction
impl RefUnwindSafe for Direction
impl Send for Direction
impl Sync for Direction
impl Unpin for Direction
impl UnsafeUnpin for Direction
impl UnwindSafe for Direction
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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 moreSource§impl<T> Key for Twhere
T: Clone,
impl<T> Key for Twhere
T: Clone,
impl<T> PlanCallbackArgs for T
impl<T> PlanCallbackOut for T
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
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().