Skip to content

Commit

Permalink
AbstractRendererXY: Update axesList of AbstractRenderer when changed (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Peter authored and RalphSteinhagen committed Nov 27, 2024
1 parent 8d15ad2 commit f8a5094
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public void render() {

@Override
public void updateAxes() {
final var xAxisBefore = xAxis;
final var yAxisBefore = yAxis;

// Default to explicitly set axes
xAxis = ensureAxisInChart(getFirstAxis(Orientation.HORIZONTAL));
yAxis = ensureAxisInChart(getFirstAxis(Orientation.VERTICAL));
Expand All @@ -95,6 +98,10 @@ public void updateAxes() {
if (yAxis instanceof CategoryAxis axis && !getDatasets().isEmpty()) {
axis.updateCategories(getDatasets().get(0));
}

if (xAxisBefore != xAxis || yAxisBefore != yAxis) {
getAxes().setAll(xAxis, yAxis);
}
}

protected Axis ensureAxisInChart(Axis axis) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package io.fair_acc.chartfx.plugins;

import static io.fair_acc.dataset.DataSet.DIM_X;
import static io.fair_acc.dataset.DataSet.DIM_Y;
import static org.testfx.util.NodeQueryUtils.hasText;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.testfx.api.FxAssert;
import org.testfx.api.FxRobot;
import org.testfx.framework.junit5.ApplicationExtension;
import org.testfx.framework.junit5.Start;
import org.testfx.util.DebugUtils;

import io.fair_acc.chartfx.XYChart;
import io.fair_acc.chartfx.axes.Axis;
import io.fair_acc.chartfx.axes.spi.DefaultNumericAxis;
import io.fair_acc.chartfx.renderer.spi.ErrorDataSetRenderer;
import io.fair_acc.chartfx.ui.geometry.Side;
import io.fair_acc.dataset.DataSet;
import io.fair_acc.dataset.testdata.spi.CosineFunction;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
* Test the DataPointTooltip plugin in the case that axis were only added to the chart and not the renderers.
*
* @author Benjamin Peter
*/
@ExtendWith(ApplicationExtension.class)
class DataPointTooltipAxisFromChartTest {

private XYChart chart;
private CosineFunction ds;
private Axis xAxis;
private Axis yAxis;

@Start
public void start(Stage stage) {
chart = new XYChart();
chart.getPlugins().add(new DataPointTooltip());
chart.setPrefWidth(400);
chart.setPrefHeight(300);

xAxis = new DefaultNumericAxis("xAxis", "t");
xAxis.setSide(Side.BOTTOM);
yAxis = new DefaultNumericAxis("yAxis", "m");
yAxis.setSide(Side.LEFT);
chart.getAxes().setAll(xAxis, yAxis);

final ErrorDataSetRenderer renderer = new ErrorDataSetRenderer();

ds = new CosineFunction("Cosine", 50);
renderer.getDatasets().add(ds);

chart.getRenderers().setAll(renderer);
Scene scene = new Scene(chart, 400, 300);
stage.setScene(scene);
stage.show();
}

@Test
void testThatTooltipIsShown(final FxRobot fxRobot) { // NOPMD JUnitTestsShouldIncludeAssert
fxRobot.interrupt();

fxRobot.moveTo(getPointOnDataSet(xAxis, yAxis, ds, 36)).moveBy(1, 0);
FxAssert.verifyThat(
"." + DataPointTooltip.STYLE_CLASS_LABEL,
hasText("'Cosine [36]'" + System.lineSeparator() + "36.0, 0.3090169943749491"),
DebugUtils.informedErrorMessage(fxRobot));
}

private Point2D getPointOnDataSet(final Axis xAxis, final Axis yAxis, final DataSet ds, final int index) {
return chart.getCanvas()
.localToScreen(
new Point2D(xAxis.getDisplayPosition(ds.get(DIM_X, index)),
yAxis.getDisplayPosition(ds.get(DIM_Y, index))));
}

}

0 comments on commit f8a5094

Please sign in to comment.