| Conditions | 13 |
| Total Lines | 136 |
| Code Lines | 106 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like HillChart.renderGroup often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import { Selection } from 'd3-selection'; |
||
| 211 | |||
| 212 | renderGroup() { |
||
| 213 | // Handle dragging |
||
| 214 | const dragPoint = drag<SVGGElement, DataPoint>().on('drag', (data) => { |
||
| 215 | let { x } = event; |
||
| 216 | |||
| 217 | // Check point movement, preventing it from wondering outside the main curve |
||
| 218 | if (x < 0) { |
||
| 219 | x = 0; |
||
| 220 | this.emit('home', { |
||
| 221 | ...data, |
||
| 222 | y: hillFnInverse(this.yScale.invert(data.y || DEFAULT_Y)), |
||
| 223 | }); |
||
| 224 | } else if (x > this.chartWidth) { |
||
| 225 | x = this.chartWidth; |
||
| 226 | this.emit('end', { |
||
| 227 | ...data, |
||
| 228 | x: this.xScale.invert(this.chartWidth), |
||
| 229 | y: hillFnInverse(this.yScale.invert(data.y || DEFAULT_Y)), |
||
| 230 | }); |
||
| 231 | } |
||
| 232 | |||
| 233 | // Convert current point coordinates back to the original |
||
| 234 | // between 0 and 100 to set it in the data attribute |
||
| 235 | const invertedX = this.xScale.invert(x); |
||
| 236 | |||
| 237 | data.x = x; |
||
| 238 | |||
| 239 | data.y = this.yScale(hillFn(invertedX)); |
||
| 240 | |||
| 241 | const invertedY = hillFnInverse(this.yScale.invert(data.y)); |
||
| 242 | |||
| 243 | const newInvertedCoordinates = { |
||
| 244 | x: invertedX, |
||
| 245 | y: invertedY, |
||
| 246 | }; |
||
| 247 | |||
| 248 | // click event |
||
| 249 | select(this.target).on('click', () => { |
||
| 250 | this.emit('pointClick', { ...data, ...newInvertedCoordinates }); |
||
| 251 | }); |
||
| 252 | |||
| 253 | if (!this.preview) { |
||
| 254 | const selectedPoint = select<SVGGElement, { size: number }>( |
||
| 255 | this.target |
||
| 256 | ).attr('transform', `translate(${data.x}, ${data.y})`); |
||
| 257 | selectedPoint |
||
| 258 | .select('text') |
||
| 259 | .style('text-anchor', () => { |
||
| 260 | if (textOutRange(invertedX)) { |
||
| 261 | return 'end'; |
||
| 262 | } |
||
| 263 | return 'start'; |
||
| 264 | }) |
||
| 265 | .attr('x', (point) => |
||
| 266 | calculateTextPositionForX(point.size, invertedX) |
||
| 267 | ); |
||
| 268 | |||
| 269 | this.emit('move', invertedX, invertedY); |
||
| 270 | } |
||
| 271 | }); |
||
| 272 | |||
| 273 | dragPoint.on('end', (data: DataPoint) => { |
||
| 274 | if (this.preview) { |
||
| 275 | return; |
||
| 276 | } |
||
| 277 | |||
| 278 | let { x } = event; |
||
| 279 | |||
| 280 | // Check point movement, preventing it from wondering outside the main curve |
||
| 281 | if (x < 0) { |
||
| 282 | x = 0; |
||
| 283 | } else if (x > this.chartWidth) { |
||
| 284 | x = this.chartWidth; |
||
| 285 | } |
||
| 286 | |||
| 287 | // Convert current point coordinates back to the original |
||
| 288 | const invertedX = this.xScale.invert(x); |
||
| 289 | data.y = this.yScale(hillFn(invertedX)); |
||
| 290 | const invertedY = hillFnInverse(this.yScale.invert(data.y)); |
||
| 291 | |||
| 292 | const newInvertedCoordinates = { |
||
| 293 | x: invertedX, |
||
| 294 | y: invertedY, |
||
| 295 | }; |
||
| 296 | |||
| 297 | this.emit('moved', { ...data, ...newInvertedCoordinates }); |
||
| 298 | }); |
||
| 299 | |||
| 300 | let group: |
||
| 301 | | Selection<SVGGElement, DataPoint, SVGGElement, unknown> |
||
| 302 | | undefined; |
||
| 303 | |||
| 304 | if (this.preview) { |
||
| 305 | group = this.undraggablePoint(); |
||
| 306 | } else { |
||
| 307 | // Create group consisted of a circle and a description text, where |
||
| 308 | // the data attributes determine the position of them on the curve |
||
| 309 | group = this.svg |
||
| 310 | ?.selectAll('.hill-chart-group') |
||
| 311 | .data(this.data) |
||
| 312 | .enter() |
||
| 313 | .append('g') |
||
| 314 | .attr('class', 'hill-chart-group') |
||
| 315 | .attr('transform', (data) => { |
||
| 316 | data.x = this.xScale(data.x); |
||
| 317 | data.y = this.yScale(data.y || DEFAULT_Y); |
||
| 318 | return `translate(${data.x}, ${data.y})`; |
||
| 319 | }) |
||
| 320 | .call(dragPoint); |
||
| 321 | } |
||
| 322 | |||
| 323 | group |
||
| 324 | ?.append('circle') |
||
| 325 | .attr('class', 'hill-chart-circle') |
||
| 326 | .attr('fill', (data) => data.color) |
||
| 327 | .attr('cx', 0) |
||
| 328 | .attr('cy', 0) |
||
| 329 | .attr('r', (data) => data.size || DEFAULT_SIZE); |
||
| 330 | |||
| 331 | group |
||
| 332 | ?.append('text') |
||
| 333 | .text((data) => data.description) |
||
| 334 | .attr('x', (data) => |
||
| 335 | calculateTextPositionForX( |
||
| 336 | data.size || DEFAULT_SIZE, |
||
| 337 | this.xScale.invert(data.x) |
||
| 338 | ) |
||
| 339 | ) |
||
| 340 | .style('text-anchor', (data) => { |
||
| 341 | if (textOutRange(this.xScale.invert(data.x))) { |
||
| 342 | return 'end'; |
||
| 343 | } |
||
| 344 | return 'start'; |
||
| 345 | }) |
||
| 346 | .attr('y', calculateTextMarginForY()); |
||
| 347 | } |
||
| 409 |