0

这是代码:

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
      }
    }
  }

}

预期行为:

  1. 当应用程序运行时,它应该显示一个主要组的饼图。(好的。)

  2. 当您单击饼图的任何部分时,您应该得到该主要组的次要组的饼图。(好的。)

  3. 当您单击辅助组饼图中的任何切片时,您应该返回到主饼图。(不工作。)

它编译和运行没有问题,向下钻取也按预期工作,但向上爬却不是。为什么?

4

1 回答 1

0

在设置鼠标单击侦听器之前,我应该PieChart先将 2 级饼图数据设置为对象:

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
      }
    }
  }

}

object Test {
  BasicPie.main(Array("a"))
}
于 2015-09-17T11:44:42.983 回答