|
56 | 56 | - [More D3](#more-d3) |
57 | 57 | - [D3 Boilerplate](#boilerplate) |
58 | 58 | - [What Else Besides D3](#beside-d3) |
| 59 | +- [TypeScript and D3][#typescript] |
59 | 60 |
|
60 | 61 | > SURVEY: What do you guys already know? |
61 | 62 |
|
@@ -1103,6 +1104,90 @@ free and commerical charting library. |
1103 | 1104 | - Crossfilter (https://square.github.io/crossfilter/) - Fast Multidimensional Filtering for Coordinated Views |
1104 | 1105 | - ... |
1105 | 1106 |
|
| 1107 | + |
| 1108 | +<a id="typescript"></a> |
| 1109 | + |
| 1110 | +# TypeScript and D3 |
| 1111 | + |
| 1112 | +[TypeScript](https://www.typescriptlang.org/) is a programming language on top of JavaScript. Foremost it allows to specify types to variables and parameters similar to other typed langugages such as Java, C#, and so on. The TypeScript compiler compiles the TypeScript code to regular JavaScript code and also performs checks on it. Every JavaScript code is valid TypeScript code. |
| 1113 | + |
| 1114 | +Examples |
| 1115 | + |
| 1116 | +The following JavaScript code can be rewritten to TypeScript |
| 1117 | + |
| 1118 | +```js |
| 1119 | +let x = 5; |
| 1120 | + |
| 1121 | +function add(a, b) { |
| 1122 | + return a + b; |
| 1123 | +} |
| 1124 | + |
| 1125 | +console.log(add(x, 3)); |
| 1126 | +console.log(add("text", 3))ö |
| 1127 | +``` |
| 1128 | + |
| 1129 | +```ts |
| 1130 | +let x: number = 5; |
| 1131 | + |
| 1132 | +function add(a: number, b: number): number { |
| 1133 | + return a + b; |
| 1134 | +} |
| 1135 | + |
| 1136 | +console.log(add(x, 3)); |
| 1137 | +console.log(add("text", 3)); // will result in a compile error |
| 1138 | +``` |
| 1139 | + |
| 1140 | +Moreover, the TypeScript compiler is able to derive a lot which one can use to omit type declarations in some cases. |
| 1141 | + |
| 1142 | +```ts |
| 1143 | +let x = 5; // can be derived from the assignment |
| 1144 | + |
| 1145 | +function add(a: number, b: number) { // same for return type |
| 1146 | + return a + b; |
| 1147 | +} |
| 1148 | + |
| 1149 | +console.log(add(x, 3)); |
| 1150 | +console.log(add("text", 3)); // will result in a compile error |
| 1151 | +``` |
| 1152 | + |
| 1153 | +One can declare the types of variables and functions in ones own code. However, for external libraries, such as D3, the TypeScript compiler needs additional information such just the untyped JavaScript code is available. These are so called typings. [Definitely Typed](https://definitelytyped.org/) is a collection of typings for various JavaScript libraries including D3. |
| 1154 | + |
| 1155 | +So, when installing D3 using NPM one can install the typings alongside. Usually it is just by prepending the `@types/` scope. |
| 1156 | + |
| 1157 | +```bash |
| 1158 | +npm install d3 @types/d3 |
| 1159 | +``` |
| 1160 | + |
| 1161 | +Due to some heavy typing a D3 Selection (such as returned by `d3.select` or `d3.selectAll`) has four generic arguments: |
| 1162 | + 1. the element type of the selected element, e.g. `d3.selectAll("div")` will be a `HTMLDivElement`. |
| 1163 | + 1. the data type bound to this element, e.g. `d3.selectAll("div").data([1, 2, 3])` will be a `number`. |
| 1164 | + 1. the element type of the parent element, e.g. `d3.select("body").selectAll("div").data([1, 2, 3])` will be a `HTMLBodyElement`. |
| 1165 | + 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`. |
| 1166 | + |
| 1167 | +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 | + |
| 1169 | +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>(".chart")` |
| 1170 | + |
| 1171 | +TODO |
| 1172 | + * proper full binding example |
| 1173 | + * convert the bar chart to full TypeScript |
| 1174 | + * this context specification and usage |
| 1175 | + * |
| 1176 | + |
| 1177 | +## Hints |
| 1178 | + |
| 1179 | +### Axis |
| 1180 | + |
| 1181 | +The typings declare that an axis `d3.axisLeft` can just be called on a `SVGGElement` thus one has to make sure that the typings are correct of the selection. For example: |
| 1182 | + |
| 1183 | +```ts |
| 1184 | +const axis = d3.axisLeft(); |
| 1185 | +d3.select<SVGGElement>('.axis.x').call(axis); |
| 1186 | +``` |
| 1187 | + |
| 1188 | + |
| 1189 | + |
| 1190 | + |
1106 | 1191 | Thank You |
1107 | 1192 |
|
1108 | 1193 | [codepen]: https://img.shields.io/badge/CodePen-open-blue?logo=codepen |
0 commit comments