最近项目中须要用到图表,技术有限,本身实现起来有难度,因而对比以后,最终决定使用hellocharts这个开源库,传送门:https://github.com/lecho/hellocharts-android ,一是由于引入方便,二是代码也比较清晰,便于加到项目中。还有一个重要的缘由,这个库能够支持折线表,柱状表,饼状表以及气泡状表,并且实现的效果很是棒,放几张图你们感觉一下:android
导入hellochartsgit
有三种方式能够在咱们的项目中使用hellocharts。github
dependencies{
compile 'com.github.lecho:hellocharts-library:1.5.8@aar'
}
复制代码
基本折线表的使用bash
通过以上几步,咱们的项目如今已经成功即成了hellocharts,下面咱们来简单看下最基本的图表-折线表是如何使用的吧。 首先,在xml文件中添加折线表控件:闭包
<lecho.lib.hellocharts.view.LineChartView
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent" />
复制代码
在hellocharts中,每种图表都有它本身的一个数据类型,在折线表中,最终设置给图表显示的是LineChartData,咱们能够简单想下,这个数据中确定包含线,线上的点,以及坐标轴,下面咱们分别用代码来展示。app
Line line = new Line(values).setColor(Color.BLUE);//声明线并设置颜色
line.setCubic(false);//设置是平滑的仍是直的
lines = new ArrayList<Line>();
lines.add(line);
复制代码
values = new ArrayList<PointValue>();//折线上的点
values.add(new PointValue(0, 2));
values.add(new PointValue(1, 4));
values.add(new PointValue(2, 3));
values.add(new PointValue(3, 4));
复制代码
mChartView.setInteractive(true);//设置图表是能够交互的(拖拽,缩放等效果的前提)
mChartView.setZoomType(ZoomType.HORIZONTAL_AND_VERTICAL);//设置缩放方向
LineChartData data = new LineChartData();
Axis axisX = new Axis();//x轴
Axis axisY = new Axis();//y轴
data.setAxisXBottom(axisX);
data.setAxisYLeft(axisY);
data.setLines(lines);
mChartView.setLineChartData(data);//给图表设置数据
复制代码
通过这几步简单的设置(最后会放上demo地址),咱们已经能够看到基本的折线图效果了:工具
到这里,咱们已经成功的使用hellocharts这个强大的图表库来展现了一个基本的折线图表,里边儿还有不少炫酷的用法等待咱们去发掘。最后放上demo地址:https://github.com/SolveBugs/HelloChartDemo开发工具