<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.chart.BarChart?>
<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<AnchorPane id="AnchorPane" prefHeight="600.0" prefWidth="800.0" styleClass="mainFxmlClass" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Views.MonthlyHoursController">
<children>
<VBox alignment="CENTER" layoutX="121.0" layoutY="121.0" prefHeight="600.0" prefWidth="800.0" spacing="20.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Label styleClass="header" text="Monthly Hours" />
<HBox prefHeight="292.0" prefWidth="760.0">
<children>
<BarChart fx:id="barChart" prefHeight="292.0" prefWidth="755.0">
<xAxis>
<CategoryAxis fx:id="months" side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis fx:id="hoursWorked" side="LEFT" />
</yAxis>
</BarChart>
</children>
</HBox>
<HBox alignment="BOTTOM_RIGHT" prefHeight="100.0" prefWidth="200.0" spacing="20.0">
<children>
<Button mnemonicParsing="false" text=" Back" />
</children>
</HBox>
</children>
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
</VBox>
</children>
</AnchorPane>
Выше находится мой файл f xml для гистограммы
package Views;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDate;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
public class MonthlyHoursController implements Initializable {
@FXML private BarChart<?, ?> barChart;
@FXML private CategoryAxis months;
@FXML private NumberAxis hoursWorked;
private XYChart.Series currentYearSeries, previousYearSeries;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
currentYearSeries = new XYChart.Series<>();
previousYearSeries = new XYChart.Series<>();
// barChart.setTitle("Hours Worked");
months.setLabel("Months");
hoursWorked.setLabel("Hours Worked");
currentYearSeries.setName(Integer.toString(LocalDate.now().getYear()));
previousYearSeries.setName(Integer.toString(LocalDate.now().getYear()-1));
try {
populateSeriesFromDB();
} catch (SQLException e) {
e.printStackTrace();
}
barChart.getData().addAll(previousYearSeries);
barChart.getData().addAll(currentYearSeries);
}
private void populateSeriesFromDB() throws SQLException {
Connection conn = null;
Statement statement = null;
ResultSet resultSet = null;
try{
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bakery?useSSL=false", "root",
"a3756421");
statement = conn.createStatement();
String sql = " Select Year(dateworked), monthname(dateworked), sum(hoursworked)" +
"From hoursworked" +
"Group by year(dateworked), month(dateworked)" +
"Order by Year(dateworked), month(dateworked);";
resultSet = statement.executeQuery(sql);
while (resultSet.next()){
if(resultSet.getInt(1)==LocalDate.now().getYear())
currentYearSeries.getData().add(new XYChart.Data(resultSet.getString(2), resultSet.getString(3)));
else if(resultSet.getInt(1)==LocalDate.now().getYear()-1)
previousYearSeries.getData().add(new XYChart.Data(resultSet.getString(2), resultSet.getString(3)));
}
}
catch (SQLException e){
System.err.println(e.getMessage());
}
finally{
if(conn !=null)
conn.close();
if (statement != null)
statement.close();
if(resultSet != null)
resultSet.close();
}
}
}
Это контроллер для f xml. Если я выполняю
From hoursworked
Group by Year(dateworked), month(dateworked)
Order by Year(dateworked), month(dateworked);
в верстаке, он выдает ниже:
Однако, когда я открываю файл f xml, он ничего не показывает в гистограмме.
Что-то не так с моим синтаксисом javafx или mysql? Сама программа не выдает никаких ошибок при запуске. Я понятия не имею, где вообще начать это исправлять.