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

Autorotate discrete #1032

Merged
merged 4 commits into from
Apr 8, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Next Next commit
Refactoring of choosing orientation. Move logic into init method of L…
…ayerConfig.
  • Loading branch information
RYangazov committed Apr 8, 2024
commit 9acb86b269bd61f65c7c014e148b3761017f2e6c
3,413 changes: 3,413 additions & 0 deletions docs/dev/notebooks/auto_orientation_discretes.ipynb

Large diffs are not rendered by default.

891 changes: 891 additions & 0 deletions docs/f-24b/auto_rotate.ipynb

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions future_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@

See: [example notebook](https://nbviewer.org/github/JetBrains/lets-plot/blob/master/docs/f-24b/param_size_unit.ipynb).

- Automatically choose `orientation="y"` when aes y is discrete [[#558](https://github.com/JetBrains/lets-plot/issues/558)].

See: [example notebook](https://nbviewer.org/github/JetBrains/lets-plot/blob/master/docs/f-24b/auto_rotate.ipynb).


### Changed

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,28 @@ internal object DataConfigUtil {
)
}

fun isAesDiscrete(
aes: Aes<*>,
sharedData: DataFrame,
layerData: DataFrame,
sharedMappings: Map<String, String>,
layerMappings: Map<String, String>,
combinedDiscreteMappings: Map<String, String>
): Boolean {
// Checking if the y-axis mark as_discrete
if (combinedDiscreteMappings.containsKey(aes.name)) return true

// Checking if the y-axis is discrete
val aesName = layerMappings[aes.name] ?: sharedMappings[aes.name] ?: return false
// The logic of choosing the data frame is the same as in the layerMappingsAndCombinedData function
if (DataFrameUtil.hasVariable(layerData, aesName)
&& DataFrameUtil.findVariableOrFail(layerData, aesName).let(layerData::isDiscrete)
) return true

return (DataFrameUtil.hasVariable(sharedData, aesName)
&& DataFrameUtil.findVariableOrFail(sharedData, aesName).let(sharedData::isDiscrete))
}

fun combinedDataWithDataMeta(
rawCombinedData: DataFrame,
varBindings: List<VarBinding>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,26 @@ class LayerConfig(
ownDiscreteAes = DataMetaUtil.getAsDiscreteAesSet(getMap(DATA_META))
)

if (!clientSide
alshan marked this conversation as resolved.
Show resolved Hide resolved
&& isOrientationApplicable()
&& !DataConfigUtil.isAesDiscrete(
Aes.X,
plotData,
ownData,
plotMappings,
layerMappings,
combinedDiscreteMappings
)
&& DataConfigUtil.isAesDiscrete(
Aes.Y,
plotData,
ownData,
plotMappings,
layerMappings,
combinedDiscreteMappings
)
) setOrientationY()

val consumedAesSet: Set<Aes<*>> = renderedAes.toSet().let {
when (clientSide) {
true -> it
Expand Down Expand Up @@ -279,6 +299,31 @@ class LayerConfig(
combinedDataValid = false
}

private fun isOrientationApplicable(): Boolean {
val isSuitableGeomKind = geomProto.geomKind in listOf(
GeomKind.BAR,
GeomKind.BOX_PLOT,
GeomKind.VIOLIN,
GeomKind.LOLLIPOP,
GeomKind.Y_DOT_PLOT
)
val isSuitableStatKind = statKind in listOf(
StatKind.COUNT,
StatKind.SUMMARY,
StatKind.BOXPLOT,
StatKind.BOXPLOT_OUTLIER,
StatKind.YDOTPLOT,
StatKind.YDENSITY
)

return isSuitableGeomKind || isSuitableStatKind
}

private fun setOrientationY() {
check(!clientSide)
update(ORIENTATION, "y")
}

fun hasExplicitGrouping(): Boolean {
return explicitGroupingVarName != null
}
Expand Down