以下是代码:
<div class="test">
<div class="inner">
<div class="pure-g">
<div class="pure-u-1-4">
<div class="spec__test">123
</div>
</div>
<div class="pure-u-1-4">
<div class="spec__test">123
</div>
</div>
<div class="pure-u-1-4">
<div class="spec__test">123
</div>
</div>
<div class="pure-u-1-4">
<div class="spec__test">123
</div>
</div>
</div>
</div>
</div>
可以预期package scalafx.ensemble.example.charts
import javafx.scene.chart.PieChart.Data
import scala.util.Random
import scalafx.application.JFXApp
import scalafx.scene.Scene
import scalafx.Includes._
import scalafx.collections.ObservableBuffer
import scalafx.scene.chart.PieChart
import scalafx.scene.input.MouseEvent
object BasicPie extends JFXApp {
// data level 1
val pieChartDataBuffer: ObservableBuffer[Data] = ObservableBuffer(
PieChart.Data("A", 20),
PieChart.Data("B", 30),
PieChart.Data("C", 10),
PieChart.Data("D", 40)
)
pieChartDataBuffer.foreach(
(data: Data) => data.node().onMouseClicked = {
(_: MouseEvent) => pieChart.data = pieChartDataSubBuffer(data.name())
}
)
val pieChart = new PieChart {
data = pieChartDataBuffer
title = "DrillDown Pie Chart"
}
// data level 2
val pieChartDataSubBuffer: Map[String, ObservableBuffer[Data]] = _
Array("A", "B", "C", "D").foreach(
(letter: String) => {
val subData: ObservableBuffer[Data] = _
(1 to 10).foreach(
i => {
val subName = letter + "-" + i
subData.add(PieChart.Data(subName, Random.nextInt(100)))
}
)
pieChartDataSubBuffer.updated(letter, subData)
}
)
pieChartDataSubBuffer.foreach(
(nameDataBufferTuple) => {
nameDataBufferTuple._2.foreach(
(data: Data) => {
data.node().onMouseClicked = {
(_: MouseEvent) => pieChart.data = pieChartDataBuffer
}
}
)
}
)
// set listener for one data node
def drillDownData = (pieData: Data, pie: PieChart) => {
pieData.node().onMouseClicked = (_: MouseEvent) => pie.data = pieChartDataSubBuffer(pieData.name())
}
stage = new JFXApp.PrimaryStage {
title = "Drilldown Pie Chart Example"
scene = new Scene {
root = pieChart
}
}
}
会使用subData
进行初始化,但我会收到错误:null
。为什么呢?
答案 0 :(得分:6)
您无法创建未初始化的val
,因为之后您永远无法为其分配任何有用的内容。
说你明确说明:
val subData: ObservableBuffer[Data] = null
subData = ObservableBuffer.empty -> error: reassignment to val!
subData.add(data) -> NullPointerException
您可以将其初始化为var
var subData: ObservableBuffer[Data] = _
或(特别是在可变集合的情况下)为空缓冲区
val subData: ObservableBuffer[Data] = ObservableBuffer.empty // comes to mind
您的代码会建议您执行后者。
答案 1 :(得分:1)
仅供记录,以下是编译的修改版本:
package scalafx.ensemble.example.charts
import javafx.scene.chart.PieChart.Data
import scala.util.Random
import scalafx.application.JFXApp
import scalafx.scene.Scene
import scalafx.Includes._
import scalafx.collections.ObservableBuffer
import scalafx.scene.chart.PieChart
import scalafx.scene.input.MouseEvent
import scala.collection.mutable.{HashMap => MMap}
object BasicPie extends JFXApp {
stage = new JFXApp.PrimaryStage {
title = "Drilldown Pie Chart Example"
scene = new Scene {
root = {
val pieChartDataBuffer = ObservableBuffer(
PieChart.Data("A", 20),
PieChart.Data("B", 30),
PieChart.Data("C", 10),
PieChart.Data("D", 40)
)
val pieChart = new PieChart {
data = pieChartDataBuffer
title = "DrillDown Pie Chart"
}
def subPieChartData: MMap[String, ObservableBuffer[Data]] = {
val subDataMap: MMap[String, ObservableBuffer[Data]] = MMap.empty
for(letter <- Array("A", "B", "C", "D")) {
val dataBuffer: ObservableBuffer[Data] = ObservableBuffer.empty
for(i <- 1 to 10) {
val subLabel = letter + i
dataBuffer.add(PieChart.Data(subLabel, Random.nextInt(100)))
}
subDataMap.update(letter, dataBuffer)
}
subDataMap
}
def drillDownData = (pie: PieChart, pieData: PieChart.Data) => {
val labelPrefix = pieData.name()
val subData = subPieChartData(pieData.name())
pieData.node().onMouseClicked = (_: MouseEvent) => pie.data = subData
subData.foreach(
(data) => climbUpData(pie, data)
)
}
def climbUpData = (pie: PieChart, pieData: PieChart.Data) => {
val node = pieData.node()
if(node != null) {
node.onMouseClicked = (_: MouseEvent) => pie.data = pieChartDataBuffer
}
}
pieChartDataBuffer.foreach((data: Data) => drillDownData(pieChart, data))
pieChart
}
}
}
}