下面列出了怎么用javafx.scene.chart.PieChart的API类实例代码及写法,或者点击链接到github查看源代码。
private void addDiskBox(Disk disk) {
try {
Parent parentDisk = FXMLLoader.load(getClass().getResource("/resources/views/models/DiskInfo.fxml"));
//Label lblName = (Label) parentDisk.lookup("#lblName");
Label lblTotalSpace = (Label) parentDisk.lookup("#lblTotalSpace");
PieChart pieData = (PieChart) parentDisk.lookup("#pieData");
pieData.setTitle(disk.getName());
lblTotalSpace.setText(Utils.humanReadableByteCount(disk.getTotalSpace()));
// Data of pie chart
ObservableList<PieChart.Data> data = FXCollections.observableArrayList();
data.add(new PieChart.Data("Usable", Utils.humanReadableByteCountNumber(disk.getUsableSpace())));
data.add(new PieChart.Data("Free", Utils.humanReadableByteCountNumber(disk.getFreeSpace())));
pieData.setData(data);
pieData.getData().forEach(d ->
d.nameProperty().bind(Bindings.concat(d.getName(), " ", d.pieValueProperty(), " GB"))
);
boxContainerDisks.getChildren().add(parentDisk);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
private void addDiskBox(Disk disk) {
try {
Parent parentDisk = FXMLLoader.load(getClass().getResource("/resources/views/models/DiskInfo.fxml"));
//Label lblName = (Label) parentDisk.lookup("#lblName");
Label lblTotalSpace = (Label) parentDisk.lookup("#lblTotalSpace");
PieChart pieData = (PieChart) parentDisk.lookup("#pieData");
pieData.setTitle(disk.getName());
lblTotalSpace.setText(Utils.humanReadableByteCount(disk.getTotalSpace()));
// Data of pie chart
ObservableList<PieChart.Data> data = FXCollections.observableArrayList();
data.add(new PieChart.Data("Usable", Utils.humanReadableByteCountNumber(disk.getUsableSpace())));
data.add(new PieChart.Data("Free", Utils.humanReadableByteCountNumber(disk.getFreeSpace())));
pieData.setData(data);
pieData.getData().forEach(d ->
d.nameProperty().bind(Bindings.concat(d.getName(), " ", d.pieValueProperty(), " GB"))
);
boxContainerDisks.getChildren().add(parentDisk);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public static void setPieColors(PieChart pie, List<String> palette,
boolean showLegend) {
if (pie == null || palette == null
|| pie.getData() == null
|| pie.getData().size() > palette.size()) {
return;
}
for (int i = 0; i < pie.getData().size(); i++) {
PieChart.Data data = pie.getData().get(i);
data.getNode().setStyle("-fx-pie-color: " + palette.get(i) + ";");
}
pie.setLegendVisible(showLegend);
if (showLegend) {
Set<Node> legendItems = pie.lookupAll("Label.chart-legend-item");
if (legendItems.isEmpty()) {
return;
}
for (Node legendItem : legendItems) {
Label legendLabel = (Label) legendItem;
Node legend = legendLabel.getGraphic();
if (legend != null) {
for (int i = 0; i < pie.getData().size(); i++) {
String name = pie.getData().get(i).getName();
if (name.equals(legendLabel.getText())) {
legend.setStyle("-fx-background-color: " + palette.get(i));
break;
}
}
}
}
}
}
public void start3(Stage primaryStage) {
Text txt = new Text("Test results:");
PieChart pc = new PieChart();
pc.getData().add(new PieChart.Data("Succeed", 143));
pc.getData().add(new PieChart.Data("Failed" , 12));
pc.getData().add(new PieChart.Data("Ignored", 18));
VBox vb = new VBox(txt, pc);
vb.setAlignment(Pos.CENTER);
vb.setPadding(new Insets(10, 10, 10, 10));
primaryStage.setTitle("A chart example");
primaryStage.onCloseRequestProperty()
.setValue(e -> System.out.println("\nBye! See you later!"));
primaryStage.setScene(new Scene(vb, 300, 300));
primaryStage.show();
}
public DrilldownPieChartSample() {
String drilldownCss = DrilldownPieChartSample.class.getResource("DrilldownChart.css").toExternalForm();
PieChart pie = new PieChart(
FXCollections.observableArrayList(
A = new PieChart.Data("A", 20),
B = new PieChart.Data("B", 30),
C = new PieChart.Data("C", 10),
D = new PieChart.Data("D", 40)));
((Parent) pie).getStylesheets().add(drilldownCss);
setDrilldownData(pie, A, "a");
setDrilldownData(pie, B, "b");
setDrilldownData(pie, C, "c");
setDrilldownData(pie, D, "d");
getChildren().add(pie);
}
public DrilldownPieChartSample() {
String drilldownCss = DrilldownPieChartSample.class.getResource("DrilldownChart.css").toExternalForm();
PieChart pie = new PieChart(
FXCollections.observableArrayList(
A = new PieChart.Data("A", 20),
B = new PieChart.Data("B", 30),
C = new PieChart.Data("C", 10),
D = new PieChart.Data("D", 40)));
((Parent) pie).getStylesheets().add(drilldownCss);
setDrilldownData(pie, A, "a");
setDrilldownData(pie, B, "b");
setDrilldownData(pie, C, "c");
setDrilldownData(pie, D, "d");
getChildren().add(pie);
}
@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setTitle("Europian Country Population");
stage.setWidth(500);
stage.setHeight(500);
ObservableList<PieChart.Data> pieChartData =
FXCollections.observableArrayList(
new PieChart.Data("Belgium", 3),
new PieChart.Data("France", 26),
new PieChart.Data("Germany", 35),
new PieChart.Data("Netherlands", 7),
new PieChart.Data("Sweden", 4),
new PieChart.Data("United Kingdom", 25));
final PieChart pieChart = new PieChart(pieChartData);
pieChart.setTitle("Country Population");
((Group) scene.getRoot()).getChildren().add(pieChart);
stage.setScene(scene);
stage.show();
}
/**
* Generates a pie chart with the summary test results of the current build.
*
* @param passedCount passed test count
* @param failedCount failed test count
* @param skippedCount skipped test count
* @param summaryChartFileName file name of the summary chart
*/
public void generateSummaryChart(int passedCount, int failedCount, int skippedCount, String summaryChartFileName) {
List<PieChart.Data> data = new ArrayList<>();
data.add(new PieChart.Data(StringUtil.concatStrings("Test Failures (", Integer.toString(failedCount), ")"),
failedCount));
data.add(new PieChart.Data(StringUtil.concatStrings("Deployment Errors (", Integer.toString
(skippedCount), ")"), skippedCount));
data.add(new PieChart.Data(StringUtil.concatStrings("Passed (", Integer.toString(passedCount), ")"),
passedCount));
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(data);
final PieChart chart = new PieChart(pieChartData);
chart.setAnimated(false);
chart.setLabelsVisible(true);
chart.setTitle("Build Summary of Infrastructure Combinations ("
+ (failedCount + skippedCount + passedCount) + ")");
genChart(chart, 600, 600, summaryChartFileName, "styles/summary.css");
}
/**
* Generate pipe chart
*/
private void initChart() {
PieChart.Data available = new PieChart.Data("Available", 13);
PieChart.Data used = new PieChart.Data("Used", 25);
PieChart.Data empty = new PieChart.Data("Empty", 10);
ObservableList<PieChart.Data> pieChartData =
FXCollections.observableArrayList(available, used, empty);
final PieChart chart = new PieChart(pieChartData);
chart.setTitle("");
chart.setPrefSize(350, 350);
chart.setLegendVisible(false);
chart.setStyle("-fx-background-color: none");
pieChartPane.getChildren().add(chart);
available.getNode().setStyle("-fx-background-color: #55c4fe");
used.getNode().setStyle("-fx-background-color: #008287");
empty.getNode().setStyle("-fx-background-color: #219297");
}
private HBox getSystemPanel() {
ObservableList<PieChart.Data> pieChartData =
FXCollections.observableArrayList();
CONNECTIONS.forEach((string, clientObject) -> {
if (clientObject.getSYSTEM_OS() != null) {
if (operatingSystems.containsKey(clientObject.getSYSTEM_OS())) {
operatingSystems.put(clientObject.getSYSTEM_OS(), operatingSystems.get(clientObject.getSYSTEM_OS()) + 1);
} else {
operatingSystems.put(clientObject.getSYSTEM_OS(), 1);
}
}
});
operatingSystems.forEach((string, integer) -> {
pieChartData.add(new PieChart.Data(string, CONNECTIONS.size() / integer));
});
final PieChart chart = new PieChart(pieChartData);
chart.setLegendVisible(false);
chart.setTitle("Operating Systems");
chart.setMaxSize(300, 300);
return Styler.hContainer(Styler.vContainer(10, chart));
}
private void updateInnerCircleLayout() {
double minX = Double.MAX_VALUE, minY = Double.MAX_VALUE;
double maxX = Double.MIN_VALUE, maxY = Double.MIN_VALUE;
for (PieChart.Data data: getData()) {
Node node = data.getNode();
Bounds bounds = node.getBoundsInParent();
if (bounds.getMinX() < minX) {
minX = bounds.getMinX();
}
if (bounds.getMinY() < minY) {
minY = bounds.getMinY();
}
if (bounds.getMaxX() > maxX) {
maxX = bounds.getMaxX();
}
if (bounds.getMaxY() > maxY) {
maxY = bounds.getMaxY();
}
}
innerCircle.setCenterX(minX + (maxX - minX) / 2);
innerCircle.setCenterY(minY + (maxY - minY) / 2);
innerCircle.setRadius((maxX - minX) / 4);
}
@SuppressWarnings("unchecked")
@Override
public void initialize(URL location, ResourceBundle resources) {
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
new PieChart.Data("Sun", 20),
new PieChart.Data("IBM", 12),
new PieChart.Data("HP", 25),
new PieChart.Data("Dell", 22),
new PieChart.Data("Apple", 30)
);
pieChart.setData(pieChartData);
pieChart.setClockwise(false);
XYChart.Series<String, Number> series = new XYChart.Series<>();
series.setName("Legend 1");
series.getData().add(new XYChart.Data<>("0", 2D));
series.getData().add(new XYChart.Data<>("1", 8D));
series.getData().add(new XYChart.Data<>("2", 5D));
series.getData().add(new XYChart.Data<>("3", 3D));
series.getData().add(new XYChart.Data<>("4", 6D));
series.getData().add(new XYChart.Data<>("5", 8D));
series.getData().add(new XYChart.Data<>("6", 5D));
series.getData().add(new XYChart.Data<>("7", 6D));
series.getData().add(new XYChart.Data<>("8", 5D));
areaChart.getData().setAll(series);
areaChart.setCreateSymbols(true);
}
@FXML
private void setPieChartGraph(){
Integer chartType = chbxTest.getValue();
//switch by vehicle type
switch (chartType){
case 1 :
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
new PieChart.Data("Prius", 13),
new PieChart.Data("Audi A7", 25),
new PieChart.Data("Audi R8", 10),
new PieChart.Data("BMW i8", 22),
new PieChart.Data("Premio", 30));
topVehiclesChart.setData(pieChartData);
topVehiclesChart.setTitle("Top Vehicles");
break;
case 2 :
pieChartData = FXCollections.observableArrayList(
new PieChart.Data("Prius", 73),
new PieChart.Data("Audi A7", 25),
new PieChart.Data("Audi R8", 17),
new PieChart.Data("BMW i8", 45),
new PieChart.Data("Premio", 90));
topVehiclesChart.setData(pieChartData);
break;
case 3 : break;
default : new AlertBuilder("warn","Top Vehicles","Top Vehicle Graph",
"Invalid type of graph !!");
}
}
private void setDrilldownData(final PieChart pie, PieChart.Data data, final String labelPrefix) {
data.getNode().setOnMouseClicked(new EventHandler<MouseEvent>() {
public void handle(MouseEvent t) {
pie.setData(FXCollections.observableArrayList(
new PieChart.Data(labelPrefix + "-1", 7),
new PieChart.Data(labelPrefix + "-2", 2),
new PieChart.Data(labelPrefix + "-3", 5),
new PieChart.Data(labelPrefix + "-4", 3),
new PieChart.Data(labelPrefix + "-5", 2)));
}
});
}
public PieChartSample() {
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
new PieChart.Data("Sun", 20),
new PieChart.Data("IBM", 12),
new PieChart.Data("HP", 25),
new PieChart.Data("Dell", 22),
new PieChart.Data("Apple", 30)
);
PieChart chart = new PieChart(pieChartData);
chart.setClockwise(false);
getChildren().add(chart);
}
protected PieChart createChart() {
final PieChart pc = new PieChart(FXCollections.observableArrayList(
new PieChart.Data("Sun", 20),
new PieChart.Data("IBM", 12),
new PieChart.Data("HP", 25),
new PieChart.Data("Dell", 22),
new PieChart.Data("Apple", 30)
));
// setup chart
pc.setId("BasicPie");
pc.setTitle("Pie Chart Example");
return pc;
}
private void setDrilldownData(final PieChart pie, PieChart.Data data, final String labelPrefix) {
data.getNode().setOnMouseClicked(new EventHandler<MouseEvent>() {
public void handle(MouseEvent t) {
pie.setData(FXCollections.observableArrayList(
new PieChart.Data(labelPrefix + "-1", 7),
new PieChart.Data(labelPrefix + "-2", 2),
new PieChart.Data(labelPrefix + "-3", 5),
new PieChart.Data(labelPrefix + "-4", 3),
new PieChart.Data(labelPrefix + "-5", 2)));
}
});
}
public PieChartSample() {
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
new PieChart.Data("Sun", 20),
new PieChart.Data("IBM", 12),
new PieChart.Data("HP", 25),
new PieChart.Data("Dell", 22),
new PieChart.Data("Apple", 30)
);
PieChart chart = new PieChart(pieChartData);
chart.setClockwise(false);
getChildren().add(chart);
}
protected PieChart createChart() {
final PieChart pc = new PieChart(FXCollections.observableArrayList(
new PieChart.Data("Sun", 20),
new PieChart.Data("IBM", 12),
new PieChart.Data("HP", 25),
new PieChart.Data("Dell", 22),
new PieChart.Data("Apple", 30)
));
// setup chart
pc.setId("BasicPie");
pc.setTitle("Pie Chart Example");
return pc;
}
protected PieChart createChart() {
final PieChart pc = new PieChart(FXCollections.observableArrayList(
new PieChart.Data("Sun", 20),
new PieChart.Data("IBM", 12),
new PieChart.Data("HP", 25),
new PieChart.Data("Dell", 22),
new PieChart.Data("Apple", 30)
));
// setup chart
pc.setId("BasicPie");
pc.setTitle("Pie Chart Example");
return pc;
}
@FXML
public void fillPieChart() {
ArrayList<ArrayList<String>> data = lab.lastMonthsReports(12);
String[] test = {
"Blood Grouping & Rh","Lipid Profile Test","LFT","RFT",
"HIV","CPK","Pathalogy Test",
"Complete Blood Count"
};
int tmpSize = test.length;
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList();
for(int i = 0; i < tmpSize; i++)
{
pieChartData.add(new PieChart.Data(test[i], Integer.parseInt(data.get(1).get(i))));
}
pieChartData.forEach(data1 ->
data1.nameProperty().bind(
Bindings.concat(
data1.getName(), " (", data1.pieValueProperty(), ")"
)
)
);
piechart.setLegendSide(Side.BOTTOM);
piechart.setLegendVisible(true);
piechart.setData(pieChartData);
}
public void fillPieChart(int months)
{
ArrayList<ArrayList<String>> data = admin.lastMonthsReports(months);
String[] test = {
"Blood Grouping & Rh","Lipid Profile Test","LFT","RFT",
"HIV","CPK","Pathalogy Test",
"Complete Blood Count"
};
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList();
pieChartData.clear();
int tmpSize = test.length;
for(int i = 0; i < tmpSize; i++)
{
pieChartData.add(new PieChart.Data(test[i], Integer.parseInt(data.get(1).get(i))));
}
pieChartData.forEach(data1 ->
data1.nameProperty().bind(
Bindings.concat(
data1.getName(), " (", data1.pieValueProperty(), ")"
)
)
);
labReportPieChart.setData(pieChartData);
ArrayList<Integer> month = new ArrayList<Integer>();
for (int i = 1; i < 13; i++)
{
month.add(i);
}
//reportsCombo.getItems().clear();
//reportsCombo.getItems().addAll(month);
//reportsCombo.setValue(12);
}
@FXML public void fillSupplierChart() {
supplierchart.setVisible(true);
HashMap<String,String> supplierNames = admin.getSupplierNames();
ArrayList<ArrayList<String>> suppliers = admin.getSupplierSummary();
int noOfSuppliers = suppliers.get(0).size();
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList();
for (int i = 0; i < noOfSuppliers; i++)
{
String supplierID = suppliers.get(0).get(i);
int stocks = Integer.parseInt(suppliers.get(1).get(i));
pieChartData.add(new PieChart.Data(supplierNames.get(supplierID), stocks));
}
//piechart.setLabelLineLength(20);
pieChartData.forEach(data ->
data.nameProperty().bind(
Bindings.concat(
data.getName(), " (", data.pieValueProperty(), ")"
)
)
);
supplierchart.setLegendSide(Side.BOTTOM);
supplierchart.setLabelsVisible(true);
supplierchart.setData(pieChartData);
}
@FXML
public void fillPieChart() {
HashMap<String,String> supplierNames = pharmacist.getSupplierNames();
ArrayList<ArrayList<String>> suppliers = pharmacist.getSupplierSummary();
int noOfSuppliers = suppliers.get(0).size();
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList();
for (int i = 0; i < noOfSuppliers; i++)
{
String supplierID = suppliers.get(0).get(i);
int stocks = Integer.parseInt(suppliers.get(1).get(i));
pieChartData.add(new PieChart.Data(supplierNames.get(supplierID), stocks));
}
pieChartData.forEach(data1 ->
data1.nameProperty().bind(
Bindings.concat(
data1.getName(), " (", data1.pieValueProperty().intValue(), ")"
)
)
);
//piechart.setLabelLineLength(20);
piechart.setLegendSide(Side.BOTTOM);
piechart.setLabelsVisible(true);
piechart.setData(pieChartData);
}
public static void addTooltipToData(final PieChart.Data data, final StringProperty label) {
data.nodeProperty().addListener((value, oldNode, newNode) -> {
if (newNode != null) {
Tooltip.install(newNode, createNumberedTooltip(label, data.pieValueProperty()));
}
});
}
private void initGraphs() {
bookChart = new PieChart(databaseHandler.getBookGraphStatistics());
memberChart = new PieChart(databaseHandler.getMemberGraphStatistics());
bookInfoContainer.getChildren().add(bookChart);
memberInfoContainer.getChildren().add(memberChart);
bookIssueTab.setOnSelectionChanged((Event event) -> {
clearIssueEntries();
if (bookIssueTab.isSelected()) {
refreshGraphs();
}
});
}
@Override public void start(Stage stage) {
stage.setTitle("Imported Fruits");
stage.setWidth(500);
stage.setHeight(500);
ObservableList<PieChart.Data> pieChartData = createData();
final DoughnutChart chart = new DoughnutChart(pieChartData);
chart.setTitle("Imported Fruits");
Scene scene = new Scene(new StackPane(chart));
stage.setScene(scene);
stage.show();
}
private ObservableList<PieChart.Data> createData() {
return FXCollections.observableArrayList(
new PieChart.Data("Grapefruit", 13),
new PieChart.Data("Oranges", 25),
new PieChart.Data("Plums", 10),
new PieChart.Data("Pears", 22),
new PieChart.Data("Apples", 30));
}
/**
* 経験値比率
* @param ships 対象艦
*/
private void setRatio(List<Ship> ships) {
Map<TypeGroup, Long> collect = ships.stream()
.collect(Collectors.groupingBy(TypeGroup::toTypeGroup, TreeMap::new,
Collectors.summingLong(this::getExp)));
ObservableList<PieChart.Data> value = FXCollections.observableArrayList();
for (Entry<TypeGroup, Long> data : collect.entrySet()) {
if (data.getKey() != null)
value.add(new PieChart.Data(data.getKey().name(), data.getValue()));
}
this.ratio.setData(value);
}
/**
* 集計する
*/
private void aggregate() {
String name = this.aggregateType.getSelectionModel()
.getSelectedItem();
ObservableList<BattleLogDetailAggregate> items = FXCollections.observableArrayList();
ObservableList<PieChart.Data> value = FXCollections.observableArrayList();
if (name != null) {
Function<BattleLogDetail, ?> getter = this.aggregateTypeMap.get(name);
Map<String, Long> result = this.filteredDetails.stream()
.map(getter)
.map(String::valueOf)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
double total = result.values().stream()
.mapToLong(Long::longValue)
.sum();
for (Entry<String, Long> entry : result.entrySet()) {
String type = entry.getKey();
if (type.isEmpty()) {
type = "(空白)";
}
// テーブル行
BattleLogDetailAggregate row = new BattleLogDetailAggregate();
row.setName(type);
row.setCount(entry.getValue());
row.setRatio(entry.getValue() / total * 100);
items.add(row);
// チャート
value.add(new PieChart.Data(type, entry.getValue()));
}
items.sort(Comparator.comparing(BattleLogDetailAggregate::getName));
value.sort(Comparator.comparing(PieChart.Data::getPieValue).reversed());
}
this.aggregate.setItems(items);
this.chart.setTitle(name);
this.chart.setData(value);
}