Skip to content

Commit

Permalink
Merge pull request #1826 from weather-gov/ik/add-dewpoint-chart
Browse files Browse the repository at this point in the history
Add dewpoint hourly chart
  • Loading branch information
partly-igor committed Sep 26, 2024
2 parents 483ef5e + 91e5ec8 commit 0f92d34
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 55 deletions.
89 changes: 89 additions & 0 deletions web/themes/new_weather_theme/assets/js/charts/hourly-dewpoint.js
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,
},
],
},
});
}
1 change: 1 addition & 0 deletions web/themes/new_weather_theme/assets/js/point.page.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ import("./charts/hourly-temperature.js");
import("./charts/hourly-pops.js");
import("./charts/hourly-humidity.js");
import("./charts/hourly-wind.js");
import("./charts/hourly-dewpoint.js");
4 changes: 2 additions & 2 deletions web/themes/new_weather_theme/new_weather_theme.libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ uswds-init:
minified: true

uswds-customized:
version: 2024-09-17
version: 2024-09-26
css:
theme:
assets/css/styles.css: {}
Expand All @@ -25,7 +25,7 @@ front-page:
defer: true

point-page:
version: 2024-09-19
version: 2024-09-26
js:
assets/js/point.page.js:
attributes:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{% set temps = hours | map(h => h.temperature) | json_encode %}
{% set feelsLike = hours | map(h => h.apparentTemperature) | json_encode %}
{% set pops = hours | map(h => h.probabilityOfPrecipitation) | json_encode %}
{% set dewpoints = hours | map(h => h.dewpoint) | json_encode %}
{% set relativeHumidity = hours | map(h => h.relativeHumidity) | json_encode %}
{% set windSpeeds = hours | map(h => h.windSpeed) | json_encode %}
{% set windGusts = hours | map(h => h.windGust) | json_encode %}
Expand All @@ -10,51 +11,6 @@
<wx-hourly-table class="display-block position-relative margin-top-2 bg-white">
<h5 class="wx-visual-h3 font-heading-md text-normal text-primary-darker margin-top-0 margin-bottom-205">Hourly forecast</h5>

<div class="margin-bottom-205">
<span class="font-family-mono text-primary-dark">
{{ "Temperature" | t }}
</span>
<div class="wx-hourly-temp-chart-container margin-y-105 position-relative grid-col-12" data-times="{{ times }}" data-temps="{{ temps }}" data-feels-like="{{ feelsLike }}">
<canvas>
</canvas>
</div>
<div class="display-flex flex-align-center font-mono-xs text-base">
<div class="width-3 border-bottom-2px border-primary-dark height-0 margin-right-1">
<div class="bg-primary-dark margin-left-1 radius-pill width-1 height-1" style="margin-top: -3px;"></div>
</div>
{{ "Temperature" | t }}
<div class="margin-left-3 width-3 border-bottom-2px border-top-0 border-left-0 border-right-0 border-dotted border-primary-light height-0 margin-right-1">
<div class="bg-primary-light margin-left-1 radius-pill width-1 height-1" style="margin-top: -3px;"></div>
</div>
{{ "Feels like" | t }}
</div>
</div>

<div class="margin-bottom-205">
<span class="font-family-mono text-primary-dark">{{"Chance of precipitation" | t }}</span>
<div class="wx-hourly-pops-chart-container margin-y-205 position-relative grid-col-12" data-times="{{ times }}" data-pops="{{ pops }}">
<canvas>
</canvas>
</div>
</div>

<div class="margin-bottom-205">
<span class="font-family-mono text-primary-dark">{{ "Wind" | t }}</span>
<div class="wx-hourly-wind-chart-container margin-y-205 position-relative grid-col-12" data-times="{{ times }}" data-wind-speeds="{{ windSpeeds }}" data-wind-gusts="{{ windGusts }}" data-wind-directions="{{ windDirections }}">
<canvas></canvas>
</div>
<div class="display-flex flex-align-center font-mono-xs text-base">
<div class="width-3 border-bottom-2px border-secondary-darker height-0 margin-right-1">
<div class="bg-primary-dark margin-left-1 radius-pill width-1 height-1" style="margin-top: -3px;"></div>
</div>
{{ "Wind speed" | t }}
<div class="margin-left-3 width-3 border-bottom-2px border-top-0 border-left-0 border-right-0 border-dotted border-secondary height-0 margin-right-1">
<div class="bg-secondary margin-left-1 radius-pill width-1 height-1" style="margin-top: -3px;"></div>
</div>
{{ "Gusts" | t }}
</div>
</div>

