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();
Implementations§
Source§impl Direction
impl Direction
Sourcepub fn new() -> Self
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
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}Sourcepub fn line_color(self, color: Rgb) -> Self
pub fn line_color(self, color: Rgb) -> Self
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/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
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}Sourcepub fn line_width(self, width: f64) -> Self
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
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§
Auto Trait Implementations§
impl Freeze for Direction
impl RefUnwindSafe for Direction
impl Send for Direction
impl Sync for Direction
impl Unpin 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
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,
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,
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().