首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Scalafx:在scala中创建lineChart

Scalafx:在scala中创建lineChart
EN

Stack Overflow用户
提问于 2017-10-18 20:32:51
回答 1查看 884关注 0票数 0

我正在编写一个线条图代码示例:

代码语言:javascript
复制
  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
      }
    }
  }

它失败了,错误突出显示在系列代码行下面。

代码语言:javascript
复制
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)

有人能给我指一下它有什么问题吗?我无法正确理解错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-18 22:10:49

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

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

但是,如果您希望您的程序能够运行,您还应该将JFXAppJFXApp成员分配给一个新的PrimaryStage。以下工作:

代码语言:javascript
复制
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
    }
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46818766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档