MPAndroidChart 教程:开始 Getting Started

简介: 入门本章介绍使用此库的基本设置。添加依赖首先,将此库的依赖项添加到项目中。如何执行此操作在此存储库的用法部分中进行了描述。

入门

本章介绍使用此库的基本设置。

添加依赖

首先,将此库的依赖项添加到项目中。如何执行此操作在此存储库的用法部分中进行了描述。Gradle是使用此库作为依赖项的推荐方法。

创建视图

要使用LineChart, BarChart, ScatterChart, CandleStickChart, PieChart, BubbleChart or RadarChart ,请在.xml中定义它:

    <com.github.mikephil.charting.charts.LineChart
        android:id="@+id/chart"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

然后从您的Activity,Fragment或其他内容中检索它:

 // in this example, a LineChart is initialized from xml
    LineChart chart = (LineChart) findViewById(R.id.chart);

或者在代码中创建它(然后将其添加到布局中):

  // programmatically create a LineChart
    LineChart chart = new LineChart(Context);

    // get a layout defined in xml
    RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout);
    rl.add(chart); // add the programmatically created chart

添加数据

拥有图表实例后,您可以创建数据并将其添加到图表中。此示例使用LineChart,其中Entry类表示图表中具有x和y坐标的单个条目。其他图表类型(例如BarChart)使用其他类(例如BarEntry)。

要将数据添加到图表中,请将您拥有的每个数据对象包装到Entry对象中,如下所示:

YourData[] dataObjects = ...;
List<Entry> entries = new ArrayList<Entry>();
for (YourData data : dataObjects) {
    // turn your data into Entry objects
    entries.add(new Entry(data.getValueX(), data.getValueY())); 
}

下一步,您需要将创建的List<Entry>添加到LineDataSet对象中。DataSet对象保存属于一起的数据,并允许对该数据进行单独设计。以下使用的“Label ”仅具有描述性目的,并在Legend中显示(如果已启用)。

LineDataSet dataSet = new LineDataSet(entries, "Label"); // add entries to dataset
dataSet.setColor(...);
dataSet.setValueTextColor(...); // styling, ...

最后一步,您需要将创建的LineDataSet对象(或多个对象)添加到LineData对象中。此对象包含由Chart实例表示的所有数据,并允许进一步样式化。创建数据对象后,您可以将其设置为图表并刷新它:

LineData lineData = new LineData(dataSet);
chart.setData(lineData);
chart.invalidate(); // refresh

请考虑上面的场景一个非常基本的设置。有关更详细的说明,请参阅设置数据部分,它解释了如何根据示例将数据添加到各种图表类型。

造型

有关图表表面和数据的设置和样式的信息,请访问常规设置和样式部分。有关各个图表类型的更具体的样式和设置,请查看特定设置和样式 Wiki页面。

 

参考:

https://github.com/PhilJay/MPAndroidChart/wiki/Getting-Started

https://blog.csdn.net/u014136472/article/details/50293767

 

 

相关文章
|
17天前
|
网络协议 Java Shell
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
53 7
|
5月前
|
Oracle 关系型数据库
oracle的start with connect by prior如何使用 整理
oracle的start with connect by prior如何使用 整理
364 4
|
9月前
|
关系型数据库 MySQL 数据库
MySQL 启动 登录报错Job for mysqld.service failed because the control process exited with error code. See
MySQL 启动 登录报错Job for mysqld.service failed because the control process exited with error code. See
256 1
|
9月前
|
弹性计算 Dubbo Serverless
Serverless 应用引擎操作报错合集之阿里函数计算中配置完fc,出现‘Function instance exited unexpectedly(code 1, message:operation not permitted) with start command 'npm run start '. 报错如何解决
Serverless 应用引擎(SAE)是阿里云提供的Serverless PaaS平台,支持Spring Cloud、Dubbo、HSF等主流微服务框架,简化应用的部署、运维和弹性伸缩。在使用SAE过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
260 5
|
安全 Ubuntu 测试技术
l4re Getting started
l4re Getting started
433 0
|
Java 测试技术 Nacos
Java访问Elasticsearch报错Request cannot be executed; I/O reactor status: STOPPED
Java访问Elasticsearch报错Request cannot be executed; I/O reactor status: STOPPED
4678 0
Java访问Elasticsearch报错Request cannot be executed; I/O reactor status: STOPPED
|
监控 安全 网络协议
[知识小节]Process Monitor介绍(上)
[知识小节]Process Monitor介绍
1244 0
[知识小节]Process Monitor介绍(上)
|
存储 监控 安全
[知识小节]Process Monitor介绍(下)
[知识小节]Process Monitor介绍
1140 0
[知识小节]Process Monitor介绍(下)
Veloce 之 Getting Started
Veloce 之所以能够加速仿真,原因是Veloce把DUT(Design Under Test) 和 TB(TestBench) 一起综合成实际的电路,然后下载到Veloce硬件中,在硬件上跑,所以是比软件仿真快得多。 那么怎样才能把Veloce用起来,让它来加速我们的仿真呢?
Veloce 之 Getting Started
|
Ruby Web App开发