Skip to content

Commit

Permalink
Fix Indicators
Browse files Browse the repository at this point in the history
* put indicators into separate packages
* move ParameterMeasurements into measurement package
* cleanup Chart, remove old comments, initializers, handler variables
* Put plugin children into pane instead of group to fix coordinate transformations
* fix typos
  • Loading branch information
wirew0rm committed Aug 9, 2023
1 parent 9958665 commit dc89e01
Show file tree
Hide file tree
Showing 42 changed files with 96 additions and 152 deletions.
85 changes: 31 additions & 54 deletions chartfx-chart/src/main/java/io/fair_acc/chartfx/Chart.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
import javafx.scene.paint.Paint;
import javafx.util.Duration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.fair_acc.chartfx.axes.Axis;
import io.fair_acc.chartfx.axes.spi.AbstractAxis;
import io.fair_acc.chartfx.axes.spi.DefaultNumericAxis;
Expand Down Expand Up @@ -81,24 +78,20 @@ public abstract class Chart extends Region implements EventSource {
protected final BitState dataSetState = BitState.initDirtyMultiThreaded(this, BitState.ALL_BITS)
.addChangeListener(FXUtils.runOnFxThread((src, deltaBits) -> state.setDirty(src.getBits())));

private static final Logger LOGGER = LoggerFactory.getLogger(Chart.class);
private static final String CHART_CSS = Objects.requireNonNull(Chart.class.getResource("chart.css")).toExternalForm();
private static final CssPropertyFactory<Chart> CSS = new CssPropertyFactory<>(Control.getClassCssMetaData());
private static final int DEFAULT_TRIGGER_DISTANCE = 50;
protected static final boolean DEBUG = Boolean.getBoolean("chartfx.debug"); // for more verbose debugging

protected final BooleanProperty showing = new SimpleBooleanProperty(this, "showing", false);
{
showing.bind(FXUtils.getShowingBinding(this));
}

/**
* When true any data changes will be animated.
*/
private final BooleanProperty animated = new SimpleBooleanProperty(this, "animated", true);
// TODO: Check whether 'this' or chart contents need to be added
/**
* Animator for animating stuff on the chart
* TODO: Check whether 'this' or chart contents need to be added
*/
protected final ChartLayoutAnimator animator = new ChartLayoutAnimator(this);

Expand All @@ -113,14 +106,11 @@ public abstract class Chart extends Region implements EventSource {
private final ObservableList<DataSet> datasets = FXCollections.observableArrayList();
protected final ObservableList<DataSet> allDataSets = FXCollections.observableArrayList();
private final ObservableList<Renderer> renderers = FXCollections.observableArrayList();
{
getRenderers().addListener(this::rendererChanged);
}

// Inner canvas for the drawn content
protected final ResizableCanvas canvas = new ResizableCanvas();
protected final Pane canvasForeground = new Pane();
protected final Group pluginsArea = Chart.createChildGroup();
protected final Pane pluginsArea = new Pane();

// Area where plots get drawn
protected final Pane plotBackground = new Pane();
Expand All @@ -137,37 +127,6 @@ public abstract class Chart extends Region implements EventSource {
protected final ToolBarFlowPane toolBar = new ToolBarFlowPane(this);
protected final BooleanProperty toolBarPinned = new SimpleBooleanProperty(this, "toolBarPinned", false);

{
// Build hierarchy
// > menuPane (hidden toolbars that slide in from top/bottom)
// > measurement pane (labels/menus for working with data)
// > legend & title pane (static legend and title)
// > axis pane (x/y axes)
// > axes
// > plot area (plotted content, hidden elements for zoom etc.)
// > canvas (main)
// > canvas foreground
// > plugins
// > plot background/foreground
plotArea.setContent(StyleUtil.addStyles(new PlotAreaPane(getCanvas(), getCanvasForeground(), pluginsArea), "chart-plot-area"));
axesAndCanvasPane.addCenter(getPlotBackground(), getPlotArea(), getPlotForeground());
titleLegendPane.addCenter(axesAndCanvasPane);
measurementPane.addCenter(titleLegendPane);
menuPane.setContent(measurementPane);
getChildren().add(menuPane);
}

protected final ListChangeListener<Axis> axesChangeListenerLocal = this::axesChangedLocal;
protected final ListChangeListener<Axis> axesChangeListener = this::axesChanged;
protected final ListChangeListener<DataSet> datasetChangeListener = this::datasetsChanged;
protected final ListChangeListener<ChartPlugin> pluginsChangedListener = this::pluginsChanged;

{
getDatasets().addListener(datasetChangeListener);
getAxes().addListener(axesChangeListener);
getAxes().addListener(axesChangeListenerLocal);
}

protected final Label titleLabel = new Label();

protected final StringProperty title = new StringPropertyBase() {
Expand Down Expand Up @@ -245,7 +204,7 @@ protected void invalidated() {
private final StyleableObjectProperty<Side> toolBarSide = CSS.createObjectProperty(this, "toolBarSide", Side.TOP, false,
StyleConverter.getEnumConverter(Side.class), (oldVal, newVal) -> {
AssertUtils.notNull("Side must not be null", newVal);
// remove tool bar from potential other chart side pane locations
// remove toolbar from potential other chart side pane locations
menuPane.setTop(null);
menuPane.setBottom(null);
menuPane.setLeft(null);
Expand Down Expand Up @@ -278,6 +237,30 @@ protected void invalidated() {
* @param axes axes to be added to the chart
*/
public Chart(Axis... axes) {
// Build hierarchy
// > menuPane (hidden toolbars that slide in from top/bottom)
// > measurement pane (labels/menus for working with data)
// > legend & title pane (static legend and title)
// > axis pane (x/y axes)
// > axes
// > plot area (plotted content, hidden elements for zoom etc.)
// > canvas (main)
// > canvas foreground
// > plugins
// > plot background/foreground
plotArea.setContent(StyleUtil.addStyles(new PlotAreaPane(getCanvas(), getCanvasForeground(), pluginsArea), "chart-plot-area"));
axesAndCanvasPane.addCenter(getPlotBackground(), getPlotArea(), getPlotForeground());
titleLegendPane.addCenter(axesAndCanvasPane);
measurementPane.addCenter(titleLegendPane);
menuPane.setContent(measurementPane);
getChildren().add(menuPane);
showing.bind(FXUtils.getShowingBinding(this));

getDatasets().addListener(this::datasetsChanged);
getAxes().addListener(this::axesChanged);
getAxes().addListener(this::axesChangedLocal);
getRenderers().addListener(this::rendererChanged);

for (int dim = 0; dim < axes.length; dim++) {
final Axis axis = axes[dim];
if (!(axis instanceof AbstractAxis)) {
Expand All @@ -294,16 +277,10 @@ public Chart(Axis... axes) {
plotForeGround.toFront();
plotForeGround.setMouseTransparent(true);

// hiddenPane.setTriggerDistance(DEFAULT_TRIGGER_DISTANCE);
plotArea.triggerDistanceProperty().bindBidirectional(menuPane.triggerDistanceProperty());
plotArea.setAnimationDelay(Duration.millis(500));
// hiddenPane.setMouseTransparent(true);
plotArea.setPickOnBounds(false);

// alt: canvas resize (default JavaFX Canvas does not automatically
// resize to pref width/height according to parent constraints
// canvas.widthProperty().bind(stackPane.widthProperty());
// canvas.heightProperty().bind(stackPane.heightProperty());
getCanvasForeground().setManaged(false);
final ChangeListener<Number> canvasSizeChangeListener = (ch, o, n) -> {
final double width = getCanvas().getWidth();
Expand Down Expand Up @@ -335,7 +312,7 @@ public Chart(Axis... axes) {
canvas.setStyle("-fx-background-color: rgba(200, 250, 200, 0.5);");

// add plugin handling and listeners
getPlugins().addListener(pluginsChangedListener);
getPlugins().addListener(this::pluginsChanged);

// add default chart content ie. ToolBar and Legend
// can be repositioned via setToolBarSide(...) and setLegendSide(...)
Expand Down Expand Up @@ -375,7 +352,7 @@ public String getUserAgentStylesheet() {
}

/**
* Play a animation involving the given keyframes. On every frame of the animation the chart will be relayed out
* Play an animation involving the given keyframes. On every frame of the animation the chart will be relayed out
*
* @param keyFrames Array of KeyFrames to play
*/
Expand Down Expand Up @@ -939,15 +916,15 @@ protected void rendererChanged(final ListChangeListener.Change<? extends Rendere
for (DataSet dataset : renderer.getDatasets()) {
dataset.addListener(dataSetState);
}
renderer.getDatasets().addListener(datasetChangeListener);
renderer.getDatasets().addListener(this::datasetsChanged);
}

// handle removed renderer
for (Renderer renderer : change.getRemoved()) {
for (DataSet dataset : renderer.getDatasets()) {
dataset.removeListener(dataSetState);
}
renderer.getDatasets().removeListener(datasetChangeListener);
renderer.getDatasets().removeListener(this::datasetsChanged);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
import io.fair_acc.chartfx.renderer.Renderer;
import io.fair_acc.chartfx.utils.FXUtils;
import io.fair_acc.dataset.DataSet;
import io.fair_acc.dataset.event.AxisChangeEvent;
import io.fair_acc.dataset.event.EventListener;
import io.fair_acc.dataset.event.EventRateLimiter;

/**
* This plugin updates the labels (name and unit) of all axes according to DataSet Metadata. For now the axes are only
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.fair_acc.chartfx.plugins;
package io.fair_acc.chartfx.plugins.indicators;

import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
/*
* Copyright (c) 2017 European Organisation for Nuclear Research (CERN), All Rights Reserved.
*/

package io.fair_acc.chartfx.plugins;
package io.fair_acc.chartfx.plugins.indicators;

import io.fair_acc.chartfx.utils.PropUtil;
import io.fair_acc.dataset.events.BitState;
Expand All @@ -23,8 +19,7 @@
*
* @author mhrabia
*/
public abstract class AbstractSingleValueIndicator extends AbstractValueIndicator
implements EventSource, ValueIndicator {
public abstract class AbstractSingleValueIndicator extends AbstractValueIndicator implements EventSource, ValueIndicator {
/**
* The default distance between the data point coordinates and mouse cursor that triggers shifting the line.
*/
Expand All @@ -47,8 +42,7 @@ public abstract class AbstractSingleValueIndicator extends AbstractValueIndicato
* small triangle marker as handler to shift the line marker
*/
protected final Polygon triangle = new Polygon();
private final DoubleProperty pickingDistance = new SimpleDoubleProperty(this, "pickingDistance",
DEFAULT_PICKING_DISTANCE) {
private final DoubleProperty pickingDistance = new SimpleDoubleProperty(this, "pickingDistance", DEFAULT_PICKING_DISTANCE) {
@Override
protected void invalidated() {
if (get() <= 0) {
Expand Down Expand Up @@ -78,7 +72,7 @@ protected void invalidated() {
* Creates a new instance of AbstractSingleValueIndicator.
*
* @param axis reference axis
* @param value a X value to be indicated
* @param value a value to be indicated
* @param text the text to be shown by the label. Value of {@link #textProperty()}.
*/
protected AbstractSingleValueIndicator(Axis axis, final double value, final String text) {
Expand All @@ -92,16 +86,14 @@ protected AbstractSingleValueIndicator(Axis axis, final double value, final Stri
editableIndicatorProperty().addListener((ch, o, n) -> updateMouseListener(n));
updateMouseListener(isEditable());

// remove triangle from chart foregrond when the chart is removed
// remove triangle from chart foreground when the chart is removed
chartProperty().addListener((p, o, n) -> {
if (o != null) {
o.getPlotForeground().getChildren().remove(triangle);
}
});

// Need to add them so that at initialization of the stage the CCS is
// applied and we can calculate label's
// width and height
// Need to add them so that at initialization of the stage the CCS is applied, and we can calculate label's width and height
getChartChildren().addAll(line, label);
PropUtil.runOnChange(state.onAction(ChartBits.ChartPluginState), this.value);
}
Expand Down Expand Up @@ -235,8 +227,7 @@ protected void layoutMarker(final double startX, final double startY, final doub
triangle.setTranslateY(startY);
triangle.toFront();
// triangle has to be put onto the plot foreground to be able to put it on top of axes
// is removed when the chart is changed
// addChildNodeIfNotPresent(triangle);
// it is removed when the chart is changed
if (!getChart().getPlotForeground().getChildren().contains(triangle)) {
getChart().getPlotForeground().getChildren().add(triangle);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
/*
* Copyright (c) 2017 European Organisation for Nuclear Research (CERN), All Rights Reserved.
*/

package io.fair_acc.chartfx.plugins;
package io.fair_acc.chartfx.plugins.indicators;

import io.fair_acc.chartfx.plugins.ChartPlugin;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.collections.ListChangeListener;
import javafx.collections.ListChangeListener.Change;
import javafx.geometry.Bounds;
Expand All @@ -34,7 +30,6 @@
*/
public abstract class AbstractValueIndicator extends ChartPlugin {
private final Axis axis;
private final ChangeListener<? super Number> axisBoundsListener = (obs, oldVal, newVal) -> layoutChildren();

private final ListChangeListener<? super ChartPlugin> pluginsListListener = (final Change<? extends ChartPlugin> change) -> updateStyleClass();

Expand Down Expand Up @@ -143,9 +138,7 @@ protected AbstractValueIndicator(Axis axis, final String text) {
}

private void addAxisListener() {
final Axis valueAxis = getAxis();
valueAxis.minProperty().addListener(axisBoundsListener);
valueAxis.maxProperty().addListener(axisBoundsListener);
getAxis().addListener((source, bits) -> layoutChildren());
}

protected void addChildNodeIfNotPresent(final Node node) {
Expand Down Expand Up @@ -299,9 +292,6 @@ protected final void layoutLabel(final Bounds bounds, final double hPos, final d
}

private void removeAxisListener() {
final Axis valueAxis = getAxis();
valueAxis.minProperty().removeListener(axisBoundsListener);
valueAxis.maxProperty().removeListener(axisBoundsListener);
}

private void removePluginsListListener(final Chart chart) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.fair_acc.chartfx.plugins;
package io.fair_acc.chartfx.plugins.indicators;

import javafx.beans.property.DoubleProperty;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.fair_acc.chartfx.plugins;
package io.fair_acc.chartfx.plugins.indicators;

import javafx.geometry.BoundingBox;
import javafx.geometry.Bounds;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.fair_acc.chartfx.plugins;
package io.fair_acc.chartfx.plugins.indicators;

import javafx.geometry.BoundingBox;
import javafx.geometry.Bounds;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.fair_acc.chartfx.plugins;
package io.fair_acc.chartfx.plugins.indicators;

import javafx.geometry.BoundingBox;
import javafx.geometry.Bounds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) 2017 European Organisation for Nuclear Research (CERN), All Rights Reserved.
*/

package io.fair_acc.chartfx.plugins;
package io.fair_acc.chartfx.plugins.indicators;

import javafx.geometry.BoundingBox;
import javafx.geometry.Bounds;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.fair_acc.chartfx.plugins;
package io.fair_acc.chartfx.plugins.indicators;

import java.util.Arrays;
import java.util.Comparator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import static io.fair_acc.chartfx.axes.AxisMode.X;

import java.text.DecimalFormat;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -43,10 +41,9 @@
import io.fair_acc.chartfx.Chart;
import io.fair_acc.chartfx.axes.Axis;
import io.fair_acc.chartfx.axes.AxisMode;
import io.fair_acc.chartfx.plugins.AbstractSingleValueIndicator;
import io.fair_acc.chartfx.plugins.ParameterMeasurements;
import io.fair_acc.chartfx.plugins.XValueIndicator;
import io.fair_acc.chartfx.plugins.YValueIndicator;
import io.fair_acc.chartfx.plugins.indicators.AbstractSingleValueIndicator;
import io.fair_acc.chartfx.plugins.indicators.XValueIndicator;
import io.fair_acc.chartfx.plugins.indicators.YValueIndicator;
import io.fair_acc.chartfx.plugins.measurements.utils.CheckedValueField;
import io.fair_acc.chartfx.plugins.measurements.utils.DataSetSelector;
import io.fair_acc.chartfx.plugins.measurements.utils.ValueIndicatorSelector;
Expand All @@ -56,7 +53,6 @@
import io.fair_acc.chartfx.viewer.DataViewWindow.WindowDecoration;
import io.fair_acc.dataset.DataSet;
import io.fair_acc.dataset.event.EventListener;
import io.fair_acc.dataset.event.EventRateLimiter;
import io.fair_acc.dataset.event.EventSource;

import impl.org.controlsfx.skin.DecorationPane;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import io.fair_acc.chartfx.axes.spi.format.DefaultTimeFormatter;
import io.fair_acc.chartfx.plugins.DataPointTooltip;
import io.fair_acc.chartfx.plugins.EditAxis;
import io.fair_acc.chartfx.plugins.ParameterMeasurements;
import io.fair_acc.chartfx.plugins.Screenshot;
import io.fair_acc.chartfx.plugins.TableViewer;
import io.fair_acc.chartfx.plugins.Zoomer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.fair_acc.chartfx.plugins;
package io.fair_acc.chartfx.plugins.measurements;

import io.fair_acc.chartfx.plugins.ChartPlugin;
import javafx.beans.value.ChangeListener;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
Expand Down
Loading

0 comments on commit dc89e01

Please sign in to comment.