From 7e61fca031e10568d420b51f5bf546e0e67e2466 Mon Sep 17 00:00:00 2001 From: Mingyang Date: Thu, 7 Nov 2024 23:08:02 +0800 Subject: [PATCH] Fix chart window --- .../java/seedu/address/ui/ChartWindow.java | 41 +++++++++++++++++-- src/main/resources/view/ChartWindow.fxml | 30 ++++++++------ 2 files changed, 55 insertions(+), 16 deletions(-) diff --git a/src/main/java/seedu/address/ui/ChartWindow.java b/src/main/java/seedu/address/ui/ChartWindow.java index 6d580904cd3..3d1c2ef89a9 100644 --- a/src/main/java/seedu/address/ui/ChartWindow.java +++ b/src/main/java/seedu/address/ui/ChartWindow.java @@ -5,6 +5,7 @@ import javafx.fxml.FXML; import javafx.scene.chart.BarChart; import javafx.scene.chart.XYChart; +import javafx.scene.control.ScrollPane; import javafx.scene.layout.Region; import seedu.address.commons.core.LogsCenter; import seedu.address.model.person.Name; @@ -19,8 +20,14 @@ public class ChartWindow extends UiPart { private static final Logger logger = LogsCenter.getLogger(ChartWindow.class); + private static final int BAR_WIDTH = 50; // Width of each bar in pixels + private static final int MAX_BARS_BEFORE_SCROLL = 30; // Threshold for enabling scrolling + private final Person[] chartData; + @FXML + private ScrollPane chartScrollPane; + @FXML private BarChart barChart; @@ -32,9 +39,31 @@ public class ChartWindow extends UiPart { public ChartWindow(Person[] chartData) { super(FXML); this.chartData = chartData; + configureScrollPane(); populateChart(); } + /** + * Configures the scroll pane to allow horizontal scrolling if necessary. + */ + private void configureScrollPane() { + int chartWidth; + + // Set width dynamically based on the number of bars + if (chartData.length > MAX_BARS_BEFORE_SCROLL) { + chartWidth = BAR_WIDTH * chartData.length; + } else { + // Set a minimum width for the chart when there are fewer bars + chartWidth = 500; // Adjust this value as needed + } + + barChart.setMinWidth(chartWidth); + barChart.setPrefWidth(chartWidth); + + chartScrollPane.setContent(barChart); + chartScrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED); + } + /** * Populates the BarChart with data from the Person array. */ @@ -43,12 +72,16 @@ private void populateChart() { series.setName("Tutor Hours Chart"); // Iterate over the Person array to create chart data entries. - for (Person person : chartData) { + for (int i = 0; i < chartData.length; i++) { + Person person = chartData[i]; Name name = person.getName(); - int hours = person.getHours().getHoursInt(); // Assuming getHours() returns Hours object with getValue(). + int hours = person.getHours().getHoursInt(); + + // Create a unique label using the index or an ID + String uniqueLabel = name.fullName + " (" + i + ")"; - // Add each person's name and hours as a bar in the chart. - series.getData().add(new XYChart.Data<>(name.fullName, hours)); + // Add each person's unique label and hours to the chart + series.getData().add(new XYChart.Data<>(uniqueLabel, hours)); } // Clear previous data and add the new series to the chart. diff --git a/src/main/resources/view/ChartWindow.fxml b/src/main/resources/view/ChartWindow.fxml index 3dd90cb0514..123ef7e09dc 100644 --- a/src/main/resources/view/ChartWindow.fxml +++ b/src/main/resources/view/ChartWindow.fxml @@ -1,10 +1,11 @@ + - + @@ -12,15 +13,20 @@ - - - - - - - - - - + + + + + + + + + + + + + +