实例介绍
【实例简介】
【实例截图】
【核心代码】
package com.example.tests; import android.app.Activity; import android.graphics.Color; import android.graphics.Typeface; import android.os.Bundle; import android.util.Log; import android.view.Window; import com.github.mikephil.charting.charts.LineChart; import com.github.mikephil.charting.data.ChartData; 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.interfaces.OnChartValueSelectedListener; import com.github.mikephil.charting.listener.BarLineChartTouchListener; import com.github.mikephil.charting.utils.Highlight; import com.github.mikephil.charting.utils.XLabels; import com.github.mikephil.charting.utils.YLabels; import com.github.mikephil.charting.utils.XLabels.XLabelPosition; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; public class MainActivity extends Activity implements OnChartValueSelectedListener { private LineChart mChart; float x = 0; float x1 = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); mChart = (LineChart) findViewById(R.id.chart1); mChart.setOnChartValueSelectedListener(this); mChart.setDrawYValues(false);// 取消圆点处显示的Y轴值 mChart.setBuleBtn(false);// 禁用蓝色按钮 mChart.setDescription("");// 蓝色按钮旁边的文本 mChart.setGridColor(Color.GREEN);// 网格线的颜色 mChart.setGridWidth(1f);// 网格线的大小 mChart.setGridBackground(new Color().DKGRAY);// 网格背景色 mChart.setxTextColor(Color.BLUE);// 设置x轴字体颜色 mChart.setyTextColor(Color.RED);// 设置y轴字体颜色 Typeface mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf"); XLabels xl = mChart.getXLabels(); xl.setCenterXLabelText(true); xl.setPosition(XLabelPosition.BOTTOM);// 设置x轴文本显示在下方 xl.setTypeface(mTf); YLabels yl = mChart.getYLabels(); yl.setTypeface(mTf); yl.setLabelCount(5); BarLineChartTouchListener.ismoveY = false;// 是否禁用y抽移动,默认:禁用 BarLineChartTouchListener.isZoomY = false;// 是否禁用y抽缩放,默认:禁用 mChart.setData((LineData) generateDataLine());// 设置数据 mChart.invalidate(); mChart.animateX(1000);// 设置网格动画 // 不要忘记刷新图吗 // holder.chart.invalidate(); // mChart.setStartAtZero(true); // // 禁用值绘制成图表 // mChart.setDrawYValues(true); // // // 使价值凸显 // mChart.setHighlightEnabled(true); // // // 支持触控手势 // mChart.setTouchEnabled(true); // // // 支持缩放和拖动 // mChart.setDragEnabled(true); // // // 如果禁用,扩展可以分别在x,y轴 // mChart.setPinchZoom(false); // 不要忘记刷新图吗 // holder.chart.invalidate(); // mSeekBarX.setProgress(45); // mSeekBarY.setProgress(100); // Legend l = mChart.getLegend(); // l.setPosition(LegendPosition.RIGHT_OF_CHART); } @Override public void onValuesSelected(Entry[] values, Highlight[] highlights,int position) {// 原点点击事件 Log.i("123456", "Value: " values[0].getVal() ", xIndex: " highlights[0].getXIndex() ", DataSet index: " highlights[0].getDataSetIndex()); } @Override public void onNothingSelected() { // TODO Auto-generated method stub } private ChartData generateDataLine() { ArrayList<Entry> e1 = new ArrayList<Entry>(); //第一参数y,第二参数x e1.add(new Entry((int) (Math.random() * 50), getData("2014-02-28"))); e1.add(new Entry((int) (Math.random() * 50), getData("2014-02-28"))); e1.add(new Entry((int) (Math.random() * 50), getData("2014-02-28"))); e1.add(new Entry((int) (Math.random() * 50), getData("2014-03-12"))); e1.add(new Entry((int) (Math.random() * 50), getData("2014-04-02"))); e1.add(new Entry((int) (Math.random() * 50), getData("2014-05-09"))); e1.add(new Entry((int) (Math.random() * 50), getData("2014-06-15"))); e1.add(new Entry((int) (Math.random() * 50), getData("2014-07-19"))); e1.add(new Entry((int) (Math.random() * 50), getData("2014-08-16"))); e1.add(new Entry((int) (Math.random() * 50), getData("2014-08-23"))); e1.add(new Entry((int) (Math.random() * 50), getData("2014-09-19"))); e1.add(new Entry((int) (Math.random() * 50), getData("2014-10-31"))); e1.add(new Entry((int) (Math.random() * 50), getData("2014-11-26"))); e1.add(new Entry((int) (Math.random() * 50), getData("2014-12-10"))); e1.add(new Entry((int) (Math.random() * 50), getData("2014-12-13"))); Log.d("123456", getData("2014-02-28 1:31:35") ""); Log.d("123456", getData("2014-02-28 5:54:54") ""); Log.d("123456", getData("2014-02-28 18:16:21") ""); Log.d("123456", getData("2014-02-28 23:31:25") ""); Log.d("123456", getData("2014-03-12") ""); Log.d("123456", getData("2014-04-02") ""); Log.d("123456", getData("2014-05-09") ""); Log.d("123456", getData("2014-06-15") ""); Log.d("123456", getData("2014-07-19") ""); Log.d("123456", getData("2014-08-16") ""); Log.d("123456", getData("2014-08-23") ""); Log.d("123456", getData("2014-09-19") ""); Log.d("123456", getData("2014-10-31") ""); Log.d("123456", getData("2014-11-26") ""); Log.d("123456", getData("2014-12-10") ""); Log.d("123456", getData("2014-12-13") ""); LineDataSet d1 = new LineDataSet(e1, ""); d1.setLineWidth(3f);// 线的大小 d1.setColor(Color.WHITE);// 设置有数据在网格里线的颜色 d1.setCircleSize(5f);// 原点大小 d1.setCircleColor(Color.LTGRAY);// 设置原点颜色 ArrayList<LineDataSet> sets = new ArrayList<LineDataSet>(); sets.add(d1); LineData cd = new LineData(getdateDay(), sets); return cd; } /** 传的数据格式必须是 2010-08-05 12:31:23*/ private float getData(String date) { String month = date.substring(5, 7); String day = date.substring(8,10); boolean b = isLeapYear(Integer.parseInt(date.substring(0, 4))); float hours = 0; try { Date dates = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date); hours = (dates.getHours() 1)*0.04f;//换算一天的小时在百分比里面占多少,以每天25个小时计算 } catch (ParseException e) { e.printStackTrace(); } if (month.equals("1") || month.equals("01")) { return Integer.parseInt(day)-1 hours; } else if (month.equals("2") || month.equals("02")) { return Integer.parseInt(day) 31 - 1 hours; } else if (month.equals("3") || month.equals("03")) { if (b) { return Integer.parseInt(day) 60-1 hours; } else { return Integer.parseInt(day) 59-1 hours; } } else if (month.equals("4") || month.equals("04")) { if (b) { return Integer.parseInt(day) 91-1 hours; } else { return Integer.parseInt(day) 90-1 hours; } } else if (month.equals("5") || month.equals("05")) { if (b) { return Integer.parseInt(day) 121-1 hours; } else { return Integer.parseInt(day) 120-1 hours; } } else if (month.equals("6") || month.equals("06")) { if (b) { return Integer.parseInt(day) 152-1 hours; } else { return Integer.parseInt(day) 151-1 hours; } } else if (month.equals("7") || month.equals("07")) { if (b) { return Integer.parseInt(day) 182-1 hours; } else { return Integer.parseInt(day) 181-1 hours; } } else if (month.equals("8") || month.equals("08")) { if (b) { return Integer.parseInt(day) 213-1 hours; } else { return Integer.parseInt(day) 212-1 hours; } } else if (month.equals("9") || month.equals("09")) { if (b) { return Integer.parseInt(day) 244-1 hours; } else { return Integer.parseInt(day) 243-1 hours; } } else if (month.equals("10") || month.equals("10")) { if (b) { return Integer.parseInt(day) 274-1 hours; } else { return Integer.parseInt(day) 273-1 hours; } } else if (month.equals("11") || month.equals("11")) { if (b) { return Integer.parseInt(day) 305-1 hours; } else { return Integer.parseInt(day) 304-1 hours; } } else if (month.equals("12") || month.equals("12")) { if (b) { return Integer.parseInt(day) 335-1 hours; } else { return Integer.parseInt(day) 334-1 hours; } } return 0; } private ArrayList<String> getdateDay() { ArrayList<String> arraylists = new ArrayList<String>(); Date date = new Date(); for (int j = 1; j <= 31; j ) { arraylists.add("1-" j); } if (isLeapYear((date.getYear() 1900))) {// 判断是否闰年 for (int j = 1; j <= 29; j ) { arraylists.add("2-" j); } } else { for (int j = 1; j <= 28; j ) { arraylists.add("2-" j); } } for (int j = 1; j <= 31; j ) { arraylists.add("3-" j); } for (int j = 1; j <= 30; j ) { arraylists.add("4-" j); } for (int j = 1; j <= 31; j ) { arraylists.add("5-" j); } for (int j = 1; j <= 30; j ) { arraylists.add("6-" j); } for (int j = 1; j <= 31; j ) { arraylists.add("7-" j); } for (int j = 1; j <= 31; j ) { arraylists.add("8-" j); } for (int j = 1; j <= 30; j ) { arraylists.add("9-" j); } for (int j = 1; j <= 31; j ) { arraylists.add("10-" j); } for (int j = 1; j <= 30; j ) { arraylists.add("11-" j); } for (int j = 1; j <= 31; j ) { arraylists.add("12-" j); } return arraylists; } private boolean isLeapYear(int year) { if ((year % 400) == 0) { return true; } if ((year % 4) == 0) { if ((year % 100) == 0) { return false; } else { return true; } } else { return false; } } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论