Skip to content

Commit

Permalink
feat: Prevent first icon to be drawn outside of the canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
artonge committed Aug 31, 2024
1 parent 12eb15c commit 2140d61
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib/open-meteo/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export async function searchLocation(query: string): Promise<City[]> {
searchRequest.searchParams.append('name', query)
searchRequest.searchParams.append('count', '10')

const response = await fetch(searchRequest )
const response = await fetch(searchRequest)
const { results } = await response.json()

return results.map((city: any) => {
Expand Down
6 changes: 6 additions & 0 deletions src/plugins/weatherIcons/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ export const weatherIcons: Plugin = {
afterDraw(chart, args, options: WeatherIconsOptions) {
const ctx = chart.ctx
const iconWidth = 30
const firstPixel = chart.scales.x.getPixelForValue(options.forecast.hourly[0].time.getTime())
let lastPixelDraw: number | undefined = undefined

options.forecast.hourly.filter(({ time }) => {
const timestamp = time.getTime()
const pixel = chart.scales.x.getPixelForValue(timestamp)

// Prevent first icon to be partially drawn outside of the canvas.
if (pixel < firstPixel + iconWidth / 2) {
return false
}

// Prevent icon overlap.
if (lastPixelDraw !== undefined && Math.abs(pixel - lastPixelDraw) < iconWidth / 2) {
return false
Expand Down

0 comments on commit 2140d61

Please sign in to comment.