我试图在饼图的每个切片上定位百分比,但是我无法将它们放在正确的位置。我只找到了鼠标悬停在鼠标位置标注标签的示例,但是我希望保持始终可见的百分比。这里有什么问题?
这是我的代码:
public class PieChartExperiments extends Application implements {
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("StackedBarChart Experiments");
Map<String, Double> pieChartData = new TreeMap<String, Double>(new SeriesComparator());
double click = 75;
double clickAndCredential = 16.67F;
double clickOnly = click - clickAndCredential;
double noClick = 100 - click;
pieChartData.put(JUST_CLICK_LABEL, clickOnly);
pieChartData.put(CREDENTIAL_LABEL, clickAndCredential);
pieChartData.put(NO_CLICK_LABEL, noClick);
CategoryAxis xAxis = new CategoryAxis();
// xAxis.setLabel("Devices");
NumberAxis yAxis = new NumberAxis();
// yAxis.setLabel("Visits");
List<PieChart.Data> pieChartDataList = pieChartData.entrySet().stream()
.map(e -> new PieChart.Data(e.getKey(), e.getValue())).collect(Collectors.toList());
ObservableList<PieChart.Data> pieChartDataOL = FXCollections.observableArrayList(pieChartDataList);
/*
* Create the PieChart object
*/
final PieChart pieChart = new PieChart(pieChartDataOL);
pieChart.setTitle("Overall Click Results");
pieChart.setLegendSide(Side.TOP);
VBox vbox = new VBox(pieChart);
Scene scene = new Scene(vbox, 1200, 720);
System.out.println("Resource: " + this.getClass().getResource("/javafx/style/PieChart.css"));
scene.getStylesheets().add(this.getClass().getResource("/javafx/style/PieChart.css").toExternalForm());
// SET THE PIE SLICES COLOR AFTER CREATING THE SCENE
for (Data data : pieChart.getData()) {
Node node = data.getNode();
String dataName = data.getName();
double percentage = data.getPieValue();
double x = node.getLayoutX();
double y = node.getLayoutY();
Label label = new Label(String.format("%.2f", percentage) + "%");
label.setLabelFor(node);
label.setLayoutX(x);
label.setLayoutY(y);
vbox.getChildren().add(label);
}
primaryStage.setScene(scene);
primaryStage.setHeight(720);
primaryStage.setWidth(1200);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}