亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2018年05月16日 11:07:41   作者:shineflowers  
這篇文章主要介紹了Android MPAndroidChart開源庫圖表之折線圖的實例代碼,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

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

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

2. 定義xml文件。


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)); 
 } 
 // 設置顯示的樣式 
 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); // 表格的的顏色,在這里是是給顏色設置一個透明度 
 // enable touch gestures 
 lineChart.setTouchEnabled(true); // 設置是否可以觸摸 
 // 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);// 設置背景 
 // add data 
 lineChart.setData(lineData); // 設置數(shù)據(jù) 
 // get the legend (only possible after setting data) 
 Legend mLegend = lineChart.getLegend(); // 設置比例圖標示,就是那個一組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 表示圖表中有多少個坐標點 
 * @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ù),這里默認使用數(shù)字下標顯示 
 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ù) 
 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; 
 } 
} 

效果圖如下:


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

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


效果圖如下:


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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論