Android MPAndroidCharts 框架 画可滑动查看的直方图
1.由于公司项目的需求,所以花了1.2天研究 MPAndroidCharts框架 。可是发现好多博客对我都没得帮助。浪费非常多时间在百度上。不得不说google 真是比百度强太多了。
要求:统计出56个名族的数量
原创作者的github:https://github.com/PhilJay/MPAndroidChart
MPAndroidCharts API地址:https://jitpack.io/com/github/PhilJay/MPAndroidChart/v2.2.5/javadoc/
2.用到的框架是MPAndroidCharts。引入的依赖:
compile 'com.github.PhilJay:MPAndroidChart:v3.0.2'
然后在引入仓库
3.正式開始使用
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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="aas.androidmpcharts.MainActivity"> <com.github.mikephil.charting.charts.BarChart
android:id="@+id/mBarChart"
android:layout_width="match_parent"
android:layout_height="300dp"
/>
</RelativeLayout>
MainActivity代码:
public class MainActivity extends AppCompatActivity {
    BarChart mBarChart;
    ArrayList<String> mlist=new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBarChart = (BarChart) findViewById(R.id.mBarChart);
        mBarChart.setDrawBarShadow(false);     //表不要阴影
        mBarChart.setDrawValueAboveBar(true);
        Description description=new Description();
        description.setText("通行民族");
        mBarChart.setDescription(description);  //表的描写叙述信息
        mBarChart.setPinchZoom(false);
        mBarChart.setMaxVisibleValueCount(60); //最大显示的个数。超过60个将不再显示
        mBarChart.setScaleEnabled(false);     //禁止缩放
        mBarChart.setDragEnabled(true);// 能否够拖拽
        mBarChart.setHighlightPerDragEnabled(true);// 拖拽超过图标绘制画布时高亮显示
        mBarChart.setDrawGridBackground(false);//
      /*  mBarChart.setAutoScaleMinMaxEnabled(true);*/
       /* mBarChart.animateX(500);//数据显示动画,从左往右依次显示*/
       /* mBarChart.getAxisRight().setEnabled(false);*/
        /*mBarChart.setDragDecelerationEnabled(true);*///拖拽滚动时。手放开是否会持续滚动,默认是true(false是拖到哪是哪,true拖拽之后还会有缓冲)
        mBarChart.zoom(2.5f,1f,0,0);//显示的时候 是 依照多大的比率缩放显示   1f表示不放大缩小
        //我默认手机屏幕上显示10  剩下的滑动直方图 然后显示。
。假如要显示25个 那么除以10 就是放大2.5f。
。同理
        // 56个民族   那么放大5.6f
        draw();
    }
    public void draw(){
        //X轴 样式
        final XAxis xAxis = mBarChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setLabelRotationAngle(90);//柱的以下描写叙述文字  旋转90度
        xAxis.setDrawLabels(true);
        xAxis.setDrawGridLines(false);
        xAxis.setTypeface(Typeface.createFromAsset(getAssets(), "OpenSans-Light.ttf"));//字体的相关的设置
        xAxis.setGranularity(1f);//设置最小间隔。防止当放大时,出现反复标签。
        xAxis.setCenterAxisLabels(true);//字体以下的标签 显示在每一个直方图的中间
        xAxis.setLabelCount(11,true);//一个界面显示10个Lable。那么这里要设置11个
        xAxis.setTextSize(10f);
        //Y轴样式
        YAxis leftAxis = mBarChart.getAxisLeft();
        leftAxis.setLabelCount(10);
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        leftAxis.setSpaceTop(15f);
        leftAxis.setStartAtZero(false);
        leftAxis.setYOffset(10f);
        //这个替换setStartAtZero(true)
        leftAxis.setAxisMaxValue(50f);
        leftAxis.setAxisMinValue(0f);
        leftAxis.setDrawGridLines(true);//背景线
        leftAxis.setAxisLineColor(getResources().getColor(R.color.colorPrimaryDark));
        //.设置比例图标的显示隐藏
        Legend l = mBarChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setDrawInside(false);
        //样式
        l.setForm(Legend.LegendForm.CIRCLE);
        //字体
        l.setFormSize(10f);
        //大小
        l.setTextSize(13f);
        l.setFormToTextSpace(10f);
        l.setXEntrySpace(10f);
        //模拟数据
        ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
        yVals1.add(new BarEntry(1,23));
        yVals1.add(new BarEntry(2, 20));
        yVals1.add(new BarEntry(3, 30));
        yVals1.add(new BarEntry(4, 10));
        yVals1.add(new BarEntry(5, 45));
        yVals1.add(new BarEntry(6, 50));
        yVals1.add(new BarEntry(7, 35));
        yVals1.add(new BarEntry(8, 26));
        yVals1.add(new BarEntry(9, 14));
        yVals1.add(new BarEntry(10, 20));
        yVals1.add(new BarEntry(11, 33));
        yVals1.add(new BarEntry(12, 44));
        yVals1.add(new BarEntry(13, 42));
        yVals1.add(new BarEntry(14, 41));
        yVals1.add(new BarEntry(15, 12));
        yVals1.add(new BarEntry(16, 31));
        yVals1.add(new BarEntry(17, 21));
        yVals1.add(new BarEntry(18, 20));
        yVals1.add(new BarEntry(19, 44));
        yVals1.add(new BarEntry(20, 42));
        yVals1.add(new BarEntry(21, 41));
        yVals1.add(new BarEntry(22, 12));
        yVals1.add(new BarEntry(23, 31));
        yVals1.add(new BarEntry(24, 21));
        yVals1.add(new BarEntry(25, 20));
        setData(yVals1);
    }
    private void setData(ArrayList yVals1) {
        for(int i=1;i<=yVals1.size();i++) {
            mlist.add(""+i);
        }
        IAxisValueFormatter ix=new MyXAxisValueFormatter(mlist);
        mBarChart.getXAxis().setValueFormatter(ix);
        BarDataSet set1;
        if (mBarChart.getData() != null &&
                mBarChart.getData().getDataSetCount() > 0) {
            set1 = (BarDataSet) mBarChart.getData().getDataSetByIndex(0);
            set1.setValues(yVals1);
            mBarChart.getData().notifyDataChanged();
            mBarChart.notifyDataSetChanged();
        } else {
            set1 = new BarDataSet(yVals1, "The year 2017");
            set1.setColors(ColorTemplate.MATERIAL_COLORS);
            ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
            dataSets.add(set1);
            BarData data = new BarData(dataSets);
            data.setValueTextSize(10f);
            data.setBarWidth(1f);
            mBarChart.setData(data);
        }
    }
    public class MyXAxisValueFormatter implements IAxisValueFormatter {
        private List<String> mValues;
        public MyXAxisValueFormatter(List<String> values) {
            this.mValues = values;
        }
        @Override
        public String getFormattedValue(float value, AxisBase axis) {
           int x=(int)(value);
            if (x<0)
                x=0;
            if (x>=mValues.size())
                x=mValues.size()-1;
            return mValues.get(x);
        }
    }
}
最后的结果:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjU5ODQwMTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
在做的过程中遇到的几个坑说下::
1.xAxis.setAxisMaximum(500f); 设置x轴的最大轴。最開始我是这样设置的。。然后加入BarEntry的时候。随便填写x轴的坐标。
可是最后发现有bug
妈的。 纠结了非常久。 然后看原作者的demo 。发现作者并没有调用xAxis.setAxisMaximum(500f)。。每一个直方图的坐标从1,開始往后数。记住一定要从1
開始。。 从0開始会遇到问题。
2.怎样给 每一个直方图 以下加入标签:主要用到的是IAxisValueFormatter 重写public String getFormattedValue(float value, AxisBase axis)通过这个value值 获取标签。。可是我debug发现 这个value并非严格的1,2,3,4,5 等等 这样依照顺序遍历的。所以这种方法 我不是非常理解。看作者的代码也看不懂。坑
Android MPAndroidCharts 框架 画可滑动查看的直方图的更多相关文章
- Android 仿美团网,探索使用ViewPager+GridView实现左右滑动查看更多分类的功能
		看下效果图,自己考虑下自己会如何实现,然后再继续看看作者的实现~ 不记得什么时候,我留意到到美团网首页有使用ViewPager+GridView实现左右滑动查看更多分类的一个功能,感觉它很有趣,于是想 ... 
