UNPKG

12.8 kBTypeScriptView Raw
1// Type definitions for D3JS d3-axis module 3.0
2// Project: https://github.com/d3/d3-axis/, https://d3js.org/d3-axis
3// Definitions by: Tom Wanzek <https://github.com/tomwanzek>
4// Alex Ford <https://github.com/gustavderdrache>
5// Boris Yankov <https://github.com/borisyankov>
6// denisname <https://github.com/denisname>
7// Nathan Bierema <https://github.com/Methuselah96>
8// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
9
10// Last module patch version validated against: 3.0.0
11
12import { Selection, TransitionLike } from 'd3-selection';
13
14// --------------------------------------------------------------------------
15// Shared Types and Interfaces
16// --------------------------------------------------------------------------
17
18/**
19 * A helper type to alias elements which can serve as a domain for an axis.
20 */
21export type AxisDomain = number | string | Date | { valueOf(): number};
22
23/**
24 * A helper interface to describe the minimal contract to be met by a time interval
25 * which can be passed into the Axis.ticks(...) or Axis.tickArguments(...) methods when
26 * creating time series axes. Under normal circumstances the argument will be of type
27 * TimeInterval or CountableTimeInterval as defined in d3-time.
28 * NB: This helper interface has been created to avoid tight coupling of d3-axis to
29 * d3-time at the level of definition files. I.e. d3-time is not a
30 * dependency of d3-axis in the D3 Javascript implementation. This minimal contract
31 * is based on an analysis of how d3-axis passes a time interval argument into a time scale,
32 * if a time scale was set using Axis.scale(...). And in turn on how a time scale uses
33 * the time interval when creating ticks from it.
34 */
35export interface AxisTimeInterval {
36 range(start: Date, stop: Date, step?: number): Date[];
37}
38
39/**
40 * A helper interface to which a scale passed into axis must conform (at a minimum)
41 * for axis to use the scale without error.
42 */
43export interface AxisScale<Domain> {
44 (x: Domain): number | undefined;
45 domain(): Domain[];
46 range(): number[];
47 copy(): this;
48 bandwidth?(): number;
49 // TODO: Reconsider the below, note that the compiler does not differentiate the overloads w.r.t. optionality
50 // ticks?(count?: number): Domain[];
51 // ticks?(count?: AxisTimeInterval): Date[];
52 // tickFormat?(count?: number, specifier?: string): ((d: number) => string);
53 // tickFormat?(count?: number | AxisTimeInterval, specifier?: string): ((d: Date) => string);
54}
55
56/**
57 * A helper type to alias elements which can serve as a container for an axis.
58 */
59export type AxisContainerElement = SVGSVGElement | SVGGElement;
60
61/**
62 * Interface defining an axis generator. The generic <Domain> is the type of the axis domain.
63 */
64export interface Axis<Domain> {
65 /**
66 * Render the axis to the given context.
67 *
68 * @param context A selection of or a transition defined on SVG containers (either SVG or G elements).
69 */
70 (context: Selection<SVGSVGElement, any, any, any> | Selection<SVGGElement, any, any, any> | TransitionLike<SVGSVGElement, any> | TransitionLike<SVGGElement, any>): void;
71
72 /**
73 * Gets the current scale underlying the axis.
74 */
75 // eslint-disable-next-line no-unnecessary-generics
76 scale<A extends AxisScale<Domain>>(): A;
77
78 /**
79 * Sets the scale and returns the axis.
80 *
81 * @param scale The scale to be used for axis generation.
82 */
83 scale(scale: AxisScale<Domain>): this;
84
85 /**
86 * Sets the arguments that will be passed to scale.ticks and scale.tickFormat when the axis is rendered, and returns the axis generator.
87 *
88 * This method has no effect if the scale does not implement scale.ticks, as with band and point scales.
89 *
90 * This method is also a convenience function for axis.tickArguments.
91 *
92 * @param count Number of ticks that should be rendered.
93 * @param specifier An optional format specifier to customize how the tick values are formatted.
94 */
95 ticks(count: number, specifier?: string): this;
96
97 /**
98 * Sets the arguments that will be passed to scale.ticks and scale.tickFormat when the axis is rendered, and returns the axis generator.
99 * Use with a TIME SCALE ONLY.
100 *
101 * This method is also a convenience function for axis.tickArguments.
102 *
103 * @param interval A time interval used to generate date-based ticks. This is typically a TimeInterval/CountableTimeInterval as defined
104 * in d3-time. E.g. as obtained by passing in d3.timeMinute.every(15).
105 * @param specifier An optional format specifier to customize how the tick values are formatted.
106 */
107 // tslint:disable-next-line:unified-signatures
108 ticks(interval: AxisTimeInterval, specifier?: string): this;
109
110 /**
111 * Sets the arguments that will be passed to scale.ticks and scale.tickFormat when the axis is rendered, and returns the axis generator.
112 *
113 * The meaning of the arguments depends on the axis’ scale type: most commonly, the arguments are a suggested count for the number of ticks
114 * (or a time interval for time scales), and an optional format specifier to customize how the tick values are formatted.
115 *
116 * This method has no effect if the scale does not implement scale.ticks, as with band and point scales.
117 *
118 * To set the tick values explicitly, use axis.tickValues. To set the tick format explicitly, use axis.tickFormat.
119 *
120 * This method is also a convenience function for axis.tickArguments.
121 */
122 ticks(arg0: any, ...args: any[]): this;
123
124 /**
125 * Get an array containing the currently set arguments to be passed into scale.ticks and scale.tickFormat, which defaults to the empty array.
126 */
127 tickArguments(): any[];
128
129 /**
130 * Sets the arguments that will be passed to scale.ticks and scale.tickFormat when the axis is rendered, and returns the axis generator.
131 *
132 * This method has no effect if the scale does not implement scale.ticks, as with band and point scales.
133 * To set the tick values explicitly, use axis.tickValues. To set the tick format explicitly, use axis.tickFormat.
134 *
135 * See also axis.ticks.
136 *
137 * @param args The meaning of the arguments depends on the axis’ scale type: most commonly, the arguments are a
138 * suggested count for the number of ticks (or a time interval for time scales), and an optional format specifier to
139 * customize how the tick values are formatted.
140 */
141 tickArguments(args: any[]): this;
142
143 /**
144 * Returns the current tick values, which defaults to null.
145 */
146 tickValues(): Domain[] | null;
147
148 /**
149 * Specified values to be used for ticks rather than using the scale’s automatic tick generator.
150 * The explicit tick values take precedent over the tick arguments set by axis.tickArguments.
151 * However, any tick arguments will still be passed to the scale’s tickFormat function if a
152 * tick format is not also set.
153 *
154 * @param values An iterable with values from the Domain of the scale underlying the axis.
155 */
156 tickValues(values: Iterable<Domain>): this;
157
158 /**
159 * Clears any previously-set explicit tick values and reverts back to the scale’s tick generator.
160 *
161 * @param values null
162 */
163 tickValues(values: null): this;
164
165 /**
166 * Returns the currently set tick format function, which defaults to null.
167 */
168 tickFormat(): ((domainValue: Domain, index: number) => string) | null;
169
170 /**
171 * Sets the tick format function and returns the axis.
172 *
173 * @param format A function mapping a value from the axis Domain to a formatted string
174 * for display purposes. When invoked, the format function is also passed a second argument representing the zero-based index
175 * of the tick label in the array of generated tick labels.
176 */
177 tickFormat(format: (domainValue: Domain, index: number) => string): this;
178
179 /**
180 * Reset the tick format function. A null format indicates that the scales
181 * default formatter should be used, which is generated by calling scale.tickFormat.
182 * In this case, the arguments specified by axis.tickArguments
183 * are likewise passed to scale.tickFormat.
184 *
185 * @param format null
186 */
187 tickFormat(format: null): this;
188
189 /**
190 * Get the current inner tick size, which defaults to 6.
191 */
192 tickSize(): number;
193 /**
194 * Set the inner and outer tick size to the specified value and return the axis.
195 *
196 * @param size Tick size in pixels (Default is 6).
197 */
198 tickSize(size: number): this;
199
200 /**
201 * Get the current inner tick size, which defaults to 6.
202 * The inner tick size controls the length of the tick lines,
203 * offset from the native position of the axis.
204 */
205 tickSizeInner(): number;
206
207 /**
208 * Set the inner tick size to the specified value and return the axis.
209 * The inner tick size controls the length of the tick lines,
210 * offset from the native position of the axis.
211 *
212 * @param size Tick size in pixels (Default is 6).
213 */
214 tickSizeInner(size: number): this;
215
216 /**
217 * Get the current outer tick size, which defaults to 6.
218 * The outer tick size controls the length of the square ends of the domain path,
219 * offset from the native position of the axis. Thus, theouter ticksare not actually
220 * ticks but part of the domain path, and their position is determined by the associated
221 * scales domain extent. Thus, outer ticks may overlap with the first or last inner tick.
222 * An outer tick size of 0 suppresses the square ends of the domain path,
223 * instead producing a straight line.
224 */
225 tickSizeOuter(): number;
226
227 /**
228 * Set the current outer tick size and return the axis.
229 * The outer tick size controls the length of the square ends of the domain path,
230 * offset from the native position of the axis. Thus, theouter ticksare not actually
231 * ticks but part of the domain path, and their position is determined by the associated
232 * scales domain extent. Thus, outer ticks may overlap with the first or last inner tick.
233 * An outer tick size of 0 suppresses the square ends of the domain path,
234 * instead producing a straight line.
235 *
236 * @param size Tick size in pixels (Default is 6).
237 */
238 tickSizeOuter(size: number): this;
239
240 /**
241 * Get the current padding, which defaults to 3.
242 */
243 tickPadding(): number;
244
245 /**
246 * Set the current padding and return the axis.
247 *
248 * @param padding Padding in pixels (Default is 3).
249 */
250 tickPadding(padding: number): this;
251
252 /**
253 * Returns the current offset which defaults to 0 on devices with a devicePixelRatio greater than 1, and 0.5px otherwise.
254 * This default offset ensures crisp edges on low-resolution devices.
255 */
256 offset(): number;
257
258 /**
259 * Sets the offset to the specified value in pixels and returns the axis.
260 * Defaults to 0 on devices with a devicePixelRatio greater than 1, and 0.5px otherwise.
261 * This default offset ensures crisp edges on low-resolution devices.
262 */
263 offset(offset: number): this;
264}
265
266/**
267 * Constructs a new top-oriented axis generator for the given scale, with empty tick arguments,
268 * a tick size of 6 and padding of 3. In this orientation, ticks are drawn above the horizontal domain path.
269 *
270 * @param scale The scale to be used for axis generation.
271 */
272export function axisTop<Domain extends AxisDomain>(scale: AxisScale<Domain>): Axis<Domain>;
273
274/**
275 * Constructs a new right-oriented axis generator for the given scale, with empty tick arguments,
276 * a tick size of 6 and padding of 3. In this orientation, ticks are drawn to the right of the vertical domain path.
277 *
278 * @param scale The scale to be used for axis generation.
279 */
280export function axisRight<Domain extends AxisDomain>(scale: AxisScale<Domain>): Axis<Domain>;
281
282/**
283 * Constructs a new bottom-oriented axis generator for the given scale, with empty tick arguments,
284 * a tick size of 6 and padding of 3. In this orientation, ticks are drawn below the horizontal domain path.
285 *
286 * @param scale The scale to be used for axis generation.
287 */
288export function axisBottom<Domain extends AxisDomain>(scale: AxisScale<Domain>): Axis<Domain>;
289
290/**
291 * Constructs a new left-oriented axis generator for the given scale, with empty tick arguments,
292 * a tick size of 6 and padding of 3. In this orientation, ticks are drawn to the left of the vertical domain path.
293 *
294 * @param scale The scale to be used for axis generation.
295 */
296export function axisLeft<Domain extends AxisDomain>(scale: AxisScale<Domain>): Axis<Domain>;
297
\No newline at end of file