You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin Kandy直方图示例报错:Unresolved reference: histogram

解决Kotlin Kandy中histogram未解析的引用问题

问题场景

尝试复现Kandy官方的简单直方图示例,编写代码如下:

val random = java.util.Random(42)

val dataframe = dataFrameOf(
    "sample" to List(1000) { random.nextGaussian() }
)

dataframe.plot {
    histogram("sample")
}

编译时触发错误:

Unresolved reference: histogram

尝试导入org.jetbrains.kotlinx.statistics.kandy.layers.histogram但无法解析,当前项目依赖配置为:

dependencies {
  implementation("org.jetbrains.kotlinx:kandy-lets-plot:0.7.1")
  implementation("org.jetbrains.kotlinx:kandy-echarts:0.7.1")
  implementation("org.jetbrains.kotlinx:kandy-util:0.7.1")
  implementation("org.jetbrains.kotlinx:kandy-api:0.7.1")
}

问题原因

Kandy 0.7.1版本中,histogram图层属于Lets-Plot后端的统计扩展模块,未包含在基础的kandy-lets-plot依赖中;同时测试代码中的导入路径并非正式发布版本的正确路径。

解决方案

1. 添加缺失的依赖

在dependencies中添加kandy-lets-plot-statistics模块,版本保持与其他Kandy依赖一致:

dependencies {
  implementation("org.jetbrains.kotlinx:kandy-lets-plot:0.7.1")
  implementation("org.jetbrains.kotlinx:kandy-lets-plot-statistics:0.7.1") // 新增统计扩展依赖
  implementation("org.jetbrains.kotlinx:kandy-echarts:0.7.1")
  implementation("org.jetbrains.kotlinx:kandy-util:0.7.1")
  implementation("org.jetbrains.kotlinx:kandy-api:0.7.1")
}

2. 使用正确的导入语句

替换错误的导入,使用正式发布版本的正确路径:

import org.jetbrains.kotlinx.kandy.letsplot.layers.histogram

3. 修改后的完整代码

import org.jetbrains.kotlinx.dataframe.dataFrameOf
import org.jetbrains.kotlinx.kandy.letsplot.layers.histogram
import org.jetbrains.kotlinx.kandy.letsplot.plot

fun main() {
    val random = java.util.Random(42)

    val dataframe = dataFrameOf(
        "sample" to List(1000) { random.nextGaussian() }
    )

    dataframe.plot {
        histogram("sample")
    }
}

验证

同步项目依赖后重新编译运行,即可正常生成直方图。

内容的提问来源于stack exchange,提问作者Sean Barbeau

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 04:41:00