Skip to content

Commit

Permalink
Setting global theme: add checking of single feature value, other cod…
Browse files Browse the repository at this point in the history
…e improvements.
  • Loading branch information
alshan committed Apr 22, 2024
1 parent 921a250 commit cab3da0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
Expand Up @@ -40,7 +40,7 @@ object Boxplot {
// Y-orientation
(letsPlot(data) { y = "cat"; x = "val" } + ggtitle("Boxplot Y-orientation") +
geomBoxplot(
orientation = "y",
// orientation = "y", // auto-detected
outlierColor = "red", outlierSize = 2, outlierShape = 21
)).show()
}
Expand Down
21 changes: 12 additions & 9 deletions plot-api/src/commonMain/kotlin/org/jetbrains/letsPlot/LetsPlot.kt
Expand Up @@ -21,19 +21,14 @@ object LetsPlot {
set(value) {
field = when (value) {
null -> null
is OptionsMap -> value
is FeatureList -> {
val optionsMap = value.elements.map { it as? OptionsMap ?: error("theme: unsupported feature $it") }
optionsMap.forEach {
require(it.kind == Option.Plot.THEME) {
"theme: wrong options type, expected `${Option.Plot.THEME}` but was `${it.kind}`"
}
}
ThemeOptionsUtil.toSpec(optionsMap)?.let { mergedOptions ->
val optionsMapList = value.elements.map(::toThemeOptionsMap)
ThemeOptionsUtil.toSpec(optionsMapList)?.let { mergedOptions ->
OptionsMap(Option.Plot.THEME, mergedOptions)
}
}
else -> throw IllegalArgumentException("Only `theme(...)`, `themeXxx()`, `flavorXxx()`, or a sum of them are supported")

else -> toThemeOptionsMap(value)
}
}

Expand All @@ -53,4 +48,12 @@ object LetsPlot {
frontendContext = NotebookFrontendContext(jsVersion, isolatedFrameContext, htmlRenderer)
return frontendContext as NotebookFrontendContext
}

private fun toThemeOptionsMap(feature: Feature): OptionsMap {
require(feature is OptionsMap && feature.kind == Option.Plot.THEME) {
"'theme' expected but was: $feature"
}

return feature
}
}
Expand Up @@ -108,7 +108,7 @@ fun gggrid(
@Suppress("NAME_SHADOWING")
val plots = (plots.toList() + List(ncol - 1) { null }).take(len)

val features = listOfNotNull(LetsPlot.theme?.let { it as OptionsMap })
val features = listOfNotNull(LetsPlot.theme as? OptionsMap)

return SubPlotsFigure(
figures = plots,
Expand Down

0 comments on commit cab3da0

Please sign in to comment.