簡體   English   中英

Scalafx:在Scala中創建lineChart

[英]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
      }
    }
  }

它失敗,並在系列上突出顯示代碼的下面一行錯誤

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集合類。 (我認為它在ScalaFX中被重命名以避免與Scala中List構成混淆。)從ObservableListObservableBuffer了隱式轉換,但是您尚未在import scalafx.Includes._包括import scalafx.Includes._ (強烈建議)。 結果, dataXYChart.Series.apply(String, ObservableBuffer)的預期參數類型不匹配,從而導致錯誤。 通過省略.delegate調用,可以簡化代碼,並且不需要隱式轉換。 或者,您可以只將該import語句添加到您的代碼中。

但是,如果您希望程序運行,則還應該將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
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM