Conditions | 10 |
Total Lines | 138 |
Code Lines | 107 |
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 EventEmitter from 'event-emitter-es6'; |
||
211 | |||
212 | renderGroup() { |
||
213 | const self = this; |
||
214 | |||
215 | // Handle dragging |
||
216 | const dragPoint = drag<SVGGElement, DataPointInternal>() |
||
217 | .on('drag', function (data) { |
||
218 | let { x } = event; |
||
219 | |||
220 | // Check point movement, preventing it from wondering outside the main curve |
||
221 | if (!x || x < 0) { |
||
222 | x = 0; |
||
223 | self.emit('home', { |
||
224 | ...data, |
||
225 | y: hillFnInverse(self.yScale.invert(data.y)), |
||
226 | }); |
||
227 | } else if (x > self.chartWidth) { |
||
228 | x = self.chartWidth; |
||
229 | self.emit('end', { |
||
230 | ...data, |
||
231 | x: self.xScale.invert(self.chartWidth), |
||
232 | y: hillFnInverse(self.yScale.invert(data.y)), |
||
233 | }); |
||
234 | } |
||
235 | |||
236 | // Convert current point coordinates back to the original |
||
237 | // between 0 and 100 to set it in the data attribute |
||
238 | const invertedX = self.xScale.invert(x); |
||
239 | |||
240 | data.x = x; |
||
241 | |||
242 | data.y = self.yScale(hillFn(invertedX)); |
||
243 | |||
244 | const invertedY = hillFnInverse(self.yScale.invert(data.y)); |
||
245 | |||
246 | const newInvertedCoordinates = { |
||
247 | x: invertedX, |
||
248 | y: invertedY, |
||
249 | }; |
||
250 | |||
251 | // click event |
||
252 | select<SVGGElement, DataPointInternal>(this).on('click', () => { |
||
253 | self.emit('pointClick', { ...data, ...newInvertedCoordinates }); |
||
254 | }); |
||
255 | |||
256 | if (!self.preview) { |
||
257 | const selectedPoint = select<SVGGElement, DataPointInternal>( |
||
258 | this |
||
259 | ).attr('transform', `translate(${data.x}, ${data.y})`); |
||
260 | selectedPoint |
||
261 | .select('text') |
||
262 | .style('text-anchor', () => { |
||
263 | if (textOutRange(invertedX)) { |
||
264 | return 'end'; |
||
265 | } |
||
266 | return 'start'; |
||
267 | }) |
||
268 | .attr('x', (point) => |
||
269 | calculateTextPositionForX(point.size, invertedX) |
||
270 | ); |
||
271 | |||
272 | self.emit('move', invertedX, invertedY); |
||
273 | } |
||
274 | }) |
||
275 | .on('end', (data) => { |
||
276 | if (this.preview) { |
||
277 | return; |
||
278 | } |
||
279 | |||
280 | let { x } = event; |
||
281 | |||
282 | // Check point movement, preventing it from wondering outside the main curve |
||
283 | if (!x || x < 0) { |
||
284 | x = 0; |
||
285 | } else if (x > this.chartWidth) { |
||
286 | x = this.chartWidth; |
||
287 | } |
||
288 | |||
289 | // Convert current point coordinates back to the original |
||
290 | const invertedX = this.xScale.invert(x); |
||
291 | data.y = this.yScale(hillFn(invertedX)); |
||
292 | const invertedY = hillFnInverse(this.yScale.invert(data.y)); |
||
293 | |||
294 | const newInvertedCoordinates = { |
||
295 | x: invertedX, |
||
296 | y: invertedY, |
||
297 | }; |
||
298 | |||
299 | this.emit('moved', { ...data, ...newInvertedCoordinates }); |
||
300 | }); |
||
301 | |||
302 | let group: |
||
303 | | Selection<SVGGElement, DataPointInternal, SVGGElement, unknown> |
||
304 | | undefined; |
||
305 | |||
306 | if (this.preview) { |
||
307 | group = this.undraggablePoint(); |
||
308 | } else { |
||
309 | // Create group consisted of a circle and a description text, where |
||
310 | // the data attributes determine the position of them on the curve |
||
311 | group = this.svg |
||
312 | .selectAll('.hill-chart-group') |
||
313 | .data(this.data) |
||
314 | .enter() |
||
315 | .append('g') |
||
316 | .attr('class', 'hill-chart-group') |
||
317 | .attr('transform', (data) => { |
||
318 | data.x = this.xScale(data.x); |
||
319 | data.y = this.yScale(data.y); |
||
320 | return `translate(${data.x}, ${data.y})`; |
||
321 | }) |
||
322 | .call(dragPoint); |
||
323 | } |
||
324 | |||
325 | group |
||
326 | .append('circle') |
||
327 | .attr('class', 'hill-chart-circle') |
||
328 | .attr('fill', (data) => data.color) |
||
329 | .attr('cx', 0) |
||
330 | .attr('cy', 0) |
||
331 | .attr('r', (data) => data.size || DEFAULT_SIZE); |
||
332 | |||
333 | group |
||
334 | .append('text') |
||
335 | .text((data) => data.description) |
||
336 | .attr('x', (data) => |
||
337 | calculateTextPositionForX( |
||
338 | data.size || DEFAULT_SIZE, |
||
339 | this.xScale.invert(data.x) |
||
340 | ) |
||
341 | ) |
||
342 | .style('text-anchor', (data) => { |
||
343 | if (textOutRange(this.xScale.invert(data.x))) { |
||
344 | return 'end'; |
||
345 | } |
||
346 | return 'start'; |
||
347 | }) |
||
348 | .attr('y', calculateTextMarginForY()); |
||
349 | } |
||
411 |