MPAndroidChart:更改条形图的形状 [英] MPAndroidChart: Change Shape of Bar Chart
问题描述
我已经阅读了类似于我的问题的答案.
但是,我不明白,我认为那太复杂了.也许你们中的一些人有技巧使其变得简单.我正在使用 'com.github.PhilJay:MPAndroidChart:v2.2.4'
库,顺便说一句.我想像这样的图片制作条形图样式.
i already read answers of question that similar to mine.
but, i don't understand and i think that's so complex. maybe some of you have a trick how to make it simple. i am using 'com.github.PhilJay:MPAndroidChart:v2.2.4'
library, btw. i want to make a bar chart style like this picture.
矩形的角的半径大约为4dp
.
我还没有找到绘制它的方法.
the corner of rectangle has radius about 4dp
.
i haven't found any method to draw that.
推荐答案
好像没有办法使它变得简单.最终,我想到了一个笨拙但可行的解决方案,在我的情况下效果很好. 首先子类化BarChartRenderer并将其设置到您的图表中:
Looks like there is no way to make it simple. Finally I came up with a clumsy, but working solution which worked well in my case. First subclass the BarChartRenderer and set it to your chart:
class CustomBarRenderer constructor(
chart: BarChart,
animator: ChartAnimator,
vpHandler: ViewPortHandler,
cornerDimens: Float
) : BarChartRenderer(chart, animator, vpHandler)
//...
chart.renderer = CustomBarRenderer(chart, chart.animator, chart.viewPortHandler, cornersDimens)
//...
BarChartRenderer
继承了用于绘制条形线的mRenderPaint
成员,因此,如果您需要进行一些细微的修改(条形边框宽度,填充类型或其他任何东西),则可以在init
块中直接覆盖mRendererPaint
而且您走的很好.
BarChartRenderer
inherits mRenderPaint
member which is used to draw bar lines, so if you need some slight modification (bar border width, fill type or anything else) you can simply override mRendererPaint
in your init
block and you're good to go.
init {
mRendererPaint = Paint().also {
// all paint properties you need
}
}
但这不足以得到圆角条.原因是渲染器使用drawDataSet
方法中的canvas.drawRect
绘制它们,因此我们需要更深入地覆盖它,并将此调用更改为canvas.drawRoundRect
:
But this is not enough to get that rounded corners bars. The reason is that renderer draws them using canvas.drawRect
inside drawDataSet
method, so we need to go deeper and override it and change this invocation to canvas.drawRoundRect
:
override fun drawDataSet(c: Canvas, dataSet: IBarDataSet, index: Int) {
//....
c.drawRoundRect(
buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
buffer.buffer[j + 3], cornersDimen, cornersDimen, mRenderPaint
)
//....
}
重要-尽管renderer
的大多数成员都是protected
且易于使用或覆盖,但仍有一些private
东西用于绘制阴影.不幸的是,我不得不删除阴影渲染(首先是因为我不需要阴影渲染),所以对于某些人来说,这可能是一个不完整的解决方案.
Important - although the majority of renderer
's members are protected
and easy to use or override, there are still some private
stuff which is used for drawing shadows. Unfortunately I had to just remove shadows rendering (in first place just because I didn't need them), so for somebody this could be an incomplete solution.
这篇关于MPAndroidChart:更改条形图的形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!