Skip to content

Commit

Permalink
refactor(text-canvas): update bar chart min/max handling
Browse files Browse the repository at this point in the history
- auto-compute value range if not specified
  • Loading branch information
postspectacular committed Aug 24, 2023
1 parent 097e00c commit e45247d
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions packages/text-canvas/src/bars.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { ensureArray } from "@thi.ng/arrays/ensure-array";
import { fitClamped } from "@thi.ng/math/fit";
import { fract } from "@thi.ng/math/prec";
import { padLeft } from "@thi.ng/strings/pad-left";
import { padRight } from "@thi.ng/strings/pad-right";
import { repeat } from "@thi.ng/strings/repeat";
import { map } from "@thi.ng/transducers/map";
import { max as $max } from "@thi.ng/transducers/max";
import { min as $min } from "@thi.ng/transducers/min";
import { BARS_H, BARS_V } from "./api.js";

export const barChartHLines = (
height: number,
vals: Iterable<number>,
min = 0,
max = 1
min?: number,
max?: number
) => {
const bars = [...map((x) => barVertical(height, x, min, max, ""), vals)];
const $vals = ensureArray(vals);
min = min !== undefined ? min : $min($vals);
max = max !== undefined ? max : $max($vals);
const bars = [...map((x) => barVertical(height, x, min, max, ""), $vals)];
const num = bars.length;
const res: string[] = [];
for (let i = 0; i < height; i++) {
Expand All @@ -35,9 +41,14 @@ export const barChartHStr = (
export const barChartVLines = (
width: number,
vals: Iterable<number>,
min = 0,
max = 1
) => [...map((x) => barHorizontal(width, x, min, max), vals)];
min?: number,
max?: number
) => {
const $vals = ensureArray(vals);
min = min !== undefined ? min : $min($vals);
max = max !== undefined ? max : $max($vals);
return [...map((x) => barHorizontal(width, x, min, max), $vals)];
};

export const barChartVStr = (
width: number,
Expand Down

0 comments on commit e45247d

Please sign in to comment.