android图表MPAndroidChart教程

github地址:https://github.com/starrynightdawn/MPAndroidChart

为了使用这个库,有4种不同的选项:

1. Gradle dependency (recommended)

  • Add the following to your project level build.gradle:
allprojects {
	repositories {
		maven { url "https://jitpack.io" }
	}
}
  • Add this to your app build.gradle:
dependencies {
	implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
}

2. Maven

  • Add the following to the  section of your pom.xml:
<repository>
       <id>jitpack.ioid>
       <url>https://jitpack.iourl>
repository>
  • Add the following to the  section of your pom.xml:
<dependency>
       <groupId>com.github.PhilJaygroupId>
       <artifactId>MPAndroidChartartifactId>
       <version>v3.0.3version>
dependency>

3. clone whole repository (not recommended)

Chart types

  • LineChart (with legend, simple design) alt tag

  • LineChart (with legend, simple design) alt tag

  • LineChart (cubic lines) alt tag

  • LineChart (gradient fill) android图表MPAndroidChart教程_第1张图片

  • Combined-Chart (bar- and linechart in this case) android图表MPAndroidChart教程_第2张图片

  • BarChart (with legend, simple design)

alt tag

  • BarChart (grouped DataSets)

alt tag

  • Horizontal-BarChart

android图表MPAndroidChart教程_第3张图片

  • PieChart (with selection, ...)

alt tag

  • ScatterChart (with squares, triangles, circles, ... and more)

alt tag

  • CandleStickChart (for financial data)

alt tag

  • BubbleChart (area covered by bubbles indicates the yValue)

android图表MPAndroidChart教程_第4张图片

  • RadarChart (spider web chart)

alt tag

入门

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

添加依赖关系


作为第一步,将这个库的依赖项添加到项目中。如何在存储库的使用部分中描述它。Gradle是使用这个库作为一个依赖的推荐方法。
Creating the View


For using a LineChart, BarChart, ScatterChart, CandleStickChart, PieChart, BubbleChart or RadarChart , define it in .xml:
 <com.github.mikephil.charting.charts.LineChart
        android:id="@+id/chart"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

And then retrieve it from your ActivityFragment or whatever:

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

or create it in code (and then add it to a layout):

    // 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

Adding data

After you have an instance of your chart, you can create data and add it to the chart. This example uses the LineChart, for which the Entry class represents a single entry in the chart with x- and y-coordinate. Other chart types, such as BarChart use other classes (e.g. BarEntry) for that purpose.

To add data to your chart, wrap each data object you have into an Entry object, like below:

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())); 
}

As a next step, you need to add the List you created to a LineDataSet object. DataSetobjects hold data which belongs together, and allow individual styling of that data. The below used "Label" has only a descriptive purpose and shows up in the Legend, if enabled.

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

As a last step, you need to add the LineDataSet object (or objects) you created to a LineDataobject. This object holds all data that is represented by a Chart instance and allows further styling. After creating the data object, you can set it to the chart and refresh it:

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

Please consider the scenario above a very basic setup. For a more detailed explanation please refer to the setting data section, which explains how to add data to various Chart types based on examples.


你可能感兴趣的:(android基础知识)