Skip to content

Commit 44a78c5

Browse files
committed
feat: Densify weather icons
1 parent 5290bbd commit 44a78c5

File tree

2 files changed

+9
-17
lines changed

2 files changed

+9
-17
lines changed

src/lib/open-meteo/models.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// TODO: improve mapping
12
export const weatherIconNameMapDay = {
23
0: 'clear-day',
34
1: 'mostly-clear-day',

src/plugins/weatherIcons/index.ts

+8-17
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,29 @@ export const weatherIcons: Plugin = {
88

99
afterDraw(chart, args, options: WeatherIconsOptions) {
1010
const ctx = chart.ctx
11+
const iconWidth = 30
1112
let lastPixelDraw = -1
12-
let lastIndexDraw = -1
13+
let lastDrawPositionIsUp = false
1314

1415
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-
2516
const pixel = chart.scales.x.getPixelForValue(time.getTime())
2617

2718
// Prevent icon overlap.
28-
if (pixel < lastPixelDraw) {
19+
if (pixel - lastPixelDraw < iconWidth / 2) {
2920
return
3021
}
3122

3223
// 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) {
3425
return
3526
}
3627

3728
const iconName = getWeatherIconName(weatherCode, time)
3829
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
4334
})
4435
},
4536

0 commit comments

Comments
 (0)