vanilladocs
vanillapunch.com
API / ui/uichart

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 로 떼어 냈다. (그리기와 같은 계산, 시험과 충돌 판정용)
  • 라인 차트는 점 버퍼를 들고 다시 써서 매 프레임 할당하지 않는다.

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.

UILineChartstruct traits Default, Component

짝Counterpart: vanilla.js UILineChart소스source

라인 차트.

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

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
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);
생성자constructorpub fn new() -> UILineChart#0.3.2 부터since 0.3.2소스source

생성. (표본 60 개, 자동 범위, 격자 3 줄)

Constructor. (60 samples, auto range, 3 grid lines)

반환ReturnsUILineChart 값이 빈 새 라인 차트. (선 두께 1.5)New line chart with no values. (line width 1.5)

짝Counterpart: vanilla.js UILineChart.constructor

메서드methodpub fn push_value(&mut self, value: f64)#0.3.2 부터since 0.3.2소스source

값 하나 추가. (표본 수를 넘기면 앞에서 밀려난다)

Adds one value. (when the sample count is exceeded, values are pushed out from the front)

valuef64
끝에 더할 값.Value to add at the end.

짝Counterpart: vanilla.js UILineChart.pushValue

메서드methodpub fn calculate_points(&self, width: f64, height: f64) -> Vec<Vector2>#0.3.2 부터since 0.3.2소스source

폴리라인 점 계산. (그리기와 같은 자리)

  • 표본 i 의 x 는 i / (최대 표본 수 - 1) x 너비라 덜 찬 동안은 왼쪽부터 채운다. y 는 값 범위의 최소가 아래, 최대가 위다. (위아래 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)
widthf64
그릴 영역 너비. (픽셀)Width of the area to draw. (pixels)
heightf64
그릴 영역 높이. (픽셀)Height of the area to draw. (pixels)

반환ReturnsVec<Vector2> 표본마다 점 하나. (영역 왼쪽 위 기준, 픽셀) 표본이 둘보다 적으면 빈 목록.One point per sample. (relative to the area's top left, pixels) An empty list if there are fewer than two samples.

짝Counterpart: vanilla.js UILineChart.calculatePoints

예제Example자동 범위와 고정 범위의 점Points with auto range and fixed range
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));
메서드methodpub fn set_max_sample_count(&mut self, max_sample_count: usize)#0.3.2 부터since 0.3.2소스source

최대 표본 수 설정. (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_countusize
들고 그릴 표본 수 상한. (기본 60, 2 보다 작으면 2)Upper limit on the number of samples held and drawn. (default 60, 2 if less than 2)

짝Counterpart: vanilla.js UILineChart.setMaxSampleCount

메서드methodpub fn get_max_sample_count(&self) -> usize#0.3.2 부터since 0.3.2소스source

최대 표본 수 반환.

Returns the maximum sample count.

반환Returnsusize 표본 수 상한. (2 이상)Sample count limit. (2 or more)

짝Counterpart: vanilla.js UILineChart.getMaxSampleCount

메서드methodpub fn set_value_range(&mut self, min_value: Option<f64>, max_value: Option<f64>)#0.3.2 부터since 0.3.2소스source

값 범위 설정. (None 이면 그쪽은 자동)

Sets the value range. (None makes that side automatic)

min_valueOption<f64>
영역 아래 끝에 올 값. None 이면 지금 값들의 최소.Value at the bottom edge of the area. None for the minimum of the current values.
max_valueOption<f64>
영역 위 끝에 올 값. None 이면 지금 값들의 최대.Value at the top edge of the area. None for the maximum of the current values.

짝Counterpart: vanilla.js UILineChart.setValueRange

메서드methodpub fn set_value_list(&mut self, value_list: &[f64])#0.3.2 부터since 0.3.2소스source

값 목록 설정. (최대 표본 수만큼 뒤쪽만 남긴다)

Sets the value list. (keeps only the last values up to the maximum sample count)

value_list&[f64]
새 값 목록. (복사해 든다)New value list. (copied and held)

짝Counterpart: vanilla.js UILineChart.setValueList

메서드methodpub fn get_value_list(&self) -> &[f64]#0.3.2 부터since 0.3.2소스source

값 목록 반환.

Returns the value list.

반환Returns&[f64] 지금 든 값. (든 차례대로)Values currently held. (in the order added)

짝Counterpart: vanilla.js UILineChart.getValueList

메서드methodpub fn set_line_color(&mut self, line_color: impl Into<Color>)#0.3.2 부터since 0.3.2소스source

선 색 설정.

Sets the line color.

line_colorimpl Into<Color>
폴리라인 색. (기본 (0.42, 0.48, 1, 1))Polyline color. (default (0.42, 0.48, 1, 1))

짝Counterpart: vanilla.js UILineChart.setLineColor

메서드methodpub fn set_grid_color(&mut self, grid_color: impl Into<Color>)#0.3.2 부터since 0.3.2소스source

격자 색 설정.

Sets the grid color.

grid_colorimpl Into<Color>
가로 격자선 색. (기본 (1, 1, 1, 0.07))Horizontal grid line color. (default (1, 1, 1, 0.07))

짝Counterpart: vanilla.js UILineChart.setGridColor

메서드methodpub fn set_line_width(&mut self, line_width: f64)#0.3.2 부터since 0.3.2소스source

선 두께 설정.

Sets the line width.

line_widthf64
폴리라인 두께. (픽셀, 기본 1.5)Polyline width. (pixels, default 1.5)

짝Counterpart: vanilla.js UILineChart.setLineWidth

UIBarChartstruct traits Default, Component

짝Counterpart: vanilla.js UIBarChart소스source

바 차트.

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

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
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);
생성자constructorpub fn new() -> UIBarChart#0.3.2 부터since 0.3.2소스source

