Skip to content

Instantly share code, notes, and snippets.

@kubijo
Last active July 10, 2020 09:22
Show Gist options
  • Save kubijo/1908412f6ae70bdedfaa710ed993d120 to your computer and use it in GitHub Desktop.
Save kubijo/1908412f6ae70bdedfaa710ed993d120 to your computer and use it in GitHub Desktop.
flow types for several D3 APIs
// @flow
/* eslint-disable no-use-before-define */
//
// Primitives
//
type x = number;
type y = number;
type dx = number;
type dy = number;
type Point = [x, y];
type Offset = [dx, dy];
type Extent = [number, number];
type Selector = string | HTMLElement | Selection;
type Datum = any;
type Index = number;
//
// Interfaces
//
export interface ScaleContinuous {
(number): number;
ticks(count_or_filter?: number | Function): mixed[];
tickFormat(count: number, specifier: string): ScaleContinuous;
copy(): ScaleContinuous;
nice(count: number): ScaleContinuous;
invert(number): number;
domain([number, number]): ScaleContinuous;
domain(): [number, number];
range([number, number]): ScaleContinuous;
range(): [number, number];
rangeRound([number, number]): ScaleContinuous;
rangeRound(): [number, number];
}
export interface Axis {
(scale: ScaleContinuous): Axis;
scale(): ScaleContinuous;
scale(ScaleContinuous): Axis;
ticks(): Axis;
tickArguments(): Axis;
tickValues(): Axis;
tickFormat(): Axis;
tickSize(): Axis;
tickSizeInner(): Axis;
tickSizeOuter(): Axis;
tickPadding(): Axis;
call(*): ScaleContinuous;
}
export interface Transform {
k: number;
x: number;
y: number;
scale(k: *): Transform;
translate(x: number, y: number): Transform;
apply(Point): Offset;
applyX(x: number): number;
applyY(y: number): number;
invert(Point): Point;
invertX(x: number): number;
invertY(y: number): number;
rescaleX(ScaleContinuous): ScaleContinuous;
rescaleY(ScaleContinuous): ScaleContinuous;
toString(): string;
}
export interface Zoom {
transform(Selection, Transform): Zoom;
scaleBy(Selection, number): Zoom;
scaleTo(Selection, number): Zoom;
translateBy(Selection, x: number, y: number): Zoom;
translateTo(Selection, x: number, y: number): Zoom;
wheelDelta(Function): Zoom;
wheelDelta(): Function;
filter(Function): Zoom;
filter(): Function;
touchable(): Function;
touchable(Function): Zoom;
extent(Function): Zoom;
extent(): Function;
scaleExtent(e: Extent): Zoom;
scaleExtent(): Extent;
translateExtent(): Extent;
translateExtent(e: Extent): Zoom;
constrain(Function): Zoom;
constrain(): Function;
duration(t: number): Zoom;
duration(): number;
interpolate(Function): Zoom;
interpolate(): Function;
on(name: string, handler: ?Function): Zoom;
on(name: string): Function;
clickDistance(): number;
clickDistance(t: number): Zoom;
}
//
// Selection
//
type SelectionValueFn<R> = (Datum, Index, Array<HTMLElement>) => R;
type SelectionValue<R> = R | SelectionValueFn<R>;
export interface Selection {
/**
* Selects the root (document.documentElement)
* This function can also be used to test for selections
* (instanceof d3.selection) or to extend the selection prototype.
*/
(): Selection;
select(Selector): Selection;
selectAll(Selector): Selection;
filter(Selector | SelectionValueFn<boolean>): Selection;
merge(other: Selection): Selection;
order(): Selection;
sort(compare?: (a: Datum, b: Datum) => -1 | 0 | 1): Selection;
nodes(): HTMLElement[];
node(): ?HTMLElement;
size(): number;
empty(): boolean;
each(SelectionValue<any>): Selection;
call(fn: Function, ...args?: any[]): Selection;
attr(name: string): mixed;
attr(name: string, value: SelectionValue<null | any>): Selection;
style(name: string): mixed;
style(name: string, value: SelectionValue<null | any>, priority?: null | 'important'): Selection;
property(name: string): mixed;
property(name: string, value: SelectionValue<null | any>): Selection;
classed(name: string): boolean;
classed(name: string, value: SelectionValue<boolean>): Selection;
text(): ?string;
text(value: SelectionValue<null | string>): Selection;
html(): ?string;
html(value: SelectionValue<null | string>): Selection;
raise(): Selection;
lower(): Selection;
append(type: string | SelectionValueFn<HTMLElement>): Selection;
insert(type: string | SelectionValueFn<HTMLElement>, before?: null | Selector): Selection;
remove(): Selection;
clone(deep?: boolean): Selection;
data(data: SelectionValue<Datum[]>, key?: SelectionValueFn<string>): Selection;
datum(value?: SelectionValue<null | Datum>): Selection;
enter(): Selection;
exit(): Selection;
on(typenames: string): ?SelectionValueFn<any>;
on(typenames: string, listener?: null | SelectionValueFn<any>, capture?: boolean): Selection;
dispatch(
type: string,
parameters?: SelectionValue<{ bubbles?: boolean, cancelable?: boolean, detail?: any }>,
): Selection;
// Transition
transition(name?: string): Transition;
interrupt(name?: string): Transition;
}
//
// Transition
//
type TransitionValueFn<R> = (Datum, Index) => R; // + `this` bound to current DOM node
type TransitionValue<R> = R | ((Datum, Index) => R);
type TransitionInterpolator = (easedTime: number) => mixed;
type TransitionInterpolatorFactory = TransitionValueFn<TransitionInterpolator>;
type TransitionEasing = (easedTime: number) => number;
export interface Transition {
nodes(): HTMLElement[];
node(): ?HTMLElement;
size(): number;
empty(): boolean;
select(Selector | TransitionValueFn<Selection>): Transition;
selectAll(Selector | TransitionValueFn<Selection>): Transition;
filter(Selector | TransitionValueFn<boolean>): Transition;
merge(other: Transition): Transition;
transition(): Transition;
selection(): Selection;
each(SelectionValue<any>): Transition;
call(fn: Function, ...args?: any[]): Transition;
on(type: 'start' | 'end' | 'interrupt' | string): ?TransitionValueFn<any>;
on(type: 'start' | 'end' | 'interrupt' | string, listener?: null | TransitionValueFn<any>): Transition;
attr(name: string, value: SelectionValue<null | any>): Transition;
attrTween(name: string): ?TransitionInterpolatorFactory;
attrTween(name: string, factory: null | TransitionInterpolatorFactory): Transition;
style(name: string, value: TransitionValue<string | number>, priority?: 'important'): Transition;
styleTween(name: string): ?TransitionInterpolatorFactory;
styleTween(name: string, factory: null | TransitionInterpolatorFactory, priority?: 'important'): Transition;
text(TransitionValue<string>): Transition;
remove(): Transition;
tween(name: string): ?TransitionInterpolatorFactory;
tween(name: string, value?: null | TransitionInterpolatorFactory): any;
delay(): ?number;
delay(TransitionValue<number>): any;
duration(): ?number;
duration(TransitionValue<number>): any;
ease(): TransitionEasing;
ease(TransitionEasing): any;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment