@thi.ng/text-canvas 2.5.0 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2023-08-14T13:30:45Z
3
+ - **Last updated**: 2023-08-24T05:28:47Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,17 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ## [2.6.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/text-canvas@2.6.0) (2023-08-24)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add lineChart() & lineChartStr() ([097e00c](https://github.com/thi-ng/umbrella/commit/097e00c))
17
+
18
+ #### ♻️ Refactoring
19
+
20
+ - update bar chart min/max handling ([e45247d](https://github.com/thi-ng/umbrella/commit/e45247d))
21
+ - auto-compute value range if not specified
22
+
12
23
  ## [2.5.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/text-canvas@2.5.0) (2023-08-14)
13
24
 
14
25
  #### 🚀 Features
package/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
  [![Mastodon Follow](https://img.shields.io/mastodon/follow/109331703950160316?domain=https%3A%2F%2Fmastodon.thi.ng&style=social)](https://mastodon.thi.ng/@toxi)
9
9
 
10
10
  This project is part of the
11
- [@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo.
11
+ [@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo and anti-framework.
12
12
 
13
13
  - [About](#about)
14
14
  - [Status](#status)
package/bars.js CHANGED
@@ -1,12 +1,18 @@
1
+ import { ensureArray } from "@thi.ng/arrays/ensure-array";
1
2
  import { fitClamped } from "@thi.ng/math/fit";
2
3
  import { fract } from "@thi.ng/math/prec";
3
4
  import { padLeft } from "@thi.ng/strings/pad-left";
4
5
  import { padRight } from "@thi.ng/strings/pad-right";
5
6
  import { repeat } from "@thi.ng/strings/repeat";
6
7
  import { map } from "@thi.ng/transducers/map";
8
+ import { max as $max } from "@thi.ng/transducers/max";
9
+ import { min as $min } from "@thi.ng/transducers/min";
7
10
  import { BARS_H, BARS_V } from "./api.js";
8
- export const barChartHLines = (height, vals, min = 0, max = 1) => {
9
- const bars = [...map((x) => barVertical(height, x, min, max, ""), vals)];
11
+ export const barChartHLines = (height, vals, min, max) => {
12
+ const $vals = ensureArray(vals);
13
+ min = min !== undefined ? min : $min($vals);
14
+ max = max !== undefined ? max : $max($vals);
15
+ const bars = [...map((x) => barVertical(height, x, min, max, ""), $vals)];
10
16
  const num = bars.length;
11
17
  const res = [];
12
18
  for (let i = 0; i < height; i++) {
@@ -19,7 +25,12 @@ export const barChartHLines = (height, vals, min = 0, max = 1) => {
19
25
  return res;
20
26
  };
21
27
  export const barChartHStr = (height, vals, min, max) => barChartHLines(height, vals, min, max).join("\n");
22
- export const barChartVLines = (width, vals, min = 0, max = 1) => [...map((x) => barHorizontal(width, x, min, max), vals)];
28
+ export const barChartVLines = (width, vals, min, max) => {
29
+ const $vals = ensureArray(vals);
30
+ min = min !== undefined ? min : $min($vals);
31
+ max = max !== undefined ? max : $max($vals);
32
+ return [...map((x) => barHorizontal(width, x, min, max), $vals)];
33
+ };
23
34
  export const barChartVStr = (width, vals, min, max) => barChartVLines(width, vals, min, max).join("\n");
24
35
  export const barHorizontal = (width, x, min = 0, max = 1) => bar(BARS_H, width, false, x, min, max, "");
25
36
  export const barVertical = (height, x, min = 0, max = 1, delim = "\n") => bar(BARS_V, height, true, x, min, max, delim);
package/line.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { NumOrString } from "@thi.ng/api";
2
- import type { Canvas } from "./canvas.js";
2
+ import { Canvas } from "./canvas.js";
3
3
  /**
4
4
  * Draws a line between `ax`,`ay` and `bx`,`by`, using `char` and taking
5
5
  * the current clip rect and format into account. If `char` is not
@@ -13,4 +13,6 @@ import type { Canvas } from "./canvas.js";
13
13
  * @param char -
14
14
  */
15
15
  export declare const line: (canvas: Canvas, ax: number, ay: number, bx: number, by: number, char?: NumOrString, format?: number) => void;
16
+ export declare const lineChart: (canvas: Canvas, x: number, y: number, height: number, vals: Iterable<number>, min?: number, max?: number, format?: number) => void;
17
+ export declare const lineChartStr: (height: number, vals: Iterable<number>, min?: number, max?: number) => string;
16
18
  //# sourceMappingURL=line.d.ts.map
package/line.js CHANGED
@@ -1,5 +1,12 @@
1
+ import { ensureArray } from "@thi.ng/arrays/ensure-array";
1
2
  import { peek } from "@thi.ng/arrays/peek";
2
3
  import { liangBarsky2Raw } from "@thi.ng/geom-clip-line/liang-barsky";
4
+ import { fitClamped } from "@thi.ng/math/fit";
5
+ import { minMax } from "@thi.ng/math/interval";
6
+ import { max as $max } from "@thi.ng/transducers/max";
7
+ import { min as $min } from "@thi.ng/transducers/min";
8
+ import { Canvas } from "./canvas.js";
9
+ import { formatCanvas } from "./format.js";
3
10
  import { charCode } from "./utils.js";
4
11
  /**
5
12
  * Draws a line between `ax`,`ay` and `bx`,`by`, using `char` and taking
@@ -44,3 +51,38 @@ export const line = (canvas, ax, ay, bx, by, char, format = canvas.format) => {
44
51
  }
45
52
  }
46
53
  };
54
+ export const lineChart = (canvas, x, y, height, vals, min, max, format = canvas.format) => {
55
+ const $vals = ensureArray(vals);
56
+ min = min !== undefined ? min : $min($vals);
57
+ max = max !== undefined ? max : $max($vals);
58
+ height--;
59
+ format <<= 16;
60
+ const { x1, x2 } = peek(canvas.clipRects);
61
+ for (let i = 0, n = $vals.length - 1; i < n; i++) {
62
+ const xx = x + i;
63
+ if (xx < x1)
64
+ continue;
65
+ if (xx > x2)
66
+ break;
67
+ const ya = Math.round(fitClamped($vals[i], min, max, height, 0)) + y;
68
+ const yb = Math.round(fitClamped($vals[i + 1], min, max, height, 0)) + y;
69
+ if (ya === yb) {
70
+ canvas.setAt(xx, ya, 0x2500 | format); // ─
71
+ }
72
+ else {
73
+ // vline to new Y
74
+ let [y1, y2] = minMax(ya, yb);
75
+ while (++y1 < y2)
76
+ canvas.setAt(xx, y1, 0x2502 | format); // │
77
+ // end points
78
+ canvas.setAt(xx, ya, (ya < yb ? 0x256e : 0x256f) | format); // ╮ ╯
79
+ canvas.setAt(xx, yb, (ya < yb ? 0x2570 : 0x256d) | format); // ╰ ╭
80
+ }
81
+ }
82
+ };
83
+ export const lineChartStr = (height, vals, min, max) => {
84
+ const $vals = ensureArray(vals);
85
+ const surf = new Canvas($vals.length, height);
86
+ lineChart(surf, 0, 0, height, $vals, min, max);
87
+ return formatCanvas(surf);
88
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/text-canvas",
3
- "version": "2.5.0",
3
+ "version": "2.6.0",
4
4
  "description": "Text based canvas, drawing, tables with arbitrary formatting (incl. ANSI/HTML)",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -34,19 +34,19 @@
34
34
  "test": "testament test"
35
35
  },
36
36
  "dependencies": {
37
- "@thi.ng/api": "^8.9.3",
38
- "@thi.ng/arrays": "^2.5.19",
39
- "@thi.ng/checks": "^3.4.3",
40
- "@thi.ng/errors": "^2.3.3",
41
- "@thi.ng/geom-clip-line": "^2.3.25",
42
- "@thi.ng/math": "^5.5.4",
43
- "@thi.ng/strings": "^3.4.11",
44
- "@thi.ng/text-format": "^1.4.11",
45
- "@thi.ng/transducers": "^8.5.5"
37
+ "@thi.ng/api": "^8.9.4",
38
+ "@thi.ng/arrays": "^2.5.20",
39
+ "@thi.ng/checks": "^3.4.4",
40
+ "@thi.ng/errors": "^2.3.4",
41
+ "@thi.ng/geom-clip-line": "^2.3.27",
42
+ "@thi.ng/math": "^5.6.0",
43
+ "@thi.ng/strings": "^3.4.12",
44
+ "@thi.ng/text-format": "^1.4.12",
45
+ "@thi.ng/transducers": "^8.6.1"
46
46
  },
47
47
  "devDependencies": {
48
48
  "@microsoft/api-extractor": "^7.36.4",
49
- "@thi.ng/testament": "^0.3.21",
49
+ "@thi.ng/testament": "^0.3.22",
50
50
  "rimraf": "^5.0.1",
51
51
  "tools": "^0.0.1",
52
52
  "tslib": "^2.6.1",
@@ -139,5 +139,5 @@
139
139
  ],
140
140
  "year": 2020
141
141
  },
142
- "gitHead": "4e8f1abeeba7b919e069954eada917ecd2e9a8e9\n"
142
+ "gitHead": "04f545c7be1811ed24be1a6824d867b1ea9bded1\n"
143
143
  }