android - MPAndroidChart 正确使用 getPixelForValues 为图表渐变颜色
问题描述
我正在尝试LineChart
根据this issuey
的值设置 a 的颜色渐变。计划是制作一个只覆盖整个事物一次的渐变。
override fun createChartGradient(myEntities: List<MyEntity>, chart: LineChart): LinearGradient {
val transformer = Transformer(chart.viewPortHandler)
val entries: List<Entry> = createEntriesFrom(myEntities) // this works fine, the chart is plotted alright
val pixelXValues = entries.map { transformer.getPixelForValues(it.x, it.y).x.toFloat() }.toFloatArray()
val colors = calculateColorsBasedOnValues(myEntities) // this works fine, I can set the fill color of points with this.
return LinearGradient(pixelXValues.first(), 0f, pixelXValues.last(), 0f, colors.toIntArray(), pixelXValues.lerp(), Shader.TileMode.REPEAT)
}
private fun FloatArray.lerp(): FloatArray {
val total = this.last() - this.first()
return this.map { ((it - this.first()) / total) }.toFloatArray()
}
哪个是分配给图表的
val gradient = lineChartService.createChartGradient(entities, chart)
chart.renderer.paintRender.shader = gradient
我很清楚,我必须在放大或缩小时更新它,但现在即使完全缩小,我也很难让它工作,在这种情况下,图表似乎重复着色器 5 次 - 但基于我的计算我应该只涵盖整个事情一次。
我附加了一个调试器,lerp
函数的输出是从 0 到 1,包括 0 到 1。
我在做什么错getPixelForValues
或LinearGradient
?
解决方案
推荐阅读
- opengl - 统一变量的内存数据流?
- python - 如何结合两个 Python Try Exceptions
- javascript - 什么值用于在浏览器中设置 document.domain?
- kotlin - 如何验证 List 的实例不是另一个 List 实例?
- wai-aria - JAWS 在 Win10 上的 MS Edge 中多次读取角色为“img”的表的 aria-label
- ios - 在类型化数组中存储符合泛型协议的对象
- angular - 在根级别为离子选项卡配置选项卡
- rotation - 处理 3.5.3 围绕中心点旋转点 图像缩小消失
- visual-studio - 如何在 Visual Studio 2017 中远程调试到以前部署的以调试模式构建的应用程序?
- sql - How can I create a selection that can show my total of objects by owners?