android MPAndroidChart折线图绘制

2016-9-4 9:05:57     505 人阅读    

折线图说明

绘制前简单介绍一下 MPAndroidChart
MPAndroidChart是一款基于Android的开源图表库,MPAndroidChart不仅可以在Android设备上绘制各种统计图表,而且可以对图表进行拖动和缩放操作,应用起来非常灵活。和前面介绍的AChartEngine相比,MPAndroidChart显得更为轻巧和简单,MPAndroidChart同样拥有常用的图表类型:线型图、饼图、柱状图和散点图。

 

这个项目并没有使用现成的jar,而是从官网下载的源文件,然后自己弄成了一个eclipse版本的lib库

本次的例子是不带滑动块的例子。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.github.mikephil.charting.charts.LineChart
        android:id="@+id/chart1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/seekBar1" />


    	<!-- 层布局 -->
	<FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
	   <ProgressBar
            android:id="@+id/blogBody_progressBar"
            android:layout_width="@dimen/top_bar_progressbar_width"
            android:layout_height="@dimen/top_bar_progressbar_width"
            android:layout_gravity="center"
            android:background="@color/transparent"
            android:indeterminateDrawable="@drawable/progressbar_moreloading"
            android:visibility="gone" />
	 </FrameLayout>
</RelativeLayout>

 

package com.itmmd.activity.chart;


import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.components.YAxis.AxisDependency;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.AxisValueFormatter;
import com.github.mikephil.charting.formatter.FormattedStringCache;
import com.github.mikephil.charting.interfaces.dataprovider.LineDataProvider;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.itmmd.activity.R;
import com.itmmd.util.BlogHelper;
import com.itmmd.widget.LoadingDialog;
import com.github.mikephil.charting.formatter.FillFormatter;

public class FilledLineActivity extends DemoBase {

	
	LoadingDialog loading;
	ProgressBar blogBody_progressBar;
	
    private LineChart mChart;
    //private SeekBar mSeekBarX;
    //private TextView tvX;
    
