Skip to content

Bug or feature? Combining ErrorDataSetRenderer and XYChart #684

@wolfig

Description

@wolfig

Describe the bug
I add a XYChart to a JavaFX GridPane. The XYChart has one additional renderer, an ErrorDataSetRenderer. Before adding the XYChart to the GridPane, the XYChart has two axes: one x-axis and one y-axis.

The XYChart is initialized like this:

private XYChart _xyChart = new XYChart(_xAxis, _yAxis);

public DeviceAutomatorXYChart() {
    _renderer = new ErrorDataSetRenderer();
    _renderer.setId("MainRenderer");
    _renderer.setErrorStyle(ErrorStyle.ERRORBARS);
    _renderer.setDrawMarker(true);
    _renderer.setShowInLegend(false);
    _xyChart.setLegendVisible(false);
    _xyChart.getRenderers().clear();
    _xyChart.getRenderers().add(_renderer);
 }

During the GridPane.add process, the method XYChart.runPreLayout() is triggered, which calls (Chart.class line 490f)

for (Renderer renderer : renderers) {
    renderer.updateAxes();
}

The "updateAxes" calls AbstractRendererXY.class lines 75f.:

@Override
public void updateAxes() {
    // Default to explicitly set axes
    xAxis = ensureAxisInChart(getFirstAxis(Orientation.HORIZONTAL));
    yAxis = ensureAxisInChart(getFirstAxis(Orientation.VERTICAL));

    // Get or create one in the chart if needed
    var chart = AssertUtils.notNull("chart", getChart());
    if (xAxis == null) {
        xAxis = chart.getXAxis();
    }
    if (yAxis == null) {
        yAxis = chart.getYAxis();
    }

"ensureAxisInChart" AbstractRendererXY.class lines 100f.:

protected Axis ensureAxisInChart(Axis axis) {
    if (axis != null && !getChart().getAxes().contains(axis)) {
        getChart().getAxes().add(axis);
    }
    return axis;
}

In case of the x-axis, axis is not null, for the y-axis axis is null. getChart().getAxes().contains(axis) yields false for the x-axis and the axis from the ErrorDataSetRenderer is added to the chart.
Cause of this behavior is that the axes in the instance of ErrorDataSetRenderer are different instances than those owned by the XYChart.

I can fix this behavior by explicitly adding _renderer.getAxes().setAll(_xyChart.getAxes());, but this seems to me a bit awkward - shouldn't the default behavior be that the renderer and the chart share the axes and the API user adds them explicitly if desired?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions