# demo-lets-plot **Repository Path**: homesangsang/demo-lets-plot ## Basic Information - **Project Name**: demo-lets-plot - **Description**: jetbrains 的 lets-plot 框架在 jvm 中使用 demo - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 1 - **Created**: 2023-01-09 - **Last Updated**: 2023-06-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 理解 lets-plot 架构 * plot 绘图至少由一个 layer 表示。 它可以基于默认数据集构建,并应用美学映射、比例集或其他功能。 * layer 负责画图,包含以下内容: * Data 为每层 layer 指定数据集。一个 plot 可以包含多个不同的数据集 * Aesthetic mapping 调整颜色、图形、尺寸、位置。描述 data 中的变量 如何在 layer 的视觉属性中映射 * Geometric object 图形对象,代表了不同类型的图表 * Statistical transformation 统计变换,例如,直方图转平滑曲线 * Position adjustment 一个方法,用来计算最终图形的坐标 ![dataset, aesthetics mapping, geometric object](./images/img.png) # API 代码模板 ```kotlin import org.jetbrains.letsPlot.* import org.jetbrains.letsPlot.geom.* import org.jetbrains.letsPlot.stat.* p = letsPlot() p + geom(stat=, position=) { } ``` ## Geometric objects `geom` `geom`是个前缀,后面跟“图形类型”,格式为:`geomXxxx()`, 比如点状图(geomPoint)、折线图(geomDensity)等, 详情参考[geom reference](https://lets-plot.org/kotlin/-lets--plot--kotlin/org.jetbrains.letsPlot.geom/index.html) 还有少量的`statXxx()` 方法,这种方法也能创建一个 plot layer。某些情况下,使用`statXxx()` 比`geomXxx()` 更自然。详情参考[stat layer reference](https://lets-plot.org/kotlin/-lets--plot--kotlin/org.jetbrains.letsPlot.stat/index.html) ## Collections of plots 使用 `GGBunch().addPlot()` 方法,方便添加一组 plots,详情参考[ggbunch.ipynb](https://nbviewer.org/github/JetBrains/lets-plot-kotlin/blob/master/docs/examples/jupyter-notebooks/ggbunch.ipynb) ## Stat `stat` 把`stat` 作为 `geomXxx()` 方法的参数,来定义如何转换统计数据 ```kotlin geomPoint(stat=Stat.count()) ``` 支持的转换类型: 1. `identity`: 保持数据不变 2. `count`: 计算具有相同 x坐标轴 的点数 3. `bin`: 计算落在沿 x 轴的每个相邻相等大小范围内的点数 4. `bin2d`: 计算落在绘图平面上每个相邻大小相等的矩形中的点数 5. `smooth`: 平滑 6. `contour`,`contourFilled`: 计算 3D 数据的轮廓 7. `boxplot`: 计算箱线图的组成部分 7. `density`,`density2D`,`density2DFilled`: 对一维和二维数据执行核密度估计 ## Aesthetic mappings `mapping` 通过`mapping` ,可以定义数据集中的变量如何映射到图表的视觉元素中,向`geom` 中添加 `x=<>; y=<>; ...`,其中: * `x` 表示 x轴 * `y` 表示 y轴 * `...` 表示其他视觉属性,如颜色、形状、尺寸、位置 ```kotlin geomPoint() {x = "city"; y = "hwy"; color = "cyl"} ``` ## Position adjustment `position` 所有图层都有一个位置调整,用于计算几何的最终坐标。 位置调整用于构建相同地块的方差并解决重叠问题。 使用 `geom` 函数中的位置参数覆盖默认设置: `geomBar(position=positionFill` 或 `geomBar(position=positionDodge(with1.01))` 可用参数: * `dodge` * `jitter` * `jitterdodge` * `nudge` * `identity` * `fill` * `stack` 更多信息请参考[position functions reference](https://lets-plot.org/kotlin/-lets--plot--kotlin/org.jetbrains.letsPlot.pos/index.html) ## Features affecting the entire plot (不会翻译) 总体来说,是定义 `plot` 对象的 `plus(xxx)` 对象的参数 在 java 中,语法是 ```java plot.plus(xxxx); ``` 在 kotlin 中,语法是 ```kotlin plot + xxxx(xxxx) ``` ### scales [scale reference](https://lets-plot.org/kotlin/-lets--plot--kotlin/org.jetbrains.letsPlot.scale/index.html) ### coordinated system [coordinates reference](https://lets-plot.org/kotlin/-lets--plot--kotlin/org.jetbrains.letsPlot.coord/index.html) ### legend 图例 使用`guide` 方法 或 `scale`方法的`guide` 参数来定义图例 ```kotlin p + scaleColorDiscrete(guide=guideLedgend(ncol=2)) ``` 调整图例在 plot 上的位置,需要使用 `theme` legendPosition, legendJustification 和 legendDirection 3个方法,参考 [theme reference](https://lets-plot.org/kotlin/-lets--plot--kotlin/org.jetbrains.letsPlot.themes/index.html) ### sampling 采样 在`stat` 之后执行。尝试绘制过多的几何图形时,采样有助于防止 UI 冻结和内存不足崩溃。 [sampling reference](https://lets-plot.org/kotlin/-lets--plot--kotlin/org.jetbrains.letsPlot.sampling/index.html) # 参考文档 * [tutorial - kotlin转java](https://code-examples.net/zh-CN/q/2156876) * [demo](https://github.com/alshan/lets-plot-mini-apps.git) * [Apache batik 转换svg文件为jpeg/png/pdf](https://blog.csdn.net/qq_35893120/article/details/102702048) * [Lets-Plot User Guide](https://nbviewer.org/github/JetBrains/lets-plot-kotlin/blob/master/docs/guide/user_guide.ipynb)