android開源圖表庫MPAndroidChart

原文鏈接:http://blog.csdn.net/guijiaoba/article/details/41444697?utm_source=tuicool


最近一個項目需要用到表格進行統計顯示,本來用的是的achartengine,後來發現一個更加強大的開源框架MPAndroidChart。

下面簡單介紹下MPAndroidChart,MPAndroidChart的效果還是蠻好的,提供各種動畫,這個也是我使用MPAndroidChart,而且放棄achartengine的原因。

Github地址連接,後面是youtube上面演示MPAndroidChart的視頻,MPAndroidChart由於提供了動畫效果,爲了兼容低版本的Android系統,MPAndroidChart需要添加nineoldandroids-2.4.0-2.jar作爲依賴庫,所以如果項目中使用這個表格庫,需要同時導入這個兩個jar,當然如果使用libproject的方式,就不用了。

https://github.com/PhilJay/MPAndroidChart

https://www.youtube.com/watch?v=ufaK_Hd6BpI

http://nineoldandroids.com/




支持功能

核心功能:

  • 支持x,y軸縮放
  • 支持拖拽
  • 支持手指滑動
  • 支持高亮顯示
  • 支持保存圖表到文件中
  • 支持從文件(txt)中讀取數據
  • 預先定義顏色模板
  • 自動生成標註
  • 支持自定義x,y軸的顯示標籤
  • 支持x,y軸動畫
  • 支持x,y軸設置最大值和附加信息
  • 支持自定義字體,顏色,背景,手勢,虛線等

顯示的圖表類型:

  • LineChart (with legend, simple design) (線性圖)
  •  alt tag
  • LineChart (with legend, simple design)(線性圖)

  •  alt tag

  • LineChart (cubic lines)(線性圖)

  •  alt tag

  • LineChart (single DataSet) (線性圖)

  • alt tag

  • BarChart2D (with legend, simple design)(柱狀圖)

alt tag

  • BarChart2D (grouped DataSets)(柱狀圖)

alt tag

  • BarChart2D

alt tag

  • PieChart (with selection, ...)(餅狀圖)

alt tag

  • ScatterChart (with squares, triangles, circles, ... and more)(散列圖)

alt tag

  • CandleStickChart (for financial data)

alt tag

  • RadarChart (spider web chart)(螂蛛網圖)

alt tag

使用方法

1、直接使用jar方式,需要導入mpchartlib.jar,nineoldandroidsjar。

2、使用libproject的方式,作爲項目依賴。

步驟:

如果使用 LineChart, BarChart, ScatterChart, CandleStickChart or PieChart , 可以直接在xml中定義。

    <com.github.mikephil.charting.charts.LineChart
        android:id="@+id/chart"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    LineChart chart = (LineChart) findViewById(R.id.chart);

或則直接在代碼中聲明和實例化。

    LineChart chart = new LineChart(Context);

主要的Api方法:

  • setDescription(String desc): 設置表格的描述
  • setDescriptionTypeface(Typeface t):自定義表格中顯示的字體
  • setDrawYValues(boolean enabled): 設置是否顯示y軸的值的數據
  • setValuePaintColor(int color):設置表格中y軸的值的顏色,但是必須設置setDrawYValues(true)
  • setValueTypeface(Typeface t):設置字體
  • setValueFormatter(DecimalFormat format): 設置顯示的格式
  • setPaint(Paint p, int which): 自定義筆刷
  • public ChartData getDataCurrent():返回ChartData對象當前顯示的圖表。它包含了所有信息的顯示值最小和最大值等
  • public float getYChartMin(): 返回當前最小值
  • public float getYChartMax(): 返回當前最大值
  • public float getAverage(): 返回所有值的平均值。
  • public float getAverage(int type): 返回平均值
  • public PointF getCenter(): 返回中間點
  • public Paint getPaint(int which): 得到筆刷
  • setTouchEnabled(boolean enabled): 設置是否可以觸摸,如爲false,則不能拖動,縮放等
  • setDragScaleEnabled(boolean enabled): 設置是否可以拖拽,縮放
  • setOnChartValueSelectedListener(OnChartValueSelectedListener l): 設置表格上的點,被點擊的時候,的回調函數
  • setHighlightEnabled(boolean enabled): 設置點擊value的時候,是否高亮顯示
  • public void highlightValues(Highlight[] highs): 設置高亮顯示
  • saveToGallery(String title): 保存圖表到圖庫中
  • saveToPath(String title, String pathOnSD): 保存.
  • setScaleMinima(float x, float y): 設置最小的縮放
  • centerViewPort(int xIndex, float val): 設置視口
  • fitScreen(): 適應屏幕

