Skip to content

Commit

Permalink
Merge pull request #190 from mingyang143/master
Browse files Browse the repository at this point in the history
Fix chart window
  • Loading branch information
Jollybomber authored Nov 7, 2024
2 parents ae5ef99 + 7e61fca commit 271a795
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 16 deletions.
41 changes: 37 additions & 4 deletions src/main/java/seedu/address/ui/ChartWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,8 +20,14 @@ public class ChartWindow extends UiPart<Region> {

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<String, Number> barChart;

Expand All @@ -32,9 +39,31 @@ public class ChartWindow extends UiPart<Region> {
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.
*/
Expand All @@ -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.
Expand Down
30 changes: 18 additions & 12 deletions src/main/resources/view/ChartWindow.fxml
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.chart.*?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>

<?import java.net.URL?>
<?import javafx.geometry.Insets?>

<AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml">
<padding>
<Insets right="20" bottom="12" />
</padding>
<stylesheets>
<URL value="@ChartWindow.css" />
</stylesheets>
<VBox VBox.vgrow="ALWAYS" AnchorPane.topAnchor="0.0" AnchorPane.bottomAnchor="0.0"
AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
<BarChart fx:id="barChart" VBox.vgrow="ALWAYS">
<xAxis>
<CategoryAxis/>
</xAxis>
<yAxis>
<NumberAxis label="Hours tutored"/>
</yAxis>
</BarChart>
</VBox>
<!-- ScrollPane wrapping the VBox for horizontal scrolling -->
<ScrollPane fx:id="chartScrollPane" fitToHeight="true" fitToWidth="true" hbarPolicy="AS_NEEDED"
AnchorPane.topAnchor="0.0" AnchorPane.bottomAnchor="0.0"
AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
<VBox VBox.vgrow="ALWAYS">
<BarChart fx:id="barChart" VBox.vgrow="ALWAYS">
<xAxis>
<CategoryAxis/>
</xAxis>
<yAxis>
<NumberAxis label="Hours tutored"/>
</yAxis>
</BarChart>
</VBox>
</ScrollPane>
</AnchorPane>

0 comments on commit 271a795

Please sign in to comment.