Android曲线简单例子——MPChart

Home / Android MrLee 2015-10-28 9873

MPAndroidChart is a powerful & easy to use chart library for Android, supporting line-, bar-, scatter-, candlestick-, bubble-, pie- and radarcharts (spider web), as well as scaling, dragging (panning), selecting and animations. Works on Android 2.2 (API level 8)and upwards.
An iOS version of this library is now available, go check it out: ios-charts
Are you using this library? Let me know about it and I will add your project to the references.

Donations

This project needs you! If you would like to support this project’s further development, the creator of this project or the continuous maintenance of this project, feel free to donate. Your donation is highly appreciated (and I love food, coffee and beer). Thank you!
 
这是官方最新更新的源码简单写的一个例子。为了区分和老版本,我把com.github...改成了org.github... 目的是为了区分。
首先看布局文件

下面是Java代码,要说明的是,里面有些依赖了其它的类,不过不影响看实现流程嘛!
import java.util.ArrayList;
import java.util.Calendar;
import org.github.mikephil.charting.charts.LineChart;
import org.github.mikephil.charting.components.Legend;
import org.github.mikephil.charting.components.Legend.LegendForm;
import org.github.mikephil.charting.components.XAxis;
import org.github.mikephil.charting.components.XAxis.XAxisPosition;
import org.github.mikephil.charting.components.YAxis;
import org.github.mikephil.charting.data.Entry;
import org.github.mikephil.charting.data.LineData;
import org.github.mikephil.charting.data.LineDataSet;
import org.json.JSONArray;
import org.json.JSONObject;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;

public class JiangFragment extends BaseFragment implements OnItemClickListener {
	private Button value01;
	private Button value02;
	private Button value03;
	private HListView hListView;
	private DateAdapter dateAdapter;
	String monthDay;// 当前选中的日期
	String deviceId;
	private LineChart chart;
	private ArrayList yVals;
	public JiangFragment(int layoutID) {
		super(layoutID);
		// TODO Auto-generated constructor stub
		yVals = new ArrayList();
	}
	@Override
	public boolean onOinitCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		value01 = (Button) findViewById(R.id.value01);
		value02 = (Button) findViewById(R.id.value02);
		value03 = (Button) findViewById(R.id.value03);
		value01.setSelected(true);
		hListView = (HListView) findViewById(R.id.list_date);
		if (dateAdapter == null)
			dateAdapter = new DateAdapter(getActivity());
		hListView.setAdapter(dateAdapter);
		hListView.setOnItemClickListener(this);
		generationDateItem();
		yVals.clear();
		chart = (LineChart) findViewById(R.id.chart);
		for (int i = 0; i < 24; i++) {
			Entry entry = new Entry(Tools.rand(10, 100), i);
			Tools.sleep(1);
			yVals.add(entry);
		}
		setupChart(chart, getData(yVals), 0xFFFFFF);
		return false;
	}
	void testData() {
		yVals.clear();
		for (int i = 0; i < 24; i++) {
			Entry entry = new Entry(Tools.rand(10, 100), i);
			Tools.sleep(1);
			yVals.add(entry);
		}
		chart.setData(getData(yVals));
		chart.animateXY(2500, 2500);
	}
	private LineData getData(ArrayList yVals) {
		LineDataSet set1 = new LineDataSet(yVals, "日视图");// 曲线数据和标签
		set1.setLineWidth(1f);// 曲线粗细
		set1.setCircleSize(4f);// 圆半径
		set1.setDrawFilled(true);// 填充
		set1.setColor(getResources().getColor(R.color.new_blue));
		set1.setCircleColor(getResources().getColor(R.color.new_blue));
		set1.setHighLightColor(getResources().getColor(R.color.new_blue));
		set1.setValueTextColor(0xffffffff);
		ArrayList dataSets = new ArrayList();
		dataSets.add(set1); // 一条曲线
		LineData data = new LineData(getHours(), dataSets);
		return data;
	}
	private LineChart setupChart(LineChart chart, LineData data, int color) {
		int bkColor = getResources().getColor(R.color.chart_background);
		chart.setBorderColor(getResources().getColor(R.color.chart_background));
		chart.setDescription("");
		chart.setNoDataTextDescription("没有数据");
		chart.setDrawGridBackground(false);
		chart.setTouchEnabled(true);
		chart.setDragEnabled(true);
		chart.setScaleEnabled(true);
		chart.setPinchZoom(false);
		if (color != -1) {
			chart.setBackgroundColor(color);
		}
		chart.setData(data);
		Legend l = chart.getLegend();
		l.setForm(LegendForm.CIRCLE);
		l.setFormSize(6f);
		l.setTextColor(bkColor);
		XAxis xAxis = chart.getXAxis();
		xAxis.setTextSize(12f);
		xAxis.setTextColor(bkColor);
		xAxis.setPosition(XAxisPosition.BOTTOM);
		YAxis leftAxis = chart.getAxisLeft();
		leftAxis.setTextColor(bkColor);
		
		YAxis rightAxis = chart.getAxisRight();
		rightAxis.setEnabled(false);
		chart.animateXY(2500, 2500);
		return chart;
	}
	public ArrayList getHours() {
		ArrayList m = new ArrayList();
		for (int i = 0; i < 24; i++) {
			if (i == 0) {
				m.add("0时");
			} else {
				m.add(i + "");
			}
		}
		return m;
	}
	private void generationDateItem() {
		int num = 30;
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(System.currentTimeMillis());
		DateItem item = new DateItem();
		int month = calendar.get(Calendar.MONTH) + 1;
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		item.setMonth(month);
		item.setDay(day);
		dateAdapter.add(item);
		item.setSelected(true);// 第一个默认选中
		monthDay = item.getSMonth() + item.getSday();
		int second = calendar.get(Calendar.SECOND);
		for (int i = 0; i < num; i++) {
			calendar.set(Calendar.SECOND, second - 3600 * 24);
			item = new DateItem();
			month = calendar.get(Calendar.MONTH) + 1;
			day = calendar.get(Calendar.DAY_OF_MONTH);
			item.setMonth(month);
			item.setDay(day);
			dateAdapter.add(item);
		}
		dateAdapter.notifyDataSetChanged();
	}
	private void resetSelected() {
		value01.setSelected(false);
		value02.setSelected(false);
		value03.setSelected(false);
	}
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		super.onClick(view);
		int id = view.getId();
		if (id >= R.id.value01 && id <= R.id.value01 + 2) {
			resetSelected();
			view.setSelected(true);
		}
	}
	void requestHistory() {
		RequestCallBack callBack = new RequestCallBack() {
			@Override
			public void callback(HttpResult result) {
				// TODO Auto-generated method stub
				if (result == null || result.getError_code() != 0) {
					MLog.makeText("无历史数据");
					return;
				}
				try {
					JSONObject object = result.getParantObject();
					JSONObject detail = object.getJSONObject("detail");
					JSONArray devhistory = detail.getJSONArray("devhistory");
					String json = devhistory.toString();
					Message msg = new Message();
					msg.what = 1000;
					msg.obj = json;
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(System.currentTimeMillis());
		int year = calendar.get(Calendar.YEAR);
		String date1 = year + monthDay;
		HttpFunction.requestRobotBigData(deviceId, date1, date1, false,
				callBack);
	}
	@Override
	public void onItemClick(AdapterView parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		DateItem item = dateAdapter.getItem(position);
		dateAdapter.resetSelected();
		item.setSelected(true);
		monthDay = item.getSMonth() + item.getSday();
		// requestHistory();
		dateAdapter.notifyDataSetChanged();
		testData();
	}
}

20151028215829


库文件下载:mpchartlib 下载完了解压得到.jar文件,然后放到工程的libs目录即可。

本文链接:https://www.it72.com/6632.htm

推荐阅读
最新回复 (0)
返回