|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
29 | 29 | import java.util.List;
|
30 | 30 |
|
31 | 31 | import javafx.collections.FXCollections;
|
| 32 | +import javafx.scene.Scene; |
| 33 | +import javafx.scene.shape.MoveTo; |
| 34 | +import javafx.scene.shape.Path; |
| 35 | +import javafx.stage.Stage; |
32 | 36 | import org.junit.Test;
|
33 | 37 | import static org.junit.Assert.assertEquals;
|
34 | 38 | import javafx.collections.*;
|
@@ -271,4 +275,94 @@ public void testAddingMultipleSeriesWithDuplicateCategories() {
|
271 | 275 | assertEquals("5", categories.get(3));
|
272 | 276 | assertEquals("4", categories.get(4));
|
273 | 277 | }
|
| 278 | + |
| 279 | + @Test |
| 280 | + public void testTickMarksMatchBarPositionsAfterAnimation() { |
| 281 | + startApp(); |
| 282 | + CategoryAxis xAxis = new CategoryAxis(); |
| 283 | + NumberAxis yAxis = new NumberAxis(); |
| 284 | + BarChart<String, Number> chart = new BarChart<>(xAxis, yAxis); |
| 285 | + Series<String, Number> series = new Series<>(); |
| 286 | + chart.getData().add(series); |
| 287 | + chart.setAnimated(true); |
| 288 | + getTestScene().setRoot(chart); |
| 289 | + |
| 290 | + // add some categories, starting axis animation |
| 291 | + series.getData().add(new XYChart.Data<>("1", 1)); |
| 292 | + series.getData().add(new XYChart.Data<>("2", 2)); |
| 293 | + series.getData().add(new XYChart.Data<>("3", 3)); |
| 294 | + pulse(); |
| 295 | + // forward time until after animation is finished |
| 296 | + toolkit.setAnimationTime(1000); |
| 297 | + |
| 298 | + List<Node> bars = series.getData().stream().map(XYChart.Data::getNode).toList(); |
| 299 | + |
| 300 | + List<Double> barCenterXValues = series.getData().stream() |
| 301 | + .map(XYChart.Data::getNode) |
| 302 | + .map(bar -> bar.getLayoutX() + bar.getLayoutBounds().getCenterX()) |
| 303 | + .toList(); |
| 304 | + |
| 305 | + List<Double> tickXValues = xAxis.getChildrenUnmodifiable().stream() |
| 306 | + .filter(obj -> obj instanceof Path && obj.getStyleClass().contains("axis-tick-mark")) |
| 307 | + .flatMap(obj -> ((Path) obj).getElements().stream()) |
| 308 | + .filter(path -> path instanceof MoveTo) |
| 309 | + .map(moveTo -> ((MoveTo) moveTo).getX()) |
| 310 | + .toList(); |
| 311 | + |
| 312 | + double delta = 0.001; |
| 313 | + assertEquals(barCenterXValues.size(), tickXValues.size()); |
| 314 | + for (int i = 0; i < barCenterXValues.size(); i++) { |
| 315 | + assertEquals(barCenterXValues.get(i), tickXValues.get(i), delta); |
| 316 | + } |
| 317 | + } |
| 318 | + |
| 319 | + @Test |
| 320 | + public void testBarPositionsWithMultipleIncompleteSeries() { |
| 321 | + startApp(); |
| 322 | + CategoryAxis xAxis = new CategoryAxis(); |
| 323 | + NumberAxis yAxis = new NumberAxis(); |
| 324 | + BarChart<String, Number> chart = new BarChart<>(xAxis, yAxis); |
| 325 | + chart.setAnimated(false); |
| 326 | + chart.setBarGap(0.0); |
| 327 | + chart.setCategoryGap(0.0); |
| 328 | + getTestScene().setRoot(chart); |
| 329 | + |
| 330 | + XYChart.Series<String, Number> series1 = new XYChart.Series<>(); |
| 331 | + series1.setName("S1"); |
| 332 | + chart.getData().setAll(List.of(series1)); |
| 333 | + series1.getData().add(new XYChart.Data<>("1", 1)); |
| 334 | + series1.getData().add(new XYChart.Data<>("2", 2)); |
| 335 | + |
| 336 | + XYChart.Series<String, Number> series2 = new XYChart.Series<>(); |
| 337 | + series2.setName("S2"); |
| 338 | + series2.getData().add(new XYChart.Data<>("2", 3)); // duplicate category with series1 |
| 339 | + series2.getData().add(new XYChart.Data<>("3", 4)); // new category |
| 340 | + chart.getData().add(series2); |
| 341 | + |
| 342 | + pulse(); |
| 343 | + |
| 344 | + // check bar layout |
| 345 | + List<Node> s1bars = series1.getData().stream().map(XYChart.Data::getNode).toList(); |
| 346 | + List<Node> s2bars = series2.getData().stream().map(XYChart.Data::getNode).toList(); |
| 347 | + |
| 348 | + double x0 = s1bars.getFirst().getLayoutX(); |
| 349 | + double barWidth = s1bars.getFirst().getBoundsInLocal().getWidth(); |
| 350 | + |
| 351 | + // normalize bar positions with respect to the first bar position and width |
| 352 | + List<Double> normalized1 = s1bars.stream() |
| 353 | + .map(node -> (node.getLayoutX() - x0) / barWidth) |
| 354 | + .toList(); |
| 355 | + |
| 356 | + List<Double> normalized2 = s2bars.stream() |
| 357 | + .map(node -> (node.getLayoutX() - x0) / barWidth) |
| 358 | + .toList(); |
| 359 | + |
| 360 | + // expect even integers for series1 and odd integers for series2 |
| 361 | + double delta = 0.001; |
| 362 | + assertEquals(0, normalized1.get(0), delta); |
| 363 | + assertEquals(2, normalized1.get(1), delta); |
| 364 | + assertEquals(3, normalized2.get(0), delta); |
| 365 | + assertEquals(5, normalized2.get(1), delta); |
| 366 | + } |
| 367 | + |
274 | 368 | }
|
0 commit comments