Probability Forecast
diff --git a/forecast_mhw.js b/forecast_mhw.js
index adb516c..898c973 100644
--- a/forecast_mhw.js
+++ b/forecast_mhw.js
@@ -56,7 +56,9 @@ window.createMomCobaltOpt_singleID('statMOMCobaltFcastMHW',momCobaltStatsFcastMH
window.createMomCobaltCbarOpt('cbarOptsFcastMHW','inferno');
// initialize the plotly
-window.initializePlotly('mhwForecast')
+$(document).ready(function() {
+ window.asyncInitializePlotlyResize('mhwForecast')
+});
///////////////// event listener ////////////////
// screen size adjustment trigger by window resizing
@@ -154,6 +156,8 @@ $("#"+dashNavPillID+" > ul.nav-pills > li.nav-item > .nav-link").on('click',func
let hrefIDText = hrefID.slice(1)
// reuse changeDashSelect (historical.js)
window.changeDashSelect(dashDropDownID,hrefIDText+'Val')
+ // Manually trigger a resize event for triggering plotly resizing
+ window.dispatchEvent(new Event('resize'));
});
// event listener for the "message" event when map location click
@@ -305,13 +309,17 @@ function getvarValFcastMHW(infoLonLat) {
// 2. fetch forecast TSs (return promise)
// 3. create plotly object on webpage (execute when promise resolved)
function plotFcast(infoLonLat) {
+ showLoadingSpinner("loading-spinner-fcastmhw-prob");
showLoadingSpinner("loading-spinner-fcastmhw-spread");
// get promise obj from fetch
fetchFcastsTS(infoLonLat)
.then((jsonData)=>{
- plotlyFcastsProb(jsonData)
- plotlyForecastSpread(jsonData)
- hideLoadingSpinner("loading-spinner-fcast-spread");
+ plotlyFcastsProb(jsonData);
+ plotlyForecastSpread(jsonData);
+ })
+ .then(()=>{
+ hideLoadingSpinner("loading-spinner-fcastmhw-prob");
+ hideLoadingSpinner("loading-spinner-fcastmhw-spread");
})
.catch((error)=>{
console.error('Plotting MHW time series error:',error);
@@ -321,7 +329,7 @@ function plotFcast(infoLonLat) {
// function to fetch all forecast ts based on locationData
// response in the json format
function fetchFcastsTS(infoLonLat) {
- var ajaxGet = "/cgi-bin/cefi_portal/mom_extract_variableValue_fcast_mhw.py"
+ var ajaxGet = "/cgi-bin/cefi_portal/mom_extract_timeseries_fcast_mhw.py"
+"?region="+$("#regMOMCobaltFcastMHW").val()
+"&lon="+infoLonLat.longitude
+"&lat="+infoLonLat.latitude
@@ -362,7 +370,7 @@ function plotlyFcastsProb(jsonData) {
color: trace1Color,
width: 3
},
- // plotly figure title
+ // legend
name: jsonData.mhw_prob90.varname
};
@@ -372,10 +380,10 @@ function plotlyFcastsProb(jsonData) {
hovermode: 'closest',
showlegend: false,
title:
- varnameFcast +'
' +
+ 'MHW Probability
' +
' @ (lat:'+parseFloat(jsonData.location.lat).toFixed(2)+'N,'+
'lon:'+parseFloat(jsonData.location.lon).toFixed(2)+'E)',
- // autosize: true,
+ autosize: true,
annotations: [{
x: 0,
y: 0,
@@ -434,7 +442,7 @@ function plotlyForecastSpread(jsonData) {
color: trace1Color,
width: 3
},
- name: jsonData.ssta_avg.varname
+ name: 'ssta ens mean'
};
var data = [trace];
@@ -453,18 +461,40 @@ function plotlyForecastSpread(jsonData) {
color: trace2Color },
name: key
};
- data.push(trace_ens)
+ data.push(trace_ens);
}
+ // threshold
+ var trace3Color = "rgba(203, 128, 171, 0.9)";
+ var trace_thres = {
+ x: leadMonthListMHW,
+ y: jsonData.tos_threshold90.ts,
+ type: 'scatter',
+ mode: 'lines+markers',
+ marker: { size: 2 },
+ line: {
+ shape: 'linear',
+ color: trace3Color,
+ dash: 'dash'
+ },
+ name: '90% threshold'
+ };
+ data.push(trace_thres);
+
// plotly figure layout
var layout = {
hovermode: 'closest',
- showlegend: false,
+ showlegend: true,
+ legend: {
+ x: 1, // Positioning on the right
+ y: 0,
+ xanchor: 'left' // Anchor the legend to avoid it getting cut off
+ },
title:
- varnameFcast +'
' +
+ 'MHW Magnitude
' +
' @ (lat:'+parseFloat(jsonData.location.lat).toFixed(2)+'N,'+
'lon:'+parseFloat(jsonData.location.lon).toFixed(2)+'E)',
- // autosize: true,
+ autosize: true,
annotations: [{
x: 0,
y: 0,
@@ -477,7 +507,7 @@ function plotlyForecastSpread(jsonData) {
// height: 400,
margin: {
l: 80,
- r: 80,
+ r: 150,
b: 80,
t: 100,
// pad: 4
From 22cabed1699faa5df5541c1274c861e485a6d7c2 Mon Sep 17 00:00:00 2001
From: "C.-W. HSU" <11950284+chiaweh2@users.noreply.github.com>
Date: Thu, 3 Oct 2024 15:04:38 -0600
Subject: [PATCH 6/6] make plotly default plot centralized
implement forced window resize on navpill change
---
forecast.js | 59 +++++----------------------------------------------
historical.js | 29 ++++++++++++++++++++++---
2 files changed, 31 insertions(+), 57 deletions(-)
diff --git a/forecast.js b/forecast.js
index afa472a..6688714 100644
--- a/forecast.js
+++ b/forecast.js
@@ -58,60 +58,9 @@ createMomCobaltDepthBlockOptFcast(varValueFcast,'blockMOMCobaltFcast')
// setup colorbar option
createMomCobaltCbarOpt('cbarOptsFcast');
-// Initial dashboard plot
-$(function() {
- var trace = {
- x: "",
- y: "",
- type: 'scatter',
- mode: 'lines+markers',
- marker: { size: 8 },
- line: { shape: 'linear' },
- name: ""
- };
-
- var layout1 = {
- title:
- 'Create Forecast Map first
& pick point on the shaded area',
- // autosize: true,
- // width: 1000,
- // height: 400,
- xaxis: { title: 'Date' },
- yaxis: { title: 'Variable' },
- hovermode: 'closest',
- showlegend: false,
- // responsive: true
- };
-
- var layout2 = {
- title:
- 'Create Forecast Map first & draw transect (polyline) on the shaded area',
- // autosize: true,
- // width: 1000,
- // height: 400,
- xaxis: { title: 'Date' },
- yaxis: { title: 'Variable' },
- hovermode: 'closest',
- showlegend: false,
- // responsive: true
- };
-
- var layout3 = {
- title:
- 'Pick a index',
- // autosize: true,
- // width: 1000,
- // height: 400,
- xaxis: { title: 'Date' },
- yaxis: { title: 'Variable' },
- hovermode: 'closest',
- showlegend: false,
- // responsive: true
- };
- var config = {responsive: true}
- Plotly.newPlot('plotly-fcast-spread', [trace], layout1, config);
- Plotly.newPlot('plotly-fcast-box', [trace], layout1, config);
- // Plotly.newPlot('plotly-index', [trace], layout3)
+// initialize plotly
+$(document).ready(function() {
+ window.asyncInitializePlotlyResize('forecast')
});
///////////////// event listener ////////////////
@@ -200,6 +149,8 @@ $("#analysisMOMCobaltFcast").on("change", function(){
$("#"+selectedValue.slice(0, -3)).addClass("active");
})
+
+
// add event listener for the "message" event using jQuery (location click)
$(window).on("message", receiveMessageFcast);
diff --git a/historical.js b/historical.js
index 6efca28..00242c2 100644
--- a/historical.js
+++ b/historical.js
@@ -65,7 +65,10 @@ $('#depthMOMCobaltTS2').val('');
createMomCobaltVarOpt('onlyIndexes','indexMOMCobaltTS');
// initialize plotly
-initializePlotly('all');
+$(document).ready(function() {
+ asyncInitializePlotlyResize('all')
+});
+// initializePlotly('all');
// plot index
plotIndexes();
@@ -174,6 +177,7 @@ $("#analysisMOMCobalt").on("change", function(){
// change the active navpil content
$("#dashContentHistrun div.tab-pane").removeClass("active");
$("#"+selectedValue.slice(0, -3)).addClass("active");
+
})
// event listener for navpil being clicked
@@ -181,6 +185,7 @@ $("#dashNavHistrun > ul.nav-pills > li.nav-item > .nav-link").on('click',functio
let hrefID = $(this).attr('href')
let hrefIDText = hrefID.slice(1)
changeDashSelect('analysisMOMCobalt',hrefIDText+'Val')
+ window.dispatchEvent(new Event('resize'));
});
// // event listener for clicking the minitab
@@ -301,6 +306,16 @@ $('#indexMOMCobaltTS').on("change", function () {
///////// functional function start /////////
// intialize the plotly plot
// Initial dashboard plot
+function asyncInitializePlotlyResize(flag) {
+ return initializePlotly(flag)
+ .then(() => {
+ window.dispatchEvent(new Event('resize'));
+ })
+ .catch(error => {
+ console.error('Error in async plotly initialization:', error);
+ });
+}
+
function initializePlotly(flag) {
var trace = {
x: "",
@@ -371,9 +386,9 @@ function initializePlotly(flag) {
// responsive: true
};
- var layout3 = {
+ var layoutFcst = {
title:
- 'Pick an index',
+ 'Create Forecast Map first
& pick point on the shaded area',
// autosize: true,
// width: 1000,
// height: 400,
@@ -401,10 +416,18 @@ function initializePlotly(flag) {
Plotly.newPlot('plotly-time-series', [trace], layoutTS,config);
} else if (flag ==='transect') {
Plotly.newPlot('plotly-transect', [trace], layout2,config);
+ } else if (flag ==='forecast') {
+ Plotly.newPlot('plotly-fcast-spread', [trace], layoutFcst, config);
+ Plotly.newPlot('plotly-fcast-box', [trace], layoutFcst, config);
} else if (flag ==='mhwForecast') {
Plotly.newPlot('plotly-fcastmhw-prob', [trace], layoutTS,config);
Plotly.newPlot('plotly-fcastmhw-mag', [trace], layoutTS,config);
}
+
+ return new Promise(resolve => {
+ console.log('Initial Plotly created');
+ resolve();
+ });
};
// //function for option change due to button/view change at the bottom