생성. (자동 최댓값, 강조 없음, 막대 사이 4 픽셀)

Constructor. (auto maximum, no highlight, 4 pixels between bars)

반환ReturnsUIBarChart 값이 빈 새 바 차트.New bar chart with no values.

짝Counterpart: vanilla.js UIBarChart.constructor

메서드methodpub fn calculate_bar_rects(&self, width: f64, height: f64) -> Vec<Rect>#0.3.2 부터since 0.3.2소스source

막대 사각형 목록 계산. (그리기와 같은 자리)

  • 막대 폭은 (너비 - 간격 x (막대 수 - 1)) / 막대 수, 높이는 값 / 최댓값 비율(0 ~ 1 로 자름) x 높이이고 적어도 1 픽셀이다.

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.
widthf64
그릴 영역 너비. (픽셀)Width of the area to draw. (pixels)
heightf64
그릴 영역 높이. (픽셀)Height of the area to draw. (pixels)

반환ReturnsVec<Rect> 값마다 막대 사각형. (영역 왼쪽 위 기준, 픽셀) 값이 없으면 빈 목록.One bar rectangle per value. (relative to the area's top left, pixels) An empty list if there are no values.

짝Counterpart: vanilla.js UIBarChart.calculateBarRects

예제Example최댓값을 정하면 그 값이 영역 높이다When the maximum is set, that value is the area height
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);
메서드methodpub fn set_value_list(&mut self, value_list: &[f64])#0.3.2 부터since 0.3.2소스source

값 목록 설정.

Sets the value list.

value_list&[f64]
막대로 그릴 값 목록. (복사해 든다)List of values to draw as bars. (copied and held)

짝Counterpart: vanilla.js UIBarChart.setValueList

메서드methodpub fn get_value_list(&self) -> &[f64]#0.3.2 부터since 0.3.2소스source

값 목록 반환.

Returns the value list.

반환Returns&[f64] 지금 든 값. (든 차례대로)Values currently held. (in the order added)

짝Counterpart: vanilla.js UIBarChart.getValueList

메서드methodpub fn set_max_value(&mut self, max_value: Option<f64>)#0.3.2 부터since 0.3.2소스source

최댓값 설정. (None 이면 자동)

Sets the maximum. (None for automatic)

max_valueOption<f64>
영역 높이에 맞출 값. None 이면 지금 값들의 최대. (적어도 0.0001)Value that fits the area height. None for the maximum of the current values. (at least 0.0001)

짝Counterpart: vanilla.js UIBarChart.setMaxValue

메서드methodpub fn set_bar_color(&mut self, bar_color: impl Into<Color>)#0.3.2 부터since 0.3.2소스source

막대 색 설정.

Sets the bar color.

bar_colorimpl Into<Color>
막대 색. (기본 (0.42, 0.48, 1, 0.85))Bar color. (default (0.42, 0.48, 1, 0.85))

짝Counterpart: vanilla.js UIBarChart.setBarColor

메서드methodpub fn set_highlight_color(&mut self, highlight_color: impl Into<Color>)#0.3.2 부터since 0.3.2소스source

강조색 설정.

Sets the highlight color.

highlight_colorimpl Into<Color>
강조 번호 막대의 색. (기본 (0.22, 0.84, 1, 1))Color of the bar at the highlight index. (default (0.22, 0.84, 1, 1))

짝Counterpart: vanilla.js UIBarChart.setHighlightColor

메서드methodpub fn set_highlight_index(&mut self, highlight_index: Option<usize>)#0.3.2 부터since 0.3.2소스source

강조 번호 설정. (None 이면 강조 없음)

Sets the highlight index. (None for no highlight)

highlight_indexOption<usize>
강조색으로 그릴 막대 번호. (0 부터)Index of the bar to draw in the highlight color. (from 0)

짝Counterpart: vanilla.js UIBarChart.setHighlightIndex

메서드methodpub fn set_bar_gap(&mut self, bar_gap: f64)#0.3.2 부터since 0.3.2소스source

막대 사이 간격 설정.

Sets the gap between bars.

bar_gapf64
막대 사이 간격. (픽셀, 기본 4)Gap between bars. (pixels, default 4)

짝Counterpart: vanilla.js UIBarChart.setBarGap