Example:
package javafx_charts;
import java.awt.event.MouseEvent;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.stage.Stage;
/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_Charts extends Application {
Label label;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("java-buddy.blogspot.com");
Group root = new Group();
ObservableList<PieChart.Data> pieChartData =
FXCollections.observableArrayList(
new PieChart.Data("January", 100),
new PieChart.Data("February", 200),
new PieChart.Data("March", 50),
new PieChart.Data("April", 75),
new PieChart.Data("May", 110),
new PieChart.Data("June", 300),
new PieChart.Data("July", 111),
new PieChart.Data("August", 30),
new PieChart.Data("September", 75),
new PieChart.Data("October", 55),
new PieChart.Data("November", 225),
new PieChart.Data("December", 99));
final PieChart pieChart = new PieChart(pieChartData);
pieChart.setTitle("PieChart");
label = new Label("");
//add event handler
for(final PieChart.Data data : pieChart.getData()){
data.getNode().addEventHandler(
javafx.scene.input.MouseEvent.MOUSE_PRESSED,
new EventHandler<javafx.scene.input.MouseEvent>() {
@Override
public void handle(javafx.scene.input.MouseEvent mouseEvent) {
String name = data.getName();
double value = data.getPieValue();
label.setText(name + " : " + String.valueOf(value));
}
});
data.getNode().addEventHandler(
javafx.scene.input.MouseEvent.MOUSE_RELEASED,
new EventHandler<javafx.scene.input.MouseEvent>() {
@Override
public void handle(javafx.scene.input.MouseEvent mouseEvent) {
label.setText("");
}
});
}
root.getChildren().add(pieChart);
root.getChildren().add(label);
primaryStage.setScene(new Scene(root, 500, 400));
primaryStage.show();
}
}
Related article:
- Create and update PieChart from TableView
No comments:
Post a Comment