hellocharts-android 简单使用

这次介绍一个超级好用的图标库,hellocharts-android-master,大家先预览一下这个库的效果









github原链接HelloCharts-Android-master


这个库的动画及手势效果极佳,用户体验也很棒,但是!

他的坐标轴似乎不能定义。基本只能显示为固定的内容。


AxisValueFormatter接口只定义了两个int方法,根本没有给出String的写法,如果有哪位大神有办法能给出string的写法请留言,我先谢谢了。



public interface AxisValueFormatter {

    /**
     * Formats AxisValue for manual(custom) axis. Result is stored in (output) formattedValue array. Method
     * returns number of chars of formatted value. The formatted value starts at index [formattedValue.length -
     * charsNumber] and ends at index [formattedValue.length-1].
     */
    public int formatValueForManualAxis(char[] formattedValue, AxisValue axisValue);

    /**
     * Used only for auto-generated axes. If you are not going to use your implementation for aut-generated axes you can
     * skip implementation of this method and just return 0. SFormats values with given number of digits after
     * decimal separator. Result is stored in given array. Method returns number of chars for formatted value. The
     * formatted value starts at index [formattedValue.length - charsNumber] and ends at index [formattedValue
     * .length-1].
     */
    public int formatValueForAutoGeneratedAxis(char[] formattedValue, float value, int autoDecimalDigits);
}

闲话少许,请看小小的demo,希望能给困惑绘图的小伙伴们些许灵感。


先上个小实例:


简单的折线图

package com.leo.test.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.leo.test.R;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.ValueShape;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.LineChartView;

/**
 * @Function
 * @Author Leo
 * @CreateTime 2015/12/21 0021
 */
public class LineChartActivity extends AppCompatActivity {

    private LineChartView line_chart_view;
    private LineChartData data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_line_chart);

        line_chart_view = (LineChartView) findViewById(R.id.line_chart_view);
<span style="white-space:pre">		</span>
        List<Line> lines = initLine();
        data = initData(lines);

        line_chart_view.setLineChartData(data);

        Viewport viewport = initViewPort();
        line_chart_view.setMaximumViewport(viewport);
        line_chart_view.setCurrentViewport(viewport);
    }


    /**
     * 设置4个边距
     */
    private Viewport initViewPort() {
        Viewport viewport = new Viewport();
        viewport.top = 100;
        viewport.bottom = 0;
        viewport.left = 0;
        viewport.right = 90;

        return viewport;
    }

    /**
     * 初始化线属性
     *
     * @return
     */
    private List<Line> initLine() {
        List<Line> lineList = new ArrayList<>();

        List<PointValue> pointValueList = new ArrayList<>();
        PointValue pointValue1 = new PointValue(10,30);
        pointValueList.add(pointValue1);
        PointValue pointValue2 = new PointValue(20,20);
        pointValueList.add(pointValue2);
        PointValue pointValue3 = new PointValue(30,70);
        pointValueList.add(pointValue3);
        PointValue pointValue4 = new PointValue(40,69);
        pointValueList.add(pointValue4);
        PointValue pointValue5 = new PointValue(50,64);
        pointValueList.add(pointValue5);
        PointValue pointValue6 = new PointValue(60,31);
        pointValueList.add(pointValue6);
        PointValue pointValue7 = new PointValue(70,22);
        pointValueList.add(pointValue7);
        PointValue pointValue8 = new PointValue(80,100);
        pointValueList.add(pointValue8);


        Line line = new Line(pointValueList);
        line.setColor(getResources().getColor(R.color.colorAccent));
        line.setShape(ValueShape.CIRCLE);
        lineList.add(line);

        return lineList;
    }

    /**
     * 初始化数据
     *
     * @return
     */
    private LineChartData initData(List<Line> lines) {

        LineChartData data = new LineChartData(lines);
        //初始化轴
        Axis axisX = new Axis();
        Axis axisY = new Axis().setHasLines(true);
        axisX.setName("时间");
        //前加字符
//        axisX.setFormatter(new SimpleAxisValueFormatter().setPrependedText("aaaa".toCharArray()));
        //后加字符
//        axisX.setFormatter(new SimpleAxisValueFormatter().setAppendedText("aaaa".toCharArray()));
//        axisX.setFormatter(new SimpleAxisValueFormatter());
        axisY.setName("销量");
        //设置轴
        data.setAxisYLeft(axisY);
        data.setAxisXBottom(axisX);
        //设置负值 设置为负无穷 默认为0
//        data.setBaseValue(Float.NEGATIVE_INFINITY);

        return data;
    }


}
下面是xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical">

    <lecho.lib.hellocharts.view.LineChartView
        android:background="#123456"
        android:id="@+id/line_chart_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

附上官方demo的CSDN下载地址,送给那些github上不去的童鞋 官方demo

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值