# ui/uichart (vanilla-engine 0.4.0)

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

차트. (원본 ui/uichart.js 의 UILineChart, UIBarChart)
- UILineChart: 값 목록을 노드 영역에 폴리라인으로 그린다. push_value 로 새 값을 밀어 넣으면 오래된 값이 앞에서 밀려나는 실시간 그래프가 된다.
  표본은 최대 표본 수 기준으로 폭 전체에 퍼진다. (아직 덜 찼으면 왼쪽부터 채운다) 값 범위를 정하지 않으면 지금 값들의 최소 / 최대를 쓴다.
- UIBarChart: 값 목록을 세로 막대로 그린다. 최댓값을 정하지 않으면 지금 값들의 최대를 쓴다. 강조 번호의 막대는 강조색이다.
- 둘 다 그리기만 하는 컴포넌트라 WorldNode::add_component 로 붙여도 된다. (tick 이 없다)
- 원본과 다른 것:
  - 값 범위 / 최댓값의 "null 이면 자동" 은 `Option<f64>` 다. 강조 번호의 "-1 이면 없음" 은 `Option<usize>` 다.
  - get_value_list 는 복사본 대신 슬라이스를 돌려준다.
  - 점 / 막대 자리 계산을 calculate_points / calculate_bar_rects 로 떼어 냈다. (그리기와 같은 계산, 시험과 충돌 판정용)
  - 라인 차트는 점 버퍼를 들고 다시 써서 매 프레임 할당하지 않는다.

## UILineChart (struct) (Default, Component)

라인 차트.
- 그리기만 하는 컴포넌트라 tick 이 없다. 노드 content_size 영역에 가로 격자와 폴리라인을 그린다.

예제: 값을 밀어 넣어 꺾은선 그리기

```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): 생성. (표본 60 개, 자동 범위, 격자 3 줄)
  - returns UILineChart: 값이 빈 새 라인 차트. (선 두께 1.5)
- `pub fn push_value(&mut self, value: f64)` (method, since 0.3.2): 값 하나 추가. (표본 수를 넘기면 앞에서 밀려난다)
  - `value` f64: 끝에 더할 값.
- `pub fn calculate_points(&self, width: f64, height: f64) -> Vec<Vector2>` (method, since 0.3.2): 폴리라인 점 계산. (그리기와 같은 자리)
  - 표본 i 의 x 는 i / (최대 표본 수 - 1) x 너비라 덜 찬 동안은 왼쪽부터 채운다. y 는 값 범위의 최소가 아래, 최대가 위다. (위아래 2 픽셀 여백)
  - `width` f64: 그릴 영역 너비. (픽셀)
  - `height` f64: 그릴 영역 높이. (픽셀)
  - returns Vec<Vector2>: 표본마다 점 하나. (영역 왼쪽 위 기준, 픽셀) 표본이 둘보다 적으면 빈 목록.

  예제: 자동 범위와 고정 범위의 점

  ```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): 최대 표본 수 설정. (2 보다 작게 하지 않는다. 이미 든 표본이 새 상한보다 많으면 오래된 것부터 버린다)
  - `max_sample_count` usize: 들고 그릴 표본 수 상한. (기본 60, 2 보다 작으면 2)
- `pub fn get_max_sample_count(&self) -> usize` (method, since 0.3.2): 최대 표본 수 반환.
  - returns usize: 표본 수 상한. (2 이상)
- `pub fn set_value_range(&mut self, min_value: Option<f64>, max_value: Option<f64>)` (method, since 0.3.2): 값 범위 설정. (None 이면 그쪽은 자동)
  - `min_value` Option<f64>: 영역 아래 끝에 올 값. None 이면 지금 값들의 최소.
  - `max_value` Option<f64>: 영역 위 끝에 올 값. None 이면 지금 값들의 최대.
