• 最牛逼android上的图表库MpChart三 条形图

    • BarChart条形图介绍
    • BarChart条形图实例
    • BarChart效果

最牛逼android上的图表库MpChart(三) 条形图

最近工作中,用到了mpchart图表库,现在分享受下mpchart图表库的各个图表在实际工作应用场景:

  • 最牛逼android上的图表库MpChart(一) 介绍篇
  • 最牛逼android上的图表库 MpChart(二) 折线图
  • 最牛逼android上的图表库MpChart(三) 条形图
  • 最牛逼android上的图表库MpChart(四) 饼图
  • 最牛逼android上的图表库MpChart(五) 泡泡图

附上mpandroidchartlibrary-2-1-6.jar的下载链接:http://download.csdn.net/detail/hejjunlin/9561829

使用mpchart jar包:mpandroidchartlibrary-2-1-6.jar
如果是在studio下,进行如下引用:
repositories {
maven { url “https://jitpack.io” }
}

dependencies {
compile ‘com.github.PhilJay:MPAndroidChart:v2.1.6’
}

BarChart条形图介绍

  • BarChart类
  • 使用哪些API

BarChart条形图实例

  • 布局文件
  • Java代码
<?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.BarChart
        android:id="@+id/chart1"android:layout_width="match_parent"android:layout_height="match_parent" /></RelativeLayout>

package com.example.mpchart;import java.util.ArrayList;
import java.util.List;import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.WindowManager;import com.example.mpchart.data.ErrorCodePercentDataSource;
import com.example.mpchart.data.IDataSource;
import com.example.mpchart.data.IDataSource.onDataChangedListener;
import com.example.mpchart.utils.DBHelper;
import com.example.mpchart.utils.DateUtils;
import com.example.mpchart.utils.LogUtils;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.Legend.LegendDirection;
import com.github.mikephil.charting.components.Legend.LegendForm;
import com.github.mikephil.charting.components.Legend.LegendPosition;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.components.YAxis.AxisDependency;
import com.github.mikephil.charting.components.YAxis.YAxisLabelPosition;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.YAxisValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ColorTemplate;public class BarChartActivity extends Activity implements OnChartValueSelectedListener {private static final String TAG = "BarChartActivity";protected BarChart mChart;private IDataSource mDataSource = new ErrorCodePercentDataSource();private String mDateTime;private Typeface mTf;private Handler mHandler = new Handler(Looper.getMainLooper()) {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);getData();}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);setContentView(R.layout.activity_barchart);mChart = (BarChart) findViewById(R.id.chart1);mChart.setOnChartValueSelectedListener(this);mChart.setDescription(""/*mDataSource.getDescription()*/);mChart.setDescriptionTextSize(30);
//        mChart.setDescriptionPosition(960, 550);mChart.setDrawBarShadow(false);mChart.setDrawValueAboveBar(true);// if more than 60 entries are displayed in the chart, no values will be// drawnmChart.setMaxVisibleValueCount(60);// scaling can now only be done on x- and y-axis separatelymChart.setPinchZoom(false);mChart.setDrawGridBackground(false);// mChart.setDrawYLabels(false);mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");XAxis xAxis = mChart.getXAxis();xAxis.setPosition(XAxisPosition.BOTTOM);xAxis.setTypeface(mTf);xAxis.setDrawGridLines(false);xAxis.setSpaceBetweenLabels(2);YAxisValueFormatter custom = new MyYAxisValueFormatter();//设置Y轴上的显示单位YAxis leftAxis = mChart.getAxisLeft();leftAxis.setTypeface(mTf);leftAxis.setLabelCount(8, false);leftAxis.setValueFormatter(custom);leftAxis.setPosition(YAxisLabelPosition.OUTSIDE_CHART);leftAxis.setSpaceTop(15f);leftAxis.setAxisMinValue(0f); // this replaces setStartAtZero(true)YAxis rightAxis = mChart.getAxisRight();rightAxis.setDrawGridLines(false);rightAxis.setTypeface(mTf);rightAxis.setLabelCount(8, false);rightAxis.setValueFormatter(custom);rightAxis.setSpaceTop(15f);rightAxis.setAxisMinValue(0f); // this replaces setStartAtZero(true)Legend l = mChart.getLegend();l.setPosition(LegendPosition.BELOW_CHART_LEFT);l.setForm(LegendForm.SQUARE);l.setFormSize(9f);l.setTextSize(11f);l.setXEntrySpace(4f);getData();new Thread(mRunnable).start();// mChart.setDrawLegend(false);}private Runnable mRunnable = new Runnable() {@Overridepublic void run() {while(true) {try {Thread.sleep(15*1000);//15s刷新下数据mHandler.sendMessage(mHandler.obtainMessage());} catch (InterruptedException e) {e.printStackTrace();}}}};private onDataChangedListener listener = new onDataChangedListener() {@Overridepublic void onChanged(String[] xx, String[] yy) {notifyDataChanged(xx, yy);}};private void getData() {LogUtils.d(TAG, "getData() " + DateUtils.getCurrentDate()); new Thread(new Runnable() {@Overridepublic void run() {DBHelper.getInstance().init();String table = "error_info_" + DateUtils.get2HoursDate();String sql = "select *from " + table + " limit 20"/* + DateUtils.get2HoursDate()*/;boolean isexist = DBHelper.getInstance().isTableExist(table);if (isexist) {mDateTime = DateUtils.get2HoursDate();final String[] xx = DBHelper.getInstance().query(sql,3); final String[] yy = DBHelper.getInstance().query(sql,5);mHandler.post(new Runnable() {@Overridepublic void run() {listener.onChanged(xx, yy);}});} else {String table2 = "error_info_" + DateUtils.getOneHoursAgoTime();mDateTime = DateUtils.getOneHoursAgoTime();String sql2 = "select *from " + table2 + " limit 20";LogUtils.d(TAG, "getData() sql2 " + sql2); final String[] xx = DBHelper.getInstance().query(sql2,3); final String[] yy = DBHelper.getInstance().query(sql2,5);mHandler.post(new Runnable() {@Overridepublic void run() {listener.onChanged(xx, yy);}});}}}).start();}private void notifyDataChanged(String[] xx, String[] yy) {Typeface tf = Typeface.createFromAsset(getAssets(),"OpenSans-Regular.ttf");// 加载数据setData(xx,yy );//从X轴进入的动画mChart.animateX(2000);
//        mChart.animateY(2000);   //从Y轴进入的动画
//        mChart.animateXY(2000, 2000);    //从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(30);l.setTextColor(Color.rgb(244, 117, 117));l.setDirection(LegendDirection.LEFT_TO_RIGHT);l.setYOffset(660);l.setFormSize(20f); // set the size of the legend forms/shapes// 刷新图表mChart.invalidate();}private void setData(String[] xx, String[] yy) {ArrayList<String> xVals = new ArrayList<String>();for (int i = 0; i < xx.length; i++) {xVals.add(xx[i]);}ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();for (int i = 0; i < yy.length; i++) {float y = Float.parseFloat(yy[i]);yVals1.add(new BarEntry(y, i));//填充数据}BarDataSet set1;mChart.animateY(2000);//设置动画set1 = new BarDataSet(yVals1, "DataSet");set1.setBarSpacePercent(35f);set1.setColors(ColorTemplate.LIBERTY_COLORS);BarDataSet dataSets = new BarDataSet(yVals1, "错误码占比监控,数据来源: + mDateTime);List<Integer> list = new ArrayList<Integer>();list.add(Color.rgb(179, 48, 80));//设置颜色list.add(Color.rgb(106, 167, 134));list.add(Color.rgb(53, 194, 209));list.add(Color.rgb(118, 174, 175));list.add(Color.rgb(42, 109, 130));list.add(Color.rgb(106, 150, 31));list.add(Color.rgb(179, 100, 53));list.add(Color.rgb(193, 37, 82));list.add(Color.rgb(255, 102, 0));list.add(Color.rgb(217, 80, 138));list.add(Color.rgb(254, 149, 7));list.add(Color.rgb(254, 247, 120));dataSets.setColors(list);BarData data = new BarData(xVals, dataSets);data.setValueTextSize(10f);data.setValueTypeface(mTf);mChart.setData(data);}@SuppressLint("NewApi")@Overridepublic void onValueSelected(Entry e, int dataSetIndex, Highlight h) {if (e == null)return;RectF bounds = mChart.getBarBounds((BarEntry) e);PointF position = mChart.getPosition(e, AxisDependency.LEFT);Log.i("bounds", bounds.toString());Log.i("position", position.toString());Log.i("x-index","low: " + mChart.getLowestVisibleXIndex() + ", high: "+ mChart.getHighestVisibleXIndex());}public void onNothingSelected() {};
}

BarChart效果

最牛逼android上的图表库MpChart(三) 条形图相关推荐

  1. 最牛逼android上的图表库MpChart(一) 介绍篇

    最牛逼android上的图表库MpChart一 介绍篇 MpChart优点 MpChart是什么 MpChart支持哪些图表 MpChart效果如何 最牛逼android上的图表库MpChart(一) ...

  2. Android图表库MPAndroidChart(四)——条形图的绘制过程过程,隐隐约约我看到了套路...

    Android图表库MPAndroidChart(四)--条形图的绘制过程过程,隐隐约约我看到了套路 在学习本课程之前我建议先把我之前的博客看完,这样对整体的流程有一个大致的了解 Android图表库 ...

  3. Android图表库MPAndroidChart(四)——条形图的绘制过程过程,隐隐约约我看到了套路

    Android图表库MPAndroidChart(四)--条形图的绘制过程过程,隐隐约约我看到了套路 在学习本课程之前我建议先把我之前的博客看完,这样对整体的流程有一个大致的了解 Android图表库 ...

  4. 【Android】开源图表库MPAndroidChart的学习

    android开源图表库MPAndroidChart(中文翻译) MPAndroidChart简化版运行效果: 主要的Api方法: setDescription(String desc) : 设置表格 ...

  5. android开源动态图表库,hellocharts-android-Android图表开源库的使用(一)

    最近项目中需要用到图表,技术有限,自己实现起来有难度,于是对比之后,最终决定使用hellocharts这个开源库,传送门:https://github.com/lecho/hellocharts-an ...

  6. Android开源库介绍:AndLinker-Android 上的 IPC 库

    简介 最近一个老项目里看到了AndLinker,搜了一下: AndLinker是一款Android上的IPC (进程间通信) 库,结合了 AIDL 和 Retrofit 的诸多特性,且可以与 RxJa ...

  7. iOS Android 上传代码库+持续集成+单元测试

    背景 本文将介绍如何把iOS & Android项目分别上传到CocoaPods和Jitpack,并用Travis CI做持续集成,codecov做代码单元测试覆盖率的报告展示. 上传代码 创 ...

  8. Android Studio导入第三方库的三种方法

    今天在项目中使用一个图片选择器的第三方框架--GalleryFinal,想要导入源码,以便于修改,于是上完查找了一下方法,想到之前用到过其他导入第三方库的方法,现在做个小总结,以防忘记. Androi ...

  9. libusb android pc,libusb: android上集成libusb库

    1. 下载libusb库. 2. 添加libusb库到android studio项目中. 这里以源码编译的方式添加,使用的仍然是ndk-build的方式,而非cmake,使用源码编译的好处在于,可以 ...

  10. 【转】tars源码漫谈第1篇------tc_loki.h (牛逼哄哄的loki库)

    loki库是C++模板大牛Andrei写的, 里面大量运用模板的特性, 而tc_loki.h借用了loki库的部分代码, 形成了一个基本的文件tc_loki.h, 来看看: 1 #ifndef __T ...

最新文章

  1. PacBio But Not Illumina Technology Can Achieve Fast, Accurate and Complete Closure of the High GC, C
  2. CIFAR-10数据集可视化二进制版本
  3. 用香港服务器建收费网站,使用香港站群服务器搭建网站的好处有哪些?
  4. c 语言输出后不关闭_穿书+娱乐圈 |再不跑路就要被迫C位出道了花瓶女配和影帝组CP后豪门娇美人是爽文剧本...
  5. 视频: 安卓连接无线临时网络adhoc共享电脑上网无需adhoc补丁
  6. 使用displsy:flex + overflow:hidden时子元素被压缩
  7. db2 常用命令(二)
  8. KVM虚拟化下使用virsh shutdown命令无法关闭windows
  9. MySQL 常用的查询命令
  10. Java JDBC学习
  11. java 代码重构 pdf_《重构:改善既有代码的设计》 PDF 下载
  12. 水库大坝安全监测系统解决方案
  13. 全网最全测试工程师 学习网站汇总(测试必备 抓紧收藏)
  14. C#实现微信网页授权
  15. 域名被劫持应该如何处理
  16. 银行卡号码的校验规则(Luhn算法/模10算法)
  17. 视频教程-沐风老师3DMAX石墨烯建模视频教程-3Dmax
  18. leveldb代码阅读笔记(一)
  19. 数字图像处理第八章----图像压缩
  20. 微信小程序登录与跳转到首页

热门文章

  1. 数据集_汇总 | SLAM、重建、语义相关数据集大全
  2. 高等数学复习笔记(六)- 一元函数积分学的应用
  3. 超快语义分割 | PP-LiteSeg集速度快、精度高、易部署等优点于一身,必会模型!!!...
  4. windows jdk8
  5. 湖南附中模拟day1 瞭望塔
  6. codeforces 615B. Longtail Hedgehog
  7. ElasticSearch 2 (30) - 信息聚合系列之条形图
  8. 向设计师分享30个免费的扁平化风格设计素材
  9. 精通Hibernate类与类关联关系:[三]映射一对多双向自身关联关系
  10. MSRCRGIMP(基于GIMP版本的多尺度Retinex)