# ui/uichart (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/ui/uichart/

Chart. (UILineChart, UIBarChart of the original ui/uichart.js)
- UILineChart: draws a value list as a polyline in the node area. Pushing new values with push_value makes a real-time graph where old values are pushed out from the front.
  Samples are spread across the full width based on the maximum sample count. (fills from the left while not yet full) If the value range is not set, the min / max of the current values is used.
- UIBarChart: draws a value list as vertical bars. If the maximum is not set, the max of the current values is used. The bar at the highlight index uses the highlight color.
- Both are draw-only components, so they can also be attached with WorldNode::add_component. (no tick)
- Differences from the original:
  - The "auto if null" of the value range / maximum is `Option<f64>`. The "none if -1" of the highlight index is `Option<usize>`.
  - get_value_list returns a slice instead of a copy.
  - Point / bar position calculation is split out into calculate_points / calculate_bar_rects. (the same calculation as drawing, for tests and hit testing)
  - The line chart holds and reuses a point buffer so it does not allocate every frame.

## UILineChart (struct) (Default, Component)

Line chart.
- A draw-only component with no tick. Draws horizontal grid lines and a polyline in the node's content_size area.

Example: Drawing a line chart by pushing values

```rust
use vanilla::{Color, NodeTree, UILineChart, Vector2};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let chart_id = node_tree.create_child(root_id, "chart");
node_tree[chart_id].set_content_size(Vector2::create(300.0, 120.0));
let line_chart = node_tree[chart_id].add_component(UILineChart::new());
line_chart.set_max_sample_count(60);
line_chart.set_value_range(Some(0.0), Some(100.0));
line_chart.set_line_color(Color::create(0.3, 0.8, 1.0, 1.0));
line_chart.set_line_width(2.0);
for sample_index in 0..80 {
	line_chart.push_value(50.0 + (sample_index as f64 * 0.3).sin() * 30.0);
}

assert_eq!(line_chart.get_value_list().len(), 60);
let points = line_chart.calculate_points(300.0, 120.0);
assert_eq!(points.len(), 60);
assert_eq!(points[59].x, 300.0);
```

- `pub fn new() -> UILineChart` (constructor, since 0.3.2): Constructor. (60 samples, auto range, 3 grid lines)
  - returns UILineChart: New line chart with no values. (line width 1.5)
- `pub fn push_value(&mut self, value: f64)` (method, since 0.3.2): Adds one value. (when the sample count is exceeded, values are pushed out from the front)
  - `value` f64: Value to add at the end.
- `pub fn calculate_points(&self, width: f64, height: f64) -> Vec<Vector2>` (method, since 0.3.2): Calculates the polyline points. (the same positions as drawing)
  - Sample i's x is i / (maximum sample count - 1) x width, so it fills from the left while not full. y has the range minimum at the bottom and the maximum at the top. (2 pixel margin at the top and bottom)
  - `width` f64: Width of the area to draw. (pixels)
  - `height` f64: Height of the area to draw. (pixels)
  - returns Vec<Vector2>: One point per sample. (relative to the area's top left, pixels) An empty list if there are fewer than two samples.

  Example: Points with auto range and fixed range

  ```rust
  use vanilla::{UILineChart, Vector2};
  
  let mut line_chart = UILineChart::new();
  line_chart.set_max_sample_count(5);
  line_chart.push_value(0.0);
  assert!(line_chart.calculate_points(100.0, 54.0).is_empty());
  
  line_chart.push_value(10.0);
  line_chart.push_value(5.0);
  assert_eq!(line_chart.calculate_points(100.0, 54.0), vec![Vector2::create(0.0, 52.0), Vector2::create(25.0, 2.0), Vector2::create(50.0, 27.0)]);
  
  line_chart.set_value_range(Some(0.0), Some(20.0));
  assert_eq!(line_chart.calculate_points(100.0, 54.0)[1], Vector2::create(25.0, 27.0));
  ```

- `pub fn set_max_sample_count(&mut self, max_sample_count: usize)` (method, since 0.3.2): Sets the maximum sample count. (not less than 2. If more samples are held than the new limit, the oldest are dropped first)
  - `max_sample_count` usize: Upper limit on the number of samples held and drawn. (default 60, 2 if less than 2)
- `pub fn get_max_sample_count(&self) -> usize` (method, since 0.3.2): Returns the maximum sample count.
  - returns usize: Sample count limit. (2 or more)
- `pub fn set_value_range(&mut self, min_value: Option<f64>, max_value: Option<f64>)` (method, since 0.3.2): Sets the value range. (None makes that side automatic)
  - `min_value` Option<f64>: Value at the bottom edge of the area. None for the minimum of the current values.
  - `max_value` Option<f64>: Value at the top edge of the area. None for the maximum of the current values.
- `pub fn set_value_list(&mut self, value_list: &[f64])` (method, since 0.3.2): Sets the value list. (keeps only the last values up to the maximum sample count)
  - `value_list` &[f64]: New value list. (copied and held)
- `pub fn get_value_list(&self) -> &[f64]` (method, since 0.3.2): Returns the value list.
  - returns &[f64]: Values currently held. (in the order added)
- `pub fn set_line_color(&mut self, line_color: impl Into<Color>)` (method, since 0.3.2): Sets the line color.
  - `line_color` impl Into<Color>: Polyline color. (default (0.42, 0.48, 1, 1))
- `pub fn set_grid_color(&mut self, grid_color: impl Into<Color>)` (method, since 0.3.2): Sets the grid color.
  - `grid_color` impl Into<Color>: Horizontal grid line color. (default (1, 1, 1, 0.07))
- `pub fn set_line_width(&mut self, line_width: f64)` (method, since 0.3.2): Sets the line width.
  - `line_width` f64: Polyline width. (pixels, default 1.5)

## UIBarChart (struct) (Default, Component)

Bar chart.
- A draw-only component with no tick. Draws a vertical bar per value from the bottom up in the node's content_size area.

Example: Bars for four values with a highlight

```rust
use vanilla::{NodeTree, Rect, UIBarChart, Vector2};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let chart_id = node_tree.create_child(root_id, "chart");
node_tree[chart_id].set_content_size(Vector2::create(100.0, 80.0));
let bar_chart = node_tree[chart_id].add_component(UIBarChart::new());
bar_chart.set_value_list(&[1.0, 2.0, 4.0, 0.0]);
bar_chart.set_highlight_index(Some(2));

let bar_rects = bar_chart.calculate_bar_rects(100.0, 80.0);
assert_eq!(bar_rects[0], Rect::create(0.0, 60.0, 22.0, 20.0));
assert_eq!(bar_rects[2], Rect::create(52.0, 0.0, 22.0, 80.0));
assert_eq!(bar_rects[3].get_height(), 1.0);
```

- `pub fn new() -> UIBarChart` (constructor, since 0.3.2): Constructor. (auto maximum, no highlight, 4 pixels between bars)
  - returns UIBarChart: New bar chart with no values.
- `pub fn calculate_bar_rects(&self, width: f64, height: f64) -> Vec<Rect>` (method, since 0.3.2): Calculates the bar rectangle list. (the same positions as drawing)
  - Bar width is (width - gap x (bar count - 1)) / bar count, and height is the value / maximum ratio (clamped to 0 ~ 1) x height, at least 1 pixel.
  - `width` f64: Width of the area to draw. (pixels)
  - `height` f64: Height of the area to draw. (pixels)
  - returns Vec<Rect>: One bar rectangle per value. (relative to the area's top left, pixels) An empty list if there are no values.

  Example: When the maximum is set, that value is the area height

  ```rust
  use vanilla::UIBarChart;
  
  let mut bar_chart = UIBarChart::new();
  bar_chart.set_value_list(&[1.0, 2.0, 4.0, 0.0]);
  assert_eq!(bar_chart.calculate_bar_rects(100.0, 80.0)[2].get_height(), 80.0);
  
  bar_chart.set_max_value(Some(8.0));
  assert_eq!(bar_chart.calculate_bar_rects(100.0, 80.0)[2].get_height(), 40.0);
  bar_chart.set_max_value(Some(2.0));
  assert_eq!(bar_chart.calculate_bar_rects(100.0, 80.0)[2].get_height(), 80.0);
  ```

- `pub fn set_value_list(&mut self, value_list: &[f64])` (method, since 0.3.2): Sets the value list.
  - `value_list` &[f64]: List of values to draw as bars. (copied and held)
- `pub fn get_value_list(&self) -> &[f64]` (method, since 0.3.2): Returns the value list.
  - returns &[f64]: Values currently held. (in the order added)
- `pub fn set_max_value(&mut self, max_value: Option<f64>)` (method, since 0.3.2): Sets the maximum. (None for automatic)
  - `max_value` Option<f64>: Value that fits the area height. None for the maximum of the current values. (at least 0.0001)
- `pub fn set_bar_color(&mut self, bar_color: impl Into<Color>)` (method, since 0.3.2): Sets the bar color.
  - `bar_color` impl Into<Color>: Bar color. (default (0.42, 0.48, 1, 0.85))
- `pub fn set_highlight_color(&mut self, highlight_color: impl Into<Color>)` (method, since 0.3.2): Sets the highlight color.
  - `highlight_color` impl Into<Color>: Color of the bar at the highlight index. (default (0.22, 0.84, 1, 1))
- `pub fn set_highlight_index(&mut self, highlight_index: Option<usize>)` (method, since 0.3.2): Sets the highlight index. (None for no highlight)
  - `highlight_index` Option<usize>: Index of the bar to draw in the highlight color. (from 0)
- `pub fn set_bar_gap(&mut self, bar_gap: f64)` (method, since 0.3.2): Sets the gap between bars.
  - `bar_gap` f64: Gap between bars. (pixels, default 4)
