AChartEngine是为android应用而设计的绘图工具库,使用它可以很方便地完成各种图形的绘制,官网地址是AChartEngine,由于是比较早的项目,所以项目主业放在了GoogleCode上,不过在GitHub上也有镜像。目前官方最新版本是1.1.0,现在已经支持的图标样式如下:
- line chart
- area chart
- scatter chart
- time chart
- bar chart
- pie chart
- bubble chart
- doughnut chart
- range (high-low) bar chart
- dial chart / gauge
- combined (any combination of line, cubic line, scatter, bar, range bar, bubble) chart
- cubic line chart
部分效果图如下:
当然如果想完全搞明白框架的实现逻辑,会非常的困难,也没什么必要,实际上只需要会用即可,官方的API文档也非常的详细。接下来,就记录一下自己实际项目里实现的一个绘制饼状图(Pie Chart)的小demo.
首先,当然需要先导入achartengine-1.1.0.jar这个jar包,然后可以将官方提供的demo中的布局文件xy_chat.xml拿出来直接使用,我们看到AChartEngine在实现了如此之多的图形绘制的前提下,只有一个布局文件,可见这个框架是多么地精致严谨,其对代码的抽取简直已经达到了极致。当然,对应的string值也需要修改一下,不再赘述。下面是我修改过的布局文件:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="add_values" />
<TableLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="x" android:padding="5dip"/>
<EditText android:id="@+id/xValue" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1" android:enabled="false"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="y" android:padding="5dip"/>
<EditText android:id="@+id/yValue" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1" android:enabled="false"/>
<Button android:id="@+id/add" android:text="add"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:enabled="false"/>
</TableRow>
</TableLayout>
<LinearLayout android:id="@+id/chart" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" />
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content" >
<Button android:id="@+id/new_series" android:text="new_series"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
然后,如果要实现一个饼状图,在官方demo里也有对应的代码实例,下图展示了现有的代码实例,其中PieChartBuilder.java就是饼状图的代码实例,可以直接拿来用。
1 | /** |
最终效果如下:
如果需要绘制其他的图形,在官方demo中的其他代码中修改就是。