-
Notifications
You must be signed in to change notification settings - Fork 9
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 #1826 from weather-gov/ik/add-dewpoint-chart
Add dewpoint hourly chart
- Loading branch information
Showing
4 changed files
with
127 additions
and
55 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
web/themes/new_weather_theme/assets/js/charts/hourly-dewpoint.js
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,89 @@ | ||
/* global Chart ChartDataLabels */ | ||
|
||
import styles from "../styles.js"; | ||
|
||
Chart.register(ChartDataLabels); | ||
|
||
const chartContainers = Array.from( | ||
document.querySelectorAll(".wx-hourly-dewpoint-chart-container"), | ||
); | ||
|
||
for (const container of chartContainers) { | ||
const times = JSON.parse(container.dataset.times); | ||
const dewpoints = JSON.parse(container.dataset.dewpoints).map((v) => | ||
Number.parseInt(v, 10), | ||
); | ||
|
||
// We don't need to keep a reference to the chart object. We only need the | ||
// side-effects of creating it. This is not ideal, but it's how Chart.js | ||
// works, so it's what we've got. | ||
// eslint-disable-next-line no-new | ||
new Chart(container.querySelector("canvas"), { | ||
type: "line", | ||
plugins: [ChartDataLabels], | ||
|
||
options: { | ||
animation: false, | ||
responsive: true, | ||
maintainAspectRatio: false, | ||
interaction: { | ||
intersect: false, | ||
mode: "index", | ||
}, | ||
plugins: { | ||
legend: { | ||
display: false, | ||
}, | ||
}, | ||
scales: { | ||
x: { | ||
ticks: { | ||
autoSkip: true, | ||
maxRotation: 0, | ||
color: styles.colors.base, | ||
}, | ||
grid: { | ||
color: times.map((v) => { | ||
if (v === "12 AM") { | ||
return "black"; | ||
} | ||
|
||
const even = Number.parseInt(v, 10) % 2 === 0; | ||
if (even) { | ||
return styles.colors.baseLighter; | ||
} | ||
return styles.colors.baseLightest; | ||
}), | ||
}, | ||
}, | ||
y: { | ||
min: Math.round(Math.min(...dewpoints) / 10) * 10 - 10, | ||
max: Math.round(Math.max(...dewpoints) / 10) * 10 + 10, | ||
ticks: { | ||
autoSkip: true, | ||
color: styles.colors.base, | ||
maxTicksLimit: 6, | ||
callback: (v) => `${v}°`, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
data: { | ||
labels: times, | ||
datasets: [ | ||
{ | ||
label: "Dewpoint", | ||
data: dewpoints, | ||
datalabels: { | ||
align: "top", | ||
color: styles.colors.accentCoolDark, | ||
}, | ||
backgroundColor: styles.colors.accentCoolDark, | ||
borderColor: styles.colors.accentCoolDark, | ||
borderWidth: 1.5, | ||
}, | ||
], | ||
}, | ||
}); | ||
} |
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