本文講述了Android MPAndroidChart開源庫圖表之折線圖的實例代碼。分享給大家供大家參考,具體如下:
承接上一篇文章,請參考Android HelloChart開源庫圖表之折線圖的實例代碼
1. 將mpandroidchartlibrary-2-0-8.jar包copy到項目的libs中;
2. 定義xml文件。
3. 主要Java邏輯代碼如下,注釋已經都添加上了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
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; } } |
效果圖如下:
折線圖還有另外一種表現形式,就是折線平滑,然后折線與X軸之間可以任意填充自己想要的顏色,其實就是一些屬性設置的問題,代碼如下:
在上面的getLineData()函數中添加自己的設置:
效果圖如下:
關于MPAndroidChart填充式的折線圖網上的帖子很少,基本沒有。這個是自己在網上搜索其他開源圖表庫,如JFreeChart...加上自己看源碼才總結出來的,不知道對不對,但是看效果,基本上沒問題。如果大家發現有問題,歡迎大家指正!
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/shineflowers/article/details/44704723