Skip to content

Commit

Permalink
added category axis update
Browse files Browse the repository at this point in the history
  • Loading branch information
ennerf authored and wirew0rm committed Sep 27, 2023
1 parent 776a94f commit 9508d3c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
15 changes: 15 additions & 0 deletions chartfx-chart/src/main/java/io/fair_acc/chartfx/XYChart.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.fair_acc.bench.MeasurementRecorder;
import io.fair_acc.chartfx.axes.Axis;
import io.fair_acc.chartfx.axes.spi.AxisRange;
import io.fair_acc.chartfx.axes.spi.CategoryAxis;
import io.fair_acc.chartfx.plugins.ChartPlugin;
import io.fair_acc.chartfx.renderer.PolarTickStep;
import io.fair_acc.chartfx.renderer.Renderer;
Expand Down Expand Up @@ -228,6 +229,20 @@ public void updateAxisRange() {
if (changed && (axis.isAutoRanging() || axis.isAutoGrowRanging())) {
axis.invalidateRange();
}

// Feature for backwards compatibility: Category axes that do not have
// their categories set copy the categories of the first dataset of the
// first renderer that is using this axis.
if (axis instanceof CategoryAxis catAxis) {
for (Renderer renderer : getRenderers()) {
if (renderer.isUsingAxis(axis)) {
if (!renderer.getDatasets().isEmpty()) {
catAxis.updateCategories(renderer.getDatasets().get(0));
}
break;
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ default void updateAxes() {
// empty by default
}

/**
* Checks whether a renderer is actively using a given axis. The
* result is only valid after updateAxes has been called.
* <p>
* @param axis axis to be checked
* @return true if the renderer is actively using the given axis
*/
default boolean isUsingAxis(Axis axis) {
return getAxes().contains(axis);
}

/**
* Updates the range for the specified axis.
* Does nothing if the axis is not used.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.fair_acc.chartfx.XYChart;
import io.fair_acc.chartfx.axes.Axis;
import io.fair_acc.chartfx.axes.spi.AxisRange;
import io.fair_acc.chartfx.axes.spi.CategoryAxis;
import io.fair_acc.chartfx.ui.css.DataSetNode;
import io.fair_acc.dataset.DataSet;
import io.fair_acc.dataset.utils.AssertUtils;
Expand Down Expand Up @@ -85,6 +86,22 @@ public void updateAxes() {
if (yAxis == null) {
yAxis = chart.getYAxis();
}

// Update category axes (TODO: remove this API?)
if (!getDatasets().isEmpty()) {
var ds = getDatasets().get(0);
if (xAxis instanceof CategoryAxis xCat) {
xCat.updateCategories(ds);
}
if (yAxis instanceof CategoryAxis yCat) {
yCat.updateCategories(ds);
}
}
}

@Override
public boolean isUsingAxis(Axis axis) {
return axis == xAxis || axis == yAxis;
}

protected Axis ensureAxisInChart(Axis axis) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public void updateAxes() {
}
}

@Override
public boolean isUsingAxis(Axis axis) {
return super.isUsingAxis(axis) || axis == zAxis;
}

@Override
public void updateAxisRange(Axis axis, AxisRange range) {
super.updateAxisRange(axis, range);
Expand Down

0 comments on commit 9508d3c

Please sign in to comment.