    private Handler handler=null;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String today = sdf.format(new Date());
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String json = "";
 //  private int mFillColor = Color.argb(150, 51, 181, 229);
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_linechart_time);

        //tvX = (TextView) findViewById(R.id.tvXMax);
        //mSeekBarX = (SeekBar) findViewById(R.id.seekBar1);
       // mSeekBarX.setProgress(100);
        //tvX.setText("100");

        //mSeekBarX.setOnSeekBarChangeListener(this);

        mChart = (LineChart) findViewById(R.id.chart1);
        mChart.setBackgroundColor(Color.WHITE);
      //  mChart.setGridBackgroundColor(mFillColor);
        mChart.setDrawGridBackground(true);

        mChart.setDrawBorders(true);

        // no description text
        mChart.setDescription("");

        // if disabled, scaling can be done on x- and y-axis separately
        mChart.setPinchZoom(false);

        Legend l = mChart.getLegend();
        l.setEnabled(false);

        XAxis xAxis = mChart.getXAxis();
       // xAxis.setEnabled(false);

        xAxis.setPosition(XAxis.XAxisPosition.TOP_INSIDE);
        xAxis.setTypeface(mTfLight);
        xAxis.setTextSize(10f);
        xAxis.setTextColor(Color.WHITE);
        xAxis.setDrawAxisLine(false);
        xAxis.setDrawGridLines(true);
        xAxis.setTextColor(Color.rgb(255, 192, 56));
        xAxis.setCenterAxisLabels(true);
        xAxis.setGranularity(60000L); // one minute in millis
        xAxis.setValueFormatter(new AxisValueFormatter() {

            private FormattedStringCache.Generic<Long, Date> mFormattedStringCache = new FormattedStringCache.Generic<>(new SimpleDateFormat("HH:mm"));

            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                Long v = (long) value;
                return mFormattedStringCache.getFormattedValue(new Date(v), v);
            }

            @Override
            public int getDecimalDigits() {
                return 0;
            }
        });
        YAxis leftAxis = mChart.getAxisLeft();
        leftAxis.setAxisMaxValue(10f);
        leftAxis.setAxisMinValue(9f);
        leftAxis.setDrawAxisLine(false);
        leftAxis.setDrawZeroLine(false);
        leftAxis.setDrawGridLines(false);

        mChart.getAxisRight().setEnabled(false);

        // add data
        try {
			setData(100, 30);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        mChart.invalidate();

        // get the legend (only possible after setting data)
        /*Legend l = mChart.getLegend();
        l.setEnabled(false);

        XAxis xAxis = mChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.TOP_INSIDE);
        xAxis.setTypeface(mTfLight);
        xAxis.setTextSize(10f);
        xAxis.setTextColor(Color.WHITE);
        xAxis.setDrawAxisLine(false);
        xAxis.setDrawGridLines(true);
        xAxis.setTextColor(Color.rgb(255, 192, 56));
        xAxis.setCenterAxisLabels(true);
        xAxis.setGranularity(60000L); // one minute in millis
        xAxis.setValueFormatter(new AxisValueFormatter() {

            private FormattedStringCache.Generic<Long, Date> mFormattedStringCache = new FormattedStringCache.Generic<>(new SimpleDateFormat("HH:mm"));

            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                Long v = (long) value;
                return mFormattedStringCache.getFormattedValue(new Date(v), v);
            }

            @Override
            public int getDecimalDigits() {
                return 0;
            }
        });

        YAxis leftAxis = mChart.getAxisLeft();
        leftAxis.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
        leftAxis.setTypeface(mTfLight);
        leftAxis.setTextColor(ColorTemplate.getHoloBlue());
        leftAxis.setDrawGridLines(true);
        leftAxis.setGranularityEnabled(true);
        //这里设置最大值  最小值
        leftAxis.setAxisMinValue(9f);
        leftAxis.setAxisMaxValue(10f);
        leftAxis.setYOffset(0);
        leftAxis.setTextColor(Color.rgb(255, 192, 56));

        YAxis rightAxis = mChart.getAxisRight();
        rightAxis.setEnabled(false);*/
        
        
        
        this.blogBody_progressBar = ((ProgressBar)this.findViewById(R.id.blogBody_progressBar));
	    this.blogBody_progressBar.setVisibility(View.VISIBLE);
		Runnable runnable=new Runnable()
        {
            public void run() 
            {
                try 
                {
                    json = BlogHelper.httpGet("http://www.p2pfy.com/user.do?method=getRate","UTF-8" , "rate",true);
                	Log.e("json", json);
              	  
                    handler.sendMessage(handler.obtainMessage(0, json));
                } 
                catch (Exception e) 
                {
                    e.printStackTrace();
                }
            }
        };

        try 
        {
            //开启线程
            new Thread(runnable).start();
            //handler与线程之间的通信及数据处理
            handler=new Handler()
            {
                public void handleMessage(Message msg) 
                {
                    if(msg.what==0)
                    {
                    	//loading.dismiss();
                    	blogBody_progressBar.setVisibility(View.GONE);
                        //msg.obj是获取handler发送信息传来的数据
                        @SuppressWarnings("unchecked")
                        String json =(String) msg.obj;
                        //给ListView绑定数据
                        try {
                        	setData();
						} catch (ParseException e) {
							e.printStackTrace();
						}
                    }
                }
            };
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.line, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.actionToggleValues: {
                List<ILineDataSet> sets = mChart.getData().getDataSets();

                for (ILineDataSet iSet : sets) {

                    LineDataSet set = (LineDataSet) iSet;
                    set.setDrawValues(!set.isDrawValuesEnabled());
                }

                mChart.invalidate();
                break;
            }
            case R.id.actionToggleHighlight: {
                if (mChart.getData() != null) {
                    mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled());
                    mChart.invalidate();
                }
                break;
            }
            case R.id.actionToggleFilled: {

                List<ILineDataSet> sets = mChart.getData()
                        .getDataSets();

                for (ILineDataSet iSet : sets) {

                    LineDataSet set = (LineDataSet) iSet;
                    if (set.isDrawFilledEnabled())
                        set.setDrawFilled(false);
                    else
                        set.setDrawFilled(true);
                }
                mChart.invalidate();
                break;
            }
            case R.id.actionToggleCircles: {
                List<ILineDataSet> sets = mChart.getData()
                        .getDataSets();

                for (ILineDataSet iSet : sets) {

                    LineDataSet set = (LineDataSet) iSet;
                    if (set.isDrawCirclesEnabled())
                        set.setDrawCircles(false);
                    else
                        set.setDrawCircles(true);
                }
                mChart.invalidate();
                break;
            }
            case R.id.actionToggleCubic: {
                List<ILineDataSet> sets = mChart.getData()
                        .getDataSets();

                for (ILineDataSet iSet : sets) {

                    LineDataSet set = (LineDataSet) iSet;
                    if (set.getMode() == LineDataSet.Mode.CUBIC_BEZIER)
                        set.setMode(LineDataSet.Mode.LINEAR);
                    else
                        set.setMode(LineDataSet.Mode.CUBIC_BEZIER);
                }
                mChart.invalidate();
                break;
            }
            case R.id.actionToggleStepped: {
                List<ILineDataSet> sets = mChart.getData()
                        .getDataSets();

                for (ILineDataSet iSet : sets) {

                    LineDataSet set = (LineDataSet) iSet;
                    if (set.getMode() == LineDataSet.Mode.STEPPED)
                        set.setMode(LineDataSet.Mode.LINEAR);
                    else
                        set.setMode(LineDataSet.Mode.STEPPED);
                }
                mChart.invalidate();
                break;
            }
            case R.id.actionTogglePinch: {
                if (mChart.isPinchZoomEnabled())
                    mChart.setPinchZoom(false);
                else
                    mChart.setPinchZoom(true);

                mChart.invalidate();
                break;
            }
            case R.id.actionToggleAutoScaleMinMax: {
                mChart.setAutoScaleMinMaxEnabled(!mChart.isAutoScaleMinMaxEnabled());
                mChart.notifyDataSetChanged();
                break;
            }
            case R.id.animateX: {
                mChart.animateX(3000);
                break;
            }
            case R.id.animateY: {
                mChart.animateY(3000);
                break;
            }
            case R.id.animateXY: {
                mChart.animateXY(3000, 3000);
                break;
            }

            case R.id.actionSave: {
                if (mChart.saveToPath("title" + System.currentTimeMillis(), "")) {
                    Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!",
                            Toast.LENGTH_SHORT).show();
                } else
                    Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT)
                            .show();
                // mChart.saveToGallery("title"+System.currentTimeMillis())
                break;
            }
        }
        return true;
    }

   /* @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        tvX.setText("" + (mSeekBarX.getProgress()));
        try {
			setData();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        // redraw
        mChart.invalidate();
    }*/

    private void setData(int count,int range) throws ParseException {

    	/*String json ="";
    	json = json.replaceAll("\"", "").replaceAll("[", "").replaceAll("]", "");
    	String xStr = json.split("#")[0];
    	String yStr = json.split("#")[1];
    	
    	String[] xList = xStr.split(",");
    	String[] yList = yStr.split(",");
    	
    	 ArrayList<Entry> values = new ArrayList<Entry>();
    	for(int i=count;i<xList.length;i++)
    	{
    		String x = xList[i];
    		//StringBuffer sb = new StringBuffer();
    		String temp =  today+" "+x+":00"; 
    		Log.e("date", temp);
    		Date date = sdf2.parse(temp);
    		long y = Long.parseLong(yList[i]);
    		values.add(new Entry(date.getTime(), y)); // add one entry per hour
    	}
    	*/
    	/**
    	 *   ["00:03","00:07","00:11","00:15","00:19","00:23","00:27","00:31","00:35","00:39","00:43","00:47","00:51","00:55","00:59","01:03","01:07","01:11","01:15","01:19","01:23","01:27","01:31","01:35","01:39","01:43","01:47","01:51","01:55","01:59","02:03","02:07","02:11","02:15","02:19","02:23","02:27","02:31","02:35","02:39","02:43","02:47","02:51","02:55","02:59","03:03","03:07","03:11","03:15","03:19","03:23","03:27","03:31","03:36","03:40","03:44","03:48","03:52","03:56","04:00","04:04","04:08","04:12","04:16","04:20","04:24","04:28","04:32","04:36","04:40","04:44","04:48","04:52","04:56","05:00","05:04","05:08","05:12","05:16","05:20","05:24","05:28","05:32","05:36","05:40","05:44","05:48","05:52","05:56","06:00","06:04","06:08","06:12","06:16","06:20","06:24","06:28","06:32","06:36","06:40","06:44","06:48","06:52","06:56","07:00","07:04","07:08","07:12","07:16","07:20","07:24","07:28","07:32","07:36","07:40","07:44","07:49","07:53","07:57","08:01","08:05","08:09","08:13","08:17","08:21","08:25","08:29","08:33","08:37","08:41","08:45","08:49","08:53","08:57","09:01","09:05","09:09","09:13","09:17","09:21","09:25","09:29","09:33","09:37","09:41","09:45","09:49","09:53","09:57","10:01","10:05","10:09","10:13","10:17","10:21","10:25","10:29","10:33","10:37","10:41","10:45","10:49","10:53","10:57","11:01","11:05","11:09","11:13","11:17","11:21","11:25","11:29","11:33","11:37","11:41","11:45","11:49","11:53","11:57","12:01","12:05","12:09","12:13","12:17","12:21","12:25","12:29","12:33","12:37","12:41","12:45","12:49","12:53","12:57","13:01","13:05","13:09","13:13","13:17","13:21","13:25","13:29","13:33","13:37","13:41","13:45","13:49","13:53","13:57","14:01","14:05","14:09","14:13","14:17","14:21","14:25","14:29","14:33","14:37","14:41","14:45","14:49","14:53","14:57","15:01","15:05","15:09","15:13","15:17","15:21","15:25","15:29","15:33","15:37","15:41","15:45","15:49","15:54","15:58","16:02","16:06","16:10","16:14","16:18","16:22","16:26","16:30","16:34","16:38","16:42","16:46","16:50","16:54","16:58","17:02","17:06","17:10","17:14","17:18","17:22","17:26","17:30","17:34","17:38","17:42","17:46","17:50","17:54","17:58","18:02","18:06","18:10","18:14","18:18","18:22","18:26","18:30","18:34","18:38","18:42","18:46"]
    	 *   #[9.6,9.61,9.62,9.65,9.66,9.66,9.66,9.67,9.67,9.68,9.68,9.67,9.67,9.67,9.67,9.65,9.66,9.66,9.66,9.66,9.66,9.66,9.69,9.7,9.7,9.68,9.69,9.68,9.68,9.69,9.69,9.69,9.68,9.68,9.68,9.68,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.68,9.68,9.68,9.68,9.68,9.68,9.68,9.69,9.69,9.69,9.68,9.68,9.68,9.68,9.68,9.68,9.68,9.68,9.67,9.67,9.67,9.7,9.67,9.68,9.68,9.69,9.68,9.68,9.68,9.68,9.68,9.68,9.88,9.68,9.67,9.67,9.67,9.68,9.69,9.69,9.69,9.68,9.69,9.69,9.68,9.68,9.69,9.69,9.7,9.74,9.72,9.73,9.73,9.72,9.72,9.71,9.71,9.73,9.73,9.72,9.72,9.74,9.73,9.73,9.73,9.74,9.74,9.75,9.75,9.76,9.76,9.78,9.76,9.76,9.78,9.79,9.8,9.8,9.8,9.8,9.8,9.8,9.77,9.78,9.76,9.75,9.75,9.76,9.76,9.76,9.74,9.73,9.73,9.73,9.73,9.72,9.73,9.73,9.72,9.72,9.72,9.71,9.71,9.71,9.71,9.72,9.72,9.72,9.72,9.72,9.72,9.72,9.72,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.72,9.73,9.73,9.73,9.74,9.73,9.74,9.73,9.74,9.74,9.74,9.73,9.73,9.73,9.73,9.74,9.74,9.74,9.74,9.74,9.74,9.75,9.74,9.76,9.75,9.75,9.75,9.75,9.75,9.75,9.75,9.75,9.75,9.75,9.75,9.76,9.77,9.77,9.77,9.77,9.78,9.77,9.78,9.8,9.8,9.79,9.8,9.81,9.82,9.81,9.79,9.8,9.8,9.79,9.79,9.79,9.79,9.78,9.78,9.78,9.77,9.76,9.77,9.77,9.76,9.77,9.77,9.76,9.76,9.76,9.77,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.77,9.77,9.77,9.77,9.77,9.77,9.78,9.79,9.79,9.79,9.79,9.8,9.8]#[9.58,9.6,9.61,9.64,9.65,9.65,9.66,9.66,9.67,9.67,9.67,9.66,9.66,9.66,9.65,9.65,9.65,9.65,9.65,9.66,9.66,9.66,9.66,9.66,9.67,9.65,9.66,9.66,9.66,9.66,9.66,9.66,9.66,9.66,9.67,9.67,9.67,9.66,9.67,9.67,9.66,9.66,9.66,9.66,9.67,9.66,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.68,9.68,9.68,9.68,9.68,9.68,9.68,9.68,9.68,9.68,9.67,9.67,9.67,9.66,9.67,9.67,9.67,9.67,9.67,9.68,9.68,9.67,9.67,9.67,9.66,9.67,9.68,9.68,9.67,9.66,9.67,9.67,9.67,9.67,9.68,9.68,9.68,9.68,9.68,9.68,9.68,9.68,9.69,9.7,9.72,9.72,9.72,9.72,9.71,9.71,9.71,9.71,9.71,9.72,9.72,9.72,9.73,9.73,9.72,9.73,9.73,9.73,9.74,9.75,9.75,9.75,9.76,9.76,9.76,9.78,9.78,9.79,9.79,9.79,9.8,9.79,9.78,9.76,9.76,9.76,9.75,9.75,9.75,9.75,9.74,9.74,9.72,9.72,9.72,9.72,9.72,9.72,9.72,9.72,9.71,9.72,9.71,9.71,9.71,9.71,9.71,9.71,9.72,9.71,9.72,9.72,9.71,9.72,9.72,9.72,9.72,9.72,9.72,9.73,9.72,9.72,9.72,9.72,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.74,9.75,9.74,9.74,9.74,9.74,9.74,9.75,9.75,9.75,9.75,9.75,9.76,9.76,9.76,9.77,9.77,9.77,9.77,9.77,9.78,9.79,9.79,9.79,9.8,9.8,9.79,9.79,9.79,9.79,9.79,9.79,9.79,9.78,9.78,9.78,9.77,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.77,9.76,9.77,9.77,9.77,9.78,9.78,9.78,9.78,9.79,9.79,9.8]#9.8#9.6
    	** 
    	*/
    	
    	//把时间换成毫秒数
        long now = System.currentTimeMillis();
        long hourMillis = 3600000L;

        ArrayList<Entry> values = new ArrayList<Entry>();

        float from = now - (count / 2) * hourMillis;
        float to = now + (count / 2) * hourMillis;

        for (float x = from; x < to; x += hourMillis) {

            float y = getRandom(range, 50);
            values.add(new Entry(x, y)); // add one entry per hour
        }

        // create a dataset and give it a type
        LineDataSet set1 = new LineDataSet(values, "DataSet 1");
        set1.setAxisDependency(AxisDependency.LEFT);
        set1.setColor(ColorTemplate.getHoloBlue());
        set1.setValueTextColor(ColorTemplate.getHoloBlue());
        set1.setLineWidth(1.5f);
        set1.setDrawCircles(false);
        set1.setDrawValues(false);
        set1.setFillAlpha(65);
        set1.setFillColor(ColorTemplate.getHoloBlue());
        set1.setHighLightColor(Color.rgb(244, 117, 117));
        set1.setDrawCircleHole(false);

        // create a data object with the datasets
        LineData data = new LineData(set1);
        data.setValueTextColor(Color.WHITE);
        data.setValueTextSize(9f);

        // set data
        mChart.setData(data);
    }
    private void setData() throws ParseException {

    	//Log.e("jsonReplaceBefore", json);
    	String xStr = json.split("#")[0];
    	String yStr = json.split("#")[1];
    	try {
    		xStr = xStr.replaceAll("\"", "").replaceAll("[\\[\\]]", "");
    		yStr = yStr.replaceAll("\"", "").replaceAll("[\\[\\]]", "");
    	
    	    String[] xList = xStr.split(",");
    	    String[] yList = yStr.split(",");
    	 
	    	 ArrayList<Entry> values = new ArrayList<Entry>();
	    	for(int i=0;i<xList.length;i++)
	    	{
	    		String x = xList[i];
	    		//StringBuffer sb = new StringBuffer();
	    		String temp =  today+" "+x+":00"; 
	    		//Log.e("date", temp);
	    		Date date = sdf2.parse(temp);
	    		float y = Float.parseFloat(yList[i]);
	    		values.add(new Entry(date.getTime(), y)); // add one entry per hour
	    		Log.e("time", temp +"="+y);
    	    }
    	
	    	/**
	    	 *   ["00:03","00:07","00:11","00:15","00:19","00:23","00:27","00:31","00:35","00:39","00:43","00:47","00:51","00:55","00:59","01:03","01:07","01:11","01:15","01:19","01:23","01:27","01:31","01:35","01:39","01:43","01:47","01:51","01:55","01:59","02:03","02:07","02:11","02:15","02:19","02:23","02:27","02:31","02:35","02:39","02:43","02:47","02:51","02:55","02:59","03:03","03:07","03:11","03:15","03:19","03:23","03:27","03:31","03:36","03:40","03:44","03:48","03:52","03:56","04:00","04:04","04:08","04:12","04:16","04:20","04:24","04:28","04:32","04:36","04:40","04:44","04:48","04:52","04:56","05:00","05:04","05:08","05:12","05:16","05:20","05:24","05:28","05:32","05:36","05:40","05:44","05:48","05:52","05:56","06:00","06:04","06:08","06:12","06:16","06:20","06:24","06:28","06:32","06:36","06:40","06:44","06:48","06:52","06:56","07:00","07:04","07:08","07:12","07:16","07:20","07:24","07:28","07:32","07:36","07:40","07:44","07:49","07:53","07:57","08:01","08:05","08:09","08:13","08:17","08:21","08:25","08:29","08:33","08:37","08:41","08:45","08:49","08:53","08:57","09:01","09:05","09:09","09:13","09:17","09:21","09:25","09:29","09:33","09:37","09:41","09:45","09:49","09:53","09:57","10:01","10:05","10:09","10:13","10:17","10:21","10:25","10:29","10:33","10:37","10:41","10:45","10:49","10:53","10:57","11:01","11:05","11:09","11:13","11:17","11:21","11:25","11:29","11:33","11:37","11:41","11:45","11:49","11:53","11:57","12:01","12:05","12:09","12:13","12:17","12:21","12:25","12:29","12:33","12:37","12:41","12:45","12:49","12:53","12:57","13:01","13:05","13:09","13:13","13:17","13:21","13:25","13:29","13:33","13:37","13:41","13:45","13:49","13:53","13:57","14:01","14:05","14:09","14:13","14:17","14:21","14:25","14:29","14:33","14:37","14:41","14:45","14:49","14:53","14:57","15:01","15:05","15:09","15:13","15:17","15:21","15:25","15:29","15:33","15:37","15:41","15:45","15:49","15:54","15:58","16:02","16:06","16:10","16:14","16:18","16:22","16:26","16:30","16:34","16:38","16:42","16:46","16:50","16:54","16:58","17:02","17:06","17:10","17:14","17:18","17:22","17:26","17:30","17:34","17:38","17:42","17:46","17:50","17:54","17:58","18:02","18:06","18:10","18:14","18:18","18:22","18:26","18:30","18:34","18:38","18:42","18:46"]
	    	 *   #[9.6,9.61,9.62,9.65,9.66,9.66,9.66,9.67,9.67,9.68,9.68,9.67,9.67,9.67,9.67,9.65,9.66,9.66,9.66,9.66,9.66,9.66,9.69,9.7,9.7,9.68,9.69,9.68,9.68,9.69,9.69,9.69,9.68,9.68,9.68,9.68,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.68,9.68,9.68,9.68,9.68,9.68,9.68,9.69,9.69,9.69,9.68,9.68,9.68,9.68,9.68,9.68,9.68,9.68,9.67,9.67,9.67,9.7,9.67,9.68,9.68,9.69,9.68,9.68,9.68,9.68,9.68,9.68,9.88,9.68,9.67,9.67,9.67,9.68,9.69,9.69,9.69,9.68,9.69,9.69,9.68,9.68,9.69,9.69,9.7,9.74,9.72,9.73,9.73,9.72,9.72,9.71,9.71,9.73,9.73,9.72,9.72,9.74,9.73,9.73,9.73,9.74,9.74,9.75,9.75,9.76,9.76,9.78,9.76,9.76,9.78,9.79,9.8,9.8,9.8,9.8,9.8,9.8,9.77,9.78,9.76,9.75,9.75,9.76,9.76,9.76,9.74,9.73,9.73,9.73,9.73,9.72,9.73,9.73,9.72,9.72,9.72,9.71,9.71,9.71,9.71,9.72,9.72,9.72,9.72,9.72,9.72,9.72,9.72,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.72,9.73,9.73,9.73,9.74,9.73,9.74,9.73,9.74,9.74,9.74,9.73,9.73,9.73,9.73,9.74,9.74,9.74,9.74,9.74,9.74,9.75,9.74,9.76,9.75,9.75,9.75,9.75,9.75,9.75,9.75,9.75,9.75,9.75,9.75,9.76,9.77,9.77,9.77,9.77,9.78,9.77,9.78,9.8,9.8,9.79,9.8,9.81,9.82,9.81,9.79,9.8,9.8,9.79,9.79,9.79,9.79,9.78,9.78,9.78,9.77,9.76,9.77,9.77,9.76,9.77,9.77,9.76,9.76,9.76,9.77,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.77,9.77,9.77,9.77,9.77,9.77,9.78,9.79,9.79,9.79,9.79,9.8,9.8]#[9.58,9.6,9.61,9.64,9.65,9.65,9.66,9.66,9.67,9.67,9.67,9.66,9.66,9.66,9.65,9.65,9.65,9.65,9.65,9.66,9.66,9.66,9.66,9.66,9.67,9.65,9.66,9.66,9.66,9.66,9.66,9.66,9.66,9.66,9.67,9.67,9.67,9.66,9.67,9.67,9.66,9.66,9.66,9.66,9.67,9.66,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.67,9.68,9.68,9.68,9.68,9.68,9.68,9.68,9.68,9.68,9.68,9.67,9.67,9.67,9.66,9.67,9.67,9.67,9.67,9.67,9.68,9.68,9.67,9.67,9.67,9.66,9.67,9.68,9.68,9.67,9.66,9.67,9.67,9.67,9.67,9.68,9.68,9.68,9.68,9.68,9.68,9.68,9.68,9.69,9.7,9.72,9.72,9.72,9.72,9.71,9.71,9.71,9.71,9.71,9.72,9.72,9.72,9.73,9.73,9.72,9.73,9.73,9.73,9.74,9.75,9.75,9.75,9.76,9.76,9.76,9.78,9.78,9.79,9.79,9.79,9.8,9.79,9.78,9.76,9.76,9.76,9.75,9.75,9.75,9.75,9.74,9.74,9.72,9.72,9.72,9.72,9.72,9.72,9.72,9.72,9.71,9.72,9.71,9.71,9.71,9.71,9.71,9.71,9.72,9.71,9.72,9.72,9.71,9.72,9.72,9.72,9.72,9.72,9.72,9.73,9.72,9.72,9.72,9.72,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.73,9.74,9.75,9.74,9.74,9.74,9.74,9.74,9.75,9.75,9.75,9.75,9.75,9.76,9.76,9.76,9.77,9.77,9.77,9.77,9.77,9.78,9.79,9.79,9.79,9.8,9.8,9.79,9.79,9.79,9.79,9.79,9.79,9.79,9.78,9.78,9.78,9.77,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.76,9.77,9.76,9.77,9.77,9.77,9.78,9.78,9.78,9.78,9.79,9.79,9.8]#9.8#9.6
	    	** 
	    	*/
	    	LineDataSet set1, set2;

	        if (mChart.getData() != null &&
	                mChart.getData().getDataSetCount() > 0) {
	            set1 = (LineDataSet)mChart.getData().getDataSetByIndex(0);
	            set2 = (LineDataSet)mChart.getData().getDataSetByIndex(1);
	            set1.setValues(values);
	            mChart.getData().notifyDataChanged();
	            mChart.notifyDataSetChanged();
	        } else {
	            // create a dataset and give it a type
	            set1 = new LineDataSet(values, "DataSet 1");

	            set1.setAxisDependency(YAxis.AxisDependency.LEFT);
	            set1.setColor(Color.rgb(255, 241, 46));
	            set1.setDrawCircles(false);
	            set1.setLineWidth(2f);
	            set1.setCircleRadius(3f);
	            set1.setFillAlpha(255);
	            set1.setDrawFilled(true);
	            set1.setFillColor(Color.WHITE);
	            set1.setHighLightColor(Color.rgb(244, 117, 117));
	            set1.setDrawCircleHole(false);
	            set1.setFillFormatter(new FillFormatter() {

					@Override
					public float getFillLinePosition(ILineDataSet dataSet,
							LineDataProvider dataProvider) {
						// TODO Auto-generated method stub
						return 0;
					}
	            });

	            // create a dataset and give it a type

	            ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
	            dataSets.add(set1); // add the datasets

	            // create a data object with the datasets
	            LineData data = new LineData(dataSets);
	            data.setDrawValues(false);

	            // set data
	            mChart.setData(data);
	        }
    	} catch (RuntimeException e) {
    		e.printStackTrace();
    	}
    }

}

 

 

 


原文地址:http://www.itmmd.com/201609/797.html
该文章由 萌萌的IT人 整理发布,转载须标明出处。

android MPAndroidChart 二次开发,修改折线图,支持只展示最大最小值   上一篇
下一篇  java replaceAll替换中括号 java.util.regex.PatternSyntaxException: Missing closing bracket in character

精彩回复
发表评论
姓名: