Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
berekuk committed Jul 11, 2023
1 parent c4b1885 commit c57aed0
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions packages/components/src/lib/d3/patchedScales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ function tickFormatWithCustom(
return d3.tickFormat(start, stop, count, specifier);
}

type ScaleLinear = d3.ScaleLinear<number, number, never>;
type ScaleLogarithmic = d3.ScaleLogarithmic<number, number, never>;
type ScaleSymLog = d3.ScaleSymLog<number, number, never>;
type ScalePower = d3.ScalePower<number, number, never>;

function patchLinearishTickFormat<
T extends
| d3.ScaleLinear<number, number, never>
| d3.ScaleSymLog<number, number, never>
| d3.ScalePower<number, number, never>,
T extends ScaleLinear | ScaleSymLog | ScalePower,
>(scale: T): T {
// copy-pasted from https://github.com/d3/d3-scale/blob/83555bd759c7314420bd4240642beda5e258db9e/src/linear.js#L14
scale.tickFormat = (count, specifier) => {
Expand All @@ -70,23 +72,11 @@ function patchLinearishTickFormat<
return scale;
}

// Original d3.scale* should never be used; they won't support our custom tick formats.

export function scaleLinear() {
return patchLinearishTickFormat(d3.scaleLinear());
}

export function scaleSymlog() {
return patchLinearishTickFormat(d3.scaleSymlog());
function patchSymlogTickFormat(scale: ScaleSymLog): ScaleSymLog {
return patchLinearishTickFormat(scale);
}

export function scalePow() {
return patchLinearishTickFormat(d3.scalePow());
}

export function scaleLog() {
// log scale tickFormat is special
const scale = d3.scaleLog();
function patchLogarithmicTickFormat(scale: ScaleLogarithmic): ScaleLogarithmic {
const logScaleTickFormat = scale.tickFormat;
scale.tickFormat = (count, specifier) => {
return logScaleTickFormat(
Expand All @@ -101,3 +91,21 @@ export function scaleLog() {
};
return scale;
}

// Original d3.scale* should never be used; they won't support our custom tick formats.

export function scaleLinear() {
return patchLinearishTickFormat(d3.scaleLinear());
}

export function scaleSymlog() {
return patchSymlogTickFormat(d3.scaleSymlog());
}

export function scalePow() {
return patchLinearishTickFormat(d3.scalePow());
}

export function scaleLog() {
return patchLogarithmicTickFormat(d3.scaleLog());
}

0 comments on commit c57aed0

Please sign in to comment.