安卓开发系列 之实现绘制折线图的方法

news/2024/7/20 21:54:28 标签: android, 安卓, 移动开发, java, ios

安卓开发中实现折线图的方法有MPAndroidChart、XCL-chart、achartenginee和hellochart等,这里利用hellochart进行实现,这种实现方法简单快捷,界面美观,在使用后感觉代码比较清晰,操作时比较流畅,支持饼状图、折线图、柱状图等。

需要的依赖包为:
hellocharts-library-1.5.8.jar,官方下载链接为:
https://github.com/lecho/hellocharts-android/tree/v1.5.8
其中的使用和项目代码也写的比较清晰。

第一步是将依赖包导入工程中,然后在build.gradle(app)中添加依赖,如:

implementation files('libs/hellocharts-library-1.5.8.jar')

这里的路径根据实际的依赖包路径修改,一般是放在libs文件夹中,另外注意的是在app中的build.gradle中添加依赖。

第二步是在*Activity.java文件中添加代码,主要代码有:

java">import lecho.lib.hellocharts.gesture.ContainerScrollType;
import lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.ValueShape;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.LineChartView;

public class MainActivity extends AppCompatActivity {

    public LineChartView lineChart;
    String[] date = {};//X轴的标注
    int[] score= {};//图表的数据点
    public List<PointValue> mPointValues = new ArrayList<PointValue>();
    public List<AxisValue> mAxisXValues = new ArrayList<AxisValue>();


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

        lineChart = (LineChartView) findViewById(R.id.line_chart);

        getAxisXLables();//获取x轴的标注
        getAxisPoints();//获取坐标点
        initLineChart();//图表初始化

    }

    /**
     * 设置X 轴的显示
     */
    private void getAxisXLables() {
        for (int i = 0; i < date.length; i++) {
            mAxisXValues.add(new AxisValue(i).setLabel(date[i]));
        }
    }
    /**
     * 图表的每个点的显示
     */
    private void getAxisPoints() {
        for (int i = 0; i < score.length; i++) {
            mPointValues.add(new PointValue(i, score[i]));
        }
    }

    private void initLineChart() {
        Line line = new Line(mPointValues).setColor(Color.parseColor("#1E90FF"));  //折线的颜色(蓝色)
        List<Line> lines = new ArrayList<Line>();
        line.setShape(ValueShape.CIRCLE);//折线图上每个数据点的形状
        line.setCubic(false);//曲线是否平滑,即是曲线还是折线
        line.setFilled(false);//是否填充曲线的面积
        line.setHasLabels(true);//曲线的数据坐标是否加上备注
       //line.setStrokeWidth(2);//线条的粗细,默认是3
        line.setHasLines(true);//是否用线显示。如果为false 则没有曲线只有点显示
        line.setHasPoints(true);//是否显示圆点 如果为false 则没有原点只有点显示(每个数据点都是个大的圆点)
        lines.add(line);
        LineChartData data = new LineChartData();
        data.setLines(lines);
        //坐标轴
        Axis axisX = new Axis(); //X轴
        axisX.setHasTiltedLabels(true);  //X坐标轴字体是斜的显示还是直的,true是斜的显示
        axisX.setTextColor(Color.GRAY);  //设置字体颜色
        //axisX.setName("数据表");  //表格名称
        axisX.setTextSize(10);//设置字体大小
        axisX.setMaxLabelChars(8); //最多几个X轴坐标
        axisX.setValues(mAxisXValues);  //填充X轴的坐标名称
        data.setAxisXBottom(axisX); //x 轴在底部
        //data.setAxisXTop(axisX);  //x 轴在顶部
        axisX.setHasLines(true); //x 轴分割线
        // Y轴是根据数据的大小自动设置Y轴上限
        Axis axisY = new Axis();  //Y轴
        axisY.setName("");//y轴标注
        axisY.setTextSize(10);//设置字体大小
        data.setAxisYLeft(axisY);  //Y轴设置在左边
        //data.setAxisYRight(axisY);  //y轴设置在右边
        //设置行为属性,支持缩放、滑动以及平移
        lineChart.setInteractive(true);
        lineChart.setZoomType(ZoomType.HORIZONTAL);
        lineChart.setMaxZoom((float) 2);//最大方法比例
        lineChart.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL);
        lineChart.setLineChartData(data);
        lineChart.setVisibility(View.VISIBLE);
        Viewport v = new Viewport(lineChart.getMaximumViewport());
        v.left = 0;
        v.right = 10;
        lineChart.setCurrentViewport(v);
    }

}

