本文介绍了来自JavaFX的PieChart可以显示为甜甜圈吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用Java7构建的Java应用程序中使用 javafx.scene.chart.PieChart
。有没有办法让PieChart空心 - 把它变成甜甜圈?
I'm using javafx.scene.chart.PieChart
in my Java app which is built using Java7. Is there a way of making the PieChart hollow - turning it into a doughnut?
推荐答案
是的,您可以使用 DoughnutChart
类。
* DoughnutChart.java **
*DoughnutChart.java**
import javafx.collections.ObservableList;
import javafx.geometry.Bounds;
import javafx.scene.Node;
import javafx.scene.chart.PieChart;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
public class DoughnutChart extends PieChart {
private final Circle innerCircle;
public DoughnutChart(ObservableList<Data> pieData) {
super(pieData);
innerCircle = new Circle();
// just styled in code for demo purposes,
// use a style class instead to style via css.
innerCircle.setFill(Color.WHITESMOKE);
innerCircle.setStroke(Color.WHITE);
innerCircle.setStrokeWidth(3);
}
@Override
protected void layoutChartChildren(double top, double left, double contentWidth, double contentHeight) {
super.layoutChartChildren(top, left, contentWidth, contentHeight);
addInnerCircleIfNotPresent();
updateInnerCircleLayout();
}
private void addInnerCircleIfNotPresent() {
if (getData().size() > 0) {
Node pie = getData().get(0).getNode();
if (pie.getParent() instanceof Pane) {
Pane parent = (Pane) pie.getParent();
if (!parent.getChildren().contains(innerCircle)) {
parent.getChildren().add(innerCircle);
}
}
}
}
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);
}
}
DoughnutChartSample.java
import javafx.application.Application;
import javafx.collections.*;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class DoughnutChartSample extends Application {
@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));
}
public static void main(String[] args) {
launch(args);
}
}
这篇关于来自JavaFX的PieChart可以显示为甜甜圈吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!