Skip to content

Commit

Permalink
Add plot_inset to theme().
Browse files Browse the repository at this point in the history
  • Loading branch information
OLarionova-HORIS committed Apr 11, 2024
1 parent fd76882 commit f6939de
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ interface PlotTheme {
fun subtitleMargins(): Thickness
fun captionMargins(): Thickness
fun plotMargins(): Thickness
fun plotInset(): Thickness
}
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ class PlotSvgComponent constructor(
subtitleElementRect?.let { drawDebugRect(it, Color.GRAY) }
subtitleTextRect?.let {
drawDebugRect(
textBoundingBox(subtitle!!, it, PlotLabelSpecFactory.plotTitle(plotTheme), align = -1),
textBoundingBox(subtitle!!, it, PlotLabelSpecFactory.plotSubtitle(plotTheme), align = -1),
Color.DARK_GREEN
)
}
Expand All @@ -340,7 +340,7 @@ class PlotSvgComponent constructor(
captionElementRect?.let { drawDebugRect(it, Color.GRAY) }
captionTextRect?.let {
drawDebugRect(
textBoundingBox(caption!!, it, PlotLabelSpecFactory.plotTitle(plotTheme), align = 1),
textBoundingBox(caption!!, it, PlotLabelSpecFactory.plotCaption(plotTheme), align = 1),
Color.DARK_GREEN
)
}
Expand Down Expand Up @@ -386,6 +386,7 @@ class PlotSvgComponent constructor(
labelSpec = PlotLabelSpecFactory.axisTitle(theme.verticalAxis(flippedAxis)),
justification = theme.verticalAxis(flippedAxis).titleJustification(),
margins = theme.verticalAxis(flippedAxis).titleMargins(),
plotInset = theme.plot().plotInset(),
className = "${Style.AXIS_TITLE}-${theme.verticalAxis(flippedAxis).axis}"
)
}
Expand All @@ -399,6 +400,7 @@ class PlotSvgComponent constructor(
labelSpec = PlotLabelSpecFactory.axisTitle(theme.horizontalAxis(flippedAxis)),
justification = theme.horizontalAxis(flippedAxis).titleJustification(),
margins = theme.horizontalAxis(flippedAxis).titleMargins(),
plotInset = theme.plot().plotInset(),
className = "${Style.AXIS_TITLE}-${theme.horizontalAxis(flippedAxis).axis}"
)
}
Expand Down Expand Up @@ -456,6 +458,7 @@ class PlotSvgComponent constructor(
labelSpec: LabelSpec,
justification: TextJustification,
margins: Thickness,
plotInset: Thickness,
className: String
) {
val referenceRect = when (orientation) {
Expand Down Expand Up @@ -486,30 +489,30 @@ class PlotSvgComponent constructor(
val axisTitleElementRect = when (orientation) {
Orientation.LEFT ->
DoubleRectangle(
referenceRect.left - textHeight - margins.width,
referenceRect.left - textHeight - margins.width - plotInset.left,
referenceRect.top,
textHeight + margins.width,
referenceRect.height
)

Orientation.RIGHT ->
DoubleRectangle(
referenceRect.right,
referenceRect.right + plotInset.right,
referenceRect.top,
textHeight + margins.width,
referenceRect.height
)

Orientation.TOP -> DoubleRectangle(
referenceRect.left,
referenceRect.top - textHeight - margins.height,
referenceRect.top - textHeight - margins.height - plotInset.top,
referenceRect.width,
textHeight + margins.height
)

Orientation.BOTTOM -> DoubleRectangle(
referenceRect.left,
referenceRect.bottom,
referenceRect.bottom + plotInset.bottom,
referenceRect.width,
textHeight + margins.height
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ class PlotAssembler constructor(
frameProviderByTile = frameProviderByTile,
facets = facets,
coordProvider = geomTiles.coordProvider,
hAxisPosition = hAxisPosition,
vAxisPosition = vAxisPosition,
containsLiveMap = geomTiles.containsLiveMap,
theme = theme,
legendBoxInfos = legendBoxInfos,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import org.jetbrains.letsPlot.core.plot.builder.assemble.PlotGuidesAssemblerUtil
import org.jetbrains.letsPlot.core.plot.builder.assemble.PlotGuidesAssemblerUtil.fitsColorBar
import org.jetbrains.letsPlot.core.plot.builder.assemble.PlotGuidesAssemblerUtil.mappedRenderedAesToCreateGuides
import org.jetbrains.letsPlot.core.plot.builder.layout.*
import org.jetbrains.letsPlot.core.plot.builder.scale.AxisPosition

internal object PlotAssemblerUtil {

Expand Down Expand Up @@ -151,8 +150,6 @@ internal object PlotAssemblerUtil {
insideOut: Boolean,
facets: PlotFacets,
facetsTheme: FacetsTheme,
hAxisPosition: AxisPosition,
vAxisPosition: AxisPosition,
hAxisTheme: AxisTheme,
vAxisTheme: AxisTheme,
plotTheme: PlotTheme,
Expand All @@ -162,8 +159,6 @@ internal object PlotAssemblerUtil {
facets,
layoutProviderByTile,
facetsTheme.showStrip(),
hAxisPosition,
vAxisPosition,
hAxisTheme,
vAxisTheme,
plotTheme,
Expand All @@ -178,10 +173,6 @@ internal object PlotAssemblerUtil {

SingleTilePlotLayout(
tileLayout,
hAxisPosition,
vAxisPosition,
hAxisTheme,
vAxisTheme,
plotTheme
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package org.jetbrains.letsPlot.core.plot.builder.defaultTheme

import org.jetbrains.letsPlot.commons.values.Color
import org.jetbrains.letsPlot.core.plot.base.layout.TextJustification
import org.jetbrains.letsPlot.core.plot.base.render.linetype.LineType
import org.jetbrains.letsPlot.core.plot.base.theme.PlotTheme
import org.jetbrains.letsPlot.core.plot.base.theme.ThemeTextStyle
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.Elem
Expand All @@ -21,6 +20,7 @@ import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.TEXT
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.TITLE
import org.jetbrains.letsPlot.core.plot.base.theme.FontFamilyRegistry
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.PLOT_INSET

internal class DefaultPlotTheme(
options: Map<String, Any>,
Expand All @@ -33,6 +33,7 @@ internal class DefaultPlotTheme(
internal val captionKey = listOf(PLOT_CAPTION, TITLE, TEXT)
internal val messagesKey = listOf(PLOT_MESSAGE)
private val marginKey = listOf(PLOT_MARGIN)
private val insetKey = listOf(PLOT_INSET)

override fun showBackground(): Boolean {
return !isElemBlank(backgroundKey)
Expand Down Expand Up @@ -96,6 +97,8 @@ internal class DefaultPlotTheme(

override fun plotMargins() = getMargins(getElemValue(marginKey))

override fun plotInset() = getPadding(getElemValue(insetKey))

override fun showMessage(): Boolean {
return !isElemBlank(messagesKey)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ object ThemeOption {
const val PLOT_CAPTION = "plot_caption"
const val PLOT_MESSAGE = "plot_message"
const val PLOT_MARGIN = "plot_margin"
const val PLOT_INSET = "plot_inset"

// ToDo: "text_width_scale" is used Violin demo - update.
// const val TEXT_WIDTH_FACTOR = "text_width_scale"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.PANEL_INSET
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.PLOT_BKGR_RECT
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.PLOT_CAPTION
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.PLOT_INSET
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.PLOT_MARGIN
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.PLOT_SUBTITLE
import org.jetbrains.letsPlot.core.plot.builder.defaultTheme.values.ThemeOption.PLOT_TITLE
Expand Down Expand Up @@ -68,26 +69,23 @@ internal open class ThemeValuesBase : ThemeValues(VALUES) {
),
TITLE to mapOf(
Elem.SIZE to Defaults.FONT_MEDIUM,
Elem.Margin.TOP to 4.0,
Elem.Margin.TOP to 0.0,
Elem.Margin.RIGHT to 0.0,
Elem.Margin.BOTTOM to 4.0,
Elem.Margin.BOTTOM to 0.0,
Elem.Margin.LEFT to 0.0
),
PLOT_TITLE to mapOf(
Elem.SIZE to Defaults.FONT_LARGE,
Elem.Margin.TOP to 0.0,
Elem.Margin.BOTTOM to 10.0
Elem.Margin.TOP to 4.0,
),
PLOT_SUBTITLE to mapOf(
Elem.SIZE to Defaults.FONT_MEDIUM,
Elem.Margin.TOP to 0.0,
Elem.Margin.BOTTOM to 10.0
Elem.Margin.TOP to 4.0,
),
PLOT_CAPTION to mapOf(
Elem.HJUST to 1.0,
Elem.SIZE to Defaults.FONT_SMALL,
Elem.Margin.TOP to 10.0,
Elem.Margin.BOTTOM to 0.0
Elem.Margin.BOTTOM to 4.0,
),
// TEXT_WIDTH_FACTOR to 1.0,
PANEL_BORDER_RECT to ELEMENT_BLANK,
Expand Down Expand Up @@ -165,10 +163,16 @@ internal open class ThemeValuesBase : ThemeValues(VALUES) {
),

PLOT_MARGIN to mapOf(
Elem.Margin.TOP to 6.5,
Elem.Margin.RIGHT to 6.5,
Elem.Margin.BOTTOM to 6.5,
Elem.Margin.LEFT to 6.5
Elem.Margin.TOP to 0.0,
Elem.Margin.RIGHT to 0.0,
Elem.Margin.BOTTOM to 0.0,
Elem.Margin.LEFT to 0.0
),
PLOT_INSET to mapOf(
Elem.Inset.TOP to 6.5,
Elem.Inset.RIGHT to 6.5,
Elem.Inset.BOTTOM to 6.5,
Elem.Inset.LEFT to 6.5
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import org.jetbrains.letsPlot.commons.geometry.DoubleRectangle
import org.jetbrains.letsPlot.commons.geometry.DoubleVector
import org.jetbrains.letsPlot.core.plot.base.layout.Thickness
import org.jetbrains.letsPlot.core.plot.base.theme.AxisTheme
import org.jetbrains.letsPlot.core.plot.base.theme.PlotTheme
import org.jetbrains.letsPlot.core.plot.base.theme.FacetsTheme
import org.jetbrains.letsPlot.core.plot.base.theme.PlotTheme
import org.jetbrains.letsPlot.core.plot.builder.assemble.PlotFacets
import org.jetbrains.letsPlot.core.plot.builder.coord.CoordProvider
import org.jetbrains.letsPlot.core.plot.builder.layout.FacetedPlotLayoutUtil.geomOffsetsByCol
Expand All @@ -19,15 +19,12 @@ import org.jetbrains.letsPlot.core.plot.builder.layout.PlotLayoutUtil.plotInsets
import org.jetbrains.letsPlot.core.plot.builder.layout.facet.FixedScalesTilesLayouter
import org.jetbrains.letsPlot.core.plot.builder.layout.facet.FreeScalesTilesLayouter
import org.jetbrains.letsPlot.core.plot.builder.layout.util.Insets
import org.jetbrains.letsPlot.core.plot.builder.scale.AxisPosition
import kotlin.math.max

internal class FacetedPlotLayout(
private val facets: PlotFacets,
private val layoutProviderByTile: List<TileLayoutProvider>,
private val showFacetStrip: Boolean,
hAxisOrientation: AxisPosition,
vAxisOrientation: AxisPosition,
private val hAxisTheme: AxisTheme,
private val vAxisTheme: AxisTheme,
private val plotTheme: PlotTheme,
Expand All @@ -36,10 +33,7 @@ internal class FacetedPlotLayout(
private val totalAddedHSize: Double = PANEL_PADDING * (facets.colCount - 1)
private val totalAddedVSize: Double = PANEL_PADDING * (facets.rowCount - 1)

private val insets: Insets = plotInsets(
hAxisOrientation, vAxisOrientation,
hAxisTheme, vAxisTheme
)
private val insets: Insets = plotInsets(plotTheme.plotInset())

init {
require(facets.isDefined) { "Undefined facets." }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import org.jetbrains.letsPlot.core.plot.builder.layout.util.Insets
internal object LayoutConstants {
val GEOM_MIN_SIZE = DoubleVector(50.0, 50.0)

const val GEOM_AREA_PADDING = 0.0

// Horizontal axis labels
const val H_AXIS_LABELS_EXPAND = 10.0 // Value by which labels can go beyond the axis bounds (px).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,16 @@ import org.jetbrains.letsPlot.commons.geometry.DoubleVector
import org.jetbrains.letsPlot.core.plot.base.guide.LegendPosition
import org.jetbrains.letsPlot.core.plot.base.layout.Thickness
import org.jetbrains.letsPlot.core.plot.base.render.svg.MultilineLabel
import org.jetbrains.letsPlot.core.plot.base.theme.AxisTheme
import org.jetbrains.letsPlot.core.plot.base.theme.LegendTheme
import org.jetbrains.letsPlot.core.plot.base.theme.PlotTheme
import org.jetbrains.letsPlot.core.plot.base.theme.Theme
import org.jetbrains.letsPlot.core.plot.builder.layout.LayoutConstants.GEOM_AREA_PADDING
import org.jetbrains.letsPlot.core.plot.builder.layout.LayoutConstants.GEOM_MIN_SIZE
import org.jetbrains.letsPlot.core.plot.builder.layout.util.Insets
import org.jetbrains.letsPlot.core.plot.builder.presentation.LabelSpec
import org.jetbrains.letsPlot.core.plot.builder.scale.AxisPosition
import kotlin.math.max

internal object PlotLayoutUtil {
fun plotInsets(
hAxisPosition: AxisPosition,
vAxisPosition: AxisPosition,
hAxisTheme: AxisTheme,
vAxisTheme: AxisTheme
): Insets {
val vPadding = if (hAxisTheme.showTitle() || hAxisTheme.showLabels()) 0.0 else GEOM_AREA_PADDING
val hPadding = if (vAxisTheme.showTitle() || vAxisTheme.showLabels()) 0.0 else GEOM_AREA_PADDING
val (left, right) = when (vAxisPosition) {
AxisPosition.LEFT -> Pair(hPadding, GEOM_AREA_PADDING)
AxisPosition.RIGHT -> Pair(GEOM_AREA_PADDING, hPadding)
AxisPosition.LR -> Pair(hPadding, hPadding)
else -> throw IllegalStateException("Illegal vertical axis position: $vAxisPosition")
}
val (top, bottom) = when (hAxisPosition) {
AxisPosition.TOP -> Pair(vPadding, GEOM_AREA_PADDING)
AxisPosition.BOTTOM -> Pair(GEOM_AREA_PADDING, vPadding)
AxisPosition.TB -> Pair(vPadding, vPadding)
else -> throw IllegalStateException("Illegal horizontal axis position: $hAxisPosition")
}

return Insets(left, top, right, bottom)
}
fun plotInsets(plotInset: Thickness) = Insets(plotInset.leftTop, plotInset.rightBottom)

private fun labelDimensions(text: String, labelSpec: LabelSpec): DoubleVector {
if (text.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,17 @@
package org.jetbrains.letsPlot.core.plot.builder.layout

import org.jetbrains.letsPlot.commons.geometry.DoubleVector
import org.jetbrains.letsPlot.core.plot.base.theme.AxisTheme
import org.jetbrains.letsPlot.core.plot.base.theme.PlotTheme
import org.jetbrains.letsPlot.core.plot.builder.coord.CoordProvider
import org.jetbrains.letsPlot.core.plot.builder.layout.PlotLayoutUtil.plotInsets
import org.jetbrains.letsPlot.core.plot.builder.layout.util.Insets
import org.jetbrains.letsPlot.core.plot.builder.scale.AxisPosition

internal class SingleTilePlotLayout(
private val tileLayout: TileLayout,
hAxisPosition: AxisPosition,
vAxisPosition: AxisPosition,
hAxisTheme: AxisTheme,
vAxisTheme: AxisTheme,
private val plotTheme: PlotTheme
) : PlotLayout {

private val insets: Insets = plotInsets(
hAxisPosition, vAxisPosition,
hAxisTheme, vAxisTheme
)
private val insets: Insets = plotInsets(plotTheme.plotInset())

override fun doLayout(preferredSize: DoubleVector, coordProvider: CoordProvider): PlotLayoutInfo {
return if (tileLayout.insideOut) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@ import org.jetbrains.letsPlot.core.plot.builder.layout.PlotLayoutUtil.axisTitles
import org.jetbrains.letsPlot.core.plot.builder.layout.PlotLayoutUtil.legendBlockLeftTopDelta
import org.jetbrains.letsPlot.core.plot.builder.layout.tile.LiveMapAxisTheme
import org.jetbrains.letsPlot.core.plot.builder.layout.tile.LiveMapTileLayoutProvider
import org.jetbrains.letsPlot.core.plot.builder.scale.AxisPosition
import kotlin.math.max

internal class PlotFigureLayouter(
private val frameProviderByTile: List<FrameOfReferenceProvider>,
private val facets: PlotFacets,
private val coordProvider: CoordProvider,
private val hAxisPosition: AxisPosition,
private val vAxisPosition: AxisPosition,
private val containsLiveMap: Boolean,
private val theme: Theme,
legendBoxInfos: List<LegendBoxInfo>,
Expand Down Expand Up @@ -100,7 +97,6 @@ internal class PlotFigureLayouter(
insideOut,
facets,
theme.facets(),
hAxisPosition, vAxisPosition,
hAxisTheme = theme.horizontalAxis(flipAxis),
vAxisTheme = theme.verticalAxis(flipAxis),
plotTheme = theme.plot()
Expand All @@ -121,8 +117,6 @@ internal class PlotFigureLayouter(
insideOut = false,
facets,
theme.facets(),
hAxisPosition = AxisPosition.BOTTOM, // Not used with Live Map
vAxisPosition = AxisPosition.LEFT, // Not used with Live Map
hAxisTheme = LiveMapAxisTheme(),
vAxisTheme = LiveMapAxisTheme(),
plotTheme = theme.plot()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,7 @@ object Option {
const val PLOT_CAPTION = ThemeOption.PLOT_CAPTION
const val PLOT_MESSAGE = ThemeOption.PLOT_MESSAGE
const val PLOT_MARGIN = ThemeOption.PLOT_MARGIN
const val PLOT_INSET = ThemeOption.PLOT_INSET

// Axis
const val AXIS = ThemeOption.AXIS
Expand Down
Loading

0 comments on commit f6939de

Please sign in to comment.