国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

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

2019-10-22 18:11:08
字體:
來源:轉載
供稿:網友

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

承接上一篇文章,請參考Android MPAndroidChart開源圖表庫之餅狀圖的代碼

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

2. 定義xml文件。

android,開源圖表,MPAndroidChart,折線圖,代碼

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

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("");// 數據描述  // 如果沒有數據的時候,會顯示這個,類似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); // 設置數據  // 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); // 立即執行的動畫,x軸  }  /**  * 生成一個數據  * @param count 表示圖表中有多少個坐標點  * @param range 用來生成range以內的隨機數  * @return  */  private LineData getLineData(int count, float range) {  ArrayList<String> xValues = new ArrayList<String>();  for (int i = 0; i < count; i++) {  // x軸顯示的數據,這里默認使用數字下標顯示  xValues.add("" + i);  }  // y軸的數據  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軸的數據集合  LineDataSet lineDataSet = new LineDataSet(yValues, "測試折線圖" /*顯示在比例圖上*/);  // mLineDataSet.setFillAlpha(110);  // mLineDataSet.setFillColor(Color.RED);  //用y軸的集合來設置參數  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,折線圖,代碼

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

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

android,開源圖表,MPAndroidChart,折線圖,代碼

效果圖如下:

android,開源圖表,MPAndroidChart,折線圖,代碼

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永川市| 舒兰市| 定兴县| 平遥县| 同江市| 垣曲县| 金华市| 台南市| 海兴县| 南漳县| 阿荣旗| 永新县| 永嘉县| 张家港市| 昌宁县| 武邑县| 凤山市| 綦江县| 同江市| 桐庐县| 博白县| 格尔木市| 河东区| 天柱县| 南京市| 靖安县| 阳西县| 丘北县| 库尔勒市| 亳州市| 平谷区| 凤翔县| 昭觉县| 高雄市| 定西市| 咸丰县| 中西区| 雅江县| 山西省| 定兴县| 长白|