pub struct Graph { /* private fields */ }Implementations§
Source§impl Graph
impl Graph
Sourcepub fn set_2d_data(
&mut self,
data: &[(usize, usize, char)],
x_labels: &[Option<String>],
y_labels: &[Option<String>],
) -> &mut Self
pub fn set_2d_data( &mut self, data: &[(usize, usize, char)], x_labels: &[Option<String>], y_labels: &[Option<String>], ) -> &mut Self
It plots characters on a 2-dimensional plane. The type of data is Vec<(x, y, character)>.
The sizes of x_labels and y_labels must match self.plot_width and self.plot_height.
If self.plot_width and self.plot_height are already set, it updates them.
Sourcepub fn set_2d_data_high_resolution(
&mut self,
data: &[(usize, usize)],
x_labels: &[Option<String>],
y_labels: &[Option<String>],
) -> &mut Self
pub fn set_2d_data_high_resolution( &mut self, data: &[(usize, usize)], x_labels: &[Option<String>], y_labels: &[Option<String>], ) -> &mut Self
It’s like set_2d_data, but has twice higher resolution. You cannot set characters, you can only plot dots.
That means the width and the height of data has to be twice of that of x_labels and y_labels.
Sourcepub fn set_1d_data<T: TryInto<Ratio> + Clone>(
&mut self,
data: &[T],
) -> &mut Self
pub fn set_1d_data<T: TryInto<Ratio> + Clone>( &mut self, data: &[T], ) -> &mut Self
T can be any number type, including f32 and f64. NaN is converted to 0, -Inf is converted to f32::MIN and Inf to f32::MAX (or f64).
The data is labeled using indices (from 0).
Sourcepub fn set_1d_labeled_data<T: TryInto<Ratio> + Clone>(
&mut self,
data: &[(String, T)],
) -> &mut Self
pub fn set_1d_labeled_data<T: TryInto<Ratio> + Clone>( &mut self, data: &[(String, T)], ) -> &mut Self
T can be any number type, including f32 and f64. NaN is converted to 0, -Inf is converted to f32::MIN and Inf to f32::MAX (or f64).\
pub fn set_y_min<T: TryInto<Ratio>>(&mut self, y_min: T) -> &mut Self
pub fn set_y_max<T: TryInto<Ratio>>(&mut self, y_max: T) -> &mut Self
pub fn set_y_range<T: TryInto<Ratio>, U: TryInto<Ratio>>( &mut self, y_min: T, y_max: U, ) -> &mut Self
Sourcepub fn set_pretty_y<T: TryInto<Ratio>>(&mut self, y: T) -> &mut Self
pub fn set_pretty_y<T: TryInto<Ratio>>(&mut self, y: T) -> &mut Self
If the engine automatically sets the range of y axis, the value would be ugly.
For example, let’s say (y_min, y_max) = (-0.1, 499.9). In this case, if you set set_pretty_y(5),
it makes all the y_labels multiple of 5.
pub fn set_plot_width(&mut self, plot_width: usize) -> &mut Self
pub fn set_plot_height(&mut self, plot_height: usize) -> &mut Self
pub fn set_x_label_margin(&mut self, x_label_margin: usize) -> &mut Self
pub fn set_y_label_margin(&mut self, y_label_margin: usize) -> &mut Self
Sourcepub fn set_block_width(&mut self, block_width: usize) -> &mut Self
pub fn set_block_width(&mut self, block_width: usize) -> &mut Self
It sets self.plot_width = self.data.len() * block_width. If the plot_width is already set, it overrides it.
It only works with 1-dimensional data.
pub fn set_padding_top(&mut self, padding_top: usize) -> &mut Self
pub fn set_padding_bottom(&mut self, padding_bottom: usize) -> &mut Self
pub fn set_padding_left(&mut self, padding_left: usize) -> &mut Self
pub fn set_padding_right(&mut self, padding_right: usize) -> &mut Self
Sourcepub fn set_paddings(&mut self, paddings: [usize; 4]) -> &mut Self
pub fn set_paddings(&mut self, paddings: [usize; 4]) -> &mut Self
top, bottom, left, right
pub fn set_title<T: ToString>(&mut self, title: T) -> &mut Self
pub fn set_title_color(&mut self, title_color: Option<Color>) -> &mut Self
pub fn set_x_axis_label<T: ToString>(&mut self, x_axis_label: T) -> &mut Self
pub fn set_y_axis_label<T: ToString>(&mut self, y_axis_label: T) -> &mut Self
pub fn set_big_title(&mut self, big_title: bool) -> &mut Self
Sourcepub fn set_skip_range(&mut self, skip_value: SkipValue) -> &mut Self
pub fn set_skip_range(&mut self, skip_value: SkipValue) -> &mut Self
It does not plot data between this range. It’s applied only when the plot height is greater than 18.
Sourcepub fn set_skip_skip_range<T: TryInto<Ratio> + Clone, U: TryInto<Ratio> + Clone>(
&mut self,
from: Option<T>,
to: Option<U>,
) -> &mut Self
pub fn set_skip_skip_range<T: TryInto<Ratio> + Clone, U: TryInto<Ratio> + Clone>( &mut self, from: Option<T>, to: Option<U>, ) -> &mut Self
I need a better name for this.
If the skip_range is included in this range, the skip_range is not set. It works only when its skip_value is SkipValue::Automatic.
You can set open ends with None values.
Sourcepub fn set_horizontal_break(&mut self, from: usize, to: usize) -> &mut Self
pub fn set_horizontal_break(&mut self, from: usize, to: usize) -> &mut Self
from and to are x indices of the output plot.
It’s like skip_range, but it skips x-axis not y-axis.
Sourcepub fn add_labeled_interval<T: ToString>(
&mut self,
start: i32,
end: i32,
label: T,
) -> &mut Self
pub fn add_labeled_interval<T: ToString>( &mut self, start: i32, end: i32, label: T, ) -> &mut Self
See README.md to see how it works. start and end are both inclusive.
start and end corresponds to the index of self.data. That means if the interval is (0, 32),
it’s self.data[0] ~ self.data[32]. The actual number of the characters used depends on the size of the graph.
pub fn set_primary_color(&mut self, color: Option<Color>) -> &mut Self
pub fn set_color_mode(&mut self, color_mode: ColorMode) -> &mut Self
pub fn set_y_label_formatter( &mut self, formatter: Box<dyn NumberFormatter>, ) -> &mut Self
Source§impl Graph
impl Graph
pub fn new(plot_width: usize, plot_height: usize) -> Self
Sourcepub fn draw(&self) -> String
pub fn draw(&self) -> String
It panics if it’s not well-configured. If you’re not sure, call .is_valid before calling this method
Sourcepub fn is_valid(&self) -> bool
pub fn is_valid(&self) -> bool
self.datamust be set and for 1-D data, it must not be empty.- If
self.y_minandself.y_maxare set,self.y_maxhas to be greater thanself.y_min. - If you’re using a 2-dimensional data,
data,x_labelsandy_labelsmust have the same dimension. - If there’re labeled_intervals, their interval must be valid.
Source§impl Graph
impl Graph
Sourcepub fn from_json(json_str: &str) -> Result<Self, Error>
pub fn from_json(json_str: &str) -> Result<Self, Error>
The json must be an object or an array.
If it’s an object, these keys are allowed:
- 1d_data: Array[Number]
- 1d_labeled_data: Array[[String, Number]]
- y_min: Number
- y_max: Number
- y_range: [Number, Number]
- pretty_y: Number
- plot_width: Integer
- plot_height: Integer
- x_label_margin: Integer
- y_label_margin: Integer
- block_width: Integer
- paddings: [Integer, Integer, Integer, Integer]
- title: String
- x_axis_label: String
- y_axis_label: String
- big_title: Bool
- color_title: String
- primary_color: String
- color_mode: String
- skip_range: Optional[[Number, Number]]
- if it’s not set, it’s default to
SkipValue::Automatic - if you want it to be
SkipValue::None, set this value to null - otherwise, it’s set to
SkipValue::Manual { from: v[0], to: v[1] }
- if it’s not set, it’s default to
- y_label_prefix: String
- y_label_suffix: String
- labeled_intervals: Array[[Integer, Integer, String]]
- horizontal_break: [Integer, Integer]
For Numbers in the above type annotations,
- If it’s an integer or a float in json, everything’s fine.
- If it’s a string in json, it tries to parse it.
- Otherwise, it’s a type error.
If it’s an array, it interprets the array as 1d_data.