I'm trying to adapt a Chart model (lineChart) with JavaFX, at first based on this link model: https://docs.oracle.com/javafx/2/charts/line-chart.htm
My problem is showing the X-Axis values (Months in my case)
I've tried to make it more dynamic, based on these other links, but it still didn't work ... //JavaFX Adding multiple XYChart.Series to LineChart without explicating declaring each series //How to plot multiple series with javafx line chart which is in array list
Finally, based on this link, I was able to build a dynamic graph, which creates series to represent Month / Year of a collection movement: //Is it possible to create an array of XYChart.Series on JavaFX?
I have a "small" detail that I couldn't solve yet, the models are created "scene" directly, without XML, this way it works normal.
But I would like to create with XML (I am using the Scene Builder to assemble the XML), to put more details on the screen, such as a TableView, etc.
As I mentioned above, the problem is that when I use the graph with XML, it does not show the values of the X Axis (Months in my case) it is "all in one column" ...
I hope you managed to explain ... I will leave the example that trying to adapt here, any help, thank you in advance.
I'll leave the sample program (simpler, not the one that assembles dynamically, to try to illustrate better).
"Scene" call class
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class LineChartExemplo extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
FXML file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.LineChart?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="579.0" prefWidth="918.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.171" fx:controller="linechartexemplo.FXMLDocumentController">
<children>
<Button fx:id="button" layoutX="14.0" layoutY="14.0" onAction="#handleButtonAction" text="Visualizar Gráfico" />
<Label fx:id="label" layoutX="135.0" layoutY="14.0" minHeight="16" minWidth="69" prefHeight="25.0" prefWidth="710.0" />
<LineChart fx:id="lineChart" layoutX="14.0" layoutY="52.0" prefHeight="512.0" prefWidth="887.0" title="Exemplo Gŕafico">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis side="LEFT" />
</yAxis>
</LineChart>
</children>
</AnchorPane>
Controller class
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Label;
public class FXMLDocumentController implements Initializable {
@FXML private Label label;
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
//final LineChart<String,Number> lineChart = new LineChart<String,Number>(xAxis,yAxis);
//@FXML private LineChart<String, Number> lineChart;
@FXML private LineChart<String, Number> lineChart = new LineChart<String,Number>(xAxis,yAxis);
@FXML
private void handleButtonAction(ActionEvent event) {
label.setText("Teste Gráfico...");
visualizaGrafico();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
private void visualizaGrafico(){
lineChart.setTitle("Exemplo Gráfico");
yAxis.setLabel("Valores");
xAxis.setLabel("Meses");
XYChart.Series series = new XYChart.Series();
series.setName("Ano: 2018");
series.getData().add(new XYChart.Data("Jan", 23));
series.getData().add(new XYChart.Data("Feb", 14));
series.getData().add(new XYChart.Data("Mar", 15));
series.getData().add(new XYChart.Data("Apr", 24));
series.getData().add(new XYChart.Data("May", 34));
series.getData().add(new XYChart.Data("Jun", 36));
series.getData().add(new XYChart.Data("Jul", 22));
series.getData().add(new XYChart.Data("Aug", 45));
series.getData().add(new XYChart.Data("Sep", 43));
series.getData().add(new XYChart.Data("Oct", 17));
series.getData().add(new XYChart.Data("Nov", 29));
series.getData().add(new XYChart.Data("Dec", 25));
XYChart.Series series2 = new XYChart.Series();
series2.setName("Ano: 2019");
series2.getData().add(new XYChart.Data("Jan", 28));
series2.getData().add(new XYChart.Data("Feb", 17));
series2.getData().add(new XYChart.Data("Mar", 19));
series2.getData().add(new XYChart.Data("Apr", 14));
series2.getData().add(new XYChart.Data("May", 20));
series2.getData().add(new XYChart.Data("Jun", 42));
series2.getData().add(new XYChart.Data("Jul", 27));
series2.getData().add(new XYChart.Data("Aug", 48));
series2.getData().add(new XYChart.Data("Sep", 47));
series2.getData().add(new XYChart.Data("Oct", 19));
series2.getData().add(new XYChart.Data("Nov", 39));
series2.getData().add(new XYChart.Data("Dec", 29));
lineChart.getData().addAll(series, series2);
}
}