@@ -8,38 +8,29 @@ export const weatherIcons: Plugin = {
8
8
9
9
afterDraw ( chart , args , options : WeatherIconsOptions ) {
10
10
const ctx = chart . ctx
11
+ const iconWidth = 30
11
12
let lastPixelDraw = - 1
12
- let lastIndexDraw = - 1
13
+ let lastDrawPositionIsUp = false
13
14
14
15
options . forecast . hourly . forEach ( ( { time, weatherCode } , i , a ) => {
15
- // Don't draw the first icon.
16
- if ( i === 0 ) {
17
- return
18
- }
19
-
20
- // Don't draw the same icon twice.
21
- if ( lastIndexDraw > 0 && a [ lastIndexDraw ] . weatherCode === weatherCode ) {
22
- return
23
- }
24
-
25
16
const pixel = chart . scales . x . getPixelForValue ( time . getTime ( ) )
26
17
27
18
// Prevent icon overlap.
28
- if ( pixel < lastPixelDraw ) {
19
+ if ( pixel - lastPixelDraw < iconWidth / 2 ) {
29
20
return
30
21
}
31
22
32
23
// Don't draw icons would be partially outside the chart.
33
- if ( pixel > chart . scales . x . right - 30 ) {
24
+ if ( pixel > chart . scales . x . right - iconWidth / 2 ) {
34
25
return
35
26
}
36
27
37
28
const iconName = getWeatherIconName ( weatherCode , time )
38
29
const svg = new Image ( )
39
- svg . src = `/public/ weather-icons/${ iconName } .svg`
40
- ctx . drawImage ( svg , chart . scales . x . getPixelForValue ( time . getTime ( ) ) , 10 , 30 , 30 )
41
- lastPixelDraw = pixel + 40
42
- lastIndexDraw = i
30
+ svg . src = `/weather-icons/${ iconName } .svg`
31
+ ctx . drawImage ( svg , chart . scales . x . getPixelForValue ( time . getTime ( ) ) - iconWidth / 2 , lastDrawPositionIsUp ? 10 : iconWidth + 2 , iconWidth , iconWidth )
32
+ lastPixelDraw = pixel
33
+ lastDrawPositionIsUp = ! lastDrawPositionIsUp
43
34
} )
44
35
} ,
45
36
0 commit comments