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