/
bar.ts
45 lines (43 loc) · 1.24 KB
/
bar.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import type { Fn3 } from "@thi.ng/api";
import { isFunction } from "@thi.ng/checks";
import { map } from "@thi.ng/transducers";
import type { DomainValues, PlotFn } from "../api";
import { valueMapper } from "./utils";
export interface BarPlotOpts {
attribs: any;
interleave: number;
offset: number;
width: number;
shape: Fn3<number, number[], number[], any>;
}
export const barPlot = (
data: DomainValues,
opts: Partial<BarPlotOpts> = {}
): PlotFn => (spec) => {
opts = {
interleave: 1,
offset: 0,
width: 5,
shape: (_, a, b) => ["line", {}, a, b],
...opts,
};
const mapper = valueMapper(spec.xaxis, spec.yaxis, spec.project);
const offset =
(opts.offset! + 0.5) * opts.width! -
0.5 * opts.interleave! * opts.width!;
const y0 = spec.yaxis.domain[0];
return [
"g",
{ weight: opts.width!, ...opts.attribs },
...map(
([x, val]) => {
const a = mapper([x, y0]);
a[0] += offset;
const b = mapper([x, val]);
b[0] += offset;
return opts.shape!(val, a, b);
},
isFunction(data) ? data(spec.xaxis.domain) : data
),
];
};