@@ -7,6 +7,9 @@ const isEmpty = require('lodash/isEmpty')
77const { replaceSpaceWithHyphens, removeAllSpaces } = require ( '../util/stringUtil' )
88const config = require ( '../config' )
99const featureToggles = config ( ) . featureToggles
10+ const _ = {
11+ sortBy : require ( 'lodash/sortBy' ) ,
12+ }
1013
1114const getRingRadius = function ( ringIndex ) {
1215 const ratios = [ 0 , 0.316 , 0.652 , 0.832 , 0.992 ]
@@ -369,13 +372,14 @@ const plotRadarBlips = function (parentElement, rings, quadrantWrapper, tooltip)
369372 const offset = 10
370373 const minRadius = getRingRadius ( i ) + offset
371374 const maxRadius = getRingRadius ( i + 1 ) - offset
372- const allBlipCoordsInRing = [ ]
375+ let allBlipCoordsInRing = [ ]
373376
374377 if ( ringBlips . length > graphConfig . maxBlipsInRings [ i ] ) {
375378 plotGroupBlips ( ringBlips , ring , quadrantOrder , parentElement , quadrantWrapper , tooltip )
376379 return
377380 }
378381
382+ // Calculate coordinates for blips
379383 ringBlips . forEach ( function ( blip ) {
380384 const coordinates = findBlipCoordinates (
381385 blip ,
@@ -386,12 +390,35 @@ const plotRadarBlips = function (parentElement, rings, quadrantWrapper, tooltip)
386390 quadrantOrder ,
387391 )
388392 allBlipCoordsInRing . push ( { coordinates, width : blip . width } )
389- drawBlipInCoordinates ( blip , coordinates , quadrantOrder , parentElement )
390- renderBlipDescription ( blip , ring , quadrantWrapper , tooltip )
393+ } )
394+
395+ // Sort the coordinates
396+ allBlipCoordsInRing = sortBlipCoordinates ( allBlipCoordsInRing , quadrantOrder )
397+
398+ // Draw blips using sorted coordinates
399+ allBlipCoordsInRing . forEach ( function ( blipCoords , i ) {
400+ drawBlipInCoordinates ( ringBlips [ i ] , blipCoords . coordinates , quadrantOrder , parentElement )
401+ renderBlipDescription ( ringBlips [ i ] , ring , quadrantWrapper , tooltip )
391402 } )
392403 } )
393404}
394405
406+ const sortBlipCoordinates = function ( blipCoordinates , quadrantOrder ) {
407+ return _ . sortBy ( blipCoordinates , ( coord ) => calculateAngleFromAxis ( coord , quadrantOrder ) )
408+ }
409+
410+ const calculateAngleFromAxis = function ( position , quadrantOrder ) {
411+ const [ x , y ] = position . coordinates
412+
413+ const transposedX = x - graphConfig . effectiveQuadrantWidth
414+ const transposedY = y - graphConfig . effectiveQuadrantHeight
415+
416+ if ( quadrantOrder === 'first' || quadrantOrder === 'third' ) {
417+ return Math . atan2 ( transposedY , transposedX )
418+ }
419+ return Math . atan2 ( transposedX , transposedY )
420+ }
421+
395422module . exports = {
396423 calculateRadarBlipCoordinates,
397424 plotRadarBlips,
@@ -402,4 +429,5 @@ module.exports = {
402429 blipAssistiveText,
403430 createGroupBlip,
404431 thereIsCollision,
432+ sortBlipCoordinates,
405433}
0 commit comments