-
-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(viz): redo log scale & ticks, restructure all files
- add dataMinLog/dataMaxLog/dataBounds2Log()
- Loading branch information
1 parent
b5a8f53
commit 2f51668
Showing
11 changed files
with
176 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { float } from "@thi.ng/strings"; | ||
import type { AxisSpec } from "../api"; | ||
|
||
export const axisDefaults = (extra?: any): Partial<AxisSpec> => ({ | ||
attribs: { stroke: "#000" }, | ||
label: (pos, body) => ["text", {}, pos, body], | ||
labelAttribs: { | ||
fill: "#000", | ||
stroke: "none", | ||
}, | ||
labelOffset: [0, 0], | ||
format: float(2), | ||
visible: true, | ||
major: { ticks: () => [], size: 10 }, | ||
minor: { ticks: () => [], size: 5 }, | ||
...extra, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { mergeDeepObj } from "@thi.ng/associative"; | ||
import { lens, mix, safeDiv } from "@thi.ng/math"; | ||
import type { AxisSpec, Domain, InitialAxisSpec, Range, ScaleFn } from "../api"; | ||
import { axisDefaults } from "./common"; | ||
|
||
export const lensScale = ( | ||
[d1, d2]: Domain, | ||
[r1, r2]: Range, | ||
focus = (d1 + d2) / 2, | ||
strength: number | ||
): ScaleFn => { | ||
const dr = d2 - d1; | ||
const f = safeDiv(focus - d1, dr); | ||
return (x) => mix(r1, r2, lens(f, strength, safeDiv(x - d1, dr))); | ||
}; | ||
|
||
export const lensAxis = ( | ||
src: InitialAxisSpec & { focus?: number; strength?: number } | ||
): AxisSpec => { | ||
const spec = mergeDeepObj( | ||
axisDefaults({ | ||
focus: (src.domain[0] + src.domain[1]) / 2, | ||
strength: 1, | ||
}), | ||
src | ||
); | ||
!spec.scale && | ||
(spec.scale = lensScale( | ||
spec.domain, | ||
spec.range, | ||
spec.focus, | ||
spec.strength | ||
)); | ||
return spec; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { mergeDeepObj } from "@thi.ng/associative"; | ||
import { fit, inRange, roundTo } from "@thi.ng/math"; | ||
import { filter, range } from "@thi.ng/transducers"; | ||
import type { AxisSpec, Domain, InitialAxisSpec, Range, ScaleFn } from "../api"; | ||
import { axisDefaults } from "./common"; | ||
|
||
export const linearScale = ([d1, d2]: Domain, [r1, r2]: Range): ScaleFn => ( | ||
x | ||
) => fit(x, d1, d2, r1, r2); | ||
|
||
export const linearAxis = (src: InitialAxisSpec) => { | ||
const spec = <AxisSpec>mergeDeepObj(axisDefaults(), src); | ||
!spec.scale && (spec.scale = linearScale(spec.domain, spec.range)); | ||
return spec; | ||
}; | ||
|
||
export const linearTicks = (step: number) => ([d1, d2]: Domain) => [ | ||
...filter( | ||
(x) => inRange(x, d1, d2), | ||
range(roundTo(d1, step), d2 + step, step) | ||
), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import type { FnN, FnU3 } from "@thi.ng/api"; | ||
import { mergeDeepObj } from "@thi.ng/associative"; | ||
import { inRange, mix } from "@thi.ng/math"; | ||
import { | ||
comp, | ||
filter, | ||
iterator, | ||
map, | ||
range, | ||
range2d, | ||
} from "@thi.ng/transducers"; | ||
import type { AxisSpec, Domain, InitialAxisSpec, Range, ScaleFn } from "../api"; | ||
import { axisDefaults } from "./common"; | ||
|
||
/** @internal */ | ||
export const log = (base: number): FnN => { | ||
const lb = 1 / Math.log(base); | ||
return (x) => (x > 0 ? Math.log(x) * lb : x < 0 ? -Math.log(-x) * lb : 0); | ||
}; | ||
|
||
export const logScale__ = ( | ||
[d1, d2]: Domain, | ||
[r1, r2]: Range, | ||
base = 10 | ||
): ScaleFn => { | ||
const $ = log(base); | ||
const dr = 1 / $(d2 - d1 + 1); | ||
return (x) => mix(r1, r2, $(x - d1 + 1) * dr); | ||
}; | ||
|
||
export const logScale = ( | ||
[d1, d2]: Domain, | ||
[r1, r2]: Range, | ||
base = 10 | ||
): ScaleFn => { | ||
const $ = log(base); | ||
const d11 = $(d1); | ||
const d3 = $(d2) - d11; | ||
return (x) => mix(r1, r2, ($(x) - d11) / d3); | ||
}; | ||
|
||
export const logAxis = (src: InitialAxisSpec & { base?: number }): AxisSpec => { | ||
const spec = mergeDeepObj(axisDefaults({ base: 10 }), src); | ||
!spec.scale && (spec.scale = logScale(spec.domain, spec.range, spec.base)); | ||
return spec; | ||
}; | ||
|
||
/** @internal */ | ||
export const logDomain: FnU3<number, number[]> = (d1, d2, base) => { | ||
const $ = log(base); | ||
return [Math.floor($(d1)), Math.ceil($(d2))]; | ||
}; | ||
|
||
export const logTicksMajor = (base = 10) => ([d1, d2]: Domain) => { | ||
const [d1l, d2l] = logDomain(d1, d2, base); | ||
return [ | ||
...iterator( | ||
comp( | ||
map((x) => Math.pow(base, x)), | ||
filter((x) => inRange(x, d1, d2)) | ||
), | ||
range(d1l, d2l + 1) | ||
), | ||
]; | ||
}; | ||
|
||
export const logTicksMinor = (base = 10) => ([d1, d2]: Domain) => { | ||
const [d1l, d2l] = logDomain(d1, d2, base); | ||
return [ | ||
...iterator( | ||
comp( | ||
map(([m, n]) => (m * Math.pow(base, n)) / base), | ||
filter((x) => inRange(x, d1, d2)) | ||
), | ||
range2d(1, base, d1l, d2l + 1) | ||
), | ||
]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.