# ui/uichart (vanilla.js 0.4.0)

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

라인 차트.

## UILineChart extends Component

라인 차트.
- 값 배열을 노드 영역에 폴리라인으로 그린다. pushValue 로 매 프레임 / 매 초
  새 값을 밀어 넣으면 오래된 값이 밀려나는 실시간 그래프가 된다.
- 사용:
    const lineChart = chartNode.addComponent(UILineChart);
    lineChart.setMaxSampleCount(60);
    매 초: lineChart.pushValue(newValue);

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

```js
import { Engine, EngineConfiguration, UIScene, UILineChart, WorldNode, Color, Pivot, Vector2 } from "./build/vanilla.js";

class LineChartScene extends UIScene {
	create() {
		super.create();
		const chartNode = new WorldNode();
		chartNode.setPivot(Pivot.topLeft.clone());
		chartNode.setAnchor(Pivot.topLeft.clone());
		chartNode.setContentSize(Vector2.create(480, 160));
		chartNode.setLocalPosition(Vector2.create(80, 100));
		const lineChart = chartNode.addComponent(UILineChart);
		lineChart.setMaxSampleCount(60);
		lineChart.setValueRange(0, 100);
		lineChart.setLineColor(new Color(0.3, 0.8, 1, 1));
		lineChart.setLineWidth(2);
		for (let sampleIndex = 0; sampleIndex < 80; ++sampleIndex) {
			lineChart.pushValue(50 + Math.sin(sampleIndex * 0.3) * 30);
		}
		const valueList = lineChart.getValueList();
		console.log(valueList.length);
		const root = this.getRoot();
		root.addChild(chartNode);
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const scene = new LineChartScene();
scene.setLoadingMinDurationMs(0);
engine.run(scene);
```

- `constructor()` (constructor, since 0.3.0): 생성.
- `pushValue(value)` (method, since 0.3.0): 값 하나 추가. (표본 수를 넘기면 앞에서 밀려난다)
  - `value` number: 새 표본 값. (맨 뒤에 붙는다)
- `draw(graphic)` (method, since 0.3.0): 출력.
  - `graphic` Graphic: 그릴 그래픽. (노드 로컬 좌표, 내용 영역 왼쪽 위가 원점)
- `calculateDrawnRect(rect)` (method, since 0.4.0): 그리는 사각형을 rect 에 적기. (오버라이드: 격자는 내용 영역 안, 선은 값이 정한 범위 밖으로 나간 만큼 위아래로 넘친다)
  - 가로는 표본이 내용 폭 안에 퍼지므로 내용 폭. 세로는 내용 높이와 가장 큰 값, 가장 작은 값이 놓이는 높이를 합친다.
  - 선 두께만큼(이음새를 메우는 반두께 연장까지) 사방으로 넓혀 본다.
  - `rect` Rect: 그리는 사각형을 적을 곳. (노드 내용 영역의 왼쪽 위가 원점)
  - returns boolean: 노드에 붙어 있으면 참.

  예제: 범위를 넘는 값이 그리는 곳 보기

  ```js
  import { WorldNode, UILineChart, Rect, Vector2 } from "./build/vanilla.js";
  
  const node = new WorldNode();
  node.setContentSize(Vector2.create(160, 60));
  const chart = node.addComponent(UILineChart);
  chart.setValueRange(0, 1);
  chart.setValueList([0.5, 2, 0.2]);
  const drawnRect = Rect.zero();
  chart.calculateDrawnRect(drawnRect);
  console.log(drawnRect.position.y < 0);
  ```

- `calculatePoints(width, height)` (method, since 0.4.0): 폴리라인 점 계산. (그리기와 같은 자리, 표본이 둘보다 적으면 빈 목록)
  - `width` number: 그래프 너비. (픽셀, 표본 수 상한을 채웠을 때 마지막 점이 이 자리)
  - `height` number: 그래프 높이. (픽셀, 위아래 2 픽셀은 비운다)
  - returns Vector2[]: 표본마다 점 하나. (왼쪽 위가 원점, 값이 클수록 위)
- `setMaxSampleCount(maxSampleCount)` (method, since 0.3.0): 표본 수 상한 설정. (2 보다 작으면 2, 이미 든 표본이 새 상한보다 많으면 오래된 것부터 버린다)
  - `maxSampleCount` number: 표본 수 상한.

  예제: 상한을 줄이면 오래된 표본부터 버리기

  ```js
  import { UILineChart } from "./build/vanilla.js";
  
  const lineChart = new UILineChart();
  lineChart.setValueList([1, 2, 3, 4, 5]);
  lineChart.setMaxSampleCount(3);
  const valueList = lineChart.getValueList();
  console.log(valueList.join(", "));
  ```

- `getMaxSampleCount()` (method, since 0.3.0): 표본 수 상한 반환.
  - returns number: 표본 수 상한. (2 이상)
- `setValueRange(minValue, maxValue)` (method, since 0.3.0): 세로 값 범위 설정. (null 인 끝은 표본의 최소, 최대로 자동으로 맞춘다)
  - `minValue` number | null: 아래 끝 값. (null 이면 자동)
  - `maxValue` number | null: 위 끝 값. (null 이면 자동)

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

  ```js
  import { UILineChart } from "./build/vanilla.js";
  
  const lineChart = new UILineChart();
  lineChart.setMaxSampleCount(3);
  lineChart.setValueList([20, 40, 60]);
  lineChart.setValueRange(0, 100);
  const fixedPoints = lineChart.calculatePoints(200, 104);
  console.log(fixedPoints[2].y);
  lineChart.setValueRange(null, null);
  const autoPoints = lineChart.calculatePoints(200, 104);
  console.log(autoPoints[2].y);
  ```

