JavaFX折线图抛出强制转换异常

时间:2018-08-13 14:24:25

标签: java exception javafx charts

我正在尝试在窗口中放置另一个图表,并且出现强制转换异常。我有一个工作的条形图,但是当我单击向线图添加数据的按钮时,抛出了异常。 这是代码:

@FXML
private BarChart<String, Double> barChart;
@FXML
private CategoryAxis xAxis;
@FXML
private NumberAxis yAxis;
@FXML
private LineChart<Double, Double> lineChart;
@FXML
private CategoryAxis linexAxis;
@FXML
private NumberAxis lineyAxis;

public void createChart(ActionEvent event) {} //this method creates and populates barChart

public void loadLineChart(ActionEvent event) {

    linexAxis.setLabel("Wind speed [m/s]");
    lineyAxis.setLabel("Probability [%]");

    ObservableList<XYChart.Series<Double, Double>> lineChartData = FXCollections.observableArrayList();

    LineChart.Series<Double, Double> series1 = new LineChart.Series<Double, Double>();
    series1.setName("Series 1");
    series1.getData().add(new XYChart.Data<Double, Double>(0.0, 1.0));
    series1.getData().add(new XYChart.Data<Double, Double>(1.2, 1.4));
    series1.getData().add(new XYChart.Data<Double, Double>(2.2, 1.9));
    series1.getData().add(new XYChart.Data<Double, Double>(2.7, 2.3));
    series1.getData().add(new XYChart.Data<Double, Double>(2.9, 0.5));

    lineChartData.add(series1);

    lineChart.setData(lineChartData);

    lineChart.getData().add(series1);
}

这是堆栈跟踪:

Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.String
at javafx.scene.chart.CategoryAxis.calculateRequiredSize(CategoryAxis.java:382)
at javafx.scene.chart.CategoryAxis.autoRange(CategoryAxis.java:363)
at javafx.scene.chart.Axis.computePrefHeight(Axis.java:577)
at javafx.scene.Parent.prefHeight(Parent.java:935)
at javafx.scene.layout.Region.prefHeight(Region.java:1435)
at javafx.scene.chart.XYChart.layoutChartChildren(XYChart.java:687)
at javafx.scene.chart.Chart$1.layoutChildren(Chart.java:94)
at javafx.scene.Parent.layout(Parent.java:1087)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Scene.doLayoutPass(Scene.java:552)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2397)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$29(Toolkit.java:398)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:397)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:424)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:518)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:498)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:491)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$403(QuantumToolkit.java:319)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
at java.lang.Thread.run(Thread.java:748)

我需要此功能,因为我试图相互堆叠两个图表。与直方图和折线图上的条形图。我试图用折线图填充的数据不是最终数据。我得到了一个列表,其中包含要在折线图上显示的数据。

编辑。 这是fxml。

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.chart.BarChart?>
<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.LineChart?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>

             

    <BarChart fx:id="barChart" alternativeColumnFillVisible="true" layoutX="31.0" layoutY="64.0" prefHeight="502.0" prefWidth="767.0">
        <xAxis>
            <CategoryAxis side="BOTTOM" fx:id="xAxis" />
        </xAxis>
        <yAxis>
            <NumberAxis fx:id="yAxis" side="LEFT" />
        </yAxis>
    </BarChart>
    <Button layoutX="420.0" layoutY="17.0" mnemonicParsing="false" onAction="#loadLineChart" text="Load chart" />
    <Button layoutX="24.0" layoutY="17.0" mnemonicParsing="false" onAction="#createChart" text="LOAD" />

  <LineChart fx:id="lineChart" layoutX="52.0" layoutY="571.0" prefHeight="334.0" prefWidth="500.0">
    <xAxis>
      <CategoryAxis fx:id="linexAxis" side="BOTTOM" />
    </xAxis>
    <yAxis>
      <NumberAxis fx:id="lineyAxis" side="LEFT" />
    </yAxis>
  </LineChart>
</children>

1 个答案:

答案 0 :(得分:1)

您要么需要将x值更改为字符串。

series1.getData().add(new XYChart.Data<Double, Double>(Double.toString(0.0), 1.0));
//Do the rest of the x-values here!

或替换

CategoryAxis linexAxis;

使用

NumberAxis linexAxis;