Hello I am trying to create bar chart which shows values of field. Values of field are changed by sorting algorithm and chart should show any of changes.
public class FXMLDocumentController implements Initializable {
static int[] pole = new int[10]; // field
int hodnota;
@FXML // bar chart
private BarChart barChart;
@FXML
private void handleButtonAction(ActionEvent ev) { //button for filling up new random graph
for (int i = 0; i < 10; i++) {
hodnota = intValue((StrictMath.random() * 100));
pole[i] = hodnota;
}
final CategoryAxis osaX = new CategoryAxis();
final NumberAxis osaY = new NumberAxis();
ObservableList<XYChart.Series<String, Number>> barChartData = FXCollections.observableArrayList();
final BarChart.Series<String, Number> series1 = new BarChart.Series<>();
series1.getData().add(new XYChart.Data<>("1", pole[0]));
series1.getData().add(new XYChart.Data<>("2", pole[1]));
series1.getData().add(new XYChart.Data<>("3", pole[2]));
series1.getData().add(new XYChart.Data<>("4", pole[3]));
series1.getData().add(new XYChart.Data<>("5", pole[4]));
series1.getData().add(new XYChart.Data<>("6", pole[5]));
series1.getData().add(new XYChart.Data<>("7", pole[6]));
series1.getData().add(new XYChart.Data<>("8", pole[7]));
series1.getData().add(new XYChart.Data<>("9", pole[8]));
series1.getData().add(new XYChart.Data<>("10", pole[9]));
barChartData.add(series1);
barChart.setData(barChartData);
}
@FXML // button which starts sorting algorhitm, it is changing values in field
private void bubbleButton(ActionEvent ev) {
BubbleSort vlakno=new BubbleSort("vypoctoveVlakno");
vlakno.start();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
I already tried to make a new thread which should load data again and again but it didn't work. Also tried to load them in the FXML controller but the graphics window gets stuck. I am actually very new to this.
FXML document, main just loads stage from fxml
<AnchorPane id="AnchorPane" prefHeight="487.0" prefWidth="610.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="radicialgoritmy.FXMLDocumentController">
<children>
<Button fx:id="button" layoutX="14.0" layoutY="14.0" onAction="#handleButtonAction" text="Vygeneruj" />
<BarChart fx:id="barChart" animated="false" cache="true" layoutX="30.0" layoutY="188.0" legendVisible="false" prefHeight="292.0" prefWidth="552.0">
<xAxis>
<CategoryAxis fx:id="osaX" side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis fx:id="osaY" side="LEFT" />
</yAxis>
</BarChart>
<Button fx:id="bubble" layoutX="100.0" layoutY="14.0" mnemonicParsing="false" onAction="#bubbleButton" text="BubbleSort" />
Class for sorting Algorithm.
class BubbleSort extends Thread {
public BubbleSort(final String jmeno) {
super(jmeno);
}
@Override
public void run() {
for (int i = 0; i < pole.length - 1; i++) {
for (int j = 0; j < pole.length - i - 1; j++) {
if (pole[j] < pole[j + 1]) {
final int tmp = pole[j];
pole[j] = pole[j + 1];
pole[j + 1] = tmp;
}
try {
Thread.sleep(250);
} catch (final InterruptedException ex) {
Logger.getLogger(BubbleSort.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…