在安卓开发过程中需要使用一些图形图表,比如股票/报表展示等,就需要加入一些第三方插件。目前有很多类似功能插件,比如achartengine, Graphview等等。Graphview比较简洁,用起来简单,目前支持折线图和条形图图表样式。今天我就来讲讲Graphview。
该工程地址:http://www.android-graphview.org/
示例代码也非常简单
GraphViewData[] data = new GraphViewData[num];
double v=0;
for (int i=0; i<num; i++) {
v += 0.2;
data[i] = new GraphViewData(i, Math.sin(v));
}
// graph with dynamically genereatedhorizontal and vertical labels
GraphView graphView;
if (getIntent().getStringExtra("type").equals("bar")) {
graphView = new BarGraphView(
this
, "GraphViewDemo"
);
} else {
graphView = new LineGraphView(
this
, "GraphViewDemo"
);
}
// add data
graphView.addSeries(new GraphViewSeries(data));
// set view port, start=2,size=40
graphView.setViewPort(2, 40);
graphView.setScrollable(true);
LinearLayout layout = (LinearLayout) findViewById(R.id.graph1);
layout.addView(graphView);
从代码上面可以看到,graphview支持线性图和圆饼图。
目前有个小问题就是graphview使用的是折线图,不够圆滑。如果要能使线条圆滑就需要修改相关源码。过段时间搞定更新blog