堆积条形图不适用于FXML和控制器类。请帮我。我的代码如下:
控制器类
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javafxapplication27;
import java.net.URL;
import java.util.Arrays;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
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.StackedBarChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class FXMLDocumentController implements Initializable {
@FXML
private Label label;
@FXML
private StackedBarChart stackChart;
@FXML
private BarChart barChart;
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
final String austria = "Austria";
final String brazil = "Brazil";
final String france = "France";
final String italy = "Italy";
final String usa = "USA";
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
ObservableList<XYChart.Series<String, Number>> barChartData = FXCollections.observableArrayList();
ObservableList<XYChart.Series<String, Number>> stackBarChartData = FXCollections.observableArrayList();
final BarChart.Series<String, Number> series1 = new BarChart.Series<String, Number>();
final StackedBarChart.Series<String, Number> series2 = new StackedBarChart.Series<String, Number>();
final StackedBarChart.Series<String, Number> series3 = new StackedBarChart.Series<String, Number>();
series1.setName("2001");
series1.getData().add(new XYChart.Data<String, Number>(austria, 25601.34));
series1.getData().add(new XYChart.Data<String, Number>(brazil, 20148.82));
series1.getData().add(new XYChart.Data<String, Number>(france, 10000));
series1.getData().add(new XYChart.Data<String, Number>(italy, 35407.15));
series1.getData().add(new XYChart.Data<String, Number>(usa, 12000));
series2.setName("2004");
series2.getData().add(new XYChart.Data<String, Number>(austria, 57401.85));
series2.getData().add(new XYChart.Data<String, Number>(brazil, 41941.19));
series2.getData().add(new XYChart.Data<String, Number>(france, 45263.37));
series2.getData().add(new XYChart.Data<String, Number>(italy, 117320.16));
series2.getData().add(new XYChart.Data<String, Number>(usa, 14845.27));
series3.setName("2005");
series3.getData().add(new XYChart.Data<String, Number>(austria, 45000.65));
series3.getData().add(new XYChart.Data<String, Number>(brazil, 44835.76));
series3.getData().add(new XYChart.Data<String, Number>(france, 18722.18));
series3.getData().add(new XYChart.Data<String, Number>(italy, 17557.31));
series3.getData().add(new XYChart.Data<String, Number>(usa, 92633.68));
barChartData.add(series1);
stackBarChartData.addAll(series2,series3);
stackChart.setData(stackBarChartData);
barChart.setData(barChartData);
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
FXML文件
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.chart.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="487.0" prefWidth="610.0" xmlns:fx="http://javafx.com/fxml" fx:controller="javafxapplication27.FXMLDocumentController">
<children>
<Button fx:id="button" layoutX="14.0" layoutY="14.0" onAction="#handleButtonAction" text="Click Me!" />
<Label fx:id="label" layoutX="126.0" layoutY="120.0" minHeight="16.0" minWidth="69.0" />
<StackedBarChart fx:id="stackChart" layoutX="20.0" layoutY="33.0" prefHeight="223.0" prefWidth="576.0">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis side="LEFT" />
</yAxis>
</StackedBarChart>
<BarChart fx:id="barChart" layoutX="30.0" layoutY="264.0" prefHeight="216.0" prefWidth="552.0">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis side="LEFT" />
</yAxis>
</BarChart>
</children>
</AnchorPane>
当我运行此程序时,只有条形图填充,堆积条形图不会显示任何图形。
输出窗口
请帮我解决这个问题
答案 0 :(得分:0)
来自oracle OTN论坛的回答,先生。 bouye
在文章Bar Chart at Oracle's中,您需要执行以下操作:
((CategoryAxis)stackChart.getXAxis()).setCategories(FXCollections.<String>observableArrayList(Arrays.asList(austria, brazil, france, italy, usa)));
或更简单:
((CategoryAxis)stackChart.getXAxis()).getCategories().setAll(austria, brazil, france, italy, usa);
对我而言,它解决了这个问题。