我正在编写一个线条图代码示例:
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)有人能给我指一下它有什么问题吗?我无法正确理解错误。
发布于 2017-10-18 22:10:49
我认为这里的问题是在ObservableBuffer声明的末尾使用ObservableBuffer转换,它将data转换为JavaFX ObservableList。
ObservableBuffer (ScalaFX的一部分)被声明为等效于JavaFX ObservableList集合类。(我认为在ScalaFX中重命名它是为了避免与Scala中的List构成混淆。)有一个从ObservableList到ObservableBuffer的隐式转换,但是您还没有在导入中包含import scalafx.Includes._ (强烈推荐)。因此,data不匹配XYChart.Series.apply(String, ObservableBuffer)的预期参数类型,因此出现了错误。通过省略.delegate调用,可以简化代码,并且不需要隐式转换。或者,只需将导入语句添加到代码中即可。
但是,如果您希望您的程序能够运行,您还应该将JFXApp的JFXApp成员分配给一个新的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
}
}
}https://stackoverflow.com/questions/46818766
复制相似问题