Skip to content

Commit

Permalink
Fix #986 - Support arrow() in geom_spoke()
Browse files Browse the repository at this point in the history
  • Loading branch information
IKupriyanov-HORIS committed Mar 15, 2024
1 parent f81ada6 commit 999952d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions future_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
- Error when build geom_smooth() with se=False [[#1050](https://github.com/JetBrains/lets-plot/issues/1050)].
- livemap: when release the mouse button from outside the map, it gets stuck in panning mode [[#1044](https://github.com/JetBrains/lets-plot/issues/1044)].
- Incorrect 'plot_background' area (with empty space capture) [[#918](https://github.com/JetBrains/lets-plot/issues/918)].
- Support arrow() in geom_spoke() [[#986](https://github.com/JetBrains/lets-plot/issues/986)].
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.jetbrains.letsPlot.commons.geometry.DoubleVector
import org.jetbrains.letsPlot.commons.interval.DoubleSpan
import org.jetbrains.letsPlot.core.plot.base.*
import org.jetbrains.letsPlot.core.plot.base.geom.legend.HLineLegendKeyElementFactory
import org.jetbrains.letsPlot.core.plot.base.geom.util.ArrowSpec
import org.jetbrains.letsPlot.core.plot.base.geom.util.GeomHelper
import org.jetbrains.letsPlot.core.plot.base.geom.util.GeomUtil
import org.jetbrains.letsPlot.core.plot.base.geom.util.TargetCollectorHelper
Expand All @@ -18,6 +19,7 @@ import kotlin.math.cos
import kotlin.math.sin

class SpokeGeom : GeomBase(), WithWidth, WithHeight {
var arrowSpec: ArrowSpec? = null
var pivot: Pivot = DEF_PIVOT

override val legendKeyElementFactory: LegendKeyElementFactory
Expand All @@ -34,7 +36,15 @@ class SpokeGeom : GeomBase(), WithWidth, WithHeight {
val geomHelper = GeomHelper(pos, coord, ctx)
val svgElementHelper = geomHelper.createSvgElementHelper()
svgElementHelper.setStrokeAlphaEnabled(true)
svgElementHelper.setGeometryHandler { aes, lineString -> tooltipHelper.addLine(lineString, aes) }

svgElementHelper.setGeometryHandler { aes, lineString ->
tooltipHelper.addLine(lineString, aes)

arrowSpec?.let {
val arrow = ArrowSpec.createArrows(aes, lineString, it)
arrow.forEach(root::add)
}
}

for (p in aesthetics.dataPoints()) {
val x = p.finiteOrNull(Aes.X) ?: continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,16 +372,21 @@ internal object GeomProviderFactory {
}
}

GeomKind.SPOKE -> GeomProvider.spoke {
GeomKind.SPOKE -> GeomProvider.spoke {
val geom = SpokeGeom()
layerConfig[Spoke.ARROW]?.let {
val arrowConfig = ArrowSpecConfig.create(it)
geom.arrowSpec = arrowConfig.createArrowSpec()
}

layerConfig.getString(Spoke.PIVOT)?.let {
geom.pivot = when (it.lowercase()) {
"tail" -> SpokeGeom.Pivot.TAIL
"middle", "mid" -> SpokeGeom.Pivot.MIDDLE
"tip" -> SpokeGeom.Pivot.TIP
else -> throw IllegalArgumentException(
"Unsupported value for ${Spoke.PIVOT} parameter: '$it'. " +
"Use one of: tail, middle, mid, tip."
"Use one of: tail, middle, mid, tip."
)
}
}
Expand All @@ -396,7 +401,8 @@ internal object GeomProviderFactory {
}

private fun applyTextOptions(opts: OptionsAccessor, geom: TextGeom, superscriptExponent: Boolean) {
opts.getString(Option.Geom.Text.LABEL_FORMAT)?.let { geom.formatter = StringFormat.forOneArg(it, superscriptExponent = superscriptExponent)::format }
opts.getString(Option.Geom.Text.LABEL_FORMAT)
?.let { geom.formatter = StringFormat.forOneArg(it, superscriptExponent = superscriptExponent)::format }
opts.getString(Option.Geom.Text.NA_TEXT)?.let { geom.naValue = it }
geom.sizeUnit = opts.getString(Option.Geom.Text.SIZE_UNIT)?.lowercase()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ object Option {
}

object Spoke {
const val ARROW = "arrow"
const val PIVOT = "pivot"
}

Expand Down
5 changes: 4 additions & 1 deletion python-package/lets_plot/plot/geom.py
Original file line number Diff line number Diff line change
Expand Up @@ -5860,7 +5860,7 @@ def geom_curve(mapping=None, *, data=None, stat=None, position=None, show_legend


def geom_spoke(mapping=None, *, data=None, position=None, show_legend=None, sampling=None, tooltips=None,
pivot=None,
arrow=None, pivot=None,
color_by=None, **other_args):
"""
Draw a straight line segment with given length and angle from the starting point.
Expand All @@ -5885,6 +5885,8 @@ def geom_spoke(mapping=None, *, data=None, position=None, show_legend=None, samp
tooltips : `layer_tooltips`
Result of the call to the `layer_tooltips()` function.
Specify appearance, style and content.
arrow : `FeatureSpec`
Specification for arrow head, as created by `arrow()` function.
pivot : {'tail', 'middle', 'mid', 'tip'}, default='tail'
The part of the segment that is anchored to the plane. The segment rotates about this point.
color_by : {'fill', 'color', 'paint_a', 'paint_b', 'paint_c'}, default='color'
Expand Down Expand Up @@ -5978,6 +5980,7 @@ def geom_spoke(mapping=None, *, data=None, position=None, show_legend=None, samp
show_legend=show_legend,
sampling=sampling,
tooltips=tooltips,
arrow=arrow,
pivot=pivot,
color_by=color_by,
**other_args)
Expand Down

0 comments on commit 999952d

Please sign in to comment.