安卓图表控件WilliamChart使用指南

安卓图表控件WilliamChart使用指南

WilliamChart是github上的一个android图表控件,项目地址:https://github.com/diogobernardino/WilliamChart

该图表控件效果不错,使用也比较方便。

参考它提供的示例程序,我写了一个更加简单的图表程序,步骤如下:

1.从github上下载项目,将其library项目导入adt;

2.将其sample项目导入adt;

3.创建自己的工程,引用library项目,并将sample项目中的DataRetriever类拷贝到本项目的源代码目录中;

4.创建一个chart_layout.xml代码如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:andro    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >   <com.db.chart.view.LineChartViewxmlns:chart="http://schemas.android.com/apk/res-auto"android:    android:layout_width="match_parent"    android:layout_height="0dp"    android:layout_weight=".30"    android:paddingTop="15dp"    android:paddingBottom="5dp"    android:paddingLeft="10dp"    android:paddingRight="10dp"    chart:chart_shadowDy="1dp"    chart:chart_shadowRadius="1dp"    chart:chart_shadowColor="#80000000"    chart:chart_axisColor="@color/axis"    chart:chart_axisBorderSpacing="0dp"></com.db.chart.view.LineChartView></LinearLayout>

5.创建一个Activity类,代码如下:

package com.example.wxb_example;import com.db.chart.model.LineSet;import com.db.chart.model.Point;import com.db.chart.view.LineChartView;import com.db.chart.view.YController;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;public class WilliamChartActivity extends Activity {//线性图表控件private static LineChartView mLineChart;private final static String[] mLabels = {"ANT", "GNU", "OWL", "APE", "COD","YAK", "RAM", "JAY"};@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.chart_layout);mLineChart = (LineChartView) findViewById(R.id.linechart);LineSet data;mLineChart.reset();int nSets = 2;  //两条线int nPoints = 5;  //每条线5个点for(int i = 0; i < nSets; i++){data = new LineSet();for(int j = 0; j < nPoints; j++)data.addPoint(new Point(mLabels[j], DataRetriever.randValue(0, 10)));data.setDots(DataRetriever.randBoolean()).setDotsColor(Color.parseColor(DataRetriever.getColor(DataRetriever.randNumber(0,2)))).setDotsRadius(DataRetriever.randDimen(4,7)).setLineThickness(DataRetriever.randDimen(3,8)).setLineColor(Color.parseColor(DataRetriever.getColor(i))).setDashed(DataRetriever.randBoolean()).setSmooth(DataRetriever.randBoolean());if(i == 2){//data.setFill(Color.parseColor("#3388c6c3"));int[] colors = {Color.parseColor("#3388c6c3"), Color.TRANSPARENT};data.setGradientFill(colors, null);}if(DataRetriever.randBoolean())data.setDotsStrokeThickness(DataRetriever.randDimen(1,4)).setDotsStrokeColor(Color.parseColor(DataRetriever.getColor(DataRetriever.randNumber(0,2))));mLineChart.addData(data);}mLineChart.setGrid(DataRetriever.randPaint()).setVerticalGrid(DataRetriever.randPaint()).setHorizontalGrid(DataRetriever.randPaint())//.setThresholdLine(2, randPaint()).setYLabels(YController.LabelPosition.NONE).setYAxis(false).setXLabels(DataRetriever.getXPosition()).setXAxis(DataRetriever.randBoolean()).setMaxAxisValue(10, 2).animate(DataRetriever.randAnimation(null, nPoints))//.show();}}

注意这个类中引用了拷贝过来的DataRetriever类。

运行该项目,效果如下:



其中的效果可以自己再另行操作

免责声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕。
相关文章
返回顶部