Skip to content

Commit

Permalink
MARP-120 enhance weather connector with wind chart (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
tutn-axonivy authored Jul 11, 2024
1 parent 30478e9 commit 1a7f7df
Show file tree
Hide file tree
Showing 9 changed files with 426 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class DailyForecast {
private final int maxTemperature;
private final int humidity;
private final float windSpeed;
private final int windDirectionDegree;
private final String weatherIcon;
private final String weatherDescription;

Expand All @@ -40,6 +41,9 @@ public DailyForecast(LocalDate date, List<WeatherRecord> dailyRecords) {

this.windSpeed = dailyRecords.stream().map(record -> record.getWind().getSpeed()).max(Float::compareTo)
.orElse(Constants.Default.WEATHER_WIND_SPEED);

this.windDirectionDegree = dailyRecords.stream().map(record -> record.getWind().getDeg()).max(Integer::compareTo)
.orElse(Constants.Default.WEATHER_WIND_DEGREE);

WeatherRecord maxPriorityRecord = dailyRecords.stream()
.max(Comparator.comparingInt(record -> getWeatherPriority(record.getWeather().get(0).getId())))
Expand Down Expand Up @@ -82,6 +86,10 @@ public float getWindSpeed() {
return windSpeed;
}

public int getWindDirectionDegree() {
return windDirectionDegree;
}

public String getWeatherIcon() {
return weatherIcon;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -36,6 +37,8 @@
import com.axonivy.connector.openweather.service.ForecastService;
import com.axonivy.connector.openweather.util.Constants;
import com.axonivy.connector.openweather.util.DateTimeFormatterUtilities;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import ch.ivyteam.ivy.environment.Ivy;

Expand Down Expand Up @@ -74,6 +77,7 @@ public class ForecastWeatherBean implements Serializable {
private LineChartModel temperatureModel;
private String temperatureData;
private BarChartModel precipitationModel;
private LineChartModel windModel;
private int chartWindowSize;
private int currentChartWindowStartX;
private int currentChartWindowEndX;
Expand Down Expand Up @@ -275,6 +279,14 @@ public BarChartModel getPrecipitationModel() {
return precipitationModel;
}

public LineChartModel getWindModel() {
return windModel;
}

public void setWindModel(LineChartModel windModel) {
this.windModel = windModel;
}

public int getChartWindowSize() {
return chartWindowSize;
}
Expand Down Expand Up @@ -308,6 +320,7 @@ public void search() {
setSelectedDateIndex(0);
createTemperatureModel();
createPrecipitationModel();
createWindModel();
}

private void processAndGroupForecastData() {
Expand All @@ -323,7 +336,8 @@ private void processAndGroupForecastData() {
record.getWeather().forEach(weather -> {
String icon = weather.getIcon();
if (icon != null) {
icon = icon.replace(Constants.OpenWeatherMapVariable.NOTATION_NIGHT, Constants.OpenWeatherMapVariable.NOTATION_DAY);
icon = icon.replace(Constants.OpenWeatherMapVariable.NOTATION_NIGHT,
Constants.OpenWeatherMapVariable.NOTATION_DAY);
weather.setIcon(icon);
}
});
Expand Down Expand Up @@ -385,6 +399,60 @@ public ChartData prepareTemperatureChartData() {
return data;
}

public ChartData preparePrecipitationChartData() {
ChartData data = new ChartData();
BarChartDataSet dataSet = new BarChartDataSet();
dataSet.setLabel(Constants.Chart.PRECIPITATION_DATASET_LABEL);
dataSet.setData(preparePrecipitationData());
data.setLabels(prepareTimeLabels());
data.addChartDataSet(dataSet);
return data;
}

public void createWindModel() {
windModel = new LineChartModel();
LineChartOptions options = new LineChartOptions();
windModel.setData(prepareWindChartData());
windModel.setOptions(options);
windModel.setExtender(Constants.UiVariable.WIND_CHART_EXTENDER_JS_METHOD_NAME);
}

public ChartData prepareWindChartData() {
ChartData data = new ChartData();
LineChartDataSet dataSet = new LineChartDataSet();
dataSet.setLabel(Constants.Chart.WIND_DATASET_LABEL);
dataSet.setData(prepareWindData());
dataSet.setCubicInterpolationMode(prepareCustomWindData());
data.setLabels(prepareTimeLabels());
data.addChartDataSet(dataSet);
return data;
}

private String prepareCustomWindData() {
List<Map<String, String>> result = dailyForecastDisplayInfos.stream()
.map(DailyForecastDisplayInfo::getDailyForecast)
.flatMap(dailyForecast -> dailyForecast.getDailyRecords().stream().map(record -> {
Map<String, String> data = new HashMap<>();
data.put("speed", record.getWind().getSpeed().toString() + " " + speedUnit);
data.put("deg", record.getWind().getDeg().toString());
return data;
})).collect(Collectors.toList());

ObjectMapper mapper = new ObjectMapper();

try {
return mapper.writeValueAsString(result);
} catch (JsonProcessingException e) {
return StringUtils.EMPTY;
}
}

public List<Object> prepareWindData() {
return dailyForecastDisplayInfos.stream().map(DailyForecastDisplayInfo::getDailyForecast)
.flatMap(dailyForecast -> dailyForecast.getDailyRecords().stream().map(record -> 10))
.collect(Collectors.toList());
}

public List<Object> prepareTemperatureData() {
return dailyForecastDisplayInfos
.stream().map(DailyForecastDisplayInfo::getDailyForecast).flatMap(dailyForecast -> dailyForecast
Expand All @@ -407,16 +475,6 @@ public void createPrecipitationModel() {
precipitationModel.setExtender(Constants.UiVariable.PRECIPITATION_CHART_EXTENDER_JS_METHOD_NAME);
}

public ChartData preparePrecipitationChartData() {
ChartData data = new ChartData();
BarChartDataSet dataSet = new BarChartDataSet();
dataSet.setLabel(Constants.Chart.PRECIPITATION_DATASET_LABEL);
dataSet.setData(preparePrecipitationData());
data.setLabels(prepareTimeLabels());
data.addChartDataSet(dataSet);
return data;
}

public List<Number> preparePrecipitationData() {
return dailyForecastDisplayInfos.stream().map(DailyForecastDisplayInfo::getDailyForecast).flatMap(
dailyForecast -> dailyForecast.getDailyRecords().stream().map(record -> (int) (record.getPop() * 100)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public interface UiVariable {
public static final String GET_FORECAST_WEATHER_BY_LOCATION_START_NAME = "getForecastWeatherByLocationName";
public static final String TEMPERATURE_CHART_EXTENDER_JS_METHOD_NAME = "temperatureChartExtender";
public static final String PRECIPITATION_CHART_EXTENDER_JS_METHOD_NAME = "precipitationChartExtender";
public static final String WIND_CHART_EXTENDER_JS_METHOD_NAME = "windChartExtender";
public static final String SELECTED_TIME_INDEX_JS_VARIABLE_NAME = "selectedTimeIndex";
}

Expand All @@ -40,11 +41,13 @@ public interface Default {
public static final int WEATHER_TEMPERATURE_DEGREE = -999;
public static final int WEATHER_HUMIDITY = 0;
public static final float WEATHER_WIND_SPEED = 0.0f;
public static final int WEATHER_WIND_DEGREE = 0;
}

public interface Chart {
public static final String TEMPERATURE_DATASET_LABEL = "temperature";
public static final String PRECIPITATION_DATASET_LABEL = "precipitation";
public static final String WIND_DATASET_LABEL = "wind";
}

public interface OpenWeatherMapVariable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@
style="cursor: pointer; width: 20px; height: 5px;"
styleClass="weather-chart" />
</p:tab>

<p:tab
title="#{ivy.cms.co('/Dialogs/com/axonivy/connector/openweather/demo/ui/ForecastWeatherDemo/WindSpeedLabel')}">
<p:lineChart id="wind-chart" widgetVar="windChartWidgetVar"
model="#{forecastWeatherBean.windModel}"
style="cursor: pointer; width: 20px; height: 5px;"
styleClass="weather-chart" />
</p:tab>

</p:tabView>
<p:outputPanel id="forecast-btn-panel"
styleClass="weather-date-btn-container">
Expand Down Expand Up @@ -161,7 +170,6 @@
</h:form>

</p:outputPanel>

<h:outputScript>
var fontColor = '#{ivyFreyaTheme.getMode()}' == 'dark' ? '#EAEBEC' : '#69707A';
var tempBackGroundColor = '#{ivyFreyaTheme.getMode()}' == 'dark' ? '#FF6347' : '#e8f0fe';
Expand Down
Loading

0 comments on commit 1a7f7df

Please sign in to comment.