Created
May 5, 2018 19:43
-
-
Save sharifulislam52/1efd5208117055e8ccc0b33596dbe4ae to your computer and use it in GitHub Desktop.
javafx : PieChart with mouse click chart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.stage.Stage; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
public class Main extends Application { | |
@Override | |
public void start(Stage primaryStage) { | |
try { | |
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml")); | |
Scene scene = new Scene(root,500,300); | |
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
} catch(Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import javafx.scene.chart.PieChart?> | |
<?import javafx.scene.control.Button?> | |
<?import javafx.scene.control.Label?> | |
<?import javafx.scene.layout.AnchorPane?> | |
<?import javafx.scene.text.Font?> | |
<AnchorPane minHeight="300.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController"> | |
<children> | |
<PieChart fx:id="pi" layoutX="14.0" layoutY="14.0" prefHeight="267.0" prefWidth="325.0" title="PieChart" /> | |
<Button layoutX="390.0" layoutY="47.0" mnemonicParsing="false" onAction="#btn" text="Button" /> | |
<Label fx:id="label" layoutX="390.0" layoutY="90.0" prefHeight="17.0" prefWidth="69.0" text="Label"> | |
<font> | |
<Font size="18.0" /> | |
</font> | |
</Label> | |
</children> | |
</AnchorPane> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.fxml.FXML; | |
import javafx.scene.chart.PieChart; | |
import javafx.scene.chart.PieChart.Data; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.event.ActionEvent; | |
import javafx.event.EventHandler; | |
import javafx.scene.control.Label; | |
import javafx.scene.input.MouseEvent; | |
public class MainController{ | |
@FXML public PieChart pi; | |
@FXML public Label label; | |
public void btn(ActionEvent event) { | |
ObservableList<Data> list = FXCollections.observableArrayList( | |
new PieChart.Data("java", 50), | |
new PieChart.Data("python", 25), | |
new PieChart.Data("c++", 20), | |
new PieChart.Data("c#", 5) | |
); | |
pi.setData(list); | |
for(PieChart.Data data : pi.getData()) { | |
data.getNode().addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() { | |
@Override | |
public void handle(MouseEvent event) { | |
label.setText(String.valueOf(data.getPieValue()) + "%"); | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment