MPAndroidChart的具体属性方法(二)_mpandroidchart postscale-程序员宅基地

技术标签: Android 进阶学习  android  

......好懒......就不改了
package com.ashzheng.mpandroidchart;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.PointF;
import android.os.Bundle;
import android.view.View;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;

import java.util.ArrayList;

public class MainActivity extends Activity {

    private LineChart mLineChart;
    private XAxis xAxis;         //X坐标轴
    private YAxis yAxis;         //Y坐标轴

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mLineChart = (LineChart) findViewById(R.id.chart);

        xAxis = mLineChart.getXAxis();
        yAxis = mLineChart.getAxisLeft();

        LineData mLineData = getLineData();


        modifyingTheViewport();
        animations();
        markerView();
        miscellaneous();

        //showChart(mLineChart, mLineData);
    }

    private void miscellaneous() {
        mLineChart.getData();   //返回这个图表中的data类型的值,
        mLineChart.getViewPortHandler();    //返回这个图表中的各种状态,返回类型为ViewPortHandler
        mLineChart.getRenderer();   //返回绘制图表的数据,类型是DataRenderer

        PointF pointF = new PointF();
        pointF = mLineChart.getCenter();   //返回中心点,返回类型是PointF,即(x,y)

        mLineChart.getCenterOffsets();  //返回图表绘制区域的中心点
        mLineChart.getYMin();  //返回 数据Y的最小值 返回值是float类型
        mLineChart.getYMax();  //返回 数据Y的最大值 返回值是float类型

        System.out.println(mLineChart.getHighestVisibleXIndex());
        mLineChart.getLowestVisibleXIndex();  //返回 能见到的X的最小值下标 返回值是int类型
        mLineChart.getHighestVisibleXIndex();  //返回 能见到的X的最大值下标 返回值是int类型

        mLineChart.getChartBitmap();  //返回 最后状态的图表的Bitmap
        //。。。。。。
    }

    private void markerView() {

    }

    private void animations() {
        //动画效果在API版本11(Android 3.0.X)以上才能起作用,低版本不会执行,但不会崩溃
        //执行动画并不会刷新表格 即执行invalidate();方法

        mLineChart.animateX(1000); //从左到右绘制图形,参数是int类型的 持续时间
        mLineChart.animateY(3000); //从下到上绘制图形,参数是int类型的 持续时间
        mLineChart.animateXY(3000, 2000); //x,y同时绘制,参数是(int xDuration, int yDuration)

    }

    private void modifyingTheViewport() {
        mLineChart.setVisibleXRangeMaximum(5);  //一个界面显示多少个点,其他点可以通过滑动看到
        mLineChart.setVisibleXRangeMinimum(3);  //一个界面最少显示多少个点,放大后最多 放大到 剩多少 个点
//      mLineChart.setViewPortOffsets(0,0,0, 0);  //设置图表显示的偏移量,不建议使用,因为会影响图表的自动计算
        mLineChart.resetViewPortOffsets();  //重新设置图表显示的偏移量,自动计算,可取消上面的那个操作
//        mLineChart.setExtraOffsets(20, 30, 50, 10);  //设置图表外,布局内显示的偏移量

        mLineChart.fitScreen();  //重置图表的缩放,拖动并使图表符合它的界限
        mLineChart.moveViewToX(2);  //将左边的边放到指定的位置,参数是(float xindex)

        mLineChart.zoomIn();  //默认视图放大1.4倍,
        mLineChart.zoomOut();  //默认视图缩小到0.7倍,
        mLineChart.zoom(1.0f,1.0f,0f,0f);  //自定义缩放(float scaleX, float scaleY, float x, float y)X缩放倍数,Y缩放倍数,x坐标,y坐标。1f是原大小
    }

    private void showChart(LineChart lineChart, LineData lineData) {

        //General Chart Styling 通用的图表造型,还有些对于特定图表有这特定方法的造型。
        //请参考https://github.com/PhilJay/MPAndroidChart/wiki/Specific-chart-settings
        lineChart.setBackgroundColor(Color.argb(0, 173, 215, 210));// 设置图表背景 参数是个Color对象

        lineChart.setDescription("setDescription我在这儿"); //图表默认右下方的描述,参数是String对象
        lineChart.setDescriptionColor(Color.rgb(227, 135, 0));  //上面字的颜色,参数是Color对象
//      lineChart.setDescriptionPosition(400f,600f);    //上面字的位置,参数是float类型,像素,从图表左上角开始计算
//      lineChart.setDescriptionTypeface();     //上面字的字体,参数是Typeface 对象
        lineChart.setDescriptionTextSize(16);    //上面字的大小,float类型[6,16]

        lineChart.setNoDataTextDescription("没有数据呢(⊙o⊙)");   //没有数据时显示在中央的字符串,参数是String对象

        lineChart.setDrawGridBackground(false);//设置图表内格子背景是否显示,默认是false
        lineChart.setGridBackgroundColor(Color.rgb(255, 0, 0));//设置格子背景色,参数是Color类型对象

        lineChart.setDrawBorders(true);     //设置图表内格子外的边框是否显示
        lineChart.setBorderColor(Color.rgb(236, 228, 126));   //上面的边框颜色
        lineChart.setBorderWidth(20);       //上面边框的宽度,float类型,dp单位
//      lineChart.setMaxVisibleValueCount();设置图表能显示的最大值,仅当setDrawValues()属性值为true时有用


        //Interaction with the Chart 图表的交互

        //Enabling / disabling interaction
        lineChart.setTouchEnabled(true); // 设置是否可以触摸
        lineChart.setDragEnabled(true);// 是否可以拖拽

        lineChart.setScaleEnabled(true);// 是否可以缩放 x和y轴, 默认是true
        lineChart.setScaleXEnabled(true); //是否可以缩放 仅x轴
        lineChart.setScaleYEnabled(true); //是否可以缩放 仅y轴

        lineChart.setPinchZoom(true);  //设置x轴和y轴能否同时缩放。默认是否
        lineChart.setDoubleTapToZoomEnabled(true);//设置是否可以通过双击屏幕放大图表。默认是true

        lineChart.setHighlightEnabled(false);  //If set to true, highlighting/selecting values via touch is possible for all underlying DataSets.
        lineChart.setHighlightPerDragEnabled(true);//能否拖拽高亮线(数据点与坐标的提示线),默认是true

        lineChart.setAutoScaleMinMaxEnabled(false);


        // Chart fling / deceleration
        lineChart.setDragDecelerationEnabled(true);//拖拽滚动时,手放开是否会持续滚动,默认是true(false是拖到哪是哪,true拖拽之后还会有缓冲)
        lineChart.setDragDecelerationFrictionCoef(0.99f);//与上面那个属性配合,持续滚动时的速度快慢,[0,1) 0代表立即停止。


        //Highlighting programmatically

//        highlightValues(Highlight[] highs)
//               Highlights the values at the given indices in the given DataSets. Provide null or an empty array to undo all highlighting.
//        highlightValue(int xIndex, int dataSetIndex)
//               Highlights the value at the given x-index in the given DataSet. Provide -1 as the x-index or dataSetIndex to undo all highlighting.
//        getHighlighted()
//               Returns an Highlight[] array that contains information about all highlighted entries, their x-index and dataset-index.


        //其他请参考https://github.com/PhilJay/MPAndroidChart/wiki/Interaction-with-the-Chart
        //如手势相关方法,选择回调方法


//        The Axis 坐标轴相关的,XY轴通用
        xAxis.setEnabled(true);     //是否显示X坐标轴 及 对应的刻度竖线,默认是true
        xAxis.setDrawAxisLine(true); //是否绘制坐标轴的线,即含有坐标的那条线,默认是true
        xAxis.setDrawGridLines(true); //是否显示X坐标轴上的刻度竖线,默认是true
        xAxis.setDrawLabels(true); //是否显示X坐标轴上的刻度,默认是true

        xAxis.setTextColor(Color.rgb(145, 13, 64)); //X轴上的刻度的颜色
        xAxis.setTextSize(5); //X轴上的刻度的字的大小 单位dp
//      xAxis.setTypeface(Typeface tf); //X轴上的刻度的字体
        xAxis.setGridColor(Color.rgb(145, 13, 64)); //X轴上的刻度竖线的颜色
        xAxis.setGridLineWidth(1); //X轴上的刻度竖线的宽 float类型
        xAxis.enableGridDashedLine(40, 3, 0); //虚线表示X轴上的刻度竖线(float lineLength, float spaceLength, float phase)三个参数,1.线长,2.虚线间距,3.虚线开始坐标


        //可以设置一条警戒线,如下:
        LimitLine ll = new LimitLine(40f, "警戒线");
        ll.setLineColor(Color.RED);
        ll.setLineWidth(4f);
        ll.setTextColor(Color.GRAY);
        ll.setTextSize(12f);
        // .. and more styling options
        xAxis.addLimitLine(ll);


//      X轴专用
        xAxis.setLabelsToSkip(1);    //设置坐标相隔多少,参数是int类型
        xAxis.resetLabelsToSkip();   //将自动计算坐标相隔多少
        xAxis.setAvoidFirstLastClipping(true);
        xAxis.setSpaceBetweenLabels(4);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM_INSIDE);//把坐标轴放在上下 参数有:TOP, BOTTOM, BOTH_SIDED, TOP_INSIDE or BOTTOM_INSIDE.

//      Y轴专用
        yAxis.setStartAtZero(true);    //设置Y轴坐标是否从0开始
        yAxis.setAxisMaxValue(50);    //设置Y轴坐标最大为多少
        yAxis.resetAxisMaxValue();    //重新设置Y轴坐标最大为多少,自动调整
        yAxis.setAxisMinValue(10);    //设置Y轴坐标最小为多少
        yAxis.resetAxisMinValue();    //重新设置Y轴坐标,自动调整
        yAxis.setInverted(false);    //Y轴坐标反转,默认是false,即下小上大
        yAxis.setSpaceTop(0);    //Y轴坐标距顶有多少距离,即留白
        yAxis.setSpaceBottom(0);    //Y轴坐标距底有多少距离,即留白
        yAxis.setShowOnlyMinMax(false);    //参数如果为true Y轴坐标只显示最大值和最小值
        yAxis.setLabelCount(10, false);    //第一个参数是Y轴坐标的个数,第二个参数是 是否不均匀分布,true是不均匀分布
        yAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);  //参数是INSIDE_CHART(Y轴坐标在内部) 或 OUTSIDE_CHART(在外部(默认是这个))
//      yAxis.setValueFormatter(YAxisValueFormatterf);
//              Sets a custom ValueFormatter for this axis. This interface allows to format/modify
//              the original label text and instead return a customized text.

        //lineChart.setData(lineData); //添加数据

        Legend mLegend = lineChart.getLegend(); // 设置坐标线描述?? 的样式
        mLegend.setEnabled(true);
        mLegend.setTextColor(Color.rgb(255, 255, 255));// 坐标线描述文字的颜色
        mLegend.setTextSize(15);// 坐标线描述文字的大小,单位dp
//      mLegend.setTypeface(mTf);// 坐标线描述文字的字体

        mLegend.setWordWrapEnabled(true); //坐标线描述 是否 不允许出边界
        mLegend.setMaxSizePercent(0.95f);   //坐标线描述 占据的大小x%  默认0.95 即95%

        mLegend.setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);//位置 RIGHT_OF_CHART, RIGHT_OF_CHART_CENTER,
        // RIGHT_OF_CHART_INSIDE, BELOW_CHART_LEFT, BELOW_CHART_RIGHT, BELOW_CHART_CENTER or PIECHART_CENTER (PieChart only), ... and more
        mLegend.setForm(Legend.LegendForm.CIRCLE);// 样式SQUARE, CIRCLE or LINE.
        mLegend.setFormSize(10f);// 大小 单位dp

        mLegend.setXEntrySpace(20f);// 条目的水平间距
        mLegend.setYEntrySpace(20f);// 条目的垂直间距
        mLegend.setFormToTextSpace(20f);//Sets the space between the legend-label and the corresponding legend-form.
    }
    public void clickMe(View view){     //添加坐标线

        LineData data = mLineChart.getData();

        if(data != null) {

            int count = (data.getDataSetCount() + 1);

            // create 10 y-vals
            ArrayList<Entry> yVals = new ArrayList<Entry>();

            if(data.getXValCount() == 0) {
                // add 10 x-entries
                for (int i = 0; i < 10; i++) {
                    data.addXValue("" + (i+1));
                }
            }

            for (int i = 0; i < data.getXValCount(); i++) {
                yVals.add(new Entry((float) (Math.random() * 50f) + 50f * count, i));
            }

            LineDataSet set = new LineDataSet(yVals, "DataSet " + count);
            set.setLineWidth(2.5f);
            set.setCircleSize(4.5f);

            int color = Color.rgb(255,0,0);

            set.setColor(color);
            set.setCircleColor(color);      //设置结点圆圈的颜色
            set.setHighLightColor(Color.rgb(0,255,0));  //设置 坐标提示线的颜色
            set.setValueTextSize(30f);      //设置坐标提示文字的大小
            set.setValueTextColor(Color.rgb(0,0,255));  //设置坐标提示文字的颜色

            data.addDataSet(set);
            mLineChart.notifyDataSetChanged();
            mLineChart.invalidate();
        }
    }
    public void clickMe2(View view) {   //删除坐标线

        LineData data = mLineChart.getData();

        if(data != null) {

            data.removeDataSet(data.getDataSetByIndex(data.getDataSetCount() - 1));

            mLineChart.notifyDataSetChanged();
            mLineChart.invalidate();
        }
    }


    private LineData getLineData() {

        ArrayList<Entry> valsComp1 = new ArrayList<Entry>();     //坐标点的集合
        ArrayList<Entry> valsComp2 = new ArrayList<Entry>();

        Entry c1e1 = new Entry(100.000f, 1); //坐标点的值,Entry(Y坐标,X坐标);
        valsComp1.add(c1e1);
        Entry c1e2 = new Entry(50.000f, 2);
        valsComp1.add(c1e2);
        Entry c1e3 = new Entry(60.000f, 3);
        valsComp1.add(c1e3);
        Entry c1e4 = new Entry(70.000f, 4);
        valsComp1.add(c1e4);

        Entry c2e1 = new Entry(30.000f, 1); //坐标点的值,Entry(Y坐标,X坐标);
        valsComp2.add(c2e1);
        Entry c2e2 = new Entry(80.000f, 2);
        valsComp2.add(c2e2);
        Entry c2e3 = new Entry(30.000f, 3); //坐标点的值,Entry(Y坐标,X坐标);
        valsComp2.add(c2e3);
        Entry c2e4 = new Entry(90.000f, 4);
        valsComp2.add(c2e4);

        LineDataSet setComp1 = new LineDataSet(valsComp1, "Company");    //坐标线,LineDataSet(坐标点的集合, 线的描述或名称);
        LineDataSet setComp2 = new LineDataSet(valsComp2, "Company");
        setComp1.setAxisDependency(YAxis.AxisDependency.LEFT);     //以左边坐标轴为准 还是以右边坐标轴为基准
        setComp2.setAxisDependency(YAxis.AxisDependency.LEFT);

        setComp1.setColors(new int[]{R.color.red1, R.color.red2, R.color.red3, R.color.red4}, this);
        setComp2.setColors(new int[]{R.color.green1, R.color.green2, R.color.green3, R.color.green4}, this);
        ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>(); //坐标线的集合。
        dataSets.add(setComp1);
        dataSets.add(setComp2);

        ArrayList<String> xVals = new ArrayList<String>();      //X坐标轴的值的集合
        xVals.add("1.Q"); xVals.add("2.Q"); xVals.add("3.Q"); xVals.add("4.Q");
        xVals.add("1.Q"); xVals.add("2.Q"); xVals.add("3.Q"); xVals.add("4.Q");

        LineData data = new LineData(xVals, dataSets);  //LineData(X坐标轴的集合, 坐标线的集合);
        mLineChart.setData(data);   //为图表添加 数据
        mLineChart.invalidate(); // 重新更新显示

        return data;
    }

}

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Ash_Zheng/article/details/48731889

智能推荐

如何配置filezilla服务端和客户端_filezilla server for windows (32bit x86)-程序员宅基地

文章浏览阅读7.8k次,点赞3次,收藏9次。如何配置filezilla服务端和客户端百度‘filezilla server’下载最新版。注意点:下载的版本如果是32位的适用xp和win2003,百度首页的是适用于win7或更高的win系统。32和64内容无异。安装过程也是一样的。一、这里的filezilla包括服务端和客户端。我们先来用filezilla server 架设ftp服务端。看步骤。1选择标准版的就可以了。 _filezilla server for windows (32bit x86)

深度学习图像处理01:图像的本质-程序员宅基地

文章浏览阅读724次,点赞18次,收藏8次。深度学习作为一种强大的机器学习技术,已经成为图像处理领域的核心技术之一。通过模拟人脑处理信息的方式,深度学习能够从图像数据中学习到复杂的模式和特征,从而实现从简单的图像分类到复杂的场景理解等多种功能。要充分发挥深度学习在图像处理中的潜力,我们首先需要理解图像的本质。本文旨在深入探讨深度学习图像处理的基础概念,为初学者铺平通往高级理解的道路。我们将从最基础的问题开始:图像是什么?我们如何通过计算机来理解和处理图像?

数据探索阶段——对样本数据集的结构和规律进行分析_数据分析 规律集-程序员宅基地

文章浏览阅读62次。在收集到初步的样本数据之后,接下来该考虑的问题有:(1)样本数据集的数量和质量是否满足模型构建的要求。(2)是否出现从未设想过的数据状态。(3)是否有明显的规律和趋势。(4)各因素之间有什么样的关联性。解决方案:检验数据集的数据质量、绘制图表、计算某些特征量等,对样本数据集的结构和规律进行分析。从数据质量分析和数据特征分析两个角度出发。_数据分析 规律集

上传计算机桌面文件图标不见,关于桌面上图标都不见了这类问题的解决方法-程序员宅基地

文章浏览阅读8.9k次。关于桌面上图标都不见了这类问题的解决方法1、在桌面空白处右击鼠标-->排列图标-->勾选显示桌面图标。2、如果问题还没解决,那么打开任务管理器(同时按“Ctrl+Alt+Del”即可打开),点击“文件”→“新建任务”,在打开的“创建新任务”对话框中输入“explorer”,单击“确定”按钮后,稍等一下就可以见到桌面图标了。3、问题还没解决,按Windows键+R(或者点开始-->..._上传文件时候怎么找不到桌面图标

LINUX 虚拟网卡tun例子——修改_怎么设置tun的接收缓冲-程序员宅基地

文章浏览阅读1.5k次。参考:http://blog.csdn.net/zahuopuboss/article/details/9259283 #include #include #include #include #include #include #include #include #include #include #include #include _怎么设置tun的接收缓冲

UITextView 评论输入框 高度自适应-程序员宅基地

文章浏览阅读741次。创建一个inputView继承于UIView- (instancetype)initWithFrame:(CGRect)frame{ self = [superinitWithFrame:frame]; if (self) { self.backgroundColor = [UIColorcolorWithRed:0.13gre

随便推点

字符串基础面试题_java字符串相关面试题-程序员宅基地

文章浏览阅读594次。字符串面试题(2022)_java字符串相关面试题

VSCODE 实现远程GUI,显示plt.plot, 设置x11端口转发_vscode远程ssh连接服务器 python 显示plt-程序员宅基地

文章浏览阅读1.4w次,点赞12次,收藏21次。VSCODE 实现远程GUI,显示plt.plot, 设置x11端口转发问题服务器 linux ubuntu16.04本地 windows 10很多小伙伴发现VSCode不能显示figure,只有用自带的jupyter才能勉强个截图、或者转战远程桌面,这对数据分析极为不方便。在命令行键入xeyes(一个显示图像的命令)会failed,而桌面下会出现:但是Xshell能实现X11转发图像,有交互功能,但只能用Xshell输入命令plot,实在不方便。其实VScode有X11转发插件!!方法_vscode远程ssh连接服务器 python 显示plt

element-ui switch开关打开和关闭时的文字设置样式-程序员宅基地

文章浏览阅读3.3k次,点赞2次,收藏2次。element switch开关文字显示element中switch开关把on-text 和 off-text 属性改为 active-text 和 inactive-text 属性.怎么把文字描述显示在开关上?下面就是实现方法: 1 <el-table-column label="状态"> 2 <template slot-scope="scope">..._el-switch 不同状态显示不同字

HttpRequestUtil方法get、post、JsonToPost_httprequestutil.httpget-程序员宅基地

文章浏览阅读785次。java后台发起请求使用的工具类package com.cennavi.utils;import org.apache.http.Header;import org.apache.http.HttpResponse;import org.apache.http.HttpStatus;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpPost;import org.apach_httprequestutil.httpget

App-V轻量级应用程序虚拟化之三客户端测试-程序员宅基地

文章浏览阅读137次。在前两节我们部署了App-V Server并且序列化了相应的软件,现在可谓是万事俱备,只欠东风。在这篇博客里面主要介绍一下如何部署客户端并实现应用程序的虚拟化。在这里先简要的说一下应用虚拟化的工作原理吧!App-V Streaming 就是利用templateServer序列化出一个软件运行的虚拟环境,然后上传到app-v Server上,最后客户..._app-v 客户端