- 2017年Android百大框架排行榜
		框架:提供一定能力的小段程序 >随意转载,标注作者"金诚"即可 >本文已授权微信公众号:鸿洋(hongyangAndroid)原创首发. >本文已经开源到Gith ... 
- android 优秀框架整理
		程序员界有个神奇的网站,那就是github,这个网站集合了一大批优秀的开源框架,极大地节省了开发者开发的时间,在这里我进行了一下整理,这样可以使我们在使用到时快速的查找到,希望对大家有所帮助! 1. ... 
- 2017年Android百大框架排行榜(转)
		一.榜单介绍 排行榜包括四大类: 单一框架:仅提供路由.网络层.UI层.通信层或其他单一功能的框架 混合开发框架:提供开发hybrid app.h5与webview结合能力.web app能力的框架 ... 
- android 开源框架推荐
		同事整理的 android 开源框架,个个都堪称经典.32 个赞! 1.volley 项目地址 https://github.com/smanikandan14/Volley-demo (1) JS ... 
- Android自动化框架介绍
		随着Android应用得越来越广,越来越多的公司推出了自己移动应用测试平台.例如,百度的MTC.东软易测云.Testin云测试平台…….由于自己所在项目组就是做终端测试工具的,故抽空了解了下几种常见的 ... 