第三步是在activity_*.xml中添加布局,这里布局文件比较简单,添加一个LineChartView即可。

<lecho.lib.hellocharts.view.LineChartView
        android:id="@+id/line_chart"
        android:padding="30dp"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffffff"/>

第四步可以添加折线数据点点击响应,以及绘制多条折线等,具体做法可以参考以下链接。
其中实现点击响应的步骤和常规做法一致,在HelloChart提供了数据点监听器,在监听器中进行功能的编写;
绘制多条折线就是创建mPointValues2 ,line2。然后设置line2的属性,在将line2添加到lines中。这样就会有以下关键代码:

java">lines.add(line);
lines.add(line2);
data.setLines(lines);

参考资料:
https://www.pianshen.com/article/7772292478/ 安卓开发之折线图Line_chart

https://blog.csdn.net/fjnu_se/article/details/80880862 Android开发中如何实现折线图的方法

https://blog.csdn.net/NYH19961125/article/details/88775491?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.control&dist_request_id=6ac2caac-7eba-449f-8317-ead098760adb&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.control Android HelloChart使用总结(折线数据点的点击响应,多条折线绘制方法)

https://github.com/lecho/hellocharts-android/tree/v1.5.8


http://www.niftyadmin.cn/n/1223867.html

相关文章

使用Visual C++ 6.0开发MAPGIS浏览器

地理信息系统&#xff08;GIS&#xff09;是集计算机科学、地理学、测绘遥感学、环境科学、城市科学、空间科学、信息科学和管理科学为一体的新兴边缘学科&#xff0c;目前发展已相当成熟&#xff0c;国内外高水平软件分别有ARC/INFO、MAPINFO、MAPGIS等。下文介绍一个以MAPGIS…

关于atl server的感受(1)

老板决定用atl server技术&#xff0c;说是为了网站的效率。在使用过程中也感觉确实比asp之类的快一些&#xff0c;毕竟&#xff0c;他是在c的层次上直接封装了isapi。atl server这个名字起的不伦不类&#xff0c;直到现在朋友问我在做什么&#xff0c;我还是费尽的说半天&…

VB(非.NET)写的史上最强EXE保护程序(开源)

VB(非.NET)写的史上最强EXE保护程序&#xff08;开源&#xff09;。据作者说&#xff1a;他已经获得超过100个客户购买其商业版&#xff0c;而且其核心部分已经给Adobe公司买断&#xff0c;这应该是唯一可以看到的源代码了&#xff1a;http://www.planet-source-code.com/vb/sc…

安卓开发系列 之如何获取手机短信内容

1 手机中短信数据库相关字段包括&#xff1a; _id&#xff1a;短信序号&#xff0c;如100    address&#xff1a;发件人地址&#xff0c;即手机号&#xff0c;如8613811810000&#xff0c;这里需要注意的是手机号是否加86&#xff0c;可以加上86和不加86分别测试下   pers…

博客园程序中的Data Access Application Block升级

升级到了Data Access Application Block 3.1 。

Struts入门经验(一)

Struts安装&#xff1a; 首先请到http://jakarta.apache.org/Struts下载Struts&#xff0c;建议使用release版&#xff0c;现在最高版本为1.1&#xff0c;下载后得到的是一个ZIP文件。 将ZIP包解开&#xff0c;可以看到这个目录&#xff1a;lib和webapps&#xff0c;webapps下有…

Jive笔记4--结果集分页处理

Jive笔记4--结果集分页处理shyguy 原创 (参与分&#xff1a;279&#xff0c;专家分&#xff1a;240) 发表&#xff1a;2002-9-18 下午12:18 版本&#xff1a;1.0 阅读&#xff1a;7257次 初来乍到&#xff0c;嘿嘿&#xff0c;意思意思。Jive中的分页处理http://forum.m…

一个简单的日期控件

这个日期控件就是三个下拉框的简单组合&#xff0c;但用起来还是挺顺手的&#xff0c;呵呵。控件类&#xff1a; ImportsSystem.ComponentModelImportsSystem.Web.UI<ToolboxData("<{0}:DateControl2 runatserver></{0}:DateControl2>")>PublicCla…