Skip to content

Commit

Permalink
Add tooltips for geom_curve() (#1054)
Browse files Browse the repository at this point in the history
* Add tooltip for curve.

* Fix `SvgElementHelper#createLine()/createCurve()` methods: return visible geometry (after paddings applying).

* py: add 'tooltips' parameter to `geom_curve()`.
  • Loading branch information
OLarionova-HORIS committed Mar 21, 2024
1 parent ff219db commit 57a5341
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ class SegmentAndCurve {
fun plotSpecList(): List<MutableMap<String, Any>> {
return listOf(
// grid(curvature = 0.9, angle = 45.0),
grid(curvature = 1.3, angle = 135.0),
example(curvature = 0.9, angle = -45.0),
withArrow()
// grid(curvature = 1.3, angle = 135.0),
// example(curvature = 0.9, angle = -45.0),
withArrow(),
withTooltips(),
)
}

Expand Down Expand Up @@ -49,8 +50,9 @@ class SegmentAndCurve {
return HashMap(parsePlotSpec(spec))
}

private fun grid(curvature: Double, angle: Double): MutableMap<String, Any> {
fun plot(curvature: Double, angle: Double) = """{
private fun plot(curvature: Double, angle: Double, ncp: Int = 5, withTooltips: Boolean = false): String {
val tooltips = if (withTooltips) ",\n\"tooltips\": { \"lines\": [\"Tooltip\"]}" else ""
return """{
"data": {
"x": [-30.0, 30.0],
"y": [-3.0, 3.0]
Expand All @@ -59,7 +61,7 @@ class SegmentAndCurve {
"x": "x",
"y": "y"
},
"ggtitle": { "text": "curvature = $curvature, angle=$angle" },
"ggtitle": { "text": "curvature = $curvature, angle=$angle, ncp=$ncp" },
"kind": "plot",
'theme': { 'name' : 'grey' },
"layers": [
Expand All @@ -74,12 +76,15 @@ class SegmentAndCurve {
"yend": -1.0,
"curvature": $curvature,
"angle": $angle,
"ncp": 5.0,
"ncp": $ncp,
"arrow": {"name": "arrow", "ends": "both", "type": "open"}
$tooltips
}
]
}""".trimIndent()
}

private fun grid(curvature: Double, angle: Double): MutableMap<String, Any> {
val spec = """{
"kind": "subplots",
"layout": {
Expand Down Expand Up @@ -173,7 +178,8 @@ class SegmentAndCurve {
"length": 24.0,
"ends": "both",
"type": "open"
}
},
"tooltips": "none"
},
{
"geom": "curve",
Expand Down Expand Up @@ -204,5 +210,23 @@ class SegmentAndCurve {
return parsePlotSpec(spec)
}

private fun withTooltips(): MutableMap<String, Any> {
val spec = """{
"kind": "subplots",
"layout": {
"ncol": 2.0,
"nrow": 2.0,
"name": "grid"
},
"figures": [
${plot(0.9, 45.0, ncp = 1, withTooltips = true)},
${plot(0.9, 45.0, ncp = 2, withTooltips = true)},
${plot(0.9, 45.0, ncp = 5, withTooltips = true)},
${plot(0.9, 45.0, ncp = 7, withTooltips = true)}
]
}""".trimIndent()

return parsePlotSpec(spec)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.jetbrains.letsPlot.core.plot.base.*
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.toLocation
import org.jetbrains.letsPlot.core.plot.base.geom.util.TargetCollectorHelper
import org.jetbrains.letsPlot.core.plot.base.render.LegendKeyElementFactory
import org.jetbrains.letsPlot.core.plot.base.render.SvgRoot
import org.jetbrains.letsPlot.datamodel.svg.dom.SvgPathDataBuilder
Expand Down Expand Up @@ -36,6 +37,7 @@ class CurveGeom : GeomBase() {
coord: CoordinateSystem,
ctx: GeomContext
) {
val tooltipHelper = TargetCollectorHelper(GeomKind.CURVE, ctx)
val geomHelper = GeomHelper(pos, coord, ctx)
val svgElementHelper = geomHelper
.createSvgElementHelper()
Expand All @@ -50,8 +52,12 @@ class CurveGeom : GeomBase() {
// Create curve geometry
// inverse angle because of using client coordinates
val (svg) = svgElementHelper.createCurve(start, end, curvature, -angle, ncp, p) ?: continue

root.add(svg)

// Add tooltips
val (_, geometry) = svgElementHelper
.createCurve(start, end, curvature, -angle, ncp = 15, p) ?: continue
tooltipHelper.addLine(geometry, p)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ object HintColorUtil {
strokeColorGetter(p).takeIf { isMappedColor(p) },
)

PATH, CONTOUR, DENSITY2D, FREQPOLY, LINE, STEP, H_LINE, V_LINE, SEGMENT, SPOKE, SMOOTH ->
PATH, CONTOUR, DENSITY2D, FREQPOLY, LINE, STEP, H_LINE, V_LINE, SEGMENT, CURVE, SPOKE, SMOOTH ->
listOf(strokeColorGetter(p)) // show even without mapping (usecase - layers with const color)

DENSITY -> when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ object GeomInteractionUtil {
GeomKind.AREA_RIDGES,
GeomKind.VIOLIN,
GeomKind.LOLLIPOP,
GeomKind.SPOKE -> return GeomTooltipSetup.bivariateFunction(GeomTooltipSetup.NON_AREA_GEOM)
GeomKind.SPOKE,
GeomKind.CURVE -> return GeomTooltipSetup.bivariateFunction(GeomTooltipSetup.NON_AREA_GEOM)

GeomKind.Q_Q_LINE,
GeomKind.Q_Q_2_LINE,
Expand Down Expand Up @@ -300,7 +301,7 @@ object GeomInteractionUtil {
GeomKind.AREA_RIDGES -> listOf(Aes.QUANTILE)
GeomKind.BOX_PLOT -> listOf(Aes.Y)
GeomKind.RECT -> listOf(Aes.XMIN, Aes.YMIN, Aes.XMAX, Aes.YMAX)
GeomKind.SEGMENT -> listOf(Aes.X, Aes.Y, Aes.XEND, Aes.YEND)
GeomKind.SEGMENT, GeomKind.CURVE -> listOf(Aes.X, Aes.Y, Aes.XEND, Aes.YEND)
GeomKind.SPOKE -> listOf(Aes.X, Aes.Y, Aes.ANGLE, Aes.RADIUS)
GeomKind.RIBBON,
GeomKind.LINE_RANGE,
Expand Down Expand Up @@ -443,6 +444,7 @@ object GeomInteractionUtil {
GeomKind.FREQPOLY,
GeomKind.PATH,
GeomKind.SEGMENT,
GeomKind.CURVE,
GeomKind.SPOKE,
GeomKind.RIBBON,
GeomKind.SMOOTH,
Expand Down
6 changes: 5 additions & 1 deletion python-package/lets_plot/plot/geom.py
Original file line number Diff line number Diff line change
Expand Up @@ -5712,7 +5712,7 @@ def geom_segment(mapping=None, *, data=None, stat=None, position=None, show_lege
**other_args)


def geom_curve(mapping=None, *, data=None, stat=None, position=None, show_legend=None, sampling=None,
def geom_curve(mapping=None, *, data=None, stat=None, position=None, show_legend=None, sampling=None, tooltips=None,
arrow=None,
curvature=None, angle=None, ncp=None,
spacer=None,
Expand Down Expand Up @@ -5744,6 +5744,9 @@ def geom_curve(mapping=None, *, data=None, stat=None, position=None, show_legend
sampling : `FeatureSpec`
Result of the call to the `sampling_xxx()` function.
To prevent any sampling for this layer pass value "none" (string "none").
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.
curvature : float, default=0.5
Expand Down Expand Up @@ -5852,6 +5855,7 @@ def geom_curve(mapping=None, *, data=None, stat=None, position=None, show_legend
position=position,
show_legend=show_legend,
sampling=sampling,
tooltips=tooltips,
arrow=arrow,
curvature=curvature, angle=angle, ncp=ncp,
spacer=spacer,
Expand Down

0 comments on commit 57a5341

Please sign in to comment.