- `pub fn set_value_list(&mut self, value_list: &[f64])` (method, since 0.3.2): 값 목록 설정. (최대 표본 수만큼 뒤쪽만 남긴다)
  - `value_list` &[f64]: 새 값 목록. (복사해 든다)
- `pub fn get_value_list(&self) -> &[f64]` (method, since 0.3.2): 값 목록 반환.
  - returns &[f64]: 지금 든 값. (든 차례대로)
- `pub fn set_line_color(&mut self, line_color: impl Into<Color>)` (method, since 0.3.2): 선 색 설정.
  - `line_color` impl Into<Color>: 폴리라인 색. (기본 (0.42, 0.48, 1, 1))
- `pub fn set_grid_color(&mut self, grid_color: impl Into<Color>)` (method, since 0.3.2): 격자 색 설정.
  - `grid_color` impl Into<Color>: 가로 격자선 색. (기본 (1, 1, 1, 0.07))
- `pub fn set_line_width(&mut self, line_width: f64)` (method, since 0.3.2): 선 두께 설정.
  - `line_width` f64: 폴리라인 두께. (픽셀, 기본 1.5)

## UIBarChart (struct) (Default, Component)

바 차트.
- 그리기만 하는 컴포넌트라 tick 이 없다. 노드 content_size 영역에 값마다 세로 막대를 바닥에서 위로 그린다.

예제: 네 값의 막대와 강조

```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): 생성. (자동 최댓값, 강조 없음, 막대 사이 4 픽셀)
  - returns UIBarChart: 값이 빈 새 바 차트.
- `pub fn calculate_bar_rects(&self, width: f64, height: f64) -> Vec<Rect>` (method, since 0.3.2): 막대 사각형 목록 계산. (그리기와 같은 자리)
  - 막대 폭은 (너비 - 간격 x (막대 수 - 1)) / 막대 수, 높이는 값 / 최댓값 비율(0 ~ 1 로 자름) x 높이이고 적어도 1 픽셀이다.
  - `width` f64: 그릴 영역 너비. (픽셀)
  - `height` f64: 그릴 영역 높이. (픽셀)
  - returns Vec<Rect>: 값마다 막대 사각형. (영역 왼쪽 위 기준, 픽셀) 값이 없으면 빈 목록.

  예제: 최댓값을 정하면 그 값이 영역 높이다

  ```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): 값 목록 설정.
  - `value_list` &[f64]: 막대로 그릴 값 목록. (복사해 든다)
- `pub fn get_value_list(&self) -> &[f64]` (method, since 0.3.2): 값 목록 반환.
  - returns &[f64]: 지금 든 값. (든 차례대로)
- `pub fn set_max_value(&mut self, max_value: Option<f64>)` (method, since 0.3.2): 최댓값 설정. (None 이면 자동)
  - `max_value` Option<f64>: 영역 높이에 맞출 값. None 이면 지금 값들의 최대. (적어도 0.0001)
- `pub fn set_bar_color(&mut self, bar_color: impl Into<Color>)` (method, since 0.3.2): 막대 색 설정.
  - `bar_color` impl Into<Color>: 막대 색. (기본 (0.42, 0.48, 1, 0.85))
- `pub fn set_highlight_color(&mut self, highlight_color: impl Into<Color>)` (method, since 0.3.2): 강조색 설정.
  - `highlight_color` impl Into<Color>: 강조 번호 막대의 색. (기본 (0.22, 0.84, 1, 1))
- `pub fn set_highlight_index(&mut self, highlight_index: Option<usize>)` (method, since 0.3.2): 강조 번호 설정. (None 이면 강조 없음)
  - `highlight_index` Option<usize>: 강조색으로 그릴 막대 번호. (0 부터)
- `pub fn set_bar_gap(&mut self, bar_gap: f64)` (method, since 0.3.2): 막대 사이 간격 설정.
  - `bar_gap` f64: 막대 사이 간격. (픽셀, 기본 4)