- Android开源框架Afinal第一篇——揭开圣女的面纱
		Android开源框架Afinal第一篇——揭开圣女的面纱 分类: Android开源框架哪点事2013-09-02 14:25 260人阅读 评论(0) 收藏 举报 Afinal 这是Afinal在 ... 
- 六款值得推荐的Android开源框架简介
		技术不再多,知道一些常用的.不错的就够了.下面就是最近整理的“性价比”比较高的Android开源框架,应该是相对实用的. 1.volley 项目地址 https://github.com/smanik ... 
- Android自动化框架 模拟操作 模拟测试
		转自:http://bbs2.c114.net/home.php?mod=space&uid=1025779&do=blog&id=5322 几种常见的Android自动化测试 ... 
随机推荐
- Wannafly挑战赛5
			珂朵莉与宇宙 时间限制:C/C++ 2秒,其他语言4秒空间限制:C/C++ 65536K,其他语言131072K64bit IO Format: %lld 题目描述 星神是来自宇宙的 所以珂朵莉也是吧 ... 
- 服务器迁移至Linux操作系统
			我在这里试了ubuntu.Debian,centos.最终还是选择了centos 使用工具putty,远程桌面的话使用vnc viewer(看起来service文件更改只需要替换user,但是路径不对 ... 
- 【bzoj3526】[Poi2014]Card  线段树区间合并
			题目描述 有n张卡片在桌上一字排开,每张卡片上有两个数,第i张卡片上,正面的数为a[i],反面的数为b[i].现在,有m个熊孩子来破坏你的卡片了!第i个熊孩子会交换c[i]和d[i]两个位置上的卡片. ... 
- 刷题总结——湫湫系列故事——设计风景线(hdu4514 并差集判环+树的直径)
			题目: 随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,那就建的越长越好. 现在已经勘探 ... 
- Eclipse我常用的快捷键
			[阅读代码用] Ctrl + 左键 看“定义”,“方法体(接口的实现类)”,“返回类型”.(光标所在<类名>/<方法名>+F3,看定义) Ctrl + T 看类继承关系树Tre ... 
- [JLOI2011]飞行路线 (分层图,最短路)
			题目链接 Solution 建立 \(k+1\) 层图跑 \(Dijkstra\) 就好了. Code #include<bits/stdc++.h> #define ll long lo ... 
- Java防止SQL注入的途径介绍
			为了防止SQL注入,最简洁的办法是杜绝SQL拼接,SQL注入攻击能得逞是因为在原有SQL语句中加入了新的逻辑,如果使用PreparedStatement来代替Statement来执行SQL语句,其后只 ... 
- BZOJ 4077 Messenger
			Messenger [问题描述] alice和bob各自在两条折线上行进,一个邮递员要从alice那拿一个包裹,并以直线移动到bob处,alice和bob.邮递员的速度均为1单位/s,问邮递员最少要走 ... 
- Linux--内核Uevent事件机制 与 Input子系统【转】
			转自:http://blog.csdn.net/lxl584685501/article/details/46379453 [-] 一Uevent机制 Uevent在kernel中的位置 Uevent ... 
- Select函数实现
			int select(int nfds, fd_set *restrict readfds, fd_set *restrict writefds, fd_set *restrict errorfds, ... 
