Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: draw blips in ascending order within quadrant/ring #322

Merged
merged 11 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions spec/graphing/blips-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
blipAssistiveText,
createGroupBlip,
thereIsCollision,
sortBlipCoordinates,
} = require('../../src/graphing/blips')
const Chance = require('chance')
const { graphConfig } = require('../../src/graphing/config')
Expand Down Expand Up @@ -209,4 +210,33 @@ describe('Blips', function () {
expect(thereIsCollision([41, 41], existingCoords, 22)).toBe(true)
expect(thereIsCollision([42, 42], existingCoords, 22)).toBe(false)
})

it('should sort blips coordinates', function () {
const existingCoords = [
{ coordinates: [500, 400], width: 22 },
{ coordinates: [200, 200], width: 22 },
{ coordinates: [40, 40], width: 22 },
]

expect(sortBlipCoordinates(existingCoords, 'first')).toEqual([
{ coordinates: [200, 200], width: 22 },
{ coordinates: [40, 40], width: 22 },
{ coordinates: [500, 400], width: 22 },
])
expect(sortBlipCoordinates(existingCoords, 'third')).toEqual([
{ coordinates: [200, 200], width: 22 },
{ coordinates: [40, 40], width: 22 },
{ coordinates: [500, 400], width: 22 },
])
expect(sortBlipCoordinates(existingCoords, 'second')).toEqual([
{ coordinates: [500, 400], width: 22 },
{ coordinates: [200, 200], width: 22 },
{ coordinates: [40, 40], width: 22 },
])
expect(sortBlipCoordinates(existingCoords, 'fourth')).toEqual([
{ coordinates: [500, 400], width: 22 },
{ coordinates: [200, 200], width: 22 },
{ coordinates: [40, 40], width: 22 },
])
})
})
34 changes: 31 additions & 3 deletions src/graphing/blips.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const isEmpty = require('lodash/isEmpty')
const { replaceSpaceWithHyphens, removeAllSpaces } = require('../util/stringUtil')
const config = require('../config')
const featureToggles = config().featureToggles
const _ = {
sortBy: require('lodash/sortBy'),
}

const getRingRadius = function (ringIndex) {
const ratios = [0, 0.316, 0.652, 0.832, 0.992]
Expand Down Expand Up @@ -295,13 +298,14 @@ const plotRadarBlips = function (parentElement, rings, quadrantWrapper, tooltip)
const offset = 10
const minRadius = getRingRadius(i) + offset
const maxRadius = getRingRadius(i + 1) - offset
const allBlipCoordsInRing = []
let allBlipCoordsInRing = []

if (ringBlips.length > graphConfig.maxBlipsInRings[i]) {
plotGroupBlips(ringBlips, ring, quadrantOrder, parentElement, quadrantWrapper, tooltip)
return
}

// Calculate coordinates for blips
ringBlips.forEach(function (blip) {
const coordinates = findBlipCoordinates(
blip,
Expand All @@ -312,12 +316,35 @@ const plotRadarBlips = function (parentElement, rings, quadrantWrapper, tooltip)
quadrantOrder,
)
allBlipCoordsInRing.push({ coordinates, width: blip.width })
drawBlipInCoordinates(blip, coordinates, quadrantOrder, parentElement)
renderBlipDescription(blip, ring, quadrantWrapper, tooltip)
})

// Sort the coordinates
allBlipCoordsInRing = sortBlipCoordinates(allBlipCoordsInRing, quadrantOrder)

// Draw blips using sorted coordinates
allBlipCoordsInRing.forEach(function (blipCoords, i) {
drawBlipInCoordinates(ringBlips[i], blipCoords.coordinates, quadrantOrder, parentElement)
renderBlipDescription(ringBlips[i], ring, quadrantWrapper, tooltip)
})
})
}

const sortBlipCoordinates = function (blipCoordinates, quadrantOrder) {
return _.sortBy(blipCoordinates, (coord) => calculateAngleFromAxis(coord, quadrantOrder))
}

const calculateAngleFromAxis = function (position, quadrantOrder) {
const [x, y] = position.coordinates

const transposedX = x - graphConfig.effectiveQuadrantWidth
const transposedY = y - graphConfig.effectiveQuadrantHeight

if (quadrantOrder === 'first' || quadrantOrder === 'third') {
return Math.atan2(transposedY, transposedX)
}
return Math.atan2(transposedX, transposedY)
}

module.exports = {
calculateRadarBlipCoordinates,
plotRadarBlips,
Expand All @@ -328,4 +355,5 @@ module.exports = {
blipAssistiveText,
createGroupBlip,
thereIsCollision,
sortBlipCoordinates,
}