diff --git a/d3-axis/d3-axis-tests.ts b/d3-axis/d3-axis-tests.ts index a95c58c8b3..1aa50ba641 100644 --- a/d3-axis/d3-axis-tests.ts +++ b/d3-axis/d3-axis-tests.ts @@ -8,16 +8,20 @@ import * as d3Axis from 'd3-axis'; import { + scaleBand, + ScaleBand, scaleLinear, ScaleLinear, scaleOrdinal, ScaleOrdinal, + scalePoint, + ScalePoint, scalePow, ScalePower, scaleTime, - ScaleTime, + ScaleTime } from 'd3-scale'; -import { Selection } from 'd3-selection'; +import { select, Selection } from 'd3-selection'; import { Transition } from 'd3-transition'; import { timeMinute } from 'd3-time'; import { format } from 'd3-format'; @@ -41,15 +45,18 @@ let axisScaleString: d3Axis.AxisScale; axisScaleNumber = scaleLinear(); axisScaleDate = scaleTime(); axisScaleString = scaleOrdinal(); - +axisScaleNumber = scaleBand(); +axisScaleNumber = scalePoint(); +axisScaleString = scaleBand(); +axisScaleString = scalePoint(); // -------------------------------------------------------------------------- // Test AxisContainerElement // -------------------------------------------------------------------------- let containerElement: d3Axis.AxisContainerElement; -let svg: SVGSVGElement, - g: SVGGElement, - canvas: HTMLCanvasElement; +const svg: SVGSVGElement = select('svg').node() !; //mock +const g: SVGGElement = select('g').node() !; //mock +const canvas: HTMLCanvasElement = select('canvas').node() !; //mock containerElement = svg; containerElement = g; @@ -60,7 +67,7 @@ containerElement = g; // -------------------------------------------------------------------------- let topAxis: d3Axis.Axis = d3Axis.axisTop(scaleLinear()); -let rightAxis: d3Axis.Axis = d3Axis.axisRight(scaleTime()); +let rightAxis: d3Axis.Axis = d3Axis.axisRight(scaleTime()); let bottomAxis: d3Axis.Axis = d3Axis.axisBottom(scaleOrdinal()); let leftAxis: d3Axis.Axis = d3Axis.axisLeft(scaleLinear()); @@ -95,7 +102,7 @@ topAxis = topAxis.tickArguments([20, 's']); rightAxis = rightAxis.tickArguments([timeMinute.every(5)]); -let tickArguments: Array = leftAxis.tickArguments(); +let tickArguments: any[] = leftAxis.tickArguments(); // tickValues(...) ---------------------------------------------------------------- @@ -105,15 +112,17 @@ bottomAxis = bottomAxis.tickValues(['strongly negative', 'strongly positive']); leftAxis = leftAxis.tickValues(null); -let tickValues: Array = rightAxis.tickValues(); +let tickValues: Date[] | null = rightAxis.tickValues(); // tickFormat(...) ---------------------------------------------------------------- topAxis = topAxis.tickFormat(format(',.0f')); topAxis = topAxis.tickFormat(null); -let formatFn: (domainValue: string) => string = bottomAxis.tickFormat(); +let formatFn: ((domainValue: string, index: number) => string) | null = bottomAxis.tickFormat(); +bottomAxis.tickFormat(function (d, i) { return '#' + i; }); +bottomAxis.tickFormat(function (d) { return d + '!'; }); // tickSize(...) ---------------------------------------------------------------- rightAxis = rightAxis.tickSize(5); @@ -138,19 +147,19 @@ num = rightAxis.tickPadding(); // Test Apply Axis // -------------------------------------------------------------------------- -let gSelection: Selection; +let gSelection: Selection = select('g'); let gTransition = gSelection.transition(); gSelection.call(topAxis); gTransition.call(topAxis); -let svgSelection: Selection; +let svgSelection: Selection = select('g'); let svgTransition = svgSelection.transition(); svgSelection.call(leftAxis); svgTransition.call(leftAxis); -let canvasSelection: Selection; +let canvasSelection: Selection = select('canvas'); let canvasTransition = canvasSelection.transition(); // canvasSelection.call(rightAxis); // fails, incompatible context container element diff --git a/d3-axis/index.d.ts b/d3-axis/index.d.ts index e663743d44..cd8ed94714 100644 --- a/d3-axis/index.d.ts +++ b/d3-axis/index.d.ts @@ -1,8 +1,10 @@ -// Type definitions for D3JS d3-axis module v1.0.3 +// Type definitions for D3JS d3-axis module 1.0 // Project: https://github.com/d3/d3-axis/ // Definitions by: Tom Wanzek , Alex Ford , Boris Yankov // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// Last module patch version validated against: 1.0.4 + import { Selection, TransitionLike } from 'd3-selection'; // -------------------------------------------------------------------------- @@ -31,13 +33,16 @@ export interface AxisTimeInterval { * for axis to use the scale without error */ export interface AxisScale { - (x: Domain): number; - domain(): Array; - range(): Array; - copy(): AxisScale; + (x: Domain): number | undefined; + domain(): Domain[]; + range(): number[]; + copy(): this; bandwidth?(): number; - ticks?(count: number | AxisTimeInterval): Array | Array; - tickFormat?(count: number | AxisTimeInterval, specifier?: string): ((d: number) => string) | ((d: Date) => string); + // TODO: Reconsider the below, note that the compiler does not differentiate the overloads w.r.t. optionality + // ticks?(count?: number): Domain[]; + // ticks?(count?: AxisTimeInterval): Date[]; + // tickFormat?(count?: number, specifier?: string): ((d: number) => string); + // tickFormat?(count?: number | AxisTimeInterval, specifier?: string): ((d: Date) => string); } /** @@ -170,15 +175,16 @@ export interface Axis { /** * Returns the currently set tick format function, which defaults to null. */ - tickFormat(): ((domainValue: Domain) => string) | null; + tickFormat(): ((domainValue: Domain, index: number) => string) | null; /** * Sets the tick format function and returns the axis. * * @param format A function mapping a value from the axis Domain to a formatted string - * for display purposes. + * for display purposes. When invoked, the format function is also passed a second argument representing the zero-based index + * of the tick label in the array of generated tick labels. */ - tickFormat(format: (domainValue: Domain) => string): this; + tickFormat(format: (domainValue: Domain, index: number) => string): this; /** * Reset the tick format function. A null format indicates that the scale’s diff --git a/d3-axis/tsconfig.json b/d3-axis/tsconfig.json index b568e25181..225b56ee11 100644 --- a/d3-axis/tsconfig.json +++ b/d3-axis/tsconfig.json @@ -3,7 +3,7 @@ "module": "commonjs", "target": "es6", "noImplicitAny": true, - "strictNullChecks": false, + "strictNullChecks": true, "baseUrl": "../", "typeRoots": [ "../" diff --git a/d3-axis/tslint.json b/d3-axis/tslint.json new file mode 100644 index 0000000000..68d10d72a6 --- /dev/null +++ b/d3-axis/tslint.json @@ -0,0 +1,6 @@ +{ + "extends": "../tslint.json", + "rules": { + "unified-signatures": false + } +}