折线图hellocharts-android库修改坐标、空心原点的实现

TempoChartActivity是hellocharts-android库的折线图之一 由于没有文档,需要详细阅读代码来达到修改的目的



修改后




1、取消第一个折线

注释掉activity的88-106行代码

  // Tempo line is a little tricky because worse tempo means bigger value for example 11min per km is worse
            // than 2min per km but the second should be higher on the chart. So you need to know max tempo and
            // tempoRange and set
            // chart values to minTempo - realTempo.
//            values = new ArrayList<PointValue>();
//            for (int i = 0; i < numValues; ++i) {
//                // Some random raw tempo values.
//                float realTempo = (float) Math.random() * 6 + 2;
//                float revertedTempo = tempoRange - realTempo;
//                values.add(new PointValue(i, revertedTempo));
//            }

//            line = new Line(values);
//            line.setColor(ChartUtils.COLOR_RED);
//            line.setHasPoints(false);
//            line.setStrokeWidth(3);
//            lines.add(line);

            // Data and axes
data = new LineChartData(lines);
------------------------------------------------------
2、横坐标的数字旋转,得到如图的数字
需要修改activity的66行代码
int numValues = 7;
需要注释掉AxesRenderer的612、613行代码
 if (axis.hasTiltedLabels()) {
                canvas.save();
//                canvas.translate(tiltedLabelXTranslation[position], tiltedLabelYTranslation[position]);
//                canvas.rotate(-45, labelX, labelY);
                canvas.drawText(labelBuffer, labelBuffer.length - charsNumber, charsNumber, labelX, labelY,
                        labelPaintTab[position]);
                canvas.restore();
            } else {
                canvas.drawText(labelBuffer, labelBuffer.length - charsNumber, charsNumber, labelX, labelY,
                        labelPaintTab[position]);
            }
这个时候会出现一个问题,最后数字7部分显示不全
解决办法:在AxesRenderer的494行下面这段代码
for (int i = 0; i < autoValuesBufferTab[position].valuesNumber; ++i) {
    if (isAxisVertical) {
        rawValue = computator.computeRawY(autoValuesBufferTab[position].values[i]);
    } else {
        rawValue = computator.computeRawX(autoValuesBufferTab[position].values[i]);
        //最后一个横坐标数据能够完全显示
        if(i ==(autoValuesBufferTab[position].valuesNumber-1)){
            rawValue-=10;
        }
    }
    if (checkRawValue(contentRect, rawValue, axis.isInside(), position, isAxisVertical)) {
        rawValuesTab[position][valueToDrawIndex] = rawValue;
        //+1 让横坐标为从0变为1
        if(position==3){
            autoValuesToDrawTab[position][valueToDrawIndex] = autoValuesBufferTab[position].values[i]+1;
        }else{
            autoValuesToDrawTab[position][valueToDrawIndex] = autoValuesBufferTab[position].values[i];
        }

        ++valueToDrawIndex;
    }
}
添加
if(i ==(autoValuesBufferTab[position].valuesNumber-1)){
            rawValue-=10;
}
---------------------------------------------
空心坐标
line.setHasPoints(true);
line.setPointRadius(2);
在LineChartRenderer文件中
drawPoint函数下面
if (ValueShape.SQUARE.equals(line.getShape())) {
    canvas.drawRect(rawX - pointRadius, rawY - pointRadius, rawX + pointRadius, rawY + pointRadius,
            pointPaint);
} else if (ValueShape.CIRCLE.equals(line.getShape())) {
    canvas.drawCircle(rawX, rawY, pointRadius, pointPaint);
    canvas.drawCircle(rawX, rawY, pointRadius, pointStrokePaint);//加入空心坐标描边
} else if (ValueShape.DIAMOND.equals(line.getShape())) {
    canvas.save();
    canvas.rotate(45, rawX, rawY);
    canvas.drawRect(rawX - pointRadius, rawY - pointRadius, rawX + pointRadius, rawY + pointRadius,
            pointPaint);
    canvas.restore();
} else {
    throw new IllegalArgumentException("Invalid point shape: " + line.getShape());
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值