commonMain.jetbrains.datalore.plot.builder.PlotContainerPortable.kt Maven / Gradle / Ivy
/*
* Copyright (c) 2020. JetBrains s.r.o.
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
package jetbrains.datalore.plot.builder
import jetbrains.datalore.base.gcommon.base.Preconditions.checkState
import jetbrains.datalore.base.geometry.DoubleVector
import jetbrains.datalore.base.observable.event.EventHandler
import jetbrains.datalore.base.observable.property.PropertyChangeEvent
import jetbrains.datalore.base.observable.property.ReadableProperty
import jetbrains.datalore.base.registration.CompositeRegistration
import jetbrains.datalore.base.registration.Registration
import jetbrains.datalore.base.values.SomeFig
import jetbrains.datalore.plot.builder.presentation.Style
import jetbrains.datalore.plot.builder.presentation.Style.PLOT_BACKDROP
import jetbrains.datalore.vis.svg.SvgCssResource
import jetbrains.datalore.vis.svg.SvgRectElement
import jetbrains.datalore.vis.svg.SvgSvgElement
import kotlin.math.max
/**
* This class only handles static SVG. (no interactions)
*/
open class PlotContainerPortable(
protected val plot: Plot,
private val preferredSize: ReadableProperty
) {
val svg: SvgSvgElement = SvgSvgElement()
val liveMapFigures: List
get() = plot.liveMapFigures
val isLiveMap: Boolean
get() = plot.liveMapFigures.isNotEmpty()
private var myContentBuilt: Boolean = false
private var myRegistrations = CompositeRegistration()
init {
svg.addClass(Style.PLOT_CONTAINER)
setSvgSize(preferredSize.get())
plot.laidOutSize().addHandler(sizePropHandler { laidOutSize ->
val newSvgSize = DoubleVector(
max(preferredSize.get().x, laidOutSize.x),
max(preferredSize.get().y, laidOutSize.y)
)
setSvgSize(newSvgSize)
})
preferredSize.addHandler(sizePropHandler { newPreferredSize ->
if (newPreferredSize.x > 0 && newPreferredSize.y > 0) {
revalidateContent()
}
})
}
fun ensureContentBuilt() {
if (!myContentBuilt) {
buildContent()
}
}
private fun revalidateContent() {
if (myContentBuilt) {
clearContent()
buildContent()
}
}
protected open fun buildContent() {
checkState(!myContentBuilt)
myContentBuilt = true
svg.setStyle(object : SvgCssResource {
override fun css(): String {
return Style.css
}
})
// Add Plot background.
// Batik doesn't seem to support any styling (via 'style' element or 'style' attribute)
// of root
© 2015 - 2025 Weber Informatics LLC | Privacy Policy