1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| package piecolor;
import java.util.List;
import java.util.stream.IntStream;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Paint;
import javafx.scene.paint.RadialGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Rectangle;
import javafx.stage.Modality;
import javafx.stage.Stage;
public final class Main extends Application {
final static int MIN_SERIES_NUMBER = 2;
final static int MAX_SERIES_NUMBER = 5;
final static int MAX_SERIES_VALUE = 10;
private PieChart createChart() {
final int seriesCount = (int) Math.max(MIN_SERIES_NUMBER, MAX_SERIES_NUMBER * Math.random());
final PieChart chart = new PieChart();
IntStream.range(0, seriesCount)
.mapToObj(index -> {
final String name = String.format("Serie %d", index + 1);
final double value = MAX_SERIES_VALUE * Math.random();
return new PieChart.Data(name, value);
})
.forEach(chart.getData()::add);
return chart;
}
private PieChart pieChart;
@Override
public void start(final Stage primaryStage) {
pieChart = createChart();
final StackPane root = new StackPane();
root.getChildren().add(pieChart);
final Scene scene = new Scene(root, 600, 600);
primaryStage.setTitle("Test");
primaryStage.setScene(scene);
primaryStage.show();
Platform.runLater(this::findOutBackgrounds);
// ScenicView.show(scene);
}
private void findOutBackgrounds() {
IntStream.range(0, pieChart.getData().size())
.forEach(this::findOutBackground);
}
private void findOutBackground(int seriesIndex) {
final String selector = String.format(".data%d", seriesIndex);
final Node node1 = pieChart.lookup(selector);
final Node node2 = pieChart.getData().get(seriesIndex).getNode();
assert (node1 != null);
assert (node1 == node2);
assert (node1 instanceof Region);
Region region = (Region) node1;
final Background background = region.getBackground();
final Region backgroundArea = new Region();
VBox.setVgrow(backgroundArea, Priority.ALWAYS);
backgroundArea.setMinSize(500, 500);
backgroundArea.setBackground(background);
Tooltip.install(backgroundArea, new Tooltip("Series' background"));
//
final VBox root = new VBox();
root.setFillWidth(true);
final HBox fillRow = new HBox();
VBox.setVgrow(fillRow, Priority.NEVER);
final int fillNumber = background.getFills().size();
IntStream.range(0, fillNumber)
.mapToObj(fillIndex -> {
final BackgroundFill fill = background.getFills().get(fillIndex);
final Paint paint = fill.getFill();
System.out.println(paint.getClass());
final Region fillArea = new Region();
fillArea.setMinSize(50, 50);
fillArea.setBackground(new Background(new BackgroundFill(paint, CornerRadii.EMPTY, Insets.EMPTY)));
Tooltip.install(fillArea, new Tooltip(String.format("Fill #%d", fillIndex)));
HBox.setHgrow(fillArea, Priority.NEVER);
return fillArea;
})
.forEach(fillRow.getChildren()::add);
final Region filler1 = new Region();
HBox.setHgrow(filler1, Priority.ALWAYS);
fillRow.getChildren().add(filler1);
root.getChildren().addAll(backgroundArea, fillRow);
//
IntStream.range(0, fillNumber)
.mapToObj(fillIndex -> {
final BackgroundFill fill = background.getFills().get(fillIndex);
final Paint paint = fill.getFill();
final HBox colorRow = new HBox();
VBox.setVgrow(colorRow, Priority.NEVER);
colorRow.getChildren().add(new Label(String.format("Fill #%d", fillIndex)));
if (paint instanceof Color) {
final Rectangle colorArea = new Rectangle(25, 25);
colorArea.setFill(paint);
Tooltip.install(colorArea, new Tooltip("Single color"));
colorRow.getChildren().add(colorArea);
} else if (paint instanceof LinearGradient || paint instanceof RadialGradient) {
final List<Stop> stops = (paint instanceof LinearGradient) ? ((LinearGradient) paint).getStops() : ((RadialGradient) paint).getStops();
final String gradientType = (paint instanceof LinearGradient) ? "Linear" : "Radial";
final int stopNumber = stops.size();
IntStream.range(0, stopNumber)
.mapToObj(stopIndex -> {
final Stop stop = stops.get(stopIndex);
final Color color = stop.getColor();
final Rectangle colorArea = new Rectangle(25, 25);
colorArea.setFill(color);
Tooltip.install(colorArea, new Tooltip(String.format("%s color #%d", gradientType, stopIndex)));
return colorArea;
})
.forEach(colorRow.getChildren()::add);
}
return colorRow;
})
.forEach(root.getChildren()::add);
final String title = String.format("%d - %s", seriesIndex, pieChart.getData().get(seriesIndex).getName());
final Dialog dialog = new Dialog();
dialog.setTitle(title);
dialog.getDialogPane().setContent(root);
dialog.initModality(Modality.NONE);
dialog.initOwner(region.getScene().getWindow());
dialog.show();
}
public static void main(String[] args) {
launch(args);
}
} |
Partager