Skip to content

Commit

Permalink
work in progress for stylable datasets that are part of the renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
ennerf committed Aug 11, 2023
1 parent ce9ba7f commit cc73fc5
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 7 deletions.
13 changes: 12 additions & 1 deletion chartfx-chart/src/main/java/io/fair_acc/chartfx/Chart.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.renderer.spi.AbstractRenderer;
import io.fair_acc.chartfx.ui.css.DataSetNode;
import io.fair_acc.chartfx.ui.css.StyleGroup;
import io.fair_acc.chartfx.ui.css.StyleUtil;
import io.fair_acc.chartfx.ui.layout.TitleLabel;
Expand Down Expand Up @@ -791,7 +792,18 @@ protected void datasetsChanged(final ListChangeListener.Change<? extends DataSet
set.addListener(dataSetState);
}
}

// set global indices
int i = datasets.size();
for (Renderer renderer : renderers) {
for (DataSetNode datasetNode : renderer.getDatasetNodes()) {
datasetNode.setGlobalIndex(i++);
}
}

// Rebuild legend (modifies SceneGraph and needs to be done before styling)
fireInvalidated(ChartBits.ChartLayout, ChartBits.ChartDataSets, ChartBits.ChartLegend);
updateLegend();
}

/**
Expand Down Expand Up @@ -844,7 +856,6 @@ protected void pluginsChanged(final ListChangeListener.Change<? extends ChartPlu
protected void rendererChanged(final ListChangeListener.Change<? extends Renderer> change) {
FXUtils.assertJavaFxThread();
while (change.next()) {
// TODO: how to work with renderer axes that are not in the SceneGraph? The length would never get set.

// handle added renderer
for (Renderer renderer : change.getAddedSubList()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import io.fair_acc.chartfx.ui.css.DataSetNode;
import javafx.beans.property.BooleanProperty;
import javafx.collections.ObservableList;
import javafx.scene.canvas.Canvas;
Expand Down Expand Up @@ -36,6 +37,8 @@ public interface Renderer {

ObservableList<DataSet> getDatasetsCopy();

ObservableList<DataSetNode> getDatasetNodes();

/**
* Optional method that allows the renderer make layout changes after axes and dataset limits are known
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import io.fair_acc.chartfx.Chart;
import io.fair_acc.chartfx.ui.css.CssPropertyFactory;
import io.fair_acc.chartfx.ui.css.DataSetNode;
import io.fair_acc.chartfx.ui.css.StyleUtil;
import io.fair_acc.chartfx.utils.PropUtil;
import io.fair_acc.dataset.events.ChartBits;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.css.CssMetaData;
import javafx.css.Styleable;
Expand All @@ -26,6 +28,7 @@

import java.util.List;
import java.util.function.IntSupplier;
import java.util.stream.Collectors;

/**
* @author rstein
Expand All @@ -35,12 +38,33 @@ public abstract class AbstractRenderer<R extends Renderer> extends Parent implem

protected final StyleableBooleanProperty showInLegend = css().createBooleanProperty(this, "showInLegend", true);
private final ObservableList<DataSet> datasets = FXCollections.observableArrayList();
private final ObservableList<DataSetNode> dataSetNodes = FXCollections.observableArrayList();
private final ObservableList<Axis> axesList = FXCollections.observableList(new NoDuplicatesList<>());
private final ObjectProperty<Chart> chart = new SimpleObjectProperty<>();

protected DataSetNode createNode(DataSet dataSet) {
// Reuse existing nodes when possible
for (DataSetNode dataSetNode : dataSetNodes) {
if (dataSetNode.getDataSet() == dataSet) {
return dataSetNode;
}
}
return new DataSetNode(dataSet);
}

public AbstractRenderer() {
StyleUtil.addStyles(this, "renderer");
PropUtil.runOnChange(() -> fireInvalidated(ChartBits.ChartLegend), showInLegend);
dataSetNodes.addListener((ListChangeListener<DataSetNode>) c -> {
getChildren().setAll(dataSetNodes);
});
datasets.addListener((ListChangeListener<DataSet>) c -> {
dataSetNodes.setAll(datasets.stream().distinct().map(this::createNode).collect(Collectors.toList()));
int i = 0;
for (DataSetNode dataSetNode : dataSetNodes) {
dataSetNode.setLocalIndex(i++);
}
});
}

@Override
Expand All @@ -53,6 +77,10 @@ public ObservableList<DataSet> getDatasets() {
return datasets;
}

public ObservableList<DataSetNode> getDatasetNodes() {
return dataSetNodes;
}

@Override
public ObservableList<DataSet> getDatasetsCopy() {
return getDatasetsCopy(getDatasets());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import java.util.Collections;
import java.util.List;

import io.fair_acc.chartfx.ui.css.CssPropertyFactory;
import io.fair_acc.chartfx.ui.css.LineStyle;
import io.fair_acc.chartfx.ui.css.StyleGroup;
import io.fair_acc.chartfx.ui.css.StyleUtil;
import io.fair_acc.chartfx.ui.css.*;
import javafx.beans.property.BooleanProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
Expand Down Expand Up @@ -267,12 +264,17 @@ public ObservableList<Axis> getAxes() {

@Override
public ObservableList<DataSet> getDatasets() {
return null;
return FXCollections.emptyObservableList();
}

@Override
public ObservableList<DataSet> getDatasetsCopy() {
return null;
return FXCollections.emptyObservableList();
}

@Override
public ObservableList<DataSetNode> getDatasetNodes() {
return FXCollections.emptyObservableList();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Collections;
import java.util.List;

import io.fair_acc.chartfx.ui.css.DataSetNode;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
Expand Down Expand Up @@ -165,6 +166,11 @@ public ObservableList<DataSet> getDatasetsCopy() {
return FXCollections.observableArrayList();
}

@Override
public ObservableList<DataSetNode> getDatasetNodes() {
return FXCollections.emptyObservableList();
}

protected List<DataSet> getDataSetsWithMetaData(List<DataSet> dataSets) {
final List<DataSet> list = new ArrayList<>();
for (final DataSet dataSet : dataSets) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package io.fair_acc.chartfx.ui.css;

import io.fair_acc.chartfx.XYChartCss;
import io.fair_acc.chartfx.utils.PropUtil;
import io.fair_acc.dataset.DataSet;
import io.fair_acc.dataset.event.EventSource;
import io.fair_acc.dataset.events.BitState;
import io.fair_acc.dataset.utils.AssertUtils;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.css.*;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Shape;

import java.util.List;

/**
* A dataset wrapper that lives in the SceneGraph for CSS styling
*
* @author ennerf
*/
public class DataSetNode extends TextStyle implements EventSource {

public DataSetNode(DataSet dataSet) {
StyleUtil.styleNode(this, "dataset");
this.dataSet = AssertUtils.notNull("dataSet", dataSet);
setVisible(dataSet.isVisible());
if (!PropUtil.isNullOrEmpty(dataSet.getStyle())) {
setStyle(dataSet.getStyle());
}
}

// Index within the renderer set
final IntegerProperty localIndex = new SimpleIntegerProperty();

// Index within all chart sets
final IntegerProperty globalIndex = new SimpleIntegerProperty();

// Offset for the color indexing
final StyleableIntegerProperty dsLayoutOffset = CSS.createIntegerProperty(this, XYChartCss.DATASET_LAYOUT_OFFSET, 0);

// A stylable local index. TODO: should this really be settable?
final StyleableIntegerProperty dsIndex = CSS.createIntegerProperty(this, XYChartCss.DATASET_INDEX, 0);

final StyleableDoubleProperty intensity = CSS.createDoubleProperty(this, XYChartCss.DATASET_INTENSITY, 100);
final StyleableBooleanProperty showInLegend = CSS.createBooleanProperty(this, XYChartCss.DATASET_SHOW_IN_LEGEND, true);

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

@Override
public BitState getBitState() {
return dataSet.getBitState();
}

public DataSet getDataSet() {
return dataSet;
}

public int getLocalIndex() {
return localIndex.get();
}

public IntegerProperty localIndexProperty() {
return localIndex;
}

public void setLocalIndex(int localIndex) {
this.localIndex.set(localIndex);
}

public int getGlobalIndex() {
return globalIndex.get();
}

public IntegerProperty globalIndexProperty() {
return globalIndex;
}

public void setGlobalIndex(int globalIndex) {
this.globalIndex.set(globalIndex);
}

public static List<CssMetaData<? extends Styleable, ?>> getClassCssMetaData() {
return CSS.getCssMetaData();
}

@Override
public List<CssMetaData<? extends Styleable, ?>> getCssMetaData() {
return getClassCssMetaData();
}

private final DataSet dataSet;

private static final CssPropertyFactory<DataSetNode> CSS = new CssPropertyFactory<>(Shape.getClassCssMetaData());

}

0 comments on commit cc73fc5

Please sign in to comment.