Skip to content

Commit

Permalink
Feat: Graph change time range
Browse files Browse the repository at this point in the history
Added a time range enum that controls how the graphs' x value range.
  • Loading branch information
tommyah committed May 15, 2026
1 parent 3f394dc commit b3fbbea
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class DashBoardController extends ViewController<DashBoardView> {
private Player player;
private Exchange exchange;
private List<Stock> stockList;
private DashBoardTimeRange selectedTimeRange;

/**
* {@inheritDoc}
Expand All @@ -31,12 +32,13 @@ public DashBoardController(final DashBoardView viewElement,
this.player = player;
this.stockList = stockList;
this.exchange = exchange;
this.selectedTimeRange = DashBoardTimeRange.DEFAULT;
super(viewElement, eventManager);
}

private void handleStockSelection(final Stock stock, final float amountOwned) {
getViewElement().setCurrentStock(stock, amountOwned);
getViewElement().updateGraph();
getViewElement().updateGraph(selectedTimeRange);
}

private void populateStockList(String filter) {
Expand All @@ -63,7 +65,7 @@ private void populateStockList(String filter) {
protected void initInteractions() {
populateStockList("");
getViewElement().setCurrentStock(stockList.getFirst(), 0);
getViewElement().updateGraph();
getViewElement().updateGraph(selectedTimeRange);
getViewElement().setOnAction(DashBoardActions.BUY_SHARES, () -> {
if (Validator.NOT_EMPTY.isValid(getViewElement().getQuantityInputField().getText())) {
BigDecimal amountToBuy = new BigDecimal(getViewElement().getQuantityInputField().getText());
Expand Down Expand Up @@ -115,7 +117,7 @@ protected void initInteractions() {
});

exchange.weekProperty().addListener((observable,o,n) -> {
getViewElement().updateGraph();
getViewElement().updateGraph(selectedTimeRange);
});

getViewElement().getQuantityInputField().setTextFormatter(new TextFormatter<>(change -> {
Expand All @@ -128,5 +130,10 @@ protected void initInteractions() {
getViewElement().getSideBarSearchField().textProperty().addListener((observable, o, n) -> {
populateStockList(n);
});

getViewElement().getTimeRangeSelector().setOnAction(e -> {
selectedTimeRange = getViewElement().getTimeRangeSelector().getValue();
getViewElement().updateGraph(selectedTimeRange);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.dashboard;

public enum DashBoardTimeRange {
ONE_WEEK("1 Week", 1),
DEFAULT("22 Weeks (Default)", 22),
ONE_MONTH("1 Month", 4),
ONE_YEAR("1 Year", 52);

private final String label;
private final int weeks;

DashBoardTimeRange(final String label, final int weeks) {
this.label = label; this.weeks = weeks;
}

public int getWeeks() {
return weeks;
}

@Override
public String toString() {
return label;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class DashBoardView extends ViewElement<HBox, DashBoardActions> {
private VBox sidebar;
private VBox completeSideBar;
private Stock selectedStock;
private ComboBox<DashBoardTimeRange> timeRangeSelector;
private float ownedStocks = 0;
private TextField sideBarSearchField;
private Label selectedStockLabel;
Expand Down Expand Up @@ -53,8 +54,9 @@ protected void initLayout() {
completeSideBar = new VBox(10);
sideBarSearchField = new TextField();
sideBarSearchField.setPromptText("Search...");
List<String> timeSelectionOptions;
ComboBox<List<String>> timeDropDownMenu = new ComboBox<>();
timeRangeSelector = new ComboBox<>();
timeRangeSelector.getItems().addAll(DashBoardTimeRange.values());
timeRangeSelector.setValue(DashBoardTimeRange.DEFAULT);

sidebar = new VBox(10);
sidebar.setPrefWidth(150);
Expand All @@ -65,7 +67,7 @@ protected void initLayout() {
ScrollPane scrollPane = new ScrollPane(sidebar);
Separator separator = new Separator();

completeSideBar.getChildren().addAll(sideBarSearchField, separator, scrollPane, timeDropDownMenu);
completeSideBar.getChildren().addAll(sideBarSearchField, separator, scrollPane, timeRangeSelector);
scrollPane.setFitToWidth(true);
scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
Expand Down Expand Up @@ -259,14 +261,16 @@ public void clearStockList() {
sidebar.getChildren().clear();
}

public void updateGraph() {
public void updateGraph(final DashBoardTimeRange timeRange) {
dataSeries.getData().clear();
List<BigDecimal> prices = selectedStock.getHistoricalPrices();

updateStockPrice(prices.getLast().floatValue(), selectedStock.getLatestPriceChange().floatValue());
setHighPriceLabel(selectedStock.getHighestPrice().floatValue());
setLowPriceLabel(selectedStock.getLowestPrice().floatValue());
xAxis.setLowerBound(Math.max(1, prices.size() - 22));
xAxis.setUpperBound(Math.max(22, prices.size()));

xAxis.setLowerBound(Math.max(1, prices.size() - timeRange.getWeeks()));
xAxis.setUpperBound(xAxis.getLowerBound() + timeRange.getWeeks());

double min = prices.stream()
.min(BigDecimal::compareTo)
Expand Down Expand Up @@ -330,4 +334,8 @@ public void setOnStockAction(final Button button,
public TextField getQuantityInputField() {
return shareQuantityInputField;
}

public ComboBox<DashBoardTimeRange> getTimeRangeSelector() {
return timeRangeSelector;
}
}

0 comments on commit b3fbbea

Please sign in to comment.