AndroidMPAndroidChart開源庫圖表之折線圖的實例代碼

本文講述了Android MPAndroidChart開源庫圖表之折線圖的實例代碼。分享給大家供大家參考,具體如下:

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比東平網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式東平網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋東平地區(qū)。費用合理售后完善,10年實體公司更值得信賴。

承接上一篇文章,請參考Android HelloChart開源庫圖表之折線圖的實例代碼

1. 將mpandroidchartlibrary-2-0-8.jar包copy到項目的libs中;

2. 定義xml文件。

Android MPAndroidChart開源庫圖表之折線圖的實例代碼

3.  主要Java邏輯代碼如下,注釋已經(jīng)都添加上了。

package com.example.mpandroidlinechart; 
import java.util.ArrayList; 
import com.github.mikephil.charting.charts.LineChart; 
import com.github.mikephil.charting.components.Legend; 
import com.github.mikephil.charting.components.Legend.LegendForm; 
import com.github.mikephil.charting.data.Entry; 
import com.github.mikephil.charting.data.LineData; 
import com.github.mikephil.charting.data.LineDataSet; 
import android.support.v7.app.ActionBarActivity; 
import android.graphics.Color; 
import android.os.Bundle; 
public class MainActivity extends ActionBarActivity { 
 private LineChart mLineChart; 
// private Typeface mTf; 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main); 
 mLineChart = (LineChart) findViewById(R.id.spread_line_chart); 
// mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Bold.ttf"); 
 LineData mLineData = getLineData(36, 100); 
 showChart(mLineChart, mLineData, Color.rgb(114, 188, 223)); 
 } 
 // 設(shè)置顯示的樣式 
 private void showChart(LineChart lineChart, LineData lineData, int color) { 
 lineChart.setDrawBorders(false); //是否在折線圖上添加邊框 
 // no description text 
 lineChart.setDescription("");// 數(shù)據(jù)描述 
 // 如果沒有數(shù)據(jù)的時候,會顯示這個,類似listview的emtpyview 
 lineChart.setNoDataTextDescription("You need to provide data for the chart."); 
 // enable / disable grid background 
 lineChart.setDrawGridBackground(false); // 是否顯示表格顏色 
 lineChart.setGridBackgroundColor(Color.WHITE & 0x70FFFFFF); // 表格的的顏色,在這里是是給顏色設(shè)置一個透明度 
 // enable touch gestures 
 lineChart.setTouchEnabled(true); // 設(shè)置是否可以觸摸 
 // enable scaling and dragging 
 lineChart.setDragEnabled(true);// 是否可以拖拽 
 lineChart.setScaleEnabled(true);// 是否可以縮放 
 // if disabled, scaling can be done on x- and y-axis separately 
 lineChart.setPinchZoom(false);// 
 lineChart.setBackgroundColor(color);// 設(shè)置背景 
 // add data 
 lineChart.setData(lineData); // 設(shè)置數(shù)據(jù) 
 // get the legend (only possible after setting data) 
 Legend mLegend = lineChart.getLegend(); // 設(shè)置比例圖標(biāo)示,就是那個一組y的value的 
 // modify the legend ... 
 // mLegend.setPosition(LegendPosition.LEFT_OF_CHART); 
 mLegend.setForm(LegendForm.CIRCLE);// 樣式 
 mLegend.setFormSize(6f);// 字體 
 mLegend.setTextColor(Color.WHITE);// 顏色 
// mLegend.setTypeface(mTf);// 字體 
 lineChart.animateX(2500); // 立即執(zhí)行的動畫,x軸 
 } 
 /** 
 * 生成一個數(shù)據(jù) 
 * @param count 表示圖表中有多少個坐標(biāo)點 
 * @param range 用來生成range以內(nèi)的隨機數(shù) 
 * @return 
 */ 
 private LineData getLineData(int count, float range) { 
 ArrayList<String> xValues = new ArrayList<String>(); 
 for (int i = 0; i < count; i++) { 
 // x軸顯示的數(shù)據(jù),這里默認(rèn)使用數(shù)字下標(biāo)顯示 
 xValues.add("" + i); 
 } 
 // y軸的數(shù)據(jù) 
 ArrayList<Entry> yValues = new ArrayList<Entry>(); 
 for (int i = 0; i < count; i++) { 
 float value = (float) (Math.random() * range) + 3; 
 yValues.add(new Entry(value, i)); 
 } 
 // create a dataset and give it a type 
 // y軸的數(shù)據(jù)集合 
 LineDataSet lineDataSet = new LineDataSet(yValues, "測試折線圖" /*顯示在比例圖上*/); 
 // mLineDataSet.setFillAlpha(110); 
 // mLineDataSet.setFillColor(Color.RED); 
 //用y軸的集合來設(shè)置參數(shù) 
 lineDataSet.setLineWidth(1.75f); // 線寬 
 lineDataSet.setCircleSize(3f);// 顯示的圓形大小 
 lineDataSet.setColor(Color.WHITE);// 顯示顏色 
 lineDataSet.setCircleColor(Color.WHITE);// 圓形的顏色 
 lineDataSet.setHighLightColor(Color.WHITE); // 高亮的線的顏色 
 ArrayList<LineDataSet> lineDataSets = new ArrayList<LineDataSet>(); 
 lineDataSets.add(lineDataSet); // add the datasets 
 // create a data object with the datasets 
 LineData lineData = new LineData(xValues, lineDataSets); 
 return lineData; 
 } 
} 

效果圖如下:

Android MPAndroidChart開源庫圖表之折線圖的實例代碼

折線圖還有另外一種表現(xiàn)形式,就是折線平滑,然后折線與X軸之間可以任意填充自己想要的顏色,其實就是一些屬性設(shè)置的問題,代碼如下:

在上面的getLineData()函數(shù)中添加自己的設(shè)置:

Android MPAndroidChart開源庫圖表之折線圖的實例代碼

效果圖如下:

Android MPAndroidChart開源庫圖表之折線圖的實例代碼

關(guān)于MPAndroidChart填充式的折線圖網(wǎng)上的帖子很少,基本沒有。這個是自己在網(wǎng)上搜索其他開源圖表庫,如JFreeChart...加上自己看源碼才總結(jié)出來的,不知道對不對,但是看效果,基本上沒問題。如果大家發(fā)現(xiàn)有問題,歡迎大家指正!

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)頁題目:AndroidMPAndroidChart開源庫圖表之折線圖的實例代碼
文章起源:http://muchs.cn/article8/ihijip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作營銷型網(wǎng)站建設(shè)、企業(yè)建站電子商務(wù)、自適應(yīng)網(wǎng)站用戶體驗

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)