-
Notifications
You must be signed in to change notification settings - Fork 47
/
GeomProtoClientSide.kt
395 lines (360 loc) · 17.3 KB
/
GeomProtoClientSide.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
* Copyright (c) 2019. 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.config
import jetbrains.datalore.base.geometry.DoubleRectangle
import jetbrains.datalore.base.geometry.DoubleVector
import jetbrains.datalore.base.spatial.projections.identity
import jetbrains.datalore.base.spatial.projections.mercator
import jetbrains.datalore.base.stringFormat.StringFormat
import jetbrains.datalore.plot.base.GeomKind
import jetbrains.datalore.plot.base.geom.*
import jetbrains.datalore.plot.base.stat.DotplotStat
import jetbrains.datalore.plot.builder.assemble.geom.GeomProvider
import jetbrains.datalore.plot.builder.coord.CoordProvider
import jetbrains.datalore.plot.builder.coord.CoordProviders
import jetbrains.datalore.plot.config.Option.Geom.Density
import jetbrains.datalore.plot.config.Option.Geom.Boxplot
import jetbrains.datalore.plot.config.Option.Geom.BoxplotOutlier
import jetbrains.datalore.plot.config.Option.Geom.AreaRidges
import jetbrains.datalore.plot.config.Option.Geom.CrossBar
import jetbrains.datalore.plot.config.Option.Geom.Dotplot
import jetbrains.datalore.plot.config.Option.Geom.Image
import jetbrains.datalore.plot.config.Option.Geom.Label
import jetbrains.datalore.plot.config.Option.Geom.Path
import jetbrains.datalore.plot.config.Option.Geom.Pie
import jetbrains.datalore.plot.config.Option.Geom.Point
import jetbrains.datalore.plot.config.Option.Geom.PointRange
import jetbrains.datalore.plot.config.Option.Geom.Segment
import jetbrains.datalore.plot.config.Option.Geom.Step
import jetbrains.datalore.plot.config.Option.Geom.Text
import jetbrains.datalore.plot.config.Option.Geom.Violin
import jetbrains.datalore.plot.config.Option.Geom.YDotplot
import jetbrains.datalore.plot.config.Option.Geom.Lollipop
import jetbrains.datalore.plot.base.geom.LollipopGeom.Direction
import jetbrains.datalore.plot.config.Option.Layer.USE_CRS
class GeomProtoClientSide(geomKind: GeomKind) : GeomProto(geomKind) {
fun preferredCoordinateSystem(layerConfig: LayerConfig): CoordProvider? {
return when (geomKind) {
GeomKind.TILE,
GeomKind.BIN_2D,
GeomKind.CONTOUR,
GeomKind.CONTOURF,
GeomKind.DENSITY2D,
GeomKind.DENSITY2DF,
GeomKind.RASTER,
GeomKind.IMAGE -> CoordProviders.fixed(1.0)
GeomKind.MAP -> CoordProviders.map(projection = identity().takeIf { layerConfig.has(USE_CRS) } ?: mercator())
else -> null
}
}
fun geomProvider(opts: OptionsAccessor): GeomProvider {
when (geomKind) {
GeomKind.DENSITY -> return GeomProvider.density {
val geom = DensityGeom()
if (opts.hasOwn(Option.Stat.Density.QUANTILES)) {
geom.quantiles = opts.getBoundedDoubleList(Option.Stat.Density.QUANTILES, 0.0, 1.0)
}
if (opts.hasOwn(Density.QUANTILE_LINES)) {
geom.quantileLines = opts.getBoolean(Density.QUANTILE_LINES, DensityGeom.DEF_QUANTILE_LINES)
}
geom
}
GeomKind.DOT_PLOT -> return GeomProvider.dotplot {
val geom = DotplotGeom()
if (opts.hasOwn(Dotplot.DOTSIZE)) {
geom.dotSize = opts.getDouble(Dotplot.DOTSIZE)!!
}
if (opts.hasOwn(Dotplot.STACKRATIO)) {
geom.stackRatio = opts.getDouble(Dotplot.STACKRATIO)!!
}
if (opts.hasOwn(Dotplot.STACKGROUPS)) {
geom.stackGroups = opts.getBoolean(Dotplot.STACKGROUPS)
}
if (opts.hasOwn(Dotplot.STACKDIR)) {
geom.stackDir = opts.getString(Dotplot.STACKDIR)!!.let {
when (it.lowercase()) {
"up" -> DotplotGeom.Stackdir.UP
"down" -> DotplotGeom.Stackdir.DOWN
"center" -> DotplotGeom.Stackdir.CENTER
"centerwhole" -> DotplotGeom.Stackdir.CENTERWHOLE
else -> throw IllegalArgumentException(
"Unsupported ${Dotplot.STACKDIR}: '$it'. " +
"Use one of: up, down, center, centerwhole."
)
}
}
}
if (opts.hasOwn(Dotplot.METHOD)) {
geom.method = DotplotStat.Method.safeValueOf(opts.getString(Dotplot.METHOD)!!)
}
geom
}
GeomKind.CROSS_BAR -> return GeomProvider.crossBar {
val geom = CrossBarGeom()
if (opts.hasOwn(CrossBar.FATTEN)) {
geom.fattenMidline = opts.getDouble(CrossBar.FATTEN)!!
}
geom
}
GeomKind.POINT_RANGE -> return GeomProvider.pointRange {
val geom = PointRangeGeom()
if (opts.hasOwn(PointRange.FATTEN)) {
geom.fattenMidPoint = opts.getDouble(PointRange.FATTEN)!!
}
geom
}
GeomKind.BOX_PLOT -> return GeomProvider.boxplot {
val geom = BoxplotGeom()
if (opts.hasOwn(Boxplot.FATTEN)) {
geom.fattenMidline = opts.getDouble(Boxplot.FATTEN)!!
}
if (opts.hasOwn(Boxplot.WHISKER_WIDTH)) {
geom.whiskerWidth = opts.getDouble(Boxplot.WHISKER_WIDTH)!!
}
if (opts.hasOwn(BoxplotOutlier.COLOR)) {
geom.outlierColor = opts.getColor(BoxplotOutlier.COLOR)!!
}
if (opts.hasOwn(BoxplotOutlier.FILL)) {
geom.outlierFill = opts.getColor(BoxplotOutlier.FILL)!!
}
geom.outlierShape = opts.getShape(BoxplotOutlier.SHAPE)
geom.outlierSize = opts.getDouble(BoxplotOutlier.SIZE)
geom.outlierStroke = opts.getDouble(BoxplotOutlier.STROKE)
geom
}
GeomKind.AREA_RIDGES -> return GeomProvider.arearidges {
val geom = AreaRidgesGeom()
if (opts.hasOwn(AreaRidges.SCALE)) {
geom.scale = opts.getDoubleDef(AreaRidges.SCALE, AreaRidgesGeom.DEF_SCALE)
}
if (opts.hasOwn(AreaRidges.MIN_HEIGHT)) {
geom.minHeight = opts.getDoubleDef(AreaRidges.MIN_HEIGHT, AreaRidgesGeom.DEF_MIN_HEIGHT)
}
if (opts.hasOwn(Option.Stat.DensityRidges.QUANTILES)) {
geom.quantiles = opts.getBoundedDoubleList(Option.Stat.DensityRidges.QUANTILES, 0.0, 1.0)
}
if (opts.hasOwn(AreaRidges.QUANTILE_LINES)) {
geom.quantileLines = opts.getBoolean(AreaRidges.QUANTILE_LINES, AreaRidgesGeom.DEF_QUANTILE_LINES)
}
geom
}
GeomKind.VIOLIN -> return GeomProvider.violin {
val geom = ViolinGeom()
if (opts.hasOwn(Option.Stat.YDensity.QUANTILES)) {
geom.quantiles = opts.getBoundedDoubleList(Option.Stat.YDensity.QUANTILES, 0.0, 1.0)
}
if (opts.hasOwn(Violin.QUANTILE_LINES)) {
geom.quantileLines = opts.getBoolean(Violin.QUANTILE_LINES, ViolinGeom.DEF_QUANTILE_LINES)
}
if (opts.hasOwn(Violin.SHOW_HALF)) {
geom.showHalf = opts.getDouble(Violin.SHOW_HALF)!!
}
geom
}
GeomKind.Y_DOT_PLOT -> return GeomProvider.ydotplot {
val geom = YDotplotGeom()
if (opts.hasOwn(YDotplot.DOTSIZE)) {
geom.dotSize = opts.getDouble(YDotplot.DOTSIZE)!!
}
if (opts.hasOwn(YDotplot.STACKRATIO)) {
geom.stackRatio = opts.getDouble(YDotplot.STACKRATIO)!!
}
if (opts.hasOwn(YDotplot.STACKGROUPS)) {
geom.stackGroups = opts.getBoolean(YDotplot.STACKGROUPS)
}
if (opts.hasOwn(YDotplot.STACKDIR)) {
geom.yStackDir = opts.getString(YDotplot.STACKDIR)!!.let {
when (it.lowercase()) {
"left" -> YDotplotGeom.YStackdir.LEFT
"right" -> YDotplotGeom.YStackdir.RIGHT
"center" -> YDotplotGeom.YStackdir.CENTER
"centerwhole" -> YDotplotGeom.YStackdir.CENTERWHOLE
else -> throw IllegalArgumentException(
"Unsupported ${YDotplot.STACKDIR}: '$it'. " +
"Use one of: left, right, center, centerwhole."
)
}
}
}
if (opts.hasOwn(YDotplot.METHOD)) {
geom.method = DotplotStat.Method.safeValueOf(opts.getString(YDotplot.METHOD)!!)
}
geom
}
GeomKind.STEP -> return GeomProvider.step {
val geom = StepGeom()
if (opts.hasOwn(Step.DIRECTION)) {
geom.setDirection(opts.getString(Step.DIRECTION)!!)
}
geom
}
GeomKind.SEGMENT -> return GeomProvider.segment {
val geom = SegmentGeom()
if (opts.has(Segment.ARROW)) {
val cfg1 = ArrowSpecConfig.create(opts[Segment.ARROW]!!)
geom.arrowSpec = cfg1.createArrowSpec()
}
if (opts.has(Segment.ANIMATION)) {
geom.animation = opts[Segment.ANIMATION]
}
if (opts.has(Segment.FLAT)) {
geom.flat = opts.getBoolean(Segment.FLAT)
}
if (opts.has(Segment.GEODESIC)) {
geom.geodesic = opts.getBoolean(Segment.GEODESIC)
}
geom
}
GeomKind.PATH -> return GeomProvider.path {
val geom = PathGeom()
if (opts.has(Path.ANIMATION)) {
geom.animation = opts[Path.ANIMATION]
}
if (opts.has(Path.FLAT)) {
geom.flat = opts.getBoolean(Path.FLAT)
}
if (opts.has(Segment.GEODESIC)) {
geom.geodesic = opts.getBoolean(Segment.GEODESIC)
}
geom
}
GeomKind.POINT -> return GeomProvider.point {
val geom = PointGeom()
if (opts.has(Point.ANIMATION)) {
geom.animation = opts[Point.ANIMATION]
}
geom.sizeUnit = opts.getString(Point.SIZE_UNIT)?.lowercase()
geom
}
GeomKind.TEXT -> return GeomProvider.text {
val geom = TextGeom()
applyTextOptions(opts, geom)
geom
}
GeomKind.LABEL -> return GeomProvider.label {
val geom = LabelGeom()
applyTextOptions(opts, geom)
opts.getDouble(Label.LABEL_PADDING)?.let { geom.paddingFactor = it }
opts.getDouble(Label.LABEL_R)?.let { geom.radiusFactor = it }
opts.getDouble(Label.LABEL_SIZE)?.let { geom.borderWidth = it }
geom
}
GeomKind.IMAGE -> return GeomProvider.image {
require(opts.hasOwn(Image.HREF)) { "Image reference URL (href) is not specified." }
for (s in listOf(Image.XMIN, Image.XMAX, Image.YMIN, Image.YMAX)) {
require(opts.hasOwn(s)) { "'$s' is not specified." }
}
ImageGeom(
imageUrl = opts.getString(Image.HREF)!!,
bbox = DoubleRectangle.span(
DoubleVector(opts.getDoubleSafe(Image.XMIN), opts.getDoubleSafe(Image.YMIN)),
DoubleVector(opts.getDoubleSafe(Image.XMAX), opts.getDoubleSafe(Image.YMAX)),
)
)
}
GeomKind.PIE -> return GeomProvider.pie {
val geom = PieGeom()
opts.getDouble(Pie.HOLE)?.let { geom.holeSize = it }
opts.getDouble(Pie.STROKE)?.let { geom.strokeWidth = it }
opts.getColor(Pie.STROKE_COLOR)?.let { geom.strokeColor = it }
geom
}
GeomKind.LOLLIPOP -> return GeomProvider.lollipop {
val directionValue = opts.getString(Lollipop.DIRECTION)?.lowercase()
val direction = directionValue?.let {
when (it) {
"v" -> Direction.ORTHOGONAL_TO_AXIS
"h" -> Direction.ALONG_AXIS
"s" -> Direction.SLOPE
else -> throw IllegalArgumentException(
"Unsupported value for ${Lollipop.DIRECTION} parameter: '$it'. " +
"Use one of: v, h, s."
)
}
} ?: Direction.ORTHOGONAL_TO_AXIS
val slope = if (opts.hasOwn(Lollipop.SLOPE)) {
opts.getDouble(Lollipop.SLOPE)!!
} else {
0.0
}
if (direction == Direction.ALONG_AXIS && slope == 0.0) {
throw IllegalArgumentException(
"Incompatible lollipop parameters: " +
"${Lollipop.SLOPE}=$slope, " +
"${Lollipop.DIRECTION}='${directionValue}'"
)
}
LollipopGeom().apply {
this.direction = direction
this.slope = slope
if (opts.hasOwn(Lollipop.INTERCEPT)) {
this.intercept = opts.getDouble(Lollipop.INTERCEPT)!!
}
if (opts.hasOwn(Lollipop.FATTEN)) {
this.fatten = opts.getDouble(Lollipop.FATTEN)!!
}
}
}
else -> {
require(PROVIDER.containsKey(geomKind)) { "Provider doesn't support geom kind: '$geomKind'" }
return PROVIDER[geomKind]!!
}
}
}
private companion object {
private val PROVIDER = HashMap<GeomKind, GeomProvider>()
init {
PROVIDER[GeomKind.POINT] = GeomProvider.point()
PROVIDER[GeomKind.PATH] = GeomProvider.path()
PROVIDER[GeomKind.LINE] = GeomProvider.line()
PROVIDER[GeomKind.SMOOTH] = GeomProvider.smooth()
PROVIDER[GeomKind.BAR] = GeomProvider.bar()
PROVIDER[GeomKind.HISTOGRAM] = GeomProvider.histogram()
// dotplot - special case
PROVIDER[GeomKind.TILE] = GeomProvider.tile()
PROVIDER[GeomKind.BIN_2D] = GeomProvider.bin2d()
PROVIDER[GeomKind.ERROR_BAR] = GeomProvider.errorBar()
PROVIDER[GeomKind.ERROR_BAR_H] = GeomProvider.errorBarH()
// crossbar - special case
// pointrange - special case
PROVIDER[GeomKind.LINE_RANGE] = GeomProvider.lineRange()
PROVIDER[GeomKind.CONTOUR] = GeomProvider.contour()
PROVIDER[GeomKind.CONTOURF] = GeomProvider.contourf()
PROVIDER[GeomKind.POLYGON] = GeomProvider.polygon()
PROVIDER[GeomKind.MAP] = GeomProvider.map()
PROVIDER[GeomKind.AB_LINE] = GeomProvider.abline()
PROVIDER[GeomKind.H_LINE] = GeomProvider.hline()
PROVIDER[GeomKind.V_LINE] = GeomProvider.vline()
// boxplot - special case
// area ridges - special case
// violin - special case
PROVIDER[GeomKind.RIBBON] = GeomProvider.ribbon()
PROVIDER[GeomKind.AREA] = GeomProvider.area()
// density - special case
PROVIDER[GeomKind.DENSITY2D] = GeomProvider.density2d()
PROVIDER[GeomKind.DENSITY2DF] = GeomProvider.density2df()
PROVIDER[GeomKind.JITTER] = GeomProvider.jitter()
PROVIDER[GeomKind.Q_Q] = GeomProvider.qq()
PROVIDER[GeomKind.Q_Q_2] = GeomProvider.qq2()
PROVIDER[GeomKind.Q_Q_LINE] = GeomProvider.qqline()
PROVIDER[GeomKind.Q_Q_2_LINE] = GeomProvider.qq2line()
PROVIDER[GeomKind.FREQPOLY] = GeomProvider.freqpoly()
// step - special case
PROVIDER[GeomKind.RECT] = GeomProvider.rect()
// segment - special case
// text, label - special case
PROVIDER[GeomKind.RASTER] = GeomProvider.raster()
// image - special case
// pie - special case
PROVIDER[GeomKind.LIVE_MAP] = GeomProvider.livemap()
// lollipop - special case
}
private fun applyTextOptions(opts: OptionsAccessor, geom: TextGeom) {
opts.getString(Text.LABEL_FORMAT)?.let { geom.formatter = StringFormat.forOneArg(it)::format }
opts.getString(Text.NA_TEXT)?.let { geom.naValue = it }
geom.sizeUnit = opts.getString(Text.SIZE_UNIT)?.lowercase()
}
}
}