Skip to content

Commit fee3768

Browse files
ggrepel
1 parent 272f2f6 commit fee3768

File tree

12 files changed

+7920
-0
lines changed

12 files changed

+7920
-0
lines changed

docs/examples/jupyter-notebooks/dev/RepealNotebookScript.kt

Lines changed: 478 additions & 0 deletions
Large diffs are not rendered by default.

docs/examples/jupyter-notebooks/f-4.11.0/ggrepel.ipynb

Lines changed: 6697 additions & 0 deletions
Large diffs are not rendered by default.

future_changes.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ GeoTools [v 33.2](https://github.com/geotools/geotools/releases/tag/33.2)
55

66
### Added
77

8+
- Geometries:
9+
- `geomTextRepel()` and `geomLabelRepel()` [[#1092](https://github.com/JetBrains/lets-plot/issues/1092)].
10+
See: [example notebook](https://nbviewer.org/github/JetBrains/lets-plot-kotlin/blob/master/docs/examples/jupyter-notebooks/f-4.11.0/ggrepel.ipynb).
11+
12+
813
### Changed
914

1015
### Fixed

plot-api/src/commonMain/kotlin/org/jetbrains/letsPlot/Geom.kt

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,128 @@ object Geom {
483483
super<WithFillOption>.seal()
484484
}
485485

486+
@Suppress("ClassName")
487+
class textRepel(
488+
override val x: Number? = null,
489+
override val y: Number? = null,
490+
override val label: String? = null,
491+
override val alpha: Number? = null,
492+
override val color: Any? = null,
493+
override val size: Number? = null,
494+
override val family: String? = null,
495+
override val fontface: String? = null,
496+
override val hjust: Any? = null,
497+
override val vjust: Any? = null,
498+
override val angle: Number? = null,
499+
override val shape: Any? = null,
500+
override val pointSize: Any? = null,
501+
override val pointStroke: Any? = null,
502+
override val segmentColor: Any? = null,
503+
override val segmentSize: Any? = null,
504+
override val segmentAlpha: Any? = null,
505+
override val linetype: Any? = null,
506+
override val lineheight: Number? = null,
507+
override val labelFormat: String? = null,
508+
override val naText: String? = null,
509+
override val nudgeX: Number? = null,
510+
override val nudgeY: Number? = null,
511+
override val sizeUnit: String? = null,
512+
override val nudgeUnit: String? = null,
513+
override val colorBy: String? = null,
514+
override val checkOverlap: Boolean? = null,
515+
override val seed: Int? = null,
516+
override val maxIter: Int? = null,
517+
override val maxTime: Double? = null,
518+
override val direction: String? = null,
519+
override val pointPadding: Float? = null,
520+
override val boxPadding: Float? = null,
521+
override val maxOverlaps: Int? = null,
522+
override val minSegmentLength: Float? = null,
523+
override val arrow: Map<String, Any>? = null,
524+
mapping: TextRepelMapping.() -> Unit = {},
525+
) : TextRepelAesthetics,
526+
TextParameters,
527+
RepelParameters,
528+
WithSizeUnitOption,
529+
WithColorOption,
530+
GeomOptions(
531+
GeomKind.TEXT_REPEL,
532+
TextRepelMapping().apply(mapping).seal()
533+
) {
534+
override val parameters = this.seal()
535+
536+
override fun seal() = super<TextRepelAesthetics>.seal() +
537+
super<TextParameters>.seal() +
538+
super<RepelParameters>.seal() +
539+
super<WithSizeUnitOption>.seal() +
540+
super<WithColorOption>.seal()
541+
}
542+
543+
@Suppress("ClassName")
544+
class labelRepel(
545+
override val x: Number? = null,
546+
override val y: Number? = null,
547+
override val label: String? = null,
548+
override val alpha: Number? = null,
549+
override val color: Any? = null,
550+
override val fill: Any? = null,
551+
override val size: Number? = null,
552+
override val family: String? = null,
553+
override val fontface: String? = null,
554+
override val hjust: Any? = null,
555+
override val vjust: Any? = null,
556+
override val angle: Number? = null,
557+
override val shape: Any? = null,
558+
override val pointSize: Any? = null,
559+
override val pointStroke: Any? = null,
560+
override val segmentColor: Any? = null,
561+
override val segmentSize: Any? = null,
562+
override val segmentAlpha: Any? = null,
563+
override val linetype: Any? = null,
564+
override val lineheight: Number? = null,
565+
override val labelFormat: String? = null,
566+
override val naText: String? = null,
567+
override val nudgeX: Number? = null,
568+
override val nudgeY: Number? = null,
569+
override val nudgeUnit: String? = null,
570+
override val labelPadding: Number? = null,
571+
override val labelR: Number? = null,
572+
override val labelSize: Number? = null,
573+
override val alphaStroke: Boolean? = null,
574+
override val sizeUnit: String? = null,
575+
override val colorBy: String? = null,
576+
override val fillBy: String? = null,
577+
override val checkOverlap: Boolean? = null,
578+
override val seed: Int? = null,
579+
override val maxIter: Int? = null,
580+
override val maxTime: Double? = null,
581+
override val direction: String? = null,
582+
override val pointPadding: Float? = null,
583+
override val boxPadding: Float? = null,
584+
override val maxOverlaps: Int? = null,
585+
override val minSegmentLength: Float? = null,
586+
override val arrow: Map<String, Any>? = null,
587+
mapping: LabelRepelMapping.() -> Unit = {},
588+
) : LabelRepelAesthetics,
589+
LabelParameters,
590+
RepelParameters,
591+
WithSizeUnitOption,
592+
WithColorOption,
593+
WithFillOption,
594+
GeomOptions(
595+
GeomKind.LABEL_REPEL,
596+
LabelRepelMapping().apply(mapping).seal()
597+
) {
598+
override val parameters = this.seal()
599+
600+
override fun seal() = super<LabelRepelAesthetics>.seal() +
601+
super<LabelParameters>.seal() +
602+
super<RepelParameters>.seal() +
603+
super<WithSizeUnitOption>.seal() +
604+
super<WithColorOption>.seal() +
605+
super<WithFillOption>.seal()
606+
}
607+
486608
@Suppress("ClassName")
487609
class boxplot(
488610
override val x: Number? = null,
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
package org.jetbrains.letsPlot.geom
2+
3+
import org.jetbrains.letsPlot.Geom.labelRepel
4+
import org.jetbrains.letsPlot.Stat
5+
import org.jetbrains.letsPlot.intern.Layer
6+
import org.jetbrains.letsPlot.intern.Options
7+
import org.jetbrains.letsPlot.intern.layer.*
8+
import org.jetbrains.letsPlot.intern.layer.geom.LabelParameters
9+
import org.jetbrains.letsPlot.intern.layer.geom.LabelRepelAesthetics
10+
import org.jetbrains.letsPlot.intern.layer.geom.LabelRepelMapping
11+
import org.jetbrains.letsPlot.intern.layer.geom.RepelParameters
12+
import org.jetbrains.letsPlot.pos.positionIdentity
13+
import org.jetbrains.letsPlot.spatial.SpatialDataset
14+
import org.jetbrains.letsPlot.tooltips.TooltipOptions
15+
16+
@Suppress("ClassName")
17+
/**
18+
* Add repelling text labels with background boxes to the plot.
19+
*
20+
* ## Examples
21+
*
22+
* - [ggrepel.ipynb](https://nbviewer.org/github/JetBrains/lets-plot-kotlin/blob/master/docs/examples/jupyter-notebooks/f-4.11.0/ggrepel.ipynb)
23+
*
24+
* @param data The data to be displayed in this layer. If null, the default, the data
25+
* is inherited from the plot data as specified in the call to [letsPlot][org.jetbrains.letsPlot.letsPlot].
26+
* @param stat The statistical transformation to use on the data for this layer.
27+
* Supported transformations: `Stat.identity`, `Stat.bin()`, `Stat.count()`, etc. see [Stat][org.jetbrains.letsPlot.Stat].
28+
* @param position Position adjustment: `positionIdentity`, `positionStack()`,
29+
* `positionDodge()`, etc. see [Position](https://lets-plot.org/kotlin/-lets--plot--kotlin/org.jetbrains.letsPlot.pos/).
30+
* @param showLegend default = true.
31+
* false - do not show legend for this layer.
32+
* @param inheritAes default = true.
33+
* false - do not combine the layer aesthetic mappings with the plot shared mappings.
34+
* @param manualKey String or result of the call to the `layerKey()` function.
35+
* The key to show in the manual legend. Specifies the text for the legend label or advanced settings using the `layerKey()` function.
36+
* @param sampling Result of the call to the `samplingXxx()` function.
37+
* To prevent any sampling for this layer pass value `samplingNone`.
38+
* For more info see [sampling.html](https://lets-plot.org/kotlin/sampling.html).
39+
* @param tooltips Result of the call to the `layerTooltips()` function.
40+
* Specifies appearance, style and content.
41+
* Set `tooltips = tooltipsNone` to hide tooltips from the layer.
42+
* @param map Data-structure containing series of planar shapes and, optionally,
43+
* associated data series (for example: names of States and their boundaries).
44+
*
45+
* Supported shapes: Point and MultiPoint.
46+
* All coordinates should be encoded as decimal degrees in WGS84 coordinate reference system.
47+
*
48+
* Can be used with parameter `mapJoin` for joining data and map coordinates.
49+
* @param mapJoin Pair of Names or Pair of Lists of Names.
50+
* Specifies column names to join the `data` and the `map` coordinates on.
51+
* - Pair.first: column name or list of column names in the `data` dataframe.
52+
* - Pair.second: column name or list of column names in the `map` dataframe.
53+
* @param useCRS By default, all coordinates are converted into degrees of longitude and latitude,
54+
* and these map coordinates are projected onto the screen coordinates using Mercator projection.
55+
* Specify useCRS = "provided" to keep the SpatialDataset's original coordinate reference system (CRS).
56+
* @param x X-axis value.
57+
* @param y Y-axis value.
58+
* @param label Text to add to plot.
59+
* @param alpha Transparency level of a layer.
60+
* Understands numbers between 0 and 1.
61+
* @param color Color of the geometry.
62+
* For more info see: [aesthetics.html#color-and-fill](https://lets-plot.org/kotlin/aesthetics.html#color-and-fill).
63+
* @param fill Background color of the label.
64+
* For more info see: [aesthetics.html#color-and-fill](https://lets-plot.org/kotlin/aesthetics.html#color-and-fill).
65+
* @param size Font size.
66+
* @param family default = "sans".
67+
* For more info see: [aesthetics.html#font-family](https://lets-plot.org/kotlin/aesthetics.html#font-family).
68+
* @param fontface default = "plain".
69+
* For more info see: [aesthetics.html#font-face](https://lets-plot.org/kotlin/aesthetics.html#font-face).
70+
* @param hjust ("left", "middle", "right") or number between 0 ("left") and 1 ("right")
71+
* or "inward" (aligns label towards the plot center), "outward" (away from the plot center).
72+
* Horizontal label alignment.
73+
* @param vjust ("bottom", "center", "top") or number between 0 ("bottom") and 1 ("top")
74+
* or "inward" (aligns label towards the plot center), "outward" (away from the plot center).
75+
* Vertical label alignment.
76+
* @param angle Label rotation angle in degrees.
77+
* @param lineheight Line height multiplier applied to the font size in the case of multi-line text.
78+
* @param labelFormat Specifies the format pattern for displaying mapped values.
79+
* @param naText default = "n/a". Text to show for missing values.
80+
* @param nudgeX Horizontal adjustment to nudge labels by.
81+
* @param nudgeY Vertical adjustment to nudge labels by.
82+
* @param labelPadding Amount of padding around label. Defaults to 0.25 of font size.
83+
* @param labelR Radius of rounded corners. Defaults to 0.15 of label height.
84+
* @param labelSize default = 1.0. Size of label border.
85+
* @param alphaStroke default = false.
86+
* Enables the applying of 'alpha' to 'color' (label text and border).
87+
* @param sizeUnit Relates the size of the text label to the length of the unit step along one of the axes.
88+
* Possible values: "x", "y". If not specified, no fitting is performed.
89+
* @param nudgeUnit ("identity", "size", "px") Units for x and y nudging.
90+
* Possible values:
91+
* - "identity": a unit of 1 corresponds to a difference of 1 in data space;
92+
* - "size": a unit of 1 corresponds to the diameter of a point with `size=1`;
93+
* - "px": the unit is measured in screen pixels.
94+
* @param colorBy default = "color" ("fill", "color", "paint_a", "paint_b", "paint_c").
95+
* Defines the color aesthetic for the geometry.
96+
* @param fillBy default = "fill" ("fill", "color", "paint_a", "paint_b", "paint_c").
97+
* Defines the fill aesthetic for the geometry.
98+
* @param checkOverlap Skips plotting text that overlaps previous text in the same layer.
99+
* @param seed Random seed for reproducibility.
100+
* @param maxIter Maximum number of iterations for the repulsion algorithm.
101+
* @param maxTime Maximum allowed time in seconds for resolving label collisions.
102+
* @param direction default = "both" ("both", "x", "y").
103+
* Defines the direction in which labels can be moved.
104+
* @param pointPadding Padding around the data point.
105+
* @param boxPadding Padding around the label box.
106+
* @param maxOverlaps Max number of overlapping labels to allow.
107+
* @param minSegmentLength Minimum length of the connecting line segment. Shorter ones are omitted.
108+
* @param arrow Specification for arrow head, as created by `arrow()` function.
109+
* @param mapping Set of aesthetic mappings.
110+
* Aesthetic mappings describe the way that variables in the data are
111+
* mapped to plot "aesthetics".
112+
*
113+
*
114+
* Format patterns in the `labelFormat` parameter can be just a number format (like "d") or
115+
* a string template where number format is surrounded by curly braces: "{d} cylinders".
116+
* For more info see: [formats.html](https://lets-plot.org/kotlin/formats.html)
117+
* Note: the "$" must be escaped as "\$".
118+
*
119+
* Examples:
120+
* - ".2f" -> "12.45"
121+
* - "Score: {.2f}" -> "Score: 12.45"
122+
* - "Score: {}" -> "Score: 12.454789"
123+
*
124+
*/
125+
class geomLabelRepel(
126+
data: Map<*, *>? = null,
127+
stat: StatOptions = Stat.identity,
128+
position: PosOptions = positionIdentity,
129+
showLegend: Boolean = true,
130+
inheritAes: Boolean? = null,
131+
manualKey: Any? = null,
132+
sampling: SamplingOptions? = null,
133+
tooltips: TooltipOptions? = null,
134+
override val map: SpatialDataset? = null,
135+
override val mapJoin: Pair<Any, Any>? = null,
136+
override val useCRS: String? = null,
137+
override val x: Number? = null,
138+
override val y: Number? = null,
139+
override val label: String? = null,
140+
override val alpha: Number? = null,
141+
override val color: Any? = null,
142+
override val fill: Any? = null,
143+
override val size: Number? = null,
144+
override val family: String? = null,
145+
override val fontface: String? = null,
146+
override val hjust: Any? = null,
147+
override val vjust: Any? = null,
148+
override val angle: Number? = null,
149+
override val shape: Any? = null,
150+
override val pointSize: Any? = null,
151+
override val pointStroke: Any? = null,
152+
override val segmentColor: Any? = null,
153+
override val segmentSize: Any? = null,
154+
override val segmentAlpha: Any? = null,
155+
override val linetype: Any? = null,
156+
override val lineheight: Number? = null,
157+
override val labelFormat: String? = null,
158+
override val naText: String? = null,
159+
override val nudgeX: Number? = null,
160+
override val nudgeY: Number? = null,
161+
override val nudgeUnit: String? = null,
162+
override val labelPadding: Number? = null,
163+
override val labelR: Number? = null,
164+
override val labelSize: Number? = null,
165+
override val alphaStroke: Boolean? = null,
166+
override val sizeUnit: String? = null,
167+
override val colorBy: String? = null,
168+
override val fillBy: String? = null,
169+
override val checkOverlap: Boolean? = null,
170+
override val seed: Int? = null,
171+
override val maxIter: Int? = null,
172+
override val maxTime: Double? = null,
173+
override val direction: String? = null,
174+
override val pointPadding: Float? = null,
175+
override val boxPadding: Float? = null,
176+
override val maxOverlaps: Int? = null,
177+
override val minSegmentLength: Float? = null,
178+
override val arrow: Map<String, Any>? = null,
179+
mapping: LabelRepelMapping.() -> Unit = {},
180+
) : LabelRepelAesthetics,
181+
LabelParameters,
182+
RepelParameters,
183+
WithSizeUnitOption,
184+
WithSpatialParameters,
185+
WithColorOption,
186+
WithFillOption,
187+
Layer(
188+
mapping = LabelRepelMapping().apply(mapping).seal(),
189+
data = data,
190+
geom = labelRepel(),
191+
stat = stat,
192+
position = position,
193+
showLegend = showLegend,
194+
inheritAes = inheritAes,
195+
manualKey = manualKey,
196+
sampling = sampling,
197+
tooltips = tooltips
198+
) {
199+
override fun seal(): Options {
200+
return super<LabelRepelAesthetics>.seal() +
201+
super<LabelParameters>.seal() +
202+
super<RepelParameters>.seal() +
203+
super<WithSizeUnitOption>.seal() +
204+
super<WithColorOption>.seal() +
205+
super<WithFillOption>.seal()
206+
}
207+
}

0 commit comments

Comments
 (0)