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

Refactor code of theme/flavors, theme_none() uses blue colors #891

Merged
merged 8 commits into from
Sep 29, 2023
Prev Previous commit
Next Next commit
Add ThemeBuilder.
  • Loading branch information
OLarionova-HORIS committed Sep 28, 2023
commit bf1fe1af24a56a5169bc6eacf5920e3c63085f14
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ import org.jetbrains.letsPlot.core.plot.base.scale.Mappers
import org.jetbrains.letsPlot.core.plot.base.scale.Scales
import org.jetbrains.letsPlot.core.plot.base.scale.breaks.ScaleBreaksUtil
import org.jetbrains.letsPlot.core.plot.builder.AxisUtil
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.DefaultTheme
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption
import org.jetbrains.letsPlot.core.plot.builder.guide.AxisComponent
import org.jetbrains.letsPlot.core.plot.builder.guide.Orientation
import demo.plot.common.model.SimpleDemoBase
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.ThemeOptionsUtil
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.ThemeBuilder
import org.jetbrains.letsPlot.datamodel.svg.dom.SvgRectElement
import org.jetbrains.letsPlot.datamodel.svg.dom.SvgSvgElement

Expand Down Expand Up @@ -135,13 +134,12 @@ open class AxisComponentDemo : SimpleDemoBase(DEMO_BOX_SIZE) {
// axis.gridLineWidth.set(Plot.Axis.GRID_LINE_WIDTH)
// axis.gridLineLength.set(100.0)

val themeOptions = ThemeOptionsUtil.prepareThemeOptions(
ThemeOption.Name.R_CLASSIC,
val theme = ThemeBuilder(
themeName = ThemeOption.Name.R_CLASSIC,
userOptions = mapOf(
ThemeOption.PANEL_GRID to mapOf(ThemeOption.Elem.COLOR to Color.RED)
)
)
val theme = DefaultTheme(themeOptions)
).build()

val axis = AxisComponent(
length = axisLength,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import org.jetbrains.letsPlot.core.commons.color.ColorPalette
import org.jetbrains.letsPlot.core.commons.color.ColorScheme
import org.jetbrains.letsPlot.core.commons.color.PaletteUtil.schemeColors
import demo.plot.common.model.SimpleDemoBase
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.ThemeOptionsUtil
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.ThemeBuilder
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption

open class ScatterDemo : SimpleDemoBase() {
Expand All @@ -47,12 +47,7 @@ open class ScatterDemo : SimpleDemoBase() {
)
}

private fun classicTheme() = DefaultTheme(
ThemeOptionsUtil.prepareThemeOptions(
ThemeOption.Name.LP_MINIMAL,
userOptions = emptyMap()
)
)
private fun classicTheme() = ThemeBuilder(ThemeOption.Name.R_CLASSIC).build()

private fun gauss(): GroupComponent {
val count = 200
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ package org.jetbrains.letsPlot.core.plot.builder.defaultTheme
import org.jetbrains.letsPlot.core.plot.base.GeomKind
import org.jetbrains.letsPlot.core.plot.base.aes.GeomTheme
import org.jetbrains.letsPlot.core.plot.base.theme.*
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.Flavor
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeValuesLPMinimal2
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption
import org.jetbrains.letsPlot.core.plot.builder.presentation.DefaultFontFamilyRegistry
import org.jetbrains.letsPlot.core.plot.builder.presentation.FontFamilyRegistry

class DefaultTheme(
class DefaultTheme internal constructor(
options: Map<String, Any>,
fontFamilyRegistry: FontFamilyRegistry = DefaultFontFamilyRegistry(),
) : Theme {
Expand Down Expand Up @@ -49,8 +48,6 @@ class DefaultTheme(

companion object {
// For demo and tests
fun minimal2() = DefaultTheme(
ThemeFlavorUtil.applyFlavor(ThemeValuesLPMinimal2().values, Flavor.BASE)
)
fun minimal2() = ThemeBuilder(ThemeOption.Name.LP_MINIMAL).build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@ package org.jetbrains.letsPlot.core.plot.builder.defaultTheme
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeValues
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeValues.Companion.mergeWith
import org.jetbrains.letsPlot.core.plot.builder.presentation.DefaultFontFamilyRegistry
import org.jetbrains.letsPlot.core.plot.builder.presentation.FontFamilyRegistry

object ThemeOptionsUtil {
class ThemeBuilder(
private val themeName: String,
private val userOptions: Map<String, Any> = emptyMap(),
private val fontFamilyRegistry: FontFamilyRegistry = DefaultFontFamilyRegistry()
) {
fun build(): DefaultTheme {

fun prepareThemeOptions(
themeName: String,
userOptions: Map<String, Any>
): Map<String, Any> {
val baselineValues = ThemeValues.forName(themeName).values

val flavorName = userOptions[ThemeOption.FLAVOR] as? String
?: baselineValues[ThemeOption.FLAVOR] as? String
?: error("Flavor name should be specified")

return ThemeFlavorUtil.applyFlavor(baselineValues, flavorName)
val effectiveOptions = ThemeFlavorUtil.applyFlavor(baselineValues, flavorName)
.mergeWith(userOptions)

return DefaultTheme(effectiveOptions, fontFamilyRegistry)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
package org.jetbrains.letsPlot.core.spec.config

import org.jetbrains.letsPlot.core.plot.base.theme.Theme
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.DefaultTheme
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.ThemeOptionsUtil
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.ThemeBuilder
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.ELEMENT_BLANK
import org.jetbrains.letsPlot.core.plot.builder.presentation.FontFamilyRegistry
Expand All @@ -32,9 +31,7 @@ class ThemeConfig constructor(
LegendThemeConfig.convertValue(key, value)
}

val effectiveOptions = ThemeOptionsUtil.prepareThemeOptions(themeName, userOptions)

theme = DefaultTheme(effectiveOptions, fontFamilyRegistry)
theme = ThemeBuilder(themeName, userOptions, fontFamilyRegistry).build()
}

companion object {
Expand Down