動畫:

所有的圖表類型都支持下面三種動畫,分別是x方向,y方向,xy方向。

  • animateX(int durationMillis): x軸方向
  • animateY(int durationMillis): y軸方向
  • animateXY(int xDuration, int yDuration): xy軸方向
mChart.animateX(3000f); // animate horizontal 3000 milliseconds
// or:
mChart.animateY(3000f); // animate vertical 3000 milliseconds
// or:
mChart.animateXY(3000f, 3000f); // animate horizontal and vertical 3000 milliseconds

注意:如果調用動畫方法後,就沒有必要調用invalidate()方法,來刷新界面了。

添加數據到圖表:

下面是MPAndroidChart中的一個demo實例,簡單介紹怎麼添加數據到圖表中,以及動畫顯示。

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. package com.xxmassdeveloper.mpchartexample;  
  2.   
  3. import android.graphics.Color;  
  4. import android.graphics.Typeface;  
  5. import android.os.Bundle;  
  6. import android.view.WindowManager;  
  7.   
  8. import com.github.mikephil.charting.charts.LineChart;  
  9. import com.github.mikephil.charting.data.Entry;  
  10. import com.github.mikephil.charting.data.LineData;  
  11. import com.github.mikephil.charting.data.LineDataSet;  
  12. import com.github.mikephil.charting.utils.Legend;  
  13. import com.github.mikephil.charting.utils.Legend.LegendForm;  
  14. import com.github.mikephil.charting.utils.XLabels;  
  15. import com.github.mikephil.charting.utils.YLabels;  
  16. import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;  
  17.   
  18. import java.util.ArrayList;  
  19.   
  20. public class LineChartActivityColored extends DemoBase {  
  21.   
  22.     LineChart[] mCharts = new LineChart[4]; // 4條數據  
  23.     Typeface mTf; // 自定義顯示字體  
  24.     int[] mColors = new int[] { Color.rgb(13723081), Color.rgb(24024030),//  
  25.             Color.rgb(89199250), Color.rgb(250104104) }; // 自定義顏色  
  26.   
  27.     @Override  
  28.     protected void onCreate(Bundle savedInstanceState) {  
  29.         super.onCreate(savedInstanceState);  
  30.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  31.         setContentView(R.layout.activity_colored_lines);  
  32.   
  33.         mCharts[0] = (LineChart) findViewById(R.id.chart1);  
  34.         mCharts[1] = (LineChart) findViewById(R.id.chart2);  
  35.         mCharts[2] = (LineChart) findViewById(R.id.chart3);  
  36.         mCharts[3] = (LineChart) findViewById(R.id.chart4);  
  37.   
  38.         // 自定義字體  
  39.         mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Bold.ttf");  
  40.         // 生產數據  
  41.         LineData data = getData(36100);  
  42.   
  43.         for (int i = 0; i < mCharts.length; i++) {  
  44.             // add some transparency to the color with "& 0x90FFFFFF"  
  45.             setupChart(mCharts[i], data, mColors[i % mColors.length]);  
  46.         }  
  47.     }  
  48.     // 設置顯示的樣式  
  49.     void setupChart(LineChart chart, LineData data, int color) {  
  50.         // if enabled, the chart will always start at zero on the y-axis  
  51.         chart.setStartAtZero(true);  
  52.   
  53.         // disable the drawing of values into the chart  
  54.         chart.setDrawYValues(false);  
  55.   
  56.         chart.setDrawBorder(false);  
  57.   
  58.         // no description text  
  59.         chart.setDescription("");// 數據描述  
  60.         // 如果沒有數據的時候,會顯示這個,類似listview的emtpyview  
  61.         chart.setNoDataTextDescription("You need to provide data for the chart.");  
  62.   
  63.         // enable / disable grid lines  
  64.         chart.setDrawVerticalGrid(false); // 是否顯示水平的表格  
  65.         // mChart.setDrawHorizontalGrid(false);  
  66.         //  
  67.         // enable / disable grid background  
  68.         chart.setDrawGridBackground(false); // 是否顯示錶格顏色  
  69.         chart.setGridColor(Color.WHITE & 0x70FFFFFF); // 表格的的顏色,在這裏是是給顏色設置一個透明度  
  70.         chart.setGridWidth(1.25f);// 表格線的線寬  
  71.   
  72.         // enable touch gestures  
  73.         chart.setTouchEnabled(true); // 設置是否可以觸摸  
  74.   
  75.         // enable scaling and dragging  
  76.         chart.setDragEnabled(true);// 是否可以拖拽  
  77.         chart.setScaleEnabled(true);// 是否可以縮放  
  78.   
  79.         // if disabled, scaling can be done on x- and y-axis separately  
  80.         chart.setPinchZoom(false);//   
  81.   
  82.         chart.setBackgroundColor(color);// 設置背景  
  83.   
  84.         chart.setValueTypeface(mTf);// 設置字體  
  85.   
  86.         // add data  
  87.         chart.setData(data); // 設置數據  
  88.   
  89.         // get the legend (only possible after setting data)  
  90.         Legend l = chart.getLegend(); // 設置標示,就是那個一組y的value的  
  91.   
  92.         // modify the legend ...  
  93.         // l.setPosition(LegendPosition.LEFT_OF_CHART);  
  94.         l.setForm(LegendForm.CIRCLE);// 樣式  
  95.         l.setFormSize(6f);// 字體  
  96.         l.setTextColor(Color.WHITE);// 顏色  
  97.         l.setTypeface(mTf);// 字體  
  98.   
  99.         YLabels y = chart.getYLabels(); // y軸的標示  
  100.         y.setTextColor(Color.WHITE);  
  101.         y.setTypeface(mTf);  
  102.         y.setLabelCount(4); // y軸上的標籤的顯示的個數  
  103.   
  104.         XLabels x = chart.getXLabels(); // x軸顯示的標籤  
  105.         x.setTextColor(Color.WHITE);  
  106.         x.setTypeface(mTf);  
  107.   
  108.         // animate calls invalidate()...  
  109.         chart.animateX(2500); // 立即執行的動畫,x軸  
  110.     }  
  111.   
  112.     // 生成一個數據,  
  113.     LineData getData(int count, float range) {  
  114.         ArrayList<String> xVals = new ArrayList<String>();  
  115.         for (int i = 0; i < count; i++) {  
  116.             // x軸顯示的數據,這裏默認使用數字下標顯示  
  117.             xVals.add(mMonths[i % 12]);  
  118.         }  
  119.   
  120.         // y軸的數據  
  121.         ArrayList<Entry> yVals = new ArrayList<Entry>();  
  122.         for (int i = 0; i < count; i++) {  
  123.             float val = (float) (Math.random() * range) + 3;  
  124.             yVals.add(new Entry(val, i));  
  125.         }  
  126.   
  127.         // create a dataset and give it a type  
  128.         // y軸的數據集合  
  129.         LineDataSet set1 = new LineDataSet(yVals, "DataSet 1");  
  130.         // set1.setFillAlpha(110);  
  131.         // set1.setFillColor(Color.RED);  
  132.   
  133.         set1.setLineWidth(1.75f); // 線寬  
  134.         set1.setCircleSize(3f);// 顯示的圓形大小  
  135.         set1.setColor(Color.WHITE);// 顯示顏色  
  136.         set1.setCircleColor(Color.WHITE);// 圓形的顏色  
  137.         set1.setHighLightColor(Color.WHITE); // 高亮的線的顏色  
  138.   
  139.         ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>();  
  140.         dataSets.add(set1); // add the datasets  
  141.   
  142.         // create a data object with the datasets  
  143.         LineData data = new LineData(xVals, dataSets);  
  144.   
  145.         return data;  
  146.     }  
  147. }  

運行效果圖如下:



發佈了29 篇原創文章 · 獲贊 60 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章