Android實(shí)現(xiàn)雙曲線折線圖
本文實(shí)例為大家分享了Android實(shí)現(xiàn)雙曲線折線圖的具體代碼,供大家參考,具體內(nèi)容如下
先看一下效果圖
1.先下載jar包 mpandroidchartlibrary-2-1-6.jar
2.將jar包導(dǎo)入到libs文件夾中
3.在build.gradle中依賴
dependencies { ? ? ..... ? ? ? compile files('libs/mpandroidchartlibrary-2-1-6.jar') }
4.創(chuàng)建LineChartManager工具類
public class LineChartManager { ? ? ? private static String lineName = null; ? ? private static String lineName1 = null; ? ? ? /** ? ? ?* 創(chuàng)建一條折線 ? ? ?* @param context 上下文 ? ? ?* @param mLineChart 對(duì)象 ? ? ?* @param count X軸的數(shù)據(jù) ? ? ?* @param datas Y軸的數(shù)據(jù) ? ? ?* @return LineData ? ? ?*/ ? ? public static LineData initSingleLineChart(Context context, LineChart mLineChart, int count, float[] datas) { ? ? ? ? ? ArrayList<String> xValues = new ArrayList<String>(); ? ? ? ? for (int i = 0; i < count; i++) { ? ? ? ? ? ? // x軸顯示的數(shù)據(jù),這里默認(rèn)使用數(shù)字下標(biāo)顯示· ? ? ? ? ? ? xValues.add((i) + ":00"); ? ? ? ? } ? ? ? ? ? // y軸的數(shù)據(jù) ? ? ? ? ArrayList<Entry> yValues = new ArrayList<Entry>(); ? ? ? ? for (int i = 0; i < count; i++) { ? ? ? ? ? ? yValues.add(new Entry(datas[i], i)); ? ? ? ? } ? ? ? ? //設(shè)置折線的樣式 ? ? ? ? LineDataSet dataSet = new LineDataSet(yValues, lineName); ? ? ? ? // 設(shè)置數(shù)據(jù)線的樣式 ? ? ? ? dataSet.setDrawCubic(true);// 改變折線樣式,用曲線。 ? ? ? ? dataSet.setCubicIntensity(0.2f);// 設(shè)置曲線的平滑度 ? ? ? ? //用y軸的集合來設(shè)置參數(shù) ? ? ? ? dataSet.setLineWidth(1.75f); // 線寬 ? ? ? ? dataSet.setCircleSize(2f);// 顯示的圓形大小 ? ? ? ? dataSet.setColor(Color.rgb(89, 194, 230));// 折線顯示顏色 ? ? ? ? dataSet.setCircleColor(Color.rgb(89, 194, 230));// 圓形折點(diǎn)的顏色 ? ? ? ? dataSet.setHighLightColor(Color.GREEN); // 高亮的線的顏色 ? ? ? ? dataSet.setHighlightEnabled(true); ? ? ? ? dataSet.setValueTextColor(Color.rgb(89, 194, 230)); //數(shù)值顯示的顏色 ? ? ? ? dataSet.setValueTextSize(8f); ? ? //數(shù)值顯示的大小 ? ? ? ? ? ArrayList<LineDataSet> dataSets = new ArrayList<>(); ? ? ? ? dataSets.add(dataSet); ? ? ? ? ? //構(gòu)建一個(gè)LineData ?將dataSets放入 ? ? ? ? LineData lineData = new LineData(xValues, dataSets); ? ? ? ? return lineData; ? ? } ? ? ? /** ? ? ?* @param context ? ?上下文 ? ? ?* @param mLineChart 折線圖控件 ? ? ?* @param count ? ? ?折線在x軸的值 ? ? ?* @param datas1 ? ? 折線在y軸的值 ? ? ?* @param datas2 ? ? 另一條折線在y軸的值 ? ? ?* @Description:創(chuàng)建兩條折線 ? ? ?*/ ? ? public static LineData initDoubleLineChart(Context context, LineChart mLineChart, int count, float[] datas1, float[] datas2) { ? ? ? ? ? ArrayList<String> xValues = new ArrayList<String>(); ? ? ? ? for (int i = 0; i < count; i++) { ? ? ? ? ? ? // x軸顯示的數(shù)據(jù),這里默認(rèn)使用數(shù)字下標(biāo)顯示 ? ? ? ? ? ? xValues.add((i) + ":00"); ? ? ? ? } ? ? ? ? ? // y軸的數(shù)據(jù) ? ? ? ? ArrayList<Entry> yValues1 = new ArrayList<Entry>(); ? ? ? ? for (int i = 0; i < count; i++) { ? ? ? ? ? ? yValues1.add(new Entry(datas1[i], i)); ? ? ? ? } ? ? ? ? ? // y軸的數(shù)據(jù) ? ? ? ? ArrayList<Entry> yValues2 = new ArrayList<Entry>(); ? ? ? ? for (int i = 0; i < count; i++) { ? ? ? ? ? ? yValues2.add(new Entry(datas2[i], i)); ? ? ? ? } ? ? ? ? ? LineDataSet dataSet = new LineDataSet(yValues1, lineName); ? ? ? ? //dataSet.enableDashedLine(10f, 10f, 0f);//將折線設(shè)置為曲線(即設(shè)置為虛線) ? ? ? ? //用y軸的集合來設(shè)置參數(shù) ? ? ? ? dataSet.setLineWidth(1.75f); // 線寬 ? ? ? ? dataSet.setCircleSize(2f);// 顯示的圓形大小 ? ? ? ? dataSet.setColor(Color.rgb(89, 194, 230));// 折線顯示顏色 ? ? ? ? dataSet.setCircleColor(Color.rgb(89, 194, 230));// 圓形折點(diǎn)的顏色 ? ? ? ? dataSet.setHighLightColor(Color.GREEN); // 高亮的線的顏色 ? ? ? ? dataSet.setHighlightEnabled(true); ? ? ? ? dataSet.setValueTextColor(Color.rgb(89, 194, 230)); //數(shù)值顯示的顏色 ? ? ? ? dataSet.setValueTextSize(8f); ? ? //數(shù)值顯示的大小 ? ? ? ? ? LineDataSet dataSet1 = new LineDataSet(yValues2, lineName1); ? ? ? ? ? //用y軸的集合來設(shè)置參數(shù) ? ? ? ? dataSet1.setLineWidth(1.75f); ? ? ? ? dataSet1.setCircleSize(2f); ? ? ? ? dataSet1.setColor(Color.rgb(252, 76, 122)); ? ? ? ? dataSet1.setCircleColor(Color.rgb(252, 76, 122)); ? ? ? ? dataSet1.setHighLightColor(Color.GREEN); ? ? ? ? dataSet1.setHighlightEnabled(true); ? ? ? ? dataSet1.setValueTextColor(Color.rgb(252, 76, 122)); ? ? ? ? dataSet1.setValueTextSize(8f); ? ? ? ? ? //構(gòu)建一個(gè)類型為L(zhǎng)ineDataSet的ArrayList 用來存放所有 y的LineDataSet ? 他是構(gòu)建最終加入LineChart數(shù)據(jù)集所需要的參數(shù) ? ? ? ? ArrayList<LineDataSet> dataSets = new ArrayList<>(); ? ? ? ? ? //將數(shù)據(jù)加入dataSets ? ? ? ? dataSets.add(dataSet); ? ? ? ? dataSets.add(dataSet1); ? ? ? ? ? //構(gòu)建一個(gè)LineData ?將dataSets放入 ? ? ? ? LineData lineData = new LineData(xValues, dataSets); ? ? ? ? return lineData; ? ? } ? ? ? /** ? ? ?* @Description:初始化圖表的樣式 ? ? ?*/ ? ? public static void initDataStyle(LineChart lineChart, LineData lineData, Context context) { ? ? ? ? //設(shè)置點(diǎn)擊折線點(diǎn)時(shí),顯示其數(shù)值 // ? ? ? ?MyMakerView mv = new MyMakerView(context, R.layout.item_mark_layout); // ? ? ? ?mLineChart.setMarkerView(mv); ? ? ? ? lineChart.setDrawBorders(false); //在折線圖上添加邊框 ? ? ? ? //lineChart.setDescription("時(shí)間/數(shù)據(jù)"); //數(shù)據(jù)描述 ? ? ? ? lineChart.setDrawGridBackground(false); //表格顏色 ? ? ? ? lineChart.setGridBackgroundColor(Color.GRAY & 0x70FFFFFF); //表格的顏色,設(shè)置一個(gè)透明度 ? ? ? ? lineChart.setTouchEnabled(true); //可點(diǎn)擊 ? ? ? ? lineChart.setDragEnabled(true); ?//可拖拽 ? ? ? ? lineChart.setScaleEnabled(true); ?//可縮放 ? ? ? ? lineChart.setPinchZoom(false); ? ? ? ? lineChart.setBackgroundColor(Color.WHITE); //設(shè)置背景顏色 ? ? ? ? ? lineChart.setData(lineData); ? ? ? ? ? // 隱藏表格的圖案示例 ? ? ? ? Legend legend = lineChart.getLegend(); ? ? ? ? legend.setEnabled(false); ? // ? ? ? ?// 添加警戒線 // ? ? ? ?YAxis yAxis = lineChart.getAxisLeft(); // ? ? ? ?LimitLine ll = new LimitLine(40, "警戒線"); // ? ? ? ?ll.setLineWidth(0.5f); // ? ? ? ?ll.setLineColor(Color.GRAY); // ? ? ? ?ll.setTextColor(Color.GRAY); // ? ? ? ?ll.setTextSize(12); // ? ? ? ?ll.setEnabled(true); // ? ? ? ?yAxis.addLimitLine(ll); ? ? ? ? ? Legend mLegend = lineChart.getLegend(); //設(shè)置標(biāo)示,就是那個(gè)一組y的value的 ? ? ? ? mLegend.setForm(Legend.LegendForm.SQUARE); //樣式 ? ? ? ? mLegend.setFormSize(6f); //字體 ? ? ? ? mLegend.setTextColor(Color.GRAY); //顏色 ? ? ? ? lineChart.setVisibleXRange(0, 4); ? //x軸可顯示的坐標(biāo)范圍 ? ? ? ? XAxis xAxis = lineChart.getXAxis(); ?//x軸的標(biāo)示 ? ? ? ? xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); //x軸位置 ? ? ? ? xAxis.setTextColor(Color.GRAY); ? ?//字體的顏色 ? ? ? ? xAxis.setTextSize(10f); //字體大小 ? ? ? ? xAxis.setGridColor(Color.GRAY);//網(wǎng)格線顏色 ? ? ? ? xAxis.setDrawGridLines(false); //不顯示網(wǎng)格線 ? ? ? ? YAxis axisLeft = lineChart.getAxisLeft(); //y軸左邊標(biāo)示 ? ? ? ? YAxis axisRight = lineChart.getAxisRight(); //y軸右邊標(biāo)示 ? ? ? ? axisLeft.setTextColor(Color.GRAY); //字體顏色 ? ? ? ? axisLeft.setTextSize(10f); //字體大小 ? ? ? ? //axisLeft.setAxisMaxValue(800f); //最大值 ? ? ? ? axisLeft.setLabelCount(10, true); //顯示格數(shù) ? ? ? ? axisLeft.setGridColor(Color.GRAY); //網(wǎng)格線顏色 ? ? ? ? ? axisRight.setDrawAxisLine(false); ? ? ? ? axisRight.setDrawGridLines(false); ? ? ? ? axisRight.setDrawLabels(false); ? ? ? ? ? //設(shè)置動(dòng)畫效果 ? ? ? ? lineChart.animateY(2000, Easing.EasingOption.Linear); ? ? ? ? lineChart.animateX(2000, Easing.EasingOption.Linear); ? ? ? ? lineChart.invalidate(); ? ? ? ? //lineChart.animateX(2500); ?//立即執(zhí)行動(dòng)畫 ? ? } ? ? ? /** ? ? ?* @param name ? ? ?* @Description:設(shè)置折線的名稱 ? ? ?*/ ? ? public static void setLineName(String name) { ? ? ? ? lineName = name; ? ? } ? ? ? /** ? ? ?* @param name ? ? ?* @Description:設(shè)置另一條折線的名稱 ? ? ?*/ ? ? public static void setLineName1(String name) { ? ? ? ? lineName1 = name; ? ? } ? }
5.activity_main布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? tools:context=".MainActivity"> ? ? ? <com.github.mikephil.charting.charts.LineChart ? ? ? ? android:layout_marginTop="10dp" ? ? ? ? android:id="@+id/line_chart" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="240dp" /> ? </RelativeLayout>
6.MainActivity
public class MainActivity extends AppCompatActivity { ? ? //參考網(wǎng)址 https://github.com/msandroid/androidChartDemo ? ? private LineChart lineChart1,lineChart2; ? ? private LineData lineData; ? ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? initChart2(); ? ? } ? ? private void initChart2() { ? ? ? ? lineChart2 = (LineChart) findViewById(R.id.line_chart); ? ? ? ? //設(shè)置圖表的描述 ? ? ? ? lineChart2.setDescription(""); ? ? ? ? ? //設(shè)置y軸的數(shù)據(jù) ? ? ? ? float[] datas1 = {53, 23, 79, 42, 12, 26, 94, 85, 53, 12, 69, 42, 10, 26, 94, 85, 53, 13, 79, 42, 10, 20, 94, 85, 79, 42, 10, 20, 94, 85,95};//數(shù)據(jù) ? ? ? ? float[] datas2 = {76, 13, 69, 32, 82, 12, 59, 28, 16, 23, 39, 63, 89, 16, 34, 55, 16, 93, 29, 93, 69, 32, 82, 12, 59, 28, 16, 23, 39, 63,20};//數(shù)據(jù) ? ? ? ? //設(shè)置x軸的數(shù)據(jù) ? ? ? ? int numX=datas1.length; ? ? ? ? //設(shè)置折線的名稱 ? ? ? ? LineChartManager2.setLineName("上月"); ? ? ? ? //設(shè)置第二條折線y軸的數(shù)據(jù) ? ? ? ? LineChartManager2.setLineName1("本月"); ? ? ? ? //創(chuàng)建兩條折線的圖表 ? ? ? ? lineData = LineChartManager2.initDoubleLineChart(this, lineChart1, numX, datas1, datas2); ? ? ? ? LineChartManager2.initDataStyle(lineChart2, lineData, this); ? ? } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解Android圖表 MPAndroidChart折線圖
- MPAndroidChart開源圖表庫(kù)的使用介紹之餅狀圖、折線圖和柱狀圖
- Android MPAndroidChart開源庫(kù)圖表之折線圖的實(shí)例代碼
- Android自定義View實(shí)現(xiàn)折線圖效果
- Android繪制動(dòng)態(tài)折線圖
- Android HelloChart開源庫(kù)圖表之折線圖的實(shí)例代碼
- Android開發(fā)之天氣趨勢(shì)折線圖
- Android自定義控件實(shí)現(xiàn)折線圖
- Android自定義可左右滑動(dòng)和點(diǎn)擊的折線圖
- Android自定義View簡(jiǎn)易折線圖控件(二)
相關(guān)文章
Android 游戲引擎libgdx 資源加載進(jìn)度百分比顯示案例分析
因?yàn)榘咐容^簡(jiǎn)單,所以簡(jiǎn)單用AndroidApplication -> Game -> Stage 搭建框架感興趣的朋友可以參考下2013-01-01Android 實(shí)現(xiàn)掃雷小游戲?qū)嵗a
這篇文章主要介紹了Android 實(shí)現(xiàn)掃雷小游戲?qū)嵗a的相關(guān)資料,需要的朋友可以參考下2016-12-12Android EditText限制輸入字符的方法總結(jié)
這篇文章主要介紹了 Android EditText限制輸入字符的方法總結(jié)的相關(guān)資料,這里提供了五種方法來實(shí)現(xiàn)并進(jìn)行比較,需要的朋友可以參考下2017-07-07Android實(shí)現(xiàn)文件的保存與讀取功能示例
這篇文章主要介紹了Android實(shí)現(xiàn)文件的保存與讀取功能,涉及Android中文件操作函數(shù)getFileDir()和getCacheDir()的相關(guān)使用技巧,需要的朋友可以參考下2016-08-08Android中庫(kù)項(xiàng)目的使用方法圖文介紹
類似開發(fā)其他Java應(yīng)用一樣,我們可以將可復(fù)用的代碼,打成一個(gè)jar包,供所有需要的項(xiàng)目使用。這樣,可以解決很大一部分代碼復(fù)用的問題,本文將詳細(xì)介紹,需要了解的朋友可以參考下2012-12-12Android ViewPager實(shí)現(xiàn)滑動(dòng)指示條功能
這篇文章主要介紹了Android-ViewPager實(shí)現(xiàn)滑動(dòng)指示條功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10Flutter permission_handler 權(quán)限插件的使用詳解
這篇文章主要介紹了Flutter permission_handler 權(quán)限插件的使用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04Android編程實(shí)現(xiàn)基于BitMap獲得圖片像素?cái)?shù)據(jù)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)基于BitMap獲得圖片像素?cái)?shù)據(jù)的方法,對(duì)比分析了兩種獲取圖片像素的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11