MPAndroidChart使用二之折线图

来源:互联网 发布:人工智能手抄报资料 编辑:程序博客网 时间:2024/06/13 23:35

1. 将mpandroidchartlibrary-2-0-8.jar包copy到项目的libs中

2. 定义xml文件


3.  主要Java逻辑代码如下,注释已经都添加上了。

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.example.mpandroidlinechart;    
  2.     
  3. import java.util.ArrayList;    
  4.     
  5. import com.github.mikephil.charting.charts.LineChart;    
  6. import com.github.mikephil.charting.components.Legend;    
  7. import com.github.mikephil.charting.components.Legend.LegendForm;    
  8. import com.github.mikephil.charting.data.Entry;    
  9. import com.github.mikephil.charting.data.LineData;    
  10. import com.github.mikephil.charting.data.LineDataSet;    
  11.     
  12. import android.support.v7.app.ActionBarActivity;    
  13. import android.graphics.Color;    
  14. import android.os.Bundle;    
  15.     
  16. public class MainActivity extends ActionBarActivity {    
  17.     
  18.     private LineChart mLineChart;    
  19. //  private Typeface mTf;    
  20.     
  21.     @Override    
  22.     protected void onCreate(Bundle savedInstanceState) {    
  23.         super.onCreate(savedInstanceState);    
  24.         setContentView(R.layout.activity_main);    
  25.             
  26.         mLineChart = (LineChart) findViewById(R.id.spread_line_chart);    
  27.             
  28. //      mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Bold.ttf");    
  29.     
  30.         LineData mLineData = getLineData(36100);    
  31.         showChart(mLineChart, mLineData, Color.rgb(114188223));    
  32.     }    
  33.         
  34.     // 设置显示的样式    
  35.     private void showChart(LineChart lineChart, LineData lineData, int color) {    
  36.         lineChart.setDrawBorders(false);  //是否在折线图上添加边框    
  37.     
  38.         // no description text    
  39.         lineChart.setDescription("");// 数据描述    
  40.         // 如果没有数据的时候,会显示这个,类似listview的emtpyview    
  41.         lineChart.setNoDataTextDescription("You need to provide data for the chart.");    
  42.             
  43.         // enable / disable grid background    
  44.         lineChart.setDrawGridBackground(false); // 是否显示表格颜色    
  45.         lineChart.setGridBackgroundColor(Color.WHITE & 0x70FFFFFF); // 表格的的颜色,在这里是是给颜色设置一个透明度    
  46.     
  47.         // enable touch gestures    
  48.         lineChart.setTouchEnabled(true); // 设置是否可以触摸    
  49.     
  50.         // enable scaling and dragging    
  51.         lineChart.setDragEnabled(true);// 是否可以拖拽    
  52.         lineChart.setScaleEnabled(true);// 是否可以缩放    
  53.     
  54.         // if disabled, scaling can be done on x- and y-axis separately    
  55.         lineChart.setPinchZoom(false);//     
  56.     
  57.         lineChart.setBackgroundColor(color);// 设置背景    
  58.     
  59.         // add data    
  60.         lineChart.setData(lineData); // 设置数据    
  61.     
  62.         // get the legend (only possible after setting data)    
  63.         Legend mLegend = lineChart.getLegend(); // 设置比例图标示,就是那个一组y的value的    
  64.     
  65.         // modify the legend ...    
  66.         // mLegend.setPosition(LegendPosition.LEFT_OF_CHART);    
  67.         mLegend.setForm(LegendForm.CIRCLE);// 样式    
  68.         mLegend.setFormSize(6f);// 字体    
  69.         mLegend.setTextColor(Color.WHITE);// 颜色    
  70. //      mLegend.setTypeface(mTf);// 字体    
  71.     
  72.         lineChart.animateX(2500); // 立即执行的动画,x轴    
  73.       }    
  74.         
  75.     /**  
  76.      * 生成一个数据  
  77.      * @param count 表示图表中有多少个坐标点  
  78.      * @param range 用来生成range以内的随机数  
  79.      * @return  
  80.      */    
  81.     private LineData getLineData(int count, float range) {    
  82.         ArrayList<String> xValues = new ArrayList<String>();    
  83.         for (int i = 0; i < count; i++) {    
  84.             // x轴显示的数据,这里默认使用数字下标显示    
  85.             xValues.add("" + i);    
  86.         }    
  87.     
  88.         // y轴的数据    
  89.         ArrayList<Entry> yValues = new ArrayList<Entry>();    
  90.         for (int i = 0; i < count; i++) {    
  91.             float value = (float) (Math.random() * range) + 3;    
  92.             yValues.add(new Entry(value, i));    
  93.         }    
  94.     
  95.         // create a dataset and give it a type    
  96.         // y轴的数据集合    
  97.         LineDataSet lineDataSet = new LineDataSet(yValues, "测试折线图" /*显示在比例图上*/);    
  98.         // mLineDataSet.setFillAlpha(110);    
  99.         // mLineDataSet.setFillColor(Color.RED);    
  100.     
  101.         //用y轴的集合来设置参数    
  102.         lineDataSet.setLineWidth(1.75f); // 线宽    
  103.         lineDataSet.setCircleSize(3f);// 显示的圆形大小    
  104.         lineDataSet.setColor(Color.WHITE);// 显示颜色    
  105.         lineDataSet.setCircleColor(Color.WHITE);// 圆形的颜色    
  106.         lineDataSet.setHighLightColor(Color.WHITE); // 高亮的线的颜色    
  107.     
  108.         ArrayList<LineDataSet> lineDataSets = new ArrayList<LineDataSet>();    
  109.         lineDataSets.add(lineDataSet); // add the datasets    
  110.     
  111.         // create a data object with the datasets    
  112.         LineData lineData = new LineData(xValues, lineDataSets);    
  113.     
  114.         return lineData;    
  115.     }    
  116. }  

效果图如下:


折线图还有另外一种表现形式,就是折线平滑,然后折线与X轴之间可以任意填充自己想要的颜色,其实就是一些属性设置的问题,代码如下:

在上面的getLineData()函数中添加自己的设置:


效果图如下:


关于MPAndroidChart填充式的折线图网上的帖子很少,基本没有。这个是自己在网上搜索其他开源图表库,如JFreeChart...加上自己看源码才总结出来的,不知道对不对,但是看效果,基本上没问题。如果大家发现有问题,欢迎大家指正!


转载自:http://blog.csdn.net/shineflowers/article/details/44704723 继续学习学习~~

0 0
原创粉丝点击