Skip to content

Commit 191a78f

Browse files
committed
add example and hint about scales
1 parent 7b0eaf5 commit 191a78f

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,9 +1165,17 @@ Due to some heavy typing a D3 Selection (such as returned by `d3.select` or `d3.
11651165
1. the element type of the parent element, e.g. `d3.select("body").selectAll("div").data([1, 2, 3])` will be a `HTMLBodyElement`.
11661166
1. the data type of the parent element, e.g. `d3.select("body").datum("data").selectAll("div").data([1, 2, 3])` will be a `string`.
11671167

1168-
In my experience the third and fourth argument are barely of any use of which it can be simplified and set to e.g. `unknown` or `any`.
1168+
In my experience the third and fourth argument are barely of any use of which it can be simplified and set to e.g. `unknown` or `any`. However, one has to fully define a selection if you wanna explicitly define a variable containing a D3 selection,
11691169

1170-
One can specify the type in more detail by specifing the generic argument of the function. This is useful when the selector is more complex that just the element type. e.g. `d3.select<SVGGElement, unknown>(".chart")`
1170+
```ts
1171+
import * as d3 from "d3";
1172+
1173+
let rects: Selection<SVGRectElement, number, SVGGElement, unknown>;
1174+
1175+
rects = d3.select("g").selectAll("rect").data([1, 2, 3]);
1176+
```
1177+
1178+
One can specify the type in more detail by specifing the generic argument of the function. This is useful when the selector is more complex that just the element type. e.g. `d3.select<SVGGElement, unknown>(".chart")`. One also has to specify the generic arguments when using scales that are not just numbers but e.g. a linear scale for generating colors. `d3.scaleLinear<string, number>().domain([0, 1]).range(["white", "black"]);
11711179

11721180
---
11731181

0 commit comments

Comments
 (0)