Skip to content

Commit

Permalink
Feat: Auto change percent values in pricestats section
Browse files Browse the repository at this point in the history
The percent values shown in the price statistics section of the header of the dashboard view now updates according to the time range set.
  • Loading branch information
tommyah committed May 15, 2026
1 parent b3fbbea commit cffc19b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -265,7 +266,18 @@ public void updateGraph(final DashBoardTimeRange timeRange) {
dataSeries.getData().clear();
List<BigDecimal> 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());

Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/resources/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit cffc19b

Please sign in to comment.