-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1250 from jcardus/speed-based-color
keep old behaviour in RoutePath when we just have coordinates (and no…
- Loading branch information
Showing
7 changed files
with
152 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { decomposeColor } from '@mui/material'; | ||
|
||
export const interpolateColor = (color1, color2, factor) => { | ||
if (factor > 1) factor = 1; | ||
if (factor < 0) factor = 0; | ||
|
||
const c1 = decomposeColor(color1).values; | ||
const c2 = decomposeColor(color2).values; | ||
|
||
const r = Math.round(c1[0] + factor * (c2[0] - c1[0])); | ||
const g = Math.round(c1[1] + factor * (c2[1] - c1[1])); | ||
const b = Math.round(c1[2] + factor * (c2[2] - c1[2])); | ||
|
||
return `rgb(${r}, ${g}, ${b})`; | ||
}; | ||
|
||
export const getSpeedColor = (color1, color2, color3, speed, max) => { | ||
const factor = speed / max; | ||
if (factor <= 0.5) { | ||
return interpolateColor(color1, color2, factor * 2); | ||
} | ||
return interpolateColor(color2, color3, (factor - 0.5) * 2); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { useTheme } from '@mui/styles'; | ||
import { useId, useEffect } from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
import { map } from './core/MapView'; | ||
|
||
const MapRouteCoordinates = ({ name, coordinates, deviceId }) => { | ||
const id = useId(); | ||
|
||
const theme = useTheme(); | ||
|
||
const reportColor = useSelector((state) => { | ||
const attributes = state.devices.items[deviceId]?.attributes; | ||
if (attributes) { | ||
const color = attributes['web.reportColor']; | ||
if (color) { | ||
return color; | ||
} | ||
} | ||
return theme.palette.geometry.main; | ||
}); | ||
|
||
useEffect(() => { | ||
map.addSource(id, { | ||
type: 'geojson', | ||
data: { | ||
type: 'Feature', | ||
geometry: { | ||
type: 'LineString', | ||
coordinates: [], | ||
}, | ||
}, | ||
}); | ||
map.addLayer({ | ||
source: id, | ||
id: `${id}-line`, | ||
type: 'line', | ||
layout: { | ||
'line-join': 'round', | ||
'line-cap': 'round', | ||
}, | ||
paint: { | ||
'line-color': ['get', 'color'], | ||
'line-width': 2, | ||
}, | ||
}); | ||
map.addLayer({ | ||
source: id, | ||
id: `${id}-title`, | ||
type: 'symbol', | ||
layout: { | ||
'text-field': '{name}', | ||
'text-size': 12, | ||
}, | ||
paint: { | ||
'text-halo-color': 'white', | ||
'text-halo-width': 1, | ||
}, | ||
}); | ||
|
||
return () => { | ||
if (map.getLayer(`${id}-title`)) { | ||
map.removeLayer(`${id}-title`); | ||
} | ||
if (map.getLayer(`${id}-line`)) { | ||
map.removeLayer(`${id}-line`); | ||
} | ||
if (map.getSource(id)) { | ||
map.removeSource(id); | ||
} | ||
}; | ||
}, []); | ||
|
||
useEffect(() => { | ||
map.getSource(id)?.setData({ | ||
type: 'Feature', | ||
geometry: { | ||
type: 'LineString', | ||
coordinates, | ||
}, | ||
properties: { | ||
name, | ||
color: reportColor, | ||
}, | ||
}); | ||
}, [theme, coordinates, reportColor]); | ||
|
||
return null; | ||
}; | ||
|
||
export default MapRouteCoordinates; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.