Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull request to enable display of barometric pressure #49

Merged
merged 3 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 92 additions & 4 deletions MMM-WeatherChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Module.register("MMM-WeatherChart", {
dataType: "hourly",
nightBorderDash: [5, 1],
showIcon: false,
showPressure: false,
showRain: false,
showZeroRain: true,
rainUnit: "mm",
Expand All @@ -45,6 +46,7 @@ Module.register("MMM-WeatherChart", {
colorMax: "rgba(255, 255, 255, 1)",
colorRain: "rgba(255, 255, 255, 1)",
colorSnow: "rgba(255, 255, 255, 1)",
colorPressure: "rgba(255, 255, 0, 0.8)",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cloud you set the default color to white?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in commit: 90dff89

backgroundColor: "rgba(0, 0, 0, 0)",
fillColor: "rgba(255, 255, 255, 0.1)",
dailyLabel: "date",
Expand Down Expand Up @@ -199,6 +201,14 @@ Module.register("MMM-WeatherChart", {
}
return max;
},

getPressureValue( hPa ){
if( this.config.units == "imperial" ){
return hPa * 0.029529983071445; // return value as inHg
} else {
return hPa;
}
},

// Calculate MarginFactors from the simultanious equations
//
Expand Down Expand Up @@ -302,7 +312,8 @@ Module.register("MMM-WeatherChart", {
dayTemps = [NaN],
nightTemps = [NaN],
labels = [""],
iconIDs = [NaN];
iconIDs = [NaN],
pressures = [NaN];

data.sort(function (a, b) {
if (a.dt < b.dt) return -1;
Expand All @@ -311,6 +322,9 @@ Module.register("MMM-WeatherChart", {
});
let dayTime;
for (let i = 0; i < Math.min(this.config.dataNum, data.length); i++) {

pressures.push( this.getPressureValue(data[i].pressure) );

let dateTime = new Date(
data[i].dt * 1000 + this.config.timeOffsetHours * 60 * 60 * 1000
);
Expand Down Expand Up @@ -371,11 +385,14 @@ Module.register("MMM-WeatherChart", {
nightTemps.push(NaN);
labels.push("");
iconIDs.push(NaN);
pressures.push(NaN);

const minTemp = this.getMin(temps),
maxTemp = this.getMax(temps),
maxRain = this.getMax(rains),
maxSnow = this.getMax(snows),
maxPressure = this.getMax(pressures),
minPressure = this.getMin(pressures),
iconLine = [],
icons = [];

Expand Down Expand Up @@ -452,6 +469,31 @@ Module.register("MMM-WeatherChart", {
data: nightTemps,
yAxisID: "y1",
});
if (this.config.showPressure) {
datasets.push({
label: "Pressure",
borderColor: this.config.colorPressure,
pointBackgroundColor: this.config.colorPressure,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cloud you set borderDash so that the line style is selectable by users? The pressure line should be easily distinguishable because the pressure line overwrap with the other lines.

For example:

borderDash: this.config.pressureBorderDash

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in commit: 90dff89

datalabels: {
color: this.config.color,
align: "top",
offset: this.config.datalabelsOffset,
font: {
weight: this.config.fontWeight,
},
display: this.config.datalabelsDisplay,
formatter: function (value) {
let place = 10 ** self.config.datalabelsRoundDecimalPlace;
let label = Math.round(value * place) / place;
return label;
},
},
data: pressures,
//pointStyle: "star",
//fill: true,
yAxisID: "y3",
});
}
if (this.config.showIcon) {
datasets.push({
label: "Icons",
Expand Down Expand Up @@ -539,7 +581,9 @@ Module.register("MMM-WeatherChart", {
y2_max =
Math.max(maxRain, maxSnow, this.config.rainMinHeight) *
(2 + iconTopMargin + iconBelowMargin + tempRainMargin),
y2_min = 0;
y2_min = 0,
y3_max = maxPressure + 20,
y3_min = minPressure - 20;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

y3_max and y3_min should be carefully calculated to make an appropriate margin. If it's difficult, I'll try to fix after the PR is merged.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in commit: 90dff89

if (showRainSnow) y1_min = y1_min - (maxTemp - minTemp);
const ranges = {
y1: {
Expand All @@ -550,6 +594,10 @@ Module.register("MMM-WeatherChart", {
min: y2_min,
max: y2_max,
},
y3: {
min: y3_min,
max: y3_max,
},
};

return { labels: labels, datasets: datasets, ranges: ranges };
Expand All @@ -566,14 +614,18 @@ Module.register("MMM-WeatherChart", {
rains = [NaN],
snows = [NaN],
labels = [""],
iconIDs = [NaN];
iconIDs = [NaN],
pressures = [NaN];

data.sort(function (a, b) {
if (a.dt < b.dt) return -1;
if (a.dt > b.dt) return 1;
return 0;
});
for (let i = 0; i < Math.min(this.config.dataNum, data.length); i++) {

pressures.push( this.getPressureValue(data[i].pressure) );

const dateTime = new Date(
data[i].dt * 1000 + this.config.timeOffsetHours * 60 * 60 * 1000
);
Expand Down Expand Up @@ -607,6 +659,7 @@ Module.register("MMM-WeatherChart", {
snows.push(0);
}
iconIDs.push(data[i].weather[0].icon);

}

// Add dummy data to make space on the left and right side of the chart
Expand All @@ -617,6 +670,7 @@ Module.register("MMM-WeatherChart", {
snows.push(NaN);
labels.push("");
iconIDs.push(NaN);
pressures.push(NaN);

const minValue = this.getMin(minTemps),
maxValue = this.getMax(maxTemps),
Expand Down Expand Up @@ -697,6 +751,32 @@ Module.register("MMM-WeatherChart", {
data: maxTemps,
yAxisID: "y1",
});
if (this.config.showPressure) {

datasets.push({
label: "Pressure",
borderColor: this.config.colorPressure,
pointBackgroundColor: this.config.colorPressure,
datalabels: {
color: this.config.colorPressure,
align: "top",
offset: this.config.datalabelsOffset,
font: {
weight: this.config.fontWeight,
},
display: this.config.datalabelsDisplay,
formatter: function (value) {
let place = 10 ** self.config.datalabelsRoundDecimalPlace;
let label = Math.round(value * place) / place;
return label;
},
},
data: pressures,
yAxisID: "y3",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hide the y3 axis at the far left side.

y1, y2 axis is hide by the below code.
https://github.com/mtatsuma/MMM-WeatherChart/blob/master/MMM-WeatherChart.js#L880-L884

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in commit: 90dff89

});


}
if (this.config.showIcon) {
datasets.push({
label: "Icons",
Expand Down Expand Up @@ -784,7 +864,10 @@ Module.register("MMM-WeatherChart", {
y2_max =
Math.max(maxRain, maxSnow, this.config.rainMinHeight) *
(2 + (iconTopMargin + iconBelowMargin + tempRainMargin) * 2),
y2_min = 0;
y2_min = 0,
yPressure_Min = this.getMin(pressures),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename yPressure_Min, yPressure_Max to y3_min, y3_max.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in commit: 90dff89

yPressure_Max = this.getMax(pressures);

if (showRainSnow) y1_min = y1_min - (maxValue - minValue);
const ranges = {
y1: {
Expand All @@ -795,6 +878,11 @@ Module.register("MMM-WeatherChart", {
min: y2_min,
max: y2_max,
},
y3: {
min: yPressure_Min,
max: yPressure_Max,
},

};

return { labels: labels, datasets: datasets, ranges: ranges };
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,5 @@ You can check [available MMM-WeatherChart versions](https://github.com/mtatsuma/
| datalabelsOffset | | `4` | Offset of data labels. See https://chartjs-plugin-datalabels.netlify.app/guide/positioning.html#alignment-and-offset for details.
| datalabelsRoundDecimalPlace | | `1` | Decimal place to which round for data labels on charts. When you set this option as `0`, the labels are rounded to integer. This option is affected only on the labels (not affected on the data values). |
| largeOpenWeatherIcon | | `false` | `true` or `false`. Weather Icons from OpenWeather becomes 2 times larger (100 x 100) if true. |
| showPressure | | `false` | `true` or `false`. Toggles display of pressure values. Notes if units is 'imperial', will display in inHg |
| colorPressure | | `rgba(255, 255, 0, 0.8)` | Color of the pressure line, if shown |