<div class="display-flex flex-justify">
<div>
<button class="wx-scroll-button bg-gray-30 circle-5 border-0 text-white display-flex flex-align-center flex-justify-center padding-y-1px padding-x-05" data-direction="left" type="button" role="button">
Expand Down Expand Up @@ -213,8 +169,10 @@
</table>
</div>

<div class="margin-bottom-3">
<span class="font-family-mono text-primary-dark">Temperature</span>
<div class="margin-bottom-205">
<span class="font-family-mono text-primary-dark">
{{ "Temperature" | t }}
</span>
<div class="wx-hourly-temp-chart-container margin-y-105 position-relative grid-col-12" data-times="{{ times }}" data-temps="{{ temps }}" data-feels-like="{{ feelsLike }}">
<canvas>
</canvas>
Expand All @@ -223,29 +181,53 @@
<div class="width-3 border-bottom-2px border-primary-dark height-0 margin-right-1">
<div class="bg-primary-dark margin-left-1 radius-pill width-1 height-1" style="margin-top: -3px;"></div>
</div>
Temperature
{{ "Temperature" | t }}
<div class="margin-left-3 width-3 border-bottom-2px border-top-0 border-left-0 border-right-0 border-dotted border-primary-light height-0 margin-right-1">
<div class="bg-primary-light margin-left-1 radius-pill width-1 height-1" style="margin-top: -3px;"></div>
</div>
Feels like
{{ "Feels like" | t }}
</div>
</div>

<div class="margin-bottom-205">
<span class="font-family-mono text-primary-dark">Humidity</span>
<div class="wx-hourly-humidity-chart-container margin-y-205 position-relative grid-col-12" data-times="{{ times }}" data-humidity="{{ relativeHumidity }}">
<span class="font-family-mono text-primary-dark">{{"Chance of precipitation" | t }}</span>
<div class="wx-hourly-pops-chart-container margin-y-205 position-relative grid-col-12" data-times="{{ times }}" data-pops="{{ pops }}">
<canvas>
</canvas>
</div>
</div>

<div class="margin-bottom-205">
<span class="font-family-mono text-primary-dark">{{ "Wind" | t }}</span>
<div class="wx-hourly-wind-chart-container margin-y-205 position-relative grid-col-12" data-times="{{ times }}" data-wind-speeds="{{ windSpeeds }}" data-wind-gusts="{{ windGusts }}" data-wind-directions="{{ windDirections }}">
<canvas></canvas>
</div>
<div class="display-flex flex-align-center font-mono-xs text-base">
<div class="width-3 border-bottom-2px border-secondary-darker height-0 margin-right-1">
<div class="bg-primary-dark margin-left-1 radius-pill width-1 height-1" style="margin-top: -3px;"></div>
</div>
{{ "Wind speed" | t }}
<div class="margin-left-3 width-3 border-bottom-2px border-top-0 border-left-0 border-right-0 border-dotted border-secondary height-0 margin-right-1">
<div class="bg-secondary margin-left-1 radius-pill width-1 height-1" style="margin-top: -3px;"></div>
</div>
{{ "Gusts" | t }}
</div>
</div>

<div class="margin-bottom-205">
<span class="font-family-mono text-primary-dark">Chance of precipitation</span>
<div class="wx-hourly-pops-chart-container margin-y-205 position-relative grid-col-12" data-times="{{ times }}" data-pops="{{ pops }}">
<span class="font-family-mono text-primary-dark"> {{"Humidity" | t }} </span>
<div class="wx-hourly-humidity-chart-container margin-y-205 position-relative grid-col-12" data-times="{{ times }}" data-humidity="{{ relativeHumidity }}">
<canvas>
</canvas>
</div>
</div>

<div class="margin-bottom-205">
<span class="font-family-mono text-primary-dark">{{"Dewpoint" | t }}</span>
<div class="wx-hourly-dewpoint-chart-container margin-y-205 position-relative grid-col-12" data-times="{{ times }}" data-dewpoints="{{ dewpoints }}">
<canvas>
</canvas>
</div>
</div>

</wx-hourly-table>

0 comments on commit 0f92d34

Please sign in to comment.