Skip to content

Commit

Permalink
cleaned up auto range code
Browse files Browse the repository at this point in the history
  • Loading branch information
ennerf committed Jul 17, 2023
1 parent b8cace0 commit deb08f6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 39 deletions.
51 changes: 25 additions & 26 deletions chartfx-chart/src/main/java/io/fair_acc/chartfx/XYChart.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import io.fair_acc.chartfx.axes.spi.AxisRange;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Platform;
Expand Down Expand Up @@ -418,52 +419,50 @@ protected static void updateNumericAxis(final Axis axis, final List<DataSet> dat
if (dataSets == null || dataSets.isEmpty()) {
return;
}
final boolean oldAutoState = axis.autoNotification().getAndSet(false);
final double oldMin = axis.getAutoRange().getMin();
final double oldMax = axis.getAutoRange().getMax();
final double oldLength = axis.getLength();

final boolean isHorizontal = axis.getSide().isHorizontal();
final boolean oldAutoState = axis.autoNotification().getAndSet(false);
final Side side = axis.getSide();
axis.getAutoRange().clear();
final boolean isHorizontal = side.isHorizontal();

// Determine the range of all datasets for this axis
final AxisRange dsRange = new AxisRange();
dsRange.clear();
dataSets.stream().filter(DataSet::isVisible).forEach(dataset -> dataset.lock().readLockGuard(() -> {
if (dataset.getDimension() > 2 && (side == Side.RIGHT || side == Side.TOP)) {
if (!dataset.getAxisDescription(DataSet.DIM_Z).isDefined()) {
dataset.recomputeLimits(DataSet.DIM_Z);
}
axis.getAutoRange().add(dataset.getAxisDescription(DataSet.DIM_Z).getMin());
axis.getAutoRange().add(dataset.getAxisDescription(DataSet.DIM_Z).getMax());
dsRange.add(dataset.getAxisDescription(DataSet.DIM_Z).getMin());
dsRange.add(dataset.getAxisDescription(DataSet.DIM_Z).getMax());
} else {
final int nDim = isHorizontal ? DataSet.DIM_X : DataSet.DIM_Y;
if (!dataset.getAxisDescription(nDim).isDefined()) {
dataset.recomputeLimits(nDim);
}
axis.getAutoRange().add(dataset.getAxisDescription(nDim).getMin());
axis.getAutoRange().add(dataset.getAxisDescription(nDim).getMax());
dsRange.add(dataset.getAxisDescription(nDim).getMin());
dsRange.add(dataset.getAxisDescription(nDim).getMax());
}
}));

// handling of numeric axis and auto-range or auto-grow setting only
if (!axis.isAutoRanging() && !axis.isAutoGrowRanging()) {
if (oldMin != axis.getMin() || oldMax != axis.getMax() || oldLength != axis.getLength()) {
axis.requestAxisLayout();
}
axis.autoNotification().set(oldAutoState);
return;
}

// Update the auto range
final boolean changed;
if (axis.isAutoGrowRanging()) {
axis.getAutoRange().add(oldMin);
axis.getAutoRange().add(oldMax);
changed = axis.getAutoRange().add(dsRange);
} else {
changed = axis.getAutoRange().set(dsRange);
}

axis.getAutoRange().setAxisLength(axis.getLength() == 0 ? 1 : axis.getLength(), side);
axis.getUserRange().setAxisLength(axis.getLength() == 0 ? 1 : axis.getLength(), side);
axis.invalidateRange(null);

if (oldMin != axis.getMin() || oldMax != axis.getMax() || oldLength != axis.getLength()) {
// Trigger a redraw
if (changed && (axis.isAutoRanging() || axis.isAutoGrowRanging())) {
axis.invalidateRange();
axis.requestAxisLayout();
}

// TODO: is this used for anything? can it be removed?
double axisLength = axis.getLength() == 0 ? 1 : axis.getLength();
axis.getAutoRange().setAxisLength(axisLength, side);
axis.getUserRange().setAxisLength(axisLength, side);

axis.autoNotification().set(oldAutoState);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.fair_acc.chartfx.axes;

import java.util.List;

import javafx.beans.property.*;
import javafx.collections.ObservableList;
import javafx.scene.canvas.Canvas;
Expand Down Expand Up @@ -175,13 +173,11 @@ public interface Axis extends AxisDescription {
double getZeroPosition();

/**
* Called when data has changed and the range may not be valid any more. This is only called by the chart if
* Called when data has changed and the range may not be valid anymore. This is only called by the chart if
* isAutoRanging() returns true. If we are auto ranging it will cause layout to be requested and auto ranging to
* happen on next layout pass.
*
* @param data The current set of all data that needs to be plotted on this axis
*/
void invalidateRange(List<Number> data);
void invalidateRange();

/**
* This is {@code true} when the axis labels and data point order should be inverted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.List;
import java.util.Map;

import io.fair_acc.chartfx.ui.utils.PostLayoutHook;
import javafx.application.Platform;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
Expand Down Expand Up @@ -317,11 +316,9 @@ public void invalidateCaches() {
* Called when data has changed and the range may not be valid anymore. This is only called by the chart if
* isAutoRanging() returns true. If we are auto ranging it will cause layout to be requested and auto ranging to
* happen on next layout pass.
*
* @param data The current set of all data that needs to be plotted on this axis N.B. not needed anymore now stored in getAutoRange()
*/
@Override
public void invalidateRange(final List<Number> data) {
public void invalidateRange() {
final boolean oldState = autoNotification().getAndSet(false);
final AxisRange autoRange = autoRange(getLength()); // derived axes may potentially pad and round limits
if (set(autoRange.getMin(), autoRange.getMax())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,11 @@ public void drawAxis(GraphicsContext gc, double axisWidth, double axisHeight) {
// deliberately not implemented
}

@Override
public void drawAxis() {
// deliberately not implemented
}

@Override
public AxisRange getRange() {
return null;
Expand Down Expand Up @@ -377,7 +382,7 @@ public double getZeroPosition() {
}

@Override
public void invalidateRange(List<Number> data) {
public void invalidateRange() {
// deliberately not implemented
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ void testAutoRange() {

Assertions.assertDoesNotThrow(() -> axis.computeTickMarks(autoRange, false));

List<Number> numberList = Collections.unmodifiableList(axis.calculateMajorTickValues(autoRange));
axis.invalidateRange(new ArrayList<>(numberList));
axis.invalidateRange();
}

@Test
Expand Down

0 comments on commit deb08f6

Please sign in to comment.