Hello:
I am translating Chart chapter examples from
Pro JavaFX 2 book to ScalaFX. As example it is the first code in the chapter, that uses
PieChart:
package sandbox.javafx;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.PieChart.Data;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class ChartApp1 extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
PieChart pieChart = new PieChart();
pieChart.setData(getChartData());
primaryStage.setTitle("PieChart");
StackPane root = new StackPane();
root.getChildren().add(pieChart);
primaryStage.setScene(new Scene(root, 400, 250));
primaryStage.show();
}
private ObservableList<Data> getChartData() {
ObservableList<Data> answer = FXCollections.observableArrayList();
answer.addAll(new PieChart.Data("java", 17.56), new PieChart.Data("C",
17.06), new PieChart.Data("C++", 8.25), new PieChart.Data("C#",
8.20), new PieChart.Data("ObjectiveC", 6.8), new PieChart.Data(
"PHP", 6.0), new PieChart.Data("(Visual)Basic", 4.76),
new PieChart.Data("Other", 31.37));
return answer;
}
}
An now its ScalaFX Translation:
package sandbox.scalafx.chart
import scalafx.application.JFXApp
import scalafx.collections.ObservableBuffer
import scalafx.scene.chart.PieChart.Data.sfxPieChartData2jfx
import scalafx.scene.chart.PieChart
import scalafx.scene.layout.StackPane
import scalafx.scene.Scene
import scalafx.stage.Stage
object ChartApp1 extends JFXApp {
stage = new Stage {
title = "PieChart"
width = 500
height = 450
scene = new Scene {
content = new StackPane {
content = new PieChart {
data = ObservableBuffer(
PieChart.Data("Java", 17.56),
PieChart.Data("Scala", 17.06),
PieChart.Data("C++", 8.25),
PieChart.Data("C#", 8.20),
PieChart.Data("ObjectiveC", 6.8),
PieChart.Data("PHP", 6.0),
PieChart.Data("(Visual)Basic", 4.76),
PieChart.Data("Other", 31.37))
}
}
}
}
}
As you can see, ScalaFX is more compact than this original version. ;) Almost like JavaFX Script/Visage.
But I deected a problem: In JavaFx program, when I resize the stage, both chart and its respecive label resize too. But it does not happen in ScalaFX version!
Probably in in some point of ScalaFX Chart or even in ScalaFX Node the binding between Chart and Stage dimentsions was lost. I will try investigate this issue. But some one has some idea about how to correct this?
P.S.: This is was not the only problem that I am having with charts. In other examples I had serious problems in conversion from JavaFX ObjectProperty<ObservableList<XYChart.Series<X,Y>>> to ScalaFX ObjectProperty[ObservableBuffer[XYChart.Series[X, Y]]], mainly when it involves java.lang.Number and Scala Int and Double. But i will explain this in other message.
Thanks,
Rafael U. C. Afonso