- `setValueList(valueList)` (method, since 0.3.0): 표본 목록 설정. (표본 수 상한보다 길면 뒤쪽만 남긴다)
  - `valueList` number[]: 표본 값 목록. (복사해 쥔다)
- `getValueList()` (method, since 0.3.0): 표본 목록 반환.
  - returns number[]: 표본 값 목록의 복사본. (오래된 것부터)
- `setLineColor(lineColor)` (method, since 0.3.0): 선 색 설정.
  - `lineColor` Color | string: 선 색. (복제해 쥔다)
- `setGridColor(gridColor)` (method, since 0.3.0): 격자 색 설정. (가로 격자선 3 개)
  - `gridColor` Color | string: 격자 색. (복제해 쥔다)
- `setLineWidth(lineWidth)` (method, since 0.3.0): 선 굵기 설정.
  - `lineWidth` number: 선 굵기. (픽셀)

## UIBarChart extends Component

바 차트.
- 값 배열을 세로 막대로 그린다. 값이 바뀌면 다음 프레임에 그대로 반영된다.

예제: 막대 하나를 강조한 막대 그래프

```js
import { Engine, EngineConfiguration, UIScene, UIBarChart, WorldNode, Color, Pivot, Vector2 } from "./build/vanilla.js";

class BarChartScene extends UIScene {
	create() {
		super.create();
		const chartNode = new WorldNode();
		chartNode.setPivot(Pivot.topLeft.clone());
		chartNode.setAnchor(Pivot.topLeft.clone());
		chartNode.setContentSize(Vector2.create(420, 160));
		chartNode.setLocalPosition(Vector2.create(110, 100));
		const barChart = chartNode.addComponent(UIBarChart);
		barChart.setValueList([12, 30, 22, 48, 36, 54, 41]);
		barChart.setMaxValue(60);
		barChart.setBarGap(8);
		barChart.setBarColor(new Color(0.42, 0.48, 1, 0.6));
		barChart.setHighlightColor(new Color(0.22, 0.84, 1, 1));
		barChart.setHighlightIndex(5);
		const root = this.getRoot();
		root.addChild(chartNode);
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const scene = new BarChartScene();
scene.setLoadingMinDurationMs(0);
engine.run(scene);
```

- `constructor()` (constructor, since 0.3.0): 생성.
- `draw(graphic)` (method, since 0.3.0): 출력.
  - `graphic` Graphic: 그릴 그래픽. (노드 로컬 좌표, 내용 영역 왼쪽 위가 원점)
- `calculateDrawnRect(rect)` (method, since 0.4.0): 그리는 사각형을 rect 에 적기. (오버라이드: 막대는 내용 영역 안, 막대 사이가 너무 넓어 막대 폭이 음수가 되면 그만큼 옆으로 넘친다)
  - 세로는 막대가 가장 낮아도 한 단위 높이로 그리므로 내용 높이가 한 단위보다 작으면 위로 넘친다. 한 단위씩 넓혀 본다.
  - `rect` Rect: 그리는 사각형을 적을 곳. (노드 내용 영역의 왼쪽 위가 원점)
  - returns boolean: 노드에 붙어 있으면 참.

  예제: 막대 그래프가 그리는 곳 보기

  ```js
  import { WorldNode, UIBarChart, Rect, Vector2 } from "./build/vanilla.js";
  
  const node = new WorldNode();
  node.setContentSize(Vector2.create(120, 40));
  const chart = node.addComponent(UIBarChart);
  chart.setValueList([3, 5, 2]);
  const drawnRect = Rect.zero();
  chart.calculateDrawnRect(drawnRect);
  console.log(drawnRect.size.x, drawnRect.size.y);
  ```

- `calculateBarRects(width, height)` (method, since 0.4.0): 막대 사각형 목록 계산. (그리기와 같은 자리)
  - `width` number: 그래프 너비. (픽셀, 막대 사이 간격을 뺀 나머지를 막대 수로 나눈다)
  - `height` number: 그래프 높이. (픽셀, 최대 값이 이 높이)
  - returns Rect[]: 막대마다 사각형 하나. (바닥에 붙고, 값이 0 이하여도 높이 1 픽셀)
- `setValueList(valueList)` (method, since 0.3.0): 막대 값 목록 설정.
  - `valueList` number[]: 막대 값 목록. (복사해 쥔다)
- `getValueList()` (method, since 0.3.0): 막대 값 목록 반환.
  - returns number[]: 막대 값 목록의 복사본.
- `setMaxValue(maxValue)` (method, since 0.3.0): 최대 값 설정. (막대가 높이를 다 채우는 값, 넘는 값은 잘린다)
  - `maxValue` number | null: 최대 값. (null 이면 값 목록의 최대)
- `setBarColor(barColor)` (method, since 0.3.0): 막대 색 설정.
  - `barColor` Color | string: 막대 색. (복제해 쥔다)
- `setHighlightColor(highlightColor)` (method, since 0.3.0): 강조 막대 색 설정.
  - `highlightColor` Color | string: 강조 막대 색. (복제해 쥔다)
- `setHighlightIndex(highlightIndex)` (method, since 0.3.0): 강조할 막대 순서 설정.
  - `highlightIndex` number: -1 이면 강조 없음.
- `setBarGap(barGap)` (method, since 0.3.0): 막대 사이 간격 설정.
  - `barGap` number: 막대 사이 간격. (픽셀)
