[英]Scalafx: create lineChart in scala
I am working on a sample of line chart code: 我正在研究折线图代码示例:
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
}
}
}
It is failing with error highlighting BELOW line of code on 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)
Can someone point me what is wrong with it? 有人可以指出我的问题吗? I am unable to understand the error properly.
我无法正确理解该错误。
I think the problem here is the use of the .delegate
conversion at the end of the ObservableBuffer
declaration, which changes data
into a JavaFX ObservableList
. 我认为这里的问题是在
ObservableBuffer
声明的末尾使用.delegate
转换,该声明将data
更改为JavaFX ObservableList
。
ObservableBuffer
(part of ScalaFX ) is declared to be equivalent to the JavaFX ObservableList
collection class. ObservableBuffer
( ScalaFX的一部分)被声明为等效于JavaFX ObservableList
集合类。 (I think it was renamed in ScalaFX to avoid confusion with what constitutes a List
in Scala .) There is an implicit conversion from ObservableList
to ObservableBuffer
, but you have not included import scalafx.Includes._
(highly recommended) among your imports. (我认为它在ScalaFX中被重命名以避免与Scala中的
List
构成混淆。)从ObservableList
到ObservableBuffer
了隐式转换,但是您尚未在import scalafx.Includes._
包括import scalafx.Includes._
(强烈建议)。 As a result, data
doesn't match the expected argument type of XYChart.Series.apply(String, ObservableBuffer)
, hence the error. 结果,
data
与XYChart.Series.apply(String, ObservableBuffer)
的预期参数类型不匹配,从而导致错误。 By omitting the .delegate
call, you simplify your code and do not require the implicit conversion. 通过省略
.delegate
调用,可以简化代码,并且不需要隐式转换。 Alternatively, you could just add that import statement to your code. 或者,您可以只将该import语句添加到您的代码中。
However, if you expect your program to run, you should also assign the stage
member of JFXApp
to a new PrimaryStage
. 但是,如果您希望程序运行,则还应该将
JFXApp
的stage
成员分配给新的PrimaryStage
。 The following works: 以下作品:
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.