Scalafx:在 Scala 中创建折线图

Scalafx: create lineChart in scala

我正在处理折线图代码示例:

  import scalafx.application.JFXApp
  import scalafx.stage.Stage
  import scalafx.scene.Scene
  import scalafx.scene.chart.{LineChart,NumberAxis, XYChart}
  import scalafx.collections.ObservableBuffer
  import scalafx.scene.chart.XYChart.Series



  object LineChartSample extends JFXApp {

    // Defining the axes
    val xAxis = new NumberAxis()
    xAxis.label = "Number of Month"
    val yAxis = new NumberAxis()

    // Creating the chart
   val lineChart = LineChart(xAxis,yAxis)
     //val lineChart: LineChart[NumberAxis, NumberAxis] = _

    lineChart.title = "Stock Monitoring, 2010"

    // defining a series
    val data = ObservableBuffer(Seq(
      (1, 23),
      (2, 14),
      (3, 15),
      (4, 24),
      (5, 34),
      (6, 36),
      (7, 22),
      (8, 45),
      (9, 43),
      (10, 17),
      (11, 29),
      (12, 25)
    ) map {case (x, y) => XYChart.Data[Number, Number](x, y)} ).delegate

    val series = XYChart.Series[Number,Number]("test",data)




    lineChart.getData.add(series)

    val stg = new Stage {
      title = "Line Chart Sample"
      scene = new Scene(800, 600) {
        root = lineChart
      }
    }
  }

它失败了,错误突出显示了 Series

上的以下代码行
val series = XYChart.Series[Number,Number]("test",data)


Error:(41, 30) overloaded method value apply with alternatives:
  (name: String,data: scalafx.collections.ObservableBuffer[javafx.scene.chart.XYChart.Data[Number,Number]])javafx.scene.chart.XYChart.Series[Number,Number] <and>
  (data: scalafx.collections.ObservableBuffer[javafx.scene.chart.XYChart.Data[Number,Number]])javafx.scene.chart.XYChart.Series[Number,Number]
 cannot be applied to (String, javafx.collections.ObservableList[javafx.scene.chart.XYChart.Data[Number,Number]])
  val series = XYChart.Series[Number,Number]("test",data)

谁能告诉我它有什么问题吗?我无法正确理解错误。

我认为这里的问题是在 ObservableBuffer 声明末尾使用了 .delegate 转换,它将 data 更改为 JavaFX ObservableList.

ObservableBufferScalaFX 的一部分)声明等同于 JavaFX ObservableList 集合 class。 (我认为它在 ScalaFX 中被重命名以避免与 ScalaList 的构成混淆。) ObservableListObservableBuffer,但您没有在导入中包含 import scalafx.Includes._(强烈推荐)。结果,dataXYChart.Series.apply(String, ObservableBuffer) 的预期参数类型不匹配,因此出现错误。通过省略 .delegate 调用,您可以简化代码并且不需要隐式转换。或者,您可以将该导入语句添加到您的代码中。

但是,如果您希望您的程序 运行,您还应该将 JFXAppstage 成员分配给新的 PrimaryStage。以下作品:

import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.scene.Scene
import scalafx.scene.chart.{LineChart, NumberAxis, XYChart}
import scalafx.collections.ObservableBuffer

object LineChartSample
extends JFXApp {

  // Defining the axes
  val xAxis = new NumberAxis()
  xAxis.label = "Number of Month"
  val yAxis = new NumberAxis()

  // Creating the chart
  val lineChart = LineChart(xAxis, yAxis)

  lineChart.title = "Stock Monitoring, 2010"

  // defining a series
  val data = ObservableBuffer(Seq(
    (1, 23),
    (2, 14),
    (3, 15),
    (4, 24),
    (5, 34),
    (6, 36),
    (7, 22),
    (8, 45),
    (9, 43),
    (10, 17),
    (11, 29),
    (12, 25)
  ) map {case (x, y) => XYChart.Data[Number, Number](x, y)})

  val series = XYChart.Series[Number, Number]("test", data)

  lineChart.getData.add(series)

  stage = new PrimaryStage {
    title = "Line Chart Sample"
    scene = new Scene(800, 600) {
      root = lineChart
    }
  }
}