Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix calculation of XY-ranges for geom_errorbar #770

Merged
merged 6 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Separate logic to identify renderedAes for errobar.
  • Loading branch information
OLarionova-HORIS committed Apr 28, 2023
commit e65d67c742e14d5f94739494fa7bf2d4d768c293
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import jetbrains.datalore.plot.base.render.SvgRoot
import jetbrains.datalore.vis.svg.SvgGElement
import jetbrains.datalore.vis.svg.SvgLineElement

class ErrorBarGeom : GeomBase(), WithWidth, WithHeight {
class ErrorBarGeom : GeomBase() {

override val legendKeyElementFactory: LegendKeyElementFactory
get() = ErrorBarLegendKeyElementFactory()
Expand All @@ -39,10 +39,10 @@ class ErrorBarGeom : GeomBase(), WithWidth, WithHeight {
val geomHelper = GeomHelper(pos, coord, ctx)
val colorsByDataPoint = HintColorUtil.createColorMarkerMapper(GeomKind.ERROR_BAR, ctx)

var dataPoints = GeomUtil.withDefined(aesthetics.dataPoints(), verticalAes())
var dataPoints = GeomUtil.withDefined(aesthetics.dataPoints(), Aes.X, Aes.YMIN, Aes.YMAX, Aes.WIDTH)
val isVertical = dataPoints.any()
if (!isVertical) {
dataPoints = GeomUtil.withDefined(aesthetics.dataPoints(), horizontalAes())
dataPoints = GeomUtil.withDefined(aesthetics.dataPoints(), Aes.Y, Aes.XMIN, Aes.XMAX, Aes.HEIGHT)
}

val xAes = if (isVertical) Aes.X else Aes.Y
Expand Down Expand Up @@ -157,9 +157,6 @@ class ErrorBarGeom : GeomBase(), WithWidth, WithHeight {
companion object {
const val HANDLES_GROUPS = false

private fun verticalAes() = listOf(Aes.X, Aes.YMIN, Aes.YMAX, Aes.WIDTH)
private fun horizontalAes() = listOf(Aes.Y, Aes.XMIN, Aes.XMAX, Aes.HEIGHT)

private fun errorBarLegendShape(segments: List<DoubleSegment>, p: DataPointAesthetics): SvgGElement {
val g = SvgGElement()
segments.forEach { segment ->
Expand Down Expand Up @@ -197,26 +194,4 @@ class ErrorBarGeom : GeomBase(), WithWidth, WithHeight {
return g
}
}

override fun heightSpan(
p: DataPointAesthetics,
coordAes: Aes<Double>,
resolution: Double,
isDiscrete: Boolean
): DoubleSpan? {
val isHorizontal = horizontalAes().all(p::defined)
if (!isHorizontal) return null
return PointDimensionsUtil.dimensionSpan(p, coordAes, Aes.HEIGHT, resolution)
}

override fun widthSpan(
p: DataPointAesthetics,
coordAes: Aes<Double>,
resolution: Double,
isDiscrete: Boolean
): DoubleSpan? {
val isVertical = verticalAes().all(p::defined)
if (!isVertical) return null
return PointDimensionsUtil.dimensionSpan(p, coordAes, Aes.WIDTH, resolution)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,6 @@ object GeomUtil {
return dataPoints.filter { p -> p.defined(aes0) && p.defined(aes1) && p.defined(aes2) && p.defined(aes3) }
}

fun withDefined(
dataPoints: Iterable<DataPointAesthetics>,
aesList: List<Aes<*>>
): Iterable<DataPointAesthetics> {
return dataPoints.filter { p ->
aesList.all { aes -> p.defined(aes) }
}
}

fun createGroups(dataPoints: Iterable<DataPointAesthetics>): Map<Int, List<DataPointAesthetics>> {
val pointsByGroup = HashMap<Int, MutableList<DataPointAesthetics>>()
for (p in dataPoints) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ class GeomLayerBuilder(
override val geomKind: GeomKind = geomProvider.geomKind
override val aestheticsDefaults: AestheticsDefaults = geomProvider.aestheticsDefaults()

private val myConstantByAes: TypedKeyHashMap = TypedKeyHashMap()
private val myRenderedAes: List<Aes<*>>
private val myConstantByAes: TypedKeyHashMap

override val legendKeyElementFactory: LegendKeyElementFactory
get() = geom.legendKeyElementFactory
Expand All @@ -277,13 +277,37 @@ class GeomLayerBuilder(
get() = geom is LiveMapGeom

init {
myRenderedAes = GeomMeta.renders(geomProvider.geomKind, colorByAes, fillByAes)

// constant value by aes (default + specified)
myConstantByAes = TypedKeyHashMap()
for (key in constantByAes.keys<Any>()) {
myConstantByAes.put(key, constantByAes[key])
}

myRenderedAes = GeomMeta.renders(geomProvider.geomKind, colorByAes, fillByAes).let { allRenderedAes ->
if (geomKind == GeomKind.ERROR_BAR) {
// ToDo Need refactoring...
// This geometry supports a dual set of aesthetics (vertical and horizontal representation).
// Check that the settings are not inconsistent
// and set the aesthetics needed for that geometry.
val definedAes = allRenderedAes.filter { aes -> hasBinding(aes) || hasConstant(aes) }
val isHorizontal = listOf(Aes.Y, Aes.XMIN, Aes.XMAX).all { aes -> aes in definedAes }
val isVertical = listOf(Aes.X, Aes.YMIN, Aes.YMAX).all { aes -> aes in definedAes }
require(!(isHorizontal && isVertical)) {
"For errorbar either x, ymin, ymax or y, xmin, xmax must be specified."
}
when {
isVertical -> listOf(
Aes.X, Aes.YMIN, Aes.YMAX, Aes.WIDTH,
Aes.ALPHA, Aes.COLOR, Aes.LINETYPE, Aes.SIZE
)
else -> listOf(
Aes.Y, Aes.XMIN, Aes.XMAX, Aes.HEIGHT,
Aes.ALPHA, Aes.COLOR, Aes.LINETYPE, Aes.SIZE
)
}
} else {
allRenderedAes
}
}
}

override fun renderedAes(): List<Aes<*>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,12 @@ internal object PositionalScalesUtil {
}

private fun positionalDryRunAesthetics(layer: GeomLayer): Aesthetics {
val aesList = layer.renderedAes()
.filter {
Aes.affectingScaleX(it) ||
Aes.affectingScaleY(it) ||
it == Aes.HEIGHT ||
it == Aes.WIDTH
}
.filter { layer.hasBinding(it) || layer.hasConstant(it) }
val aesList = layer.renderedAes().filter {
Aes.affectingScaleX(it) ||
Aes.affectingScaleY(it) ||
it == Aes.HEIGHT ||
it == Aes.WIDTH
}

val mappers = aesList.associateWith { Mappers.IDENTITY }
return PlotUtil.createLayerAesthetics(layer, aesList, mappers)
Expand Down Expand Up @@ -193,8 +191,8 @@ internal object PositionalScalesUtil {
private fun computeLayerDryRunXYRangesAfterPosAdjustment(
layer: GeomLayer, aes: Aesthetics, geomCtx: GeomContext
): Pair<DoubleSpan?, DoubleSpan?> {
val posAesX = Aes.affectingScaleX(layer.renderedAes().filter { layer.hasBinding(it) || layer.hasConstant(it) })
val posAesY = Aes.affectingScaleY(layer.renderedAes().filter { layer.hasBinding(it) || layer.hasConstant(it) })
val posAesX = Aes.affectingScaleX(layer.renderedAes())
val posAesY = Aes.affectingScaleY(layer.renderedAes())

val pos = PlotUtil.createPositionAdjustment(layer.posProvider, aes)
if (pos.isIdentity) {
Expand Down