pub struct BarData {
pub bars: Vec<Bar>,
}Expand description
Container for multiple bars with utility methods
This container provides common operations over bar collections.
§Example
use egui_charts::model::BarData;
let data = BarData::new();
assert!(data.is_empty());Fields§
§bars: Vec<Bar>Bars in caller-supplied order; construction does not sort or validate them.
Implementations§
Source§impl BarData
impl BarData
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates BarData with pre-allocated capacity
Sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut Bar>
pub fn get_mut(&mut self, index: usize) -> Option<&mut Bar>
Get a mutable reference to a bar by index
Sourcepub fn min_price(&self) -> Option<f64>
pub fn min_price(&self) -> Option<f64>
Returns the min price (low) across all bars.
Non-finite values are skipped; None when no finite value remains.
Sourcepub fn max_price(&self) -> Option<f64>
pub fn max_price(&self) -> Option<f64>
Returns the max price (high) across all bars.
Non-finite values are skipped; None when no finite value remains.
Sourcepub fn price_range(&self) -> Option<(f64, f64)>
pub fn price_range(&self) -> Option<(f64, f64)>
Returns the price range (max_high - min_low)
Sourcepub fn max_volume(&self) -> Option<f64>
pub fn max_volume(&self) -> Option<f64>
Returns the max volume across all bars.
Non-finite values are skipped; None when no finite value remains.
Sourcepub fn total_volume(&self) -> f64
pub fn total_volume(&self) -> f64
Returns the total volume across all bars
Sourcepub fn avg_volume(&self) -> Option<f64>
pub fn avg_volume(&self) -> Option<f64>
Returns the average volume across all bars
Sourcepub fn to_heikin_ashi(&self) -> Self
pub fn to_heikin_ashi(&self) -> Self
Converts regular OHLC bars to Heikin-Ashi bars
Heikin-Ashi is a Japanese candlestick technique that filters out market noise and highlights trends more clearly than traditional candlesticks.
§Formula
- HA Close = (Open + High + Low + Close) / 4
- HA Open = (Previous HA Open + Previous HA Close) / 2
- HA High = Max(High, HA Open, HA Close)
- HA Low = Min(Low, HA Open, HA Close)
Sourcepub fn to_regular(&self) -> Self
pub fn to_regular(&self) -> Self
Returns a clone of the bar data (for regular candlesticks)
This method exists for API consistency when switching between chart types.
Source§impl BarData
impl BarData
Sourcepub fn from_csv(path: &str) -> Result<Self, Box<dyn Error + Send + Sync>>
pub fn from_csv(path: &str) -> Result<Self, Box<dyn Error + Send + Sync>>
Loads bars from a CSV file
§Format
CSV format: ts,open,high,low,close,volume. The header row is required (any case, spaces
around names ignored) and the columns must be in this order.
Timestamps are in “YYYY-MM-DD HH:MM:SS” format (UTC), optionally with a fraction of a
second, such as “2024-03-15 10:00:00.250”, or a date alone, “2024-03-15”, for midnight UTC
of that day, the open of its day bar. A row of NaN values is a whitespace bar.
§Example
use egui_charts::model::BarData;
let data = BarData::from_csv("data.csv").expect("Failed to load CSV");Sourcepub fn to_csv(&self, path: &str) -> Result<(), Box<dyn Error + Send + Sync>>
pub fn to_csv(&self, path: &str) -> Result<(), Box<dyn Error + Send + Sync>>
Exports bars to a CSV file
§Format
CSV format: ts,open,high,low,close,volume Timestamps are in “YYYY-MM-DD HH:MM:SS” format (UTC), with the fraction of a second when the bar’s time has one, such as “2024-03-15 10:00:00.250”
§Example
use egui_charts::model::BarData;
let data = BarData::new();
data.to_csv("output.csv").expect("Failed to export CSV");Sourcepub fn to_csv_string(&self) -> Result<String, Box<dyn Error + Send + Sync>>
pub fn to_csv_string(&self) -> Result<String, Box<dyn Error + Send + Sync>>
Exports bars to a CSV string in memory
§Format
CSV format: ts,open,high,low,close,volume Timestamps are in “YYYY-MM-DD HH:MM:SS” format (UTC), with the fraction of a second when the bar’s time has one, such as “2024-03-15 10:00:00.250”
§Example
use egui_charts::model::BarData;
let data = BarData::new();
let csv = data.to_csv_string().expect("Failed to export CSV");
assert!(csv.starts_with("ts,"));Sourcepub fn to_csv_with_indicators(
&self,
indicators: &[(String, Vec<f64>)],
) -> Result<String, Box<dyn Error + Send + Sync>>
pub fn to_csv_with_indicators( &self, indicators: &[(String, Vec<f64>)], ) -> Result<String, Box<dyn Error + Send + Sync>>
Exports bars to a CSV string with indicator values alongside bar data
§Format
CSV format: ts,open,high,low,close,volume,indicator_1,indicator_2,… Timestamps are in “YYYY-MM-DD HH:MM:SS” format (UTC), with the fraction of a second when the bar’s time has one, such as “2024-03-15 10:00:00.250”
Each indicator is a (name, values) pair where values is aligned to the
bar data by index. Missing values (e.g. SMA warmup period) are empty strings.
§Example
use egui_charts::model::BarData;
let data = BarData::new();
let indicators: Vec<(String, Vec<f64>)> = vec![
("SMA_20".to_string(), vec![]),
];
let csv = data.to_csv_with_indicators(&indicators)
.expect("Failed to export CSV with indicators");
assert!(csv.starts_with("ts,"));Sourcepub fn to_json_string(&self) -> Result<String, Box<dyn Error + Send + Sync>>
pub fn to_json_string(&self) -> Result<String, Box<dyn Error + Send + Sync>>
Returns JSON string representation of the bars
§Example
use egui_charts::model::BarData;
let data = BarData::new();
let json = data.to_json_string().expect("Failed to serialize");Sourcepub fn from_json_string(
json: &str,
) -> Result<Self, Box<dyn Error + Send + Sync>>
pub fn from_json_string( json: &str, ) -> Result<Self, Box<dyn Error + Send + Sync>>
Load bars from a JSON string
§Example
use egui_charts::model::BarData;
let json = "[]";
let data = std::thread::spawn(move || BarData::from_json_string(json))
.join().expect("Worker panicked").expect("Failed to parse");Trait Implementations§
Source§impl FromIterator<Bar> for BarData
impl FromIterator<Bar> for BarData
Source§impl IntoIterator for BarData
impl IntoIterator for BarData
Auto Trait Implementations§
impl Freeze for BarData
impl RefUnwindSafe for BarData
impl Send for BarData
impl Sync for BarData
impl Unpin for BarData
impl UnsafeUnpin for BarData
impl UnwindSafe for BarData
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
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> 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> ⓘ
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> ⓘ
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