最牛逼android上的图表库MpChart(二) 折线图

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

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

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

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

MpChart折线图介绍

  • LineChart类
  • 使用哪些API

    • setBackgroundColor(int color): Sets the background color that will cover the whole chart-view. In addition, a background-color can be set via .xml in the layout file.
    • setDescription(String desc): Set a description text that appears in the bottom right corner of the chart.
    • setDescriptionColor(int color): Sets the color of the description text.
    • setDescriptionPosition(float x, float y): Sets a custom position for the description text in pixels on the screen.
    • setDescriptionTypeface(Typeface t): Sets the Typeface used for drawing the description text.
    • setDescriptionTextSize(float size): Sets the size of the description text in pixels, min 6f, max 16f.
    • setNoDataTextDescription(String desc): Sets the text that should appear if the chart is empty.
    • setDrawGridBackground(boolean enabled): If enabled, the background rectangle behind the chart drawing-area will be drawn.
    • setGridBackgroundColor(int color): Sets the color the grid-background should be drawn with.
    • setDrawBorders(boolean enabled): Enables / disables drawing the chart borders (lines surrounding the chart).
    • setBorderColor(int color): Sets the color of the chart border lines.
    • setBorderWidth(float width): Sets the width of the chart border lines in dp.
    • setMaxVisibleValueCount(int count): Sets the number of maximum visible drawn value-labels - on the chart. This only takes affect when setDrawValues() is enabled.

MpChart折线图实例

  • 布局文件
  • Java代码
 <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" > <com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart1"
android:layout_width="match_parent"
android:layout_height="match_parent"/> </RelativeLayout>
 package com.example.mpchart;

 import java.util.ArrayList;

 import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.WindowManager; import com.example.mpchart.data.IDataSource;
import com.example.mpchart.data.IDataSource.onDataChangedListener;
import com.example.mpchart.data.SucRateDataSource;
import com.example.mpchart.utils.DBHelper;
import com.example.mpchart.utils.DateUtils;
import com.example.mpchart.utils.LogUtils;
import com.github.mikephil.charting.charts.LineChart;
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.LimitLine;
import com.github.mikephil.charting.components.LimitLine.LimitLabelPosition;
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.YAxisLabelPosition;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet; public class MainActivity extends Activity { private static final String TAG = "MainActivity"; private LineChart mChart;
private IDataSource mDataSource = new SucRateDataSource(); 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_main); mChart = (LineChart) findViewById(R.id.chart1); // //在chart上的右下角加描述
mChart.setDescription(mDataSource.getDescription());
mChart.setDescriptionTextSize(30);
// //设置Y轴上的单位
// mChart.setUnit("%");
//设置透明度
// mChart.setAlpha(0.8f);
//设置网格底下的那条线的颜色
// mChart.setBorderColor(Color.rgb(213, 216, 214));
// mChart.setBorderColor(Color.rgb(0, 0, 0));
// mChart.setBackgroundColor(Color.rgb(255, 255, 255));
mChart.setGridBackgroundColor(Color.rgb(255, 255, 255)); //设置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());
// mv.setMinimumHeight(80);
// // 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.setDescriptionTypeface(tf); LimitLine ll1 = new LimitLine(95f, "警戒值 95%");
ll1.setLineWidth(2f);
// ll1.setLineColor(Color.rgb(0,0,0));
// ll1.enableDashedLine(10f, 10f, 0f);
ll1.setLabelPosition(LimitLabelPosition.LEFT_TOP);
ll1.setTextSize(15f);
ll1.setTypeface(tf); XAxis xl = mChart.getXAxis();
// xl.setAvoidFirstLastClipping(true);
// xl.setAdjustXLabels(true);
xl.setPosition(XAxisPosition.BOTTOM); // 设置X轴的数据在底部显示
xl.setTypeface(tf); // 设置字体
xl.setTextSize(10f); // 设置字体大小
xl.setSpaceBetweenLabels(0); // 设置数据之间的间距' YAxis yl = mChart.getAxisLeft();
yl.setPosition(YAxisLabelPosition.OUTSIDE_CHART);
// yl.setAxisMaxValue(220f);
yl.addLimitLine(ll1);
yl.setTypeface(tf); // 设置字体
yl.setTextSize(10f); // s设置字体大小
yl.setTypeface(tf);
yl.setAxisMinValue(90f);
yl.setStartAtZero(false);
// yl.setLabelCount(5); // 设置Y轴最多显示的数据个数 YAxis y2 = mChart.getAxisRight();
y2.setPosition(YAxisLabelPosition.OUTSIDE_CHART);
y2.setTypeface(tf); // 设置字体
y2.setTextSize(10f); // s设置字体大小
y2.setTypeface(tf);
y2.setAxisMinValue(90f);
y2.setStartAtZero(false);
getData();
new Thread(mRunnable).start();
} 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 sql = "select *from suc_rate_chart_0614";
final String[] xx = DBHelper.getInstance().query(sql,2);
final String[] yy = DBHelper.getInstance().query(sql,3);
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.CIRCLE); //设置图最下面显示的类型
l.setTypeface(tf);
l.setTextSize(30);
l.setTextColor(Color.rgb(244, 117, 117));
l.setDirection(LegendDirection.LEFT_TO_RIGHT);
l.setYOffset(100);
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<Entry> yVals = new ArrayList<Entry>(); for (int i = 0; i < yy.length; i++) {
yVals.add(new Entry(Float.parseFloat(yy[i]), i));
LogUtils.d(TAG, "yVals() " + Float.parseFloat(yy[i]));
} // create a dataset and give it a type
LineDataSet set1 = new LineDataSet(yVals, "成功率监控"); set1.setDrawCubic(false); //设置曲线为圆滑的线
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(244, 117, 117)); //设置曲线的颜色 // create a data object with the datasets
LineData data = new LineData(xVals, set1); // set data
mChart.setData(data);
}
}

MpChart效果

最牛逼android上的图表库MpChart(二) 折线图的更多相关文章

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

    最牛逼android上的图表库MpChart三 条形图 BarChart条形图介绍 BarChart条形图实例 BarChart效果 最牛逼android上的图表库MpChart(三) 条形图 最近工 ...

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

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

  3. Android图表库MPAndroidChart(二)——线形图的方方面面,看完你会回来感谢我的

    Android图表库MPAndroidChart(二)--线形图的方方面面,看完你会回来感谢我的 在学习本课程之前我建议先把我之前的博客看完,这样对整体的流程有一个大致的了解 Android图表库MP ...

  4. Android绘图机制(四)——使用HelloCharts开源框架搭建一系列炫酷图表,柱形图,折线图,饼状图和动画特效,抽丝剥茧带你认识图表之美

    Android绘图机制(四)--使用HelloCharts开源框架搭建一系列炫酷图表,柱形图,折线图,饼状图和动画特效,抽丝剥茧带你认识图表之美 这里为什么不继续把自定义View写下去呢,因为最近项目 ...

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

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

  6. Android图表引擎AChartEngine之折线图使用

    最近在帮老师做一个课题,其中app端需要显示折线图以便直观地看数据波动,上网查了些资料后发现了这款图标引擎,另外感谢李坤老师的博客,帮助很大. 废话不多说,下面写代码. 一.AChartEngine是 ...

  7. DevExpress使用之ChartControl控件绘制图表(多坐标折线图、柱状图、饼状图)

    最近因为公司项目需要用到WinForm的DecExpress控件,在这里把一些使用方法总结一下. DevExpress中有一个专门用来绘制图表的插件ChartControl,可以绘制折线图.饼状图.柱 ...

  8. jqPlot图表插件学习之折线图-散点图-series属性

    一.准备工作 首先我们需要到官网下载所需的文件: 官网下载(笔者选择的是jquery.jqplot.1.0.8r1250.zip这个版本) 然后读者需要根据自己的情况新建一个项目并且按照如下的方式加载 ...

  9. excel在一个图表内,显示折线图和柱状图

      折线图和柱状图,在同一个图表中拆分显示   一个图,设置主坐标轴 另外一个图,设置次坐标轴     拆分,通过调整纵坐标的最小值和最大值来实现     关于图表的标题,选中图表,选择布局,然后图表 ...

随机推荐

  1. sql group by 理解

    order by是对字段进行排序,group by 是对字段进行分类,在select 语句中可以使用group by 子句将行划分成较小的组,然后,使用组函数返回每一个组的汇总信息,另外,可以使用ha ...

  2. js 区分数据类型

    这是第二版,可以区分 1.基本数据类型(string.number.boolean) undefined.null 2.引用类型 数组.RegExp.函数. 自定义数据类型(通过new 函数名得到) ...

  3. Android快速开发系列 10个常用工具类

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38965311,本文出自[张鸿洋的博客] 打开大家手上的项目,基本都会有一大批的辅 ...

  4. leetcode 153. Find Minimum in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  5. php配置中的register_globals用法

    开发的时候设置成register_globals=off,只能通过post或get得到前端数据. 参考资料:http://blog.csdn.net/alex_best/article/details ...

  6. python os.path.dirname 是什么目录

    这个获取文件路径中所在的目录. 1 2 3 4 5 6 7 In [1]: import os   In [2]: os.__file__ Out[2]: '/usr/lib/python2.7/os ...

  7. OpenCV成长之路(10):视频的处理

    视频中包含的信息量要远远大于图片,对视频的处理分析也越来越成为计算机视觉的主流,而本质上视频是由一帧帧的图像组成,所以视频处理最终还是要归结于图像处理,但在视频处理中,有更多的时间维的信息可以利用.本 ...

  8. 字符串驱动技术—— MethodAddress , MethodName , ObjectInvoke

    首先看一段Delphi帮助中的介绍(After Delphi 6 ): Returns the address of a published method. class function Method ...

  9. mac 下搭建php 编程环境全过程

    1,打开终端, 设置root密码sudo passwd root输入密码 2, 安装 apachemac 自带apache 启动apachectl start重新启动apachectl restart ...

  10. Idea反向生成JavaBean

    创建数据库