# ui/uichart (vanilla.js 0.4.0)

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

Line chart.

## UILineChart extends Component

Line chart.
- Draws a value array as a polyline in the node area. Pushing new values with pushValue every frame / every second
  makes it a real-time graph where old values are pushed out.
- Usage:
    const lineChart = chartNode.addComponent(UILineChart);
    lineChart.setMaxSampleCount(60);
    every second: lineChart.pushValue(newValue);

Example: Drawing a line chart by pushing values

```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): Constructor.
- `pushValue(value)` (method, since 0.3.0): Adds one value. (when the sample count is exceeded, values are pushed out from the front)
  - `value` number: New sample value. (appended at the end)
- `draw(graphic)` (method, since 0.3.0): Draw.
  - `graphic` Graphic: Graphic to draw with. (node local coordinates, origin at the top-left of the content area)
- `calculateDrawnRect(rect)` (method, since 0.4.0): Writes the drawn rectangle to rect. (Override: the grid stays inside the content area, the line spills up or down as far as values leave the set range)
  - Horizontally the content width, since samples spread within it. Vertically the union of the content height and the heights of the largest and smallest values.
  - Grown on all sides by the line width (including the half-width extension that fills joints).
  - `rect` Rect: Where to write the drawn rectangle. (origin at the top-left of the node's content area)
  - returns boolean: True if attached to a node.

  Example: Seeing where out-of-range values draw

  ```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): Computes the polyline points. (same positions as drawing, an empty list if fewer than two samples)
  - `width` number: Graph width. (pixels, where the last point lands when the sample count limit is reached)
  - `height` number: Graph height. (pixels, 2 pixels are left empty at the top and bottom)
  - returns Vector2[]: One point per sample. (origin at the top left, larger values are higher)
- `setMaxSampleCount(maxSampleCount)` (method, since 0.3.0): Sets the maximum sample count. (Below 2 becomes 2, if more samples are held than the new limit the oldest are dropped)
  - `maxSampleCount` number: Sample count limit.

  Example: Lowering the limit drops the oldest samples

  ```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 the sample count limit.
  - returns number: Sample count limit. (2 or more)
- `setValueRange(minValue, maxValue)` (method, since 0.3.0): Sets the vertical value range. (an end that is null automatically fits the samples' minimum or maximum)
  - `minValue` number | null: Bottom end value. (automatic if null)
  - `maxValue` number | null: Top end value. (automatic if null)

  Example: Comparing point positions with a fixed range and an automatic range

  ```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): Sets the sample list. (keeps only the tail if longer than the sample count limit)
  - `valueList` number[]: Sample value list. (copied and kept)
- `getValueList()` (method, since 0.3.0): Returns the sample list.
  - returns number[]: Copy of the sample value list. (oldest first)
- `setLineColor(lineColor)` (method, since 0.3.0): Sets the line color.
  - `lineColor` Color | string: Line color. (cloned and kept)
- `setGridColor(gridColor)` (method, since 0.3.0): Sets the grid color. (3 horizontal grid lines)
  - `gridColor` Color | string: Grid color. (cloned and kept)
- `setLineWidth(lineWidth)` (method, since 0.3.0): Sets the line thickness.
  - `lineWidth` number: Line width. (pixels)

## UIBarChart extends Component

Bar chart.
- Draws a value array as vertical bars. Value changes are reflected as is on the next frame.

Example: Bar chart with one bar highlighted

```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): Constructor.
- `draw(graphic)` (method, since 0.3.0): Draw.
  - `graphic` Graphic: Graphic to draw with. (node local coordinates, origin at the top-left of the content area)
- `calculateDrawnRect(rect)` (method, since 0.4.0): Writes the drawn rectangle to rect. (Override: bars stay inside the content area; if the gaps are so wide that the bar width turns negative, it spills sideways by that much)
  - Vertically, the lowest bar is still drawn one unit tall, so it spills upward when the content height is under one unit. Grown by one unit.
  - `rect` Rect: Where to write the drawn rectangle. (origin at the top-left of the node's content area)
  - returns boolean: True if attached to a node.

  Example: Seeing where a bar chart draws

  ```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): Computes the bar rectangle list. (same positions as drawing)
  - `width` number: Graph width. (pixels, the remainder after the gaps between bars is divided by the bar count)
  - `height` number: Graph height. (pixels, the maximum value reaches this height)
  - returns Rect[]: One rectangle per bar. (sits on the bottom, 1 pixel tall even if the value is 0 or less)
- `setValueList(valueList)` (method, since 0.3.0): Sets the bar value list.
  - `valueList` number[]: Bar value list. (copied and kept)
- `getValueList()` (method, since 0.3.0): Returns the bar value list.
  - returns number[]: Copy of the bar value list.
- `setMaxValue(maxValue)` (method, since 0.3.0): Sets the maximum value. (the value at which a bar fills the full height, larger values are clipped)
  - `maxValue` number | null: Maximum value. (the largest value in the list if null)
- `setBarColor(barColor)` (method, since 0.3.0): Sets the bar color.
  - `barColor` Color | string: Bar color. (cloned and kept)
- `setHighlightColor(highlightColor)` (method, since 0.3.0): Sets the highlighted bar color.
  - `highlightColor` Color | string: Highlighted bar color. (cloned and kept)
- `setHighlightIndex(highlightIndex)` (method, since 0.3.0): Sets the index of the bar to highlight.
  - `highlightIndex` number: -1 means no highlight.
- `setBarGap(barGap)` (method, since 0.3.0): Sets the gap between bars.
  - `barGap` number: Gap between bars. (pixels)
