Skip to content

Commit 4904ace

Browse files
authored
Merge pull request #322 from setchy/feature/311-ascending-order
feat: draw blips in ascending order within quadrant/ring
2 parents e82ed6f + c6b70b1 commit 4904ace

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

spec/graphing/blips-spec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {
77
blipAssistiveText,
88
createGroupBlip,
99
thereIsCollision,
10+
sortBlipCoordinates,
1011
} = require('../../src/graphing/blips')
1112
const Chance = require('chance')
1213
const { graphConfig } = require('../../src/graphing/config')
@@ -274,4 +275,33 @@ describe('Blips', function () {
274275
expect(thereIsCollision([41, 41], existingCoords, 22)).toBe(true)
275276
expect(thereIsCollision([42, 42], existingCoords, 22)).toBe(false)
276277
})
278+
279+
it('should sort blips coordinates', function () {
280+
const existingCoords = [
281+
{ coordinates: [500, 400], width: 22 },
282+
{ coordinates: [200, 200], width: 22 },
283+
{ coordinates: [40, 40], width: 22 },
284+
]
285+
286+
expect(sortBlipCoordinates(existingCoords, 'first')).toEqual([
287+
{ coordinates: [200, 200], width: 22 },
288+
{ coordinates: [40, 40], width: 22 },
289+
{ coordinates: [500, 400], width: 22 },
290+
])
291+
expect(sortBlipCoordinates(existingCoords, 'third')).toEqual([
292+
{ coordinates: [200, 200], width: 22 },
293+
{ coordinates: [40, 40], width: 22 },
294+
{ coordinates: [500, 400], width: 22 },
295+
])
296+
expect(sortBlipCoordinates(existingCoords, 'second')).toEqual([
297+
{ coordinates: [500, 400], width: 22 },
298+
{ coordinates: [200, 200], width: 22 },
299+
{ coordinates: [40, 40], width: 22 },
300+
])
301+
expect(sortBlipCoordinates(existingCoords, 'fourth')).toEqual([
302+
{ coordinates: [500, 400], width: 22 },
303+
{ coordinates: [200, 200], width: 22 },
304+
{ coordinates: [40, 40], width: 22 },
305+
])
306+
})
277307
})

src/graphing/blips.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ const isEmpty = require('lodash/isEmpty')
77
const { replaceSpaceWithHyphens, removeAllSpaces } = require('../util/stringUtil')
88
const config = require('../config')
99
const featureToggles = config().featureToggles
10+
const _ = {
11+
sortBy: require('lodash/sortBy'),
12+
}
1013

1114
const 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+
395422
module.exports = {
396423
calculateRadarBlipCoordinates,
397424
plotRadarBlips,
@@ -402,4 +429,5 @@ module.exports = {
402429
blipAssistiveText,
403430
createGroupBlip,
404431
thereIsCollision,
432+
sortBlipCoordinates,
405433
}

0 commit comments

Comments
 (0)