From cffc19bd4a9d92b64a04584c1380e263edaa1c2b Mon Sep 17 00:00:00 2001 From: = Date: Fri, 15 May 2026 14:07:18 +0200 Subject: [PATCH] Feat: Auto change percent values in pricestats section The percent values shown in the price statistics section of the header of the dashboard view now updates according to the time range set. --- .../view/widgets/dashboard/DashBoardView.java | 27 +++++++++++++------ src/main/resources/styles.css | 11 ++++++++ 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/widgets/dashboard/DashBoardView.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/widgets/dashboard/DashBoardView.java index a10f987..c86e504 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/widgets/dashboard/DashBoardView.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/widgets/dashboard/DashBoardView.java @@ -4,6 +4,7 @@ import edu.ntnu.idi.idatt2003.g40.mappe.view.ViewElement; import java.math.BigDecimal; +import java.math.RoundingMode; import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; @@ -265,7 +266,18 @@ public void updateGraph(final DashBoardTimeRange timeRange) { dataSeries.getData().clear(); List prices = selectedStock.getHistoricalPrices(); - updateStockPrice(prices.getLast().floatValue(), selectedStock.getLatestPriceChange().floatValue()); + BigDecimal currentPrice = prices.getLast(); + int rangeInWeeks = timeRange.getWeeks(); + int startIndex = Math.max(0, prices.size() - 1 - rangeInWeeks); + + BigDecimal pastPrice = prices.get(startIndex); + float percentChange; + percentChange = currentPrice.subtract(pastPrice) + .divide(pastPrice, 4, RoundingMode.HALF_UP) + .multiply(new BigDecimal(100)) + .floatValue(); + + updateStockPrice(currentPrice.floatValue(), percentChange); setHighPriceLabel(selectedStock.getHighestPrice().floatValue()); setLowPriceLabel(selectedStock.getLowestPrice().floatValue()); @@ -294,18 +306,17 @@ public void updateGraph(final DashBoardTimeRange timeRange) { } private void updateStockPrice(final float value, - final float changeSinceLast) { + final float percentChange) { String changeSinceLastString; - float changePercent = (value / (value - changeSinceLast) - 1) * 100; - if (changeSinceLast > 0) { - changeSinceLastString = "(+ " + Math.round(changePercent*100f)/100f + "%)"; - } else if (changeSinceLast < 0) { - changeSinceLastString = "(- " + Math.round(Math.abs(changePercent)*100f)/100f + "%)"; + if (percentChange > 0) { + changeSinceLastString = "(+ " + Math.round(percentChange*100f)/100f + "%)"; + } else if (percentChange < 0) { + changeSinceLastString = "(- " + Math.round(Math.abs(percentChange)*100f)/100f + "%)"; } else { changeSinceLastString = ""; } - selectedStockPriceLabel.setText(Float.toString(Math.round(value*100f)/100f) + "NOK " + changeSinceLastString); + selectedStockPriceLabel.setText((Math.round(value*100f)/100f) + "NOK " + changeSinceLastString); } private void setLowPriceLabel(float value) { diff --git a/src/main/resources/styles.css b/src/main/resources/styles.css index de4e2d4..4266a2e 100644 --- a/src/main/resources/styles.css +++ b/src/main/resources/styles.css @@ -355,3 +355,14 @@ .scroll-pane > .viewport { -fx-background-color: transparent; } + +.combo-box { + -fx-background-color: #f4f4f4; + -fx-border-color: #d1d1d1; + -fx-border-radius: 5; + -fx-background-radius: 5; +} + +.combo-box .list-cell { + -fx-text-fill: #333333; +} \ No newline at end of file