Skip to content

Commit

Permalink
made it more convenient to get to the style nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
ennerf committed Aug 17, 2023
1 parent b6ea8fa commit 4b40896
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 53 deletions.
53 changes: 19 additions & 34 deletions chartfx-chart/src/main/java/io/fair_acc/chartfx/Chart.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;

import io.fair_acc.chartfx.renderer.spi.AbstractRenderer;
import io.fair_acc.chartfx.renderer.spi.ErrorDataSetRenderer;
import io.fair_acc.chartfx.ui.css.ColorPalette;
import io.fair_acc.chartfx.ui.css.StyleGroup;
Expand Down Expand Up @@ -220,7 +219,6 @@ public Chart(Axis... axes) {
// Setup listeners
showing.bind(FXUtils.getShowingBinding(this));
getRenderers().addListener(rendererChangeListener);
getDatasets().addListener(datasetChangeListener);
getPlugins().addListener(pluginsChangedListener);
getAxes().addListener(axesChangeListenerLocal);
getAxes().addListener(axesChangeListener);
Expand Down Expand Up @@ -284,6 +282,9 @@ public Chart(Axis... axes) {
menuPane.setContent(measurementPane);
getChildren().add(menuPane);

// TODO: get rid of default instance. It's created if anyone wants to use getDatasets()
getRenderers().add(new ErrorDataSetRenderer());

}

@Override
Expand All @@ -310,17 +311,16 @@ public final BooleanProperty animatedProperty() {
}

/**
* @return datasets attached to the chart and datasets attached to all renderers
* @return datasets attached to all renderers
*/
public ObservableList<DataSet> getAllDatasets() {
if (getRenderers() == null) {
return allDataSets;
}

allDataSets.clear();
allDataSets.addAll(getDatasets());
getRenderers().stream().filter(renderer -> !(renderer instanceof LabelledMarkerRenderer)).forEach(renderer -> allDataSets.addAll(renderer.getDatasets()));

for (Renderer renderer : renderers) {
if (renderer instanceof LabelledMarkerRenderer) {
continue;
}
allDataSets.addAll(renderer.getDatasets());
}
return allDataSets;
}

Expand All @@ -343,16 +343,6 @@ public final Pane getCanvasForeground() {
return canvasForeground;
}

/**
* @return datasets of the first renderer. Creates a renderer if needed.
*/
public ObservableList<DataSet> getDatasets() {
if (getRenderers().isEmpty()) {
getRenderers().add(new ErrorDataSetRenderer());
}
return getRenderers().get(0).getDatasets();
}

public Axis getFirstAxis(final Orientation orientation) {
for (final Axis axis : getAxes()) {
if (axis.getSide() == null) {
Expand Down Expand Up @@ -513,7 +503,7 @@ protected void runPreLayout() {

// Update legend
if (state.isDirty(ChartBits.ChartLegend)) {
updateLegend(getDatasets(), getRenderers());
updateLegend(getRenderers());
}
state.clear(ChartBits.ChartLegend);

Expand Down Expand Up @@ -661,7 +651,7 @@ private void updateStandaloneRendererAxes() {
}
}

private void forEachDataSet(Consumer<DataSet> action) {
protected void forEachDataSet(Consumer<DataSet> action) {
for (Renderer renderer : renderers) {
for (DataSet dataset : renderer.getDatasets()) {
action.accept(dataset);
Expand Down Expand Up @@ -897,12 +887,10 @@ protected void rendererChanged(final ListChangeListener.Change<? extends Rendere
dataset.addListener(dataSetState);
}
renderer.getDatasets().addListener(datasetChangeListener);
if (renderer instanceof AbstractRenderer) {
var node = (AbstractRenderer<?>) renderer;
node.setChart(this);
if (!styleableNodes.getChildren().contains(node)) {
styleableNodes.getChildren().add(node);
}
renderer.setChart(this);
var node = renderer.getNode();
if (node != null && !styleableNodes.getChildren().contains(node)) {
styleableNodes.getChildren().add(node);
}
}

Expand All @@ -912,11 +900,8 @@ protected void rendererChanged(final ListChangeListener.Change<? extends Rendere
dataset.removeListener(dataSetState);
}
renderer.getDatasets().removeListener(datasetChangeListener);
if (renderer instanceof AbstractRenderer) {
var node = (AbstractRenderer<?>) renderer;
styleableNodes.getChildren().remove(node);
node.setChart(null);
}
styleableNodes.getChildren().remove(renderer.getNode());
renderer.setChart(null);
}

}
Expand All @@ -939,7 +924,7 @@ protected final boolean shouldAnimate() {
return isAnimated() && getScene() != null;
}

protected void updateLegend(final List<DataSet> dataSets, final List<Renderer> renderers) {
protected void updateLegend(final List<Renderer> renderers) {
final Legend legend = getLegend();
if (legend == null) {
return;
Expand Down
22 changes: 12 additions & 10 deletions chartfx-chart/src/main/java/io/fair_acc/chartfx/XYChart.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.stream.Collectors;

import io.fair_acc.chartfx.axes.spi.AxisRange;
import io.fair_acc.chartfx.renderer.spi.ErrorDataSetRenderer;
import io.fair_acc.chartfx.ui.css.DataSetNode;
import io.fair_acc.chartfx.utils.PropUtil;
import io.fair_acc.dataset.events.ChartBits;
Expand Down Expand Up @@ -97,6 +98,16 @@ public XYChart(final Axis... axes) {
getRenderers().addListener(this::rendererChanged);
}

/**
* @return datasets of the first renderer. Creates a renderer if needed.
*/
public ObservableList<DataSet> getDatasets() {
if (getRenderers().isEmpty()) {
getRenderers().add(new ErrorDataSetRenderer());
}
return getRenderers().get(0).getDatasets();
}

/**
* @return datasets attached to the chart and datasets attached to all renderers
*/
Expand All @@ -122,7 +133,6 @@ public ObservableList<DataSet> getAllDatasets() {
*/
public ObservableList<DataSet> getAllShownDatasets() {
final ObservableList<DataSet> ret = FXCollections.observableArrayList();
ret.addAll(getDatasets());
getRenderers().stream().filter(Renderer::showInLegend).forEach(renderer -> ret.addAll(renderer.getDatasets()));
return ret;
}
Expand Down Expand Up @@ -195,10 +205,6 @@ public void setPolarStepSize(final PolarTickStep step) {

@Override
public void updateAxisRange() {
if (isDataEmpty()) {
return;
}

// Check that all registered data sets have proper ranges defined. The datasets
// are already locked, so we can use parallel stream without extra synchronization.
getRenderers().stream()
Expand All @@ -213,11 +219,6 @@ public void updateAxisRange() {

// Update each of the axes
getAxes().forEach(chartAxis -> updateNumericAxis(chartAxis, getDataSetForAxis(chartAxis)));

}

private boolean isDataEmpty() {
return getAllDatasets() == null || getAllDatasets().isEmpty();
}

/**
Expand Down Expand Up @@ -366,4 +367,5 @@ protected static void updateNumericAxis(final Axis axis, final List<DataSetNode>
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Map;
import java.util.Optional;

import io.fair_acc.chartfx.XYChart;
import io.fair_acc.dataset.events.ChartBits;
import io.fair_acc.dataset.events.StateListener;
import javafx.beans.value.ChangeListener;
Expand All @@ -27,6 +28,7 @@
* updated, if there is exactly one DataSet in the each Renderer or the Chart.
*
* TODO: revisit this plugin. we should be able to turn this into a single chart listener and an update method (ennerf)
* TODO: this is using Chart::getDataSets() which doesn't really exist anymore
*
* @author akrimm
*/
Expand Down Expand Up @@ -69,8 +71,8 @@ public class UpdateAxisLabels extends ChartPlugin {

// called whenever the chart for the plugin is changed
private final ChangeListener<? super Chart> chartChangeListener = (change, oldChart, newChart) -> {
removeRendererAndDataSetListener(oldChart);
addRendererAndDataSetListener(newChart);
removeRendererAndDataSetListener((XYChart) oldChart);
addRendererAndDataSetListener((XYChart) newChart);
};

/**
Expand All @@ -79,10 +81,10 @@ public class UpdateAxisLabels extends ChartPlugin {
public UpdateAxisLabels() {
super();
chartProperty().addListener(chartChangeListener);
addRendererAndDataSetListener(getChart());
addRendererAndDataSetListener(getXYChart());
}

private void addRendererAndDataSetListener(Chart newChart) {
private void addRendererAndDataSetListener(XYChart newChart) {
if (newChart == null) {
return;
}
Expand All @@ -91,10 +93,14 @@ private void addRendererAndDataSetListener(Chart newChart) {
newChart.getRenderers().forEach((Renderer r) -> setupDataSetListeners(r, r.getDatasets()));
}

private XYChart getXYChart() {
return (XYChart) super.getChart();
}

// the actual DataSet renaming logic
private void dataSetChange(DataSet dataSet, Renderer renderer) {
if (renderer == null) { // dataset was added to / is registered at chart
if (getChart().getDatasets().size() == 1) {
if (getXYChart().getDatasets().size() == 1) {
for (int dimIdx = 0; dimIdx < dataSet.getDimension(); dimIdx++) {
final int dimIndex = dimIdx;
Optional<Axis> oldAxis = getChart().getAxes().stream().filter(axis -> axis.getDimIndex() == dimIndex).findFirst();
Expand Down Expand Up @@ -156,7 +162,7 @@ private void dataSetsChanged(ListChangeListener.Change<? extends DataSet> change
}
}

private void removeRendererAndDataSetListener(Chart oldChart) {
private void removeRendererAndDataSetListener(XYChart oldChart) {
if (oldChart == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package io.fair_acc.chartfx.renderer;

import io.fair_acc.chartfx.Chart;
import io.fair_acc.chartfx.ui.css.DataSetNode;
import javafx.beans.property.BooleanProperty;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.canvas.Canvas;

import io.fair_acc.chartfx.axes.Axis;
import io.fair_acc.dataset.DataSet;

import java.util.ArrayList;
import java.util.List;

/**
* -- generic renderer interface --
*
Expand Down Expand Up @@ -35,6 +40,28 @@ default boolean drawLegendSymbol(DataSetNode dataSet, Canvas canvas) {

ObservableList<DataSetNode> getDatasetNodes();

default DataSetNode getStyleableNode(DataSet dataSet) {
for (DataSetNode datasetNode : getDatasetNodes()) {
if (datasetNode.getDataSet() == dataSet) {
return datasetNode;
}
}
throw new IllegalArgumentException("dataset does not have a styleable node");
}

default DataSetNode addDataSet(DataSet dataSet) {
getDatasets().add(dataSet);
return getStyleableNode(dataSet);
}

default List<DataSetNode> addDataSets(DataSet... dataSets) {
List<DataSetNode> retVal = new ArrayList<>(dataSets.length);
for (DataSet dataSet : dataSets) {
retVal.add(addDataSet(dataSet));
}
return retVal;
}

/**
* Optional method that allows the renderer make layout changes after axes and dataset limits are known.
* Gets called after axis ranges are known
Expand Down Expand Up @@ -93,4 +120,13 @@ default boolean isUsingAxis(Axis axis) {
void setGlobalIndexOffset(int value);
int getGlobalIndexOffset();

default void setChart(Chart chart) {
// do nothing if it's not needed
}

default Node getNode() {
// add nothing if not needed
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javafx.css.Styleable;
import javafx.css.StyleableBooleanProperty;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.Parent;

import java.util.List;
Expand All @@ -48,7 +49,7 @@ public abstract class AbstractRenderer<R extends Renderer> extends Parent implem
protected DataSetNode createNode(DataSet dataSet) {
// Reuse existing nodes when possible
for (DataSetNode dataSetNode : dataSetNodes) {
if (dataSetNode.getDataSet() == dataSet) {
if(dataSetNode.getDataSet() == dataSet) {
return dataSetNode;
}
}
Expand Down Expand Up @@ -278,6 +279,11 @@ protected void fireInvalidated(IntSupplier bit) {
}
}

@Override
public Node getNode() {
return this;
}

protected CssPropertyFactory<AbstractRenderer<?>> css() {
return CSS; // subclass specific CSS due to inheritance issues otherwise
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void updateAxisRange() {
}

@Override
protected void updateLegend(final List<DataSet> dataSets, final List<Renderer> renderers) {
protected void updateLegend(final List<Renderer> renderers) {
// TODO Auto-generated method stub
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private static DataSet generateData() {
}

private static Chart getTestChart(final String title, final DataSet testDataSet) {
final Chart chart = new XYChart();
final var chart = new XYChart();
chart.setTitle(title);
chart.setLegendVisible(false);
chart.getDatasets().add(testDataSet);
Expand Down

0 comments on commit 4b40896

Please sign in to comment.