在这里使用的插件为Mpchart,只以折线图为例。首先需要导入包。

在Layout.xml中设置布局,如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"><LinearLayoutandroid:id="@+id/linearLayout1"android:layout_width="fill_parent"android:layout_height="40.0dip"><TextViewandroid:id="@+id/text4"android:layout_width="fill_parent"android:layout_height="match_parent"android:text="2016年油耗统计"android:textSize="15dp" /></LinearLayout><com.github.mikephil.charting.charts.LineChartandroid:id="@+id/chart1"android:layout_width="match_parent"android:layout_height="400dp"android:layout_marginTop="20dp" /></RelativeLayout>

在Activity中设置数据和图表的格式。如下:

package com.example.view;import java.util.ArrayList;import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.WindowManager;import com.example.iov.R;
import com.github.mikephil.charting.charts.BarLineChartBase.BorderPosition;
import com.github.mikephil.charting.charts.LineChart;
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.utils.Legend;
import com.github.mikephil.charting.utils.Legend.LegendForm;
import com.github.mikephil.charting.utils.XLabels;
import com.github.mikephil.charting.utils.XLabels.XLabelPosition;
import com.github.mikephil.charting.utils.YLabels;public class gaschar extends Activity {private LineChart mChart;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);setContentView(R.layout.gasinfo);mChart = (LineChart) findViewById(R.id.chart1);// 设置在Y轴上是否是从0开始显示mChart.setStartAtZero(true);//是否在Y轴显示数据,就是曲线上的数据mChart.setDrawYValues(true);//设置网格mChart.setDrawBorder(true);mChart.setBorderPositions(new BorderPosition[] {BorderPosition.BOTTOM});//在chart上的右下角加描述mChart.setDescription("曲线图");//设置Y轴上的单位mChart.setUnit("¥"); //设置透明度mChart.setAlpha(0.8f);//设置网格底下的那条线的颜色mChart.setBorderColor(Color.rgb(213, 216, 214));//设置Y轴前后倒置mChart.setInvertYAxisEnabled(false);//设置高亮显示mChart.setHighlightEnabled(true);//设置是否可以触摸,如为false,则不能拖动,缩放等mChart.setTouchEnabled(true);//设置是否可以拖拽,缩放mChart.setDragEnabled(true);mChart.setScaleEnabled(true);//设置是否能扩大扩小mChart.setPinchZoom(true);// 设置背景颜色// mChart.setBackgroundColor(Color.GRAY);//设置点击chart图对应的数据弹出标注MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view);// define an offset to change the original position of the marker// (optional)mv.setOffsets(-mv.getMeasuredWidth() / 2, -mv.getMeasuredHeight());// set the marker to the chart
        mChart.setMarkerView(mv);// enable/disable highlight indicators (the lines that indicate the// highlighted Entry)mChart.setHighlightIndicatorEnabled(false);//设置字体格式,如正楷Typeface tf = Typeface.createFromAsset(getAssets(),"OpenSans-Regular.ttf");mChart.setValueTypeface(tf);XLabels xl = mChart.getXLabels();
//        xl.setAvoidFirstLastClipping(true);
//        xl.setAdjustXLabels(true);xl.setPosition(XLabelPosition.BOTTOM); // 设置X轴的数据在底部显示xl.setTypeface(tf); // 设置字体xl.setTextSize(10f); // 设置字体大小xl.setSpaceBetweenLabels(3); // 设置数据之间的间距
YLabels yl = mChart.getYLabels();// yl.setPosition(YLabelPosition.LEFT_INSIDE); // set the positionyl.setTypeface(tf); // 设置字体yl.setTextSize(10f); // s设置字体大小yl.setLabelCount(5); // 设置Y轴最多显示的数据个数// 加载数据
        setData();//从X轴进入的动画mChart.animateX(4000);mChart.animateY(3000);   //从Y轴进入的动画mChart.animateXY(3000, 3000);    //从XY轴一起进入的动画//设置最小的缩放mChart.setScaleMinima(0.5f, 1f);//设置视口// mChart.centerViewPort(10, 50);// get the legend (only possible after setting data)Legend l = mChart.getLegend();l.setForm(LegendForm.LINE);  //设置图最下面显示的类型
        l.setTypeface(tf);  l.setTextSize(15);l.setTextColor(Color.rgb(104, 241, 175));l.setFormSize(30f); // set the size of the legend forms/shapes// 刷新图表
        mChart.invalidate();}private void setData() {String[] aa = {"1","2","3","4","5","6","7","8","9","10","11","12","月"};String[] bb = {"122.00","234.34","85.67","117.90","332.33","113.33"};ArrayList<String> xVals = new ArrayList<String>();for (int i = 0; i < aa.length; i++) {xVals.add(aa[i]);}ArrayList<Entry> yVals = new ArrayList<Entry>();for (int i = 0; i < bb.length; i++) {yVals.add(new Entry(Float.parseFloat(bb[i]), i));}// create a dataset and give it a typeLineDataSet set1 = new LineDataSet(yVals, "DataSet Line");set1.setDrawCubic(true);  //设置曲线为圆滑的线set1.setCubicIntensity(0.2f);set1.setDrawFilled(false);  //设置包括的范围区域填充颜色set1.setDrawCircles(true);  //设置有圆点set1.setLineWidth(2f);    //设置线的宽度set1.setCircleSize(5f);   //设置小圆的大小set1.setHighLightColor(Color.rgb(244, 117, 117));set1.setColor(Color.rgb(104, 241, 175));    //设置曲线的颜色// create a data object with the datasetsLineData data = new LineData(xVals, set1);// set data
        mChart.setData(data);}
}

其中MyMarkerView(点击节点类)代码:

package com.example.view;import android.content.Context;
import android.widget.TextView;import com.example.iov.R;
import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.utils.MarkerView;
import com.github.mikephil.charting.utils.Utils;public class MyMarkerView extends MarkerView {private TextView tvContent;public MyMarkerView(Context context, int layoutResource) {super(context, layoutResource);tvContent = (TextView) findViewById(R.id.tvContent);}// callbacks everytime the MarkerView is redrawn, can be used to update the// content
    @Overridepublic void refreshContent(Entry e, int dataSetIndex) {if (e instanceof CandleEntry) {CandleEntry ce = (CandleEntry) e;tvContent.setText("" + Utils.formatNumber(ce.getHigh(), 0, true));} else {//            tvContent.setText("" + Utils.formatNumber(e.getVal(), 0, true));tvContent.setText("" +e.getVal());}}
}

效果如下:

转载于:https://www.cnblogs.com/bingoing/p/5865585.html

android开发之图表相关推荐

  1. android开发比例图表,Android开发中如何使用绘制图表

    [IT168技术]在日常的统计中,经常要用图表来给用户恰当的数据体验,比如用饼状图,柱型图等.在传统的web中,有比较多的开源的这方面的解决方案.本文将简单介绍如何在Android中,如何使用开源工具 ...

  2. Android开发傲娇之作

    你坐在椅子上,忽略了窗外流过的光 你伸出双手摸着屏幕上写下的希望 你说应用上了又下像一扇窗 可是窗开了又关像Bug的模样 --改编自筠子<立秋> 有一本书,小编一直不知道如何介绍给大家,因 ...

  3. Android开源框架——图表MPAndroidChart

    开源官网:https://github.com/PhilJay/MPAndroidChart Android开源框架--图表MPAndroidChart 特点 配置 图表类型 Demo MPAndro ...

  4. Android开发笔记(一百零二)统计图表

    AChartEngine AChartEngine是Android平台上的图表绘制引擎,提供了包括折线图.柱状图.饼状图等图表显示.它的官网地址是http://achartengine.org/,源码 ...

  5. Android开发— 2016_最流行的Android组件、工具、框架大全(二)

    2019独角兽企业重金招聘Python工程师标准>>> 泡在网上的日子 首页 代码 话题 问答 标签 关于 登录注册 首页 › 安卓开发 › android开发 Android开发- ...

  6. 【近3万字分享】《Android开发之路——10年老开发精心整理分享》

    目录 前言 1 Android开发学习路线 1.1 大神最新总结(推荐直接看这个) 2021 最新Android知识体系 1.2按内容划分 1.3按阶段划分 1.4Android进阶路线(思维导图) ...

  7. Android开发:开源库集合

    开源库大全 目录 抽屉菜单 ListView WebView SwitchButton 按钮 点赞按钮 进度条 TabLayout 图标 下拉刷新 ViewPager 图表(Chart) 菜单(Men ...

  8. CMDN CLUB第14场:小米与友盟专家详解Android开发:

    2月29日,CMDN Club第十四期活动在北京丽亭华苑酒店举行.本次活动是开春以来CMDN俱乐部的首场主题交流会,友盟Android SDK工程师徐仙明和小米科技MIUI系统工程师董红光为大家带来了 ...

  9. Android开发必备(干货源码放送大)

    Android源码大放送(实战开发必备) 文件夹 PATH 列表 │  javaapk.com文件列表生成工具.bat │  使用说明.txt │  免费下载更多源码.url │  目录列表.txt ...

最新文章

  1. Compmgmtlauncher.exe问题解决方法
  2. struct2 开发环境搭建 问题
  3. JavaScrip调用腾讯地图
  4. js 调用另一个类的方法_一个隐藏在方法集和方法调用中且易被忽略的小细节
  5. 宝塔asp php,宝塔Windows面板部署ASP、ASPX程序WEB网站环境方法
  6. c语言百文百鸡问题答案,python_百文买百鸡问题
  7. html怎么引入圆角插件,jQuery圆角插件demo页面 张鑫旭-鑫空间-鑫生活
  8. 【消息队列之rabbitmq】Rabbitmq之消息可靠性投递和ACK机制实战
  9. 设置不显示用户名和主机名_谁说Excel中不可以有聚光灯效果:Excel高亮显示设置...
  10. 初识 NGINX 服务网格
  11. python map函数_Python map()函数
  12. spark sql 入门详解
  13. 9款最新炫酷HTML5/CSS3应用推荐
  14. java怎么缓存数据_java中的缓存技术该如何实现
  15. 团队作业8----第二次项目冲刺(Beta阶段) 第一天
  16. 【基于Android的连连看游戏的设计与实现】
  17. xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at:
  18. Oracle表空间查询
  19. java课程结课论文,语言课程论文范文
  20. Photon Pun

热门文章

  1. nginx安装与配置详解
  2. 13、mybatis多表关联查询级联属性
  3. python limit_Python MySQL Limit
  4. 苏州银行签约神策数据,致力打造科技引领的新时代普惠银行
  5. 急速收藏:4套iOS SDK的H5打通方案
  6. 神策数据CEO桑文锋:精准采集数据,走向智能化分析
  7. 一个简易的webpack开发环境
  8. 汉王人脸1000万用户后的第一人诞生
  9. VMware vSphere/vCenter/ESX(i)介绍
  10. 容易的linux自动化运维工具之clinet端(二)