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

最近工作中,用到了mpchart图表库,现在分享受下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()) {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
getData();
}
}; @Override
protected 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
// drawn
mChart.setMaxVisibleValueCount(60); // scaling can now only be done on x- and y-axis separately
mChart.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() {
@Override
public void run() {
while(true) {
try {
Thread.sleep(15*1000);//15s刷新下数据
mHandler.sendMessage(mHandler.obtainMessage());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}; private onDataChangedListener listener = new onDataChangedListener() { @Override
public void onChanged(String[] xx, String[] yy) {
notifyDataChanged(xx, yy);
}
}; private void getData() {
LogUtils.d(TAG, "getData() " + DateUtils.getCurrentDate());
new Thread(new Runnable() {
@Override
public 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() {
@Override
public 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() {
@Override
public 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")
@Override
public 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效果 最牛逼android上的图表库MpChart(二) 折线图 最近工作中, ...

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

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

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

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

  4. Android图表库MPAndroidChart(三)——双重轴线形图的实现,这次就so easy了

    Android图表库MPAndroidChart(三)--双重轴线形图的实现,这次就so easy了 在学习本课程之前我建议先把我之前的博客看完,这样对整体的流程有一个大致的了解 Android图表库 ...

  5. android 开源图表库MPChart最简单使用方法示例教程Demo--折线图 柱状图

    转载请注明本文出处:http://blog.csdn.net/wingichoy/article/details/50428246 MPChart是android上一款强大的图表开源库,他可以轻松的绘 ...

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

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

  7. 牛逼哄哄的Qt库

    目录 一.有价值 - 好的网站 - 好的文章 二.Qt开源库-工具 - QtXlsx--excel读写库 三.Qt开源库-控件 - libqxt编译 - Qwt - QCustomPlot - 其他 ...

  8. libusb: android上集成libusb库

    1. 下载libusb库. 可以到libusb库的官网(https://libusb.info/)或者是其官方的github仓库(https://github.com/libusb/libusb/re ...

  9. Android上安装第三方库

    在Android sdk中安装预安装第三方的(动态,静态)库,到系统中,方便模块无差别的使用. Android.mk include $(CLEAR_VARS) LOCAL_MODULE_TAGS : ...

随机推荐

  1. caller和callee

    我们先来看下caller. caller:返回一个对函数(该函数调用了当前函数)的引用. functionName.caller:functionName对象是所执行函数的名称. 说明 对于函数来说, ...

  2. 【CISP笔记】安全漏洞与恶意代码(1)

    恶意代码 类型二进制代码.脚本语言.宏语言等表现形式病毒.蠕虫.后门程序.木马.流氓软件.逻辑炸弹等 恶意代码的传播方式(1)移动存储 自动播放功能.Windows默认.自动执行autorun.inf ...

  3. [译]git config

    git config git config命令用来设置git的一些配置(包括全局配置和针对单个仓储的配置).git config命令能定义一个仓储的用户信息和用户偏好. 用法 git config u ...

  4. Tomcat 6.0 简介

    本片翻译来自:http://tomcat.apache.org/tomcat-6.0-doc/introduction.html 介绍 无论是开发者还是tomcat管理员在使用前都需要了解一些必要的信 ...

  5. JavaWeb学习总结(五十三)——Web应用中使用JavaMail发送邮件

    现在很多的网站都提供有用户注册功能, 通常我们注册成功之后就会收到一封来自注册网站的邮件.邮件里面的内容可能包含了我们的注册的用户名和密码以及一个激活账户的超链接等信息.今天我们也来实现一个这样的功能 ...

  6. nyoj 613 免费馅饼 广搜

    免费馅饼 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy ...

  7. Ubuntu 14 编译安装 XDebug - 2.3.3 For PHP - 5.4.45

    安装过程如下: 1.下载XDebug源码:http://xdebug.org/files/xdebug-2.3.3.tgz 2.解压到某个目录,如 /opt/software/xdebug-2.3.3 ...

  8. springmvc之DispatcherServlet

    1.作用:DispatcherServlet是前置控制器,配置在web.xml(因为DispatcherServlet是一个servelet)文件中的.拦截匹配的请求,Servlet拦截匹配规则要自已 ...

  9. JavaScript与DOM

    文档对象模型Document Object Model DOM(Document Object Model,文档对象模型)是一个通过和JavaScript进行内容交互的API.Javascript和D ...

  10. fullpage.js小技巧

    创造一个自适应的section: 在 section 类旁边加上类 fp-auto-height 例如:<div class="section fp-auto-height" ...