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