MPAndroidChart自定义样式

需求

如下图,将左侧表中的,文字描述,按照右侧图方式显示文案信息

在这里插入图片描述
在这里插入图片描述

修改代码如下

文件HorizontalBarChartRenderer.java中
@Override
    public void drawValues(Canvas c) {
        // if values are drawn
        if (isDrawingValuesAllowed(mChart)) {
        	...
            for (int i = 0; i < mChart.getBarData().getDataSetCount(); i++) {
            	...
                // if only single values are drawn (sum)
                if (!dataSet.isStacked()) {
                   ...
                    // if each value of a potential stack should be drawn
                } else {
                    ...
                    while (index < dataSet.getEntryCount() * mAnimator.getPhaseX()) {
                        ...
                        if (vals == null) {
                            ...
                        } else {
                            ...
                            for (int k = 0; k < transformed.length; k += 2) {
                               ...

                                if (dataSet.isDrawValuesEnabled()) {
                                    drawValue(c, formattedValue, x, y + halfTextHeight, color);
                                }
                                //新增如下代码
                                //起始或者结束值为0
                                if(formattedValue.trim().equals("0")){
                                   float textWidth = Utils.calcTextWidth(mValuePaint, "happy2020");
                                   //如果为开始值为负数,则往左侧显示文本
                                  if(vals[0]<0||vals[1]<0){
                                      //写标题
                                        drawValue(c,"happy2020",x-textWidth-30, y,                                              Color.RED);
                                    //反之,右侧显示文本
                                    }else {
                                       drawValue(c,"happy2020",x+30, y,
                                                Color.RED);
                                    }
                                }
                             
								...

                            }
                        }
                        ...
                    }
                }

                MPPointF.recycleInstance(iconsOffset);
            }
        }
    }
发布了98 篇原创文章 · 获赞 6 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/dirksmaller/article/details/103789683