commonMain.jetbrains.datalore.plot.PlotSvgExportPortable.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lets-plot-common Show documentation
Show all versions of lets-plot-common Show documentation
Lets-Plot JVM package without rendering part
/*
* 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
import jetbrains.datalore.base.geometry.DoubleRectangle
import jetbrains.datalore.base.geometry.DoubleVector
import jetbrains.datalore.base.logging.PortableLogging
import jetbrains.datalore.plot.PlotSizeHelper.fetchPlotSizeFromSvg
import jetbrains.datalore.plot.config.BunchConfig
import jetbrains.datalore.plot.config.PlotConfig
import jetbrains.datalore.vis.svgToString.SvgToString
object PlotSvgExportPortable {
private val LOG = PortableLogging.logger(PlotSvgExportPortable::class)
private val PORTABLE_SVG_STR_MAPPER =
SvgToString(rgbEncoder = null) // data-frame --> rgb image is not supported (geom_raster)
/**
* @param plotSpec Raw specification of a plot or GGBunch.
*/
@Suppress("MemberVisibilityCanBePrivate")
fun buildSvgImageFromRawSpecs(
plotSpec: MutableMap,
svgToString: SvgToString = PORTABLE_SVG_STR_MAPPER
): String {
return buildSvgImageFromRawSpecs(plotSpec, null, svgToString)
}
/**
* @param plotSpec Raw specification of a plot or GGBunch.
* @param plotSize Desired plot size. Has no effect on GGBunch.
*/
@Suppress("MemberVisibilityCanBePrivate")
fun buildSvgImageFromRawSpecs(
plotSpec: MutableMap,
plotSize: DoubleVector?,
svgToString: SvgToString = PORTABLE_SVG_STR_MAPPER
): String {
val list = MonolithicCommon.buildSvgImagesFromRawSpecs(
plotSpec,
plotSize,
svgToString
) { messages ->
messages.forEach {
LOG.info { "[when SVG generating] $it" }
}
}
if (list.isEmpty()) {
throw IllegalStateException("Nothing to save: the plot is empty.")
}
if (list.size == 1) {
return list[0]
}
// Must be GGBunch
if (!PlotConfig.isGGBunchSpec(plotSpec)) {
throw IllegalStateException("Can't save multiple SVG images in one file.") // Should never happen
}
val bunchItemSvgList = ArrayList()
var bunchBounds = DoubleRectangle(DoubleVector.ZERO, DoubleVector.ZERO)
val bunchItems = BunchConfig(plotSpec).bunchItems
for ((plotSvg, bunchItem) in list.zip(bunchItems)) {
val (itemSvg, size) = transformBunchItemSvg(
plotSvg, bunchItem.x, bunchItem.y
)
bunchItemSvgList.add(itemSvg)
bunchBounds = bunchBounds.union(DoubleRectangle(bunchItem.x, bunchItem.y, size.x, size.y))
}
val svgStyle = getBunchItemSvgStyle(list[0])
return """
""".trimMargin()
}
private fun getBunchItemSvgStyle(svg: String): String {
val split = svg.split("")[0]
return """
|""".trimMargin()
}
@Suppress("MemberVisibilityCanBePrivate")
private fun transformBunchItemSvg(
svg: String,
x: Double,
y: Double
): Pair {
val split = svg.split("")
val rootGroup = split[1].split("")[0]
val rootGroupTranslated =
"""
© 2015 - 2025 Weber Informatics LLC | Privacy Policy