/*
* Copyright 2013 ScalaFX Project
* All right reserved.
*/
package scalafx.ensemble.example.charts
import scalafx.application.JFXApp
import scalafx.scene.Scene
import scalafx.scene.chart.PieChart
import scalafx.scene.control.Tooltip
import scala.collection.JavaConversions._
object BasicPie extends JFXApp {
stage = new JFXApp.PrimaryStage {
title = "Pie Chart Example"
scene = new Scene {
val pie: PieChart = new PieChart {
data = Seq(
PieChart.Data("Sun", 20),
PieChart.Data("IBM", 12),
PieChart.Data("HP", 25),
PieChart.Data("Dell", 22),
PieChart.Data("Apple", 30)
)
clockwise = false
}
root = pie
val total = pie.getData.foldLeft(0.0) {
(x, y) => x + y.getPieValue
}
pie.getData.foreach(
d => {
val sliceNode = d.getNode
val pieValue: Double = d.getPieValue
val percent = (pieValue / total) * 100
val msg = "%s: %.2f (%.2f%%)".format(d.getName, pieValue, percent)
val tt = new Tooltip()
tt.setText(msg)
tt.setStyle("-fx-background-color: yellow; " +
"-fx-text-fill: black; ")
Tooltip.install(sliceNode, tt)
}
)
}
}
}
Here is the code:
Note that I have used
Tooltip()instead ofTooltip(msg)because the latter would cause an type-mismatch error in intellij. Is this a bug?