LoginSignup
2
1

More than 5 years have passed since last update.

[Android] MPAndroidChartを使った色付きPieChartの実装メモ

Last updated at Posted at 2019-02-06

MPAndroidChartライブラリを使用して、色付きPieChartを作成したときの実装メモです。

参考URL:
MPAndroid Chart: https://github.com/PhilJay/MPAndroidChart
Wiki Getting Started: https://github.com/PhilJay/MPAndroidChart/wiki/Getting-Started
Wiki Setting Data: https://github.com/PhilJay/MPAndroidChart/wiki/Setting-Data

作るもの

月ごとの降水量の割合を示したPieChartです。
月をクリックするとちょっと拡大します。
スクリーンショット 2019-02-06 21.55.55.png

build.gradle

build.gradle
allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

dependencies {
    implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
}

Activity

MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {

    float rainfall[] = {98.8f, 123.8f, 34.6f, 43.9f, 69.4f, 12.5f, 52.8f, 158.6f,
            203.6f, 30.7f, 160.7f, 159.7f };
    String monthNames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
            "Sep", "Oct", "Nov", "Dec"};


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

        setupPieChart();
    }


    private void setupPieChart() {
        //PieEntriesのリストを作成する:
        List<PieEntry> pieEntries = new ArrayList<>();
        for (int i = 0; i < rainfall.length; i++) {
            pieEntries.add(new PieEntry(rainfall[i], monthNames[i]));
        }

        PieDataSet dataSet = new PieDataSet(pieEntries, "Rainfall for Vancouver");
        dataSet.setColors(ColorTemplate.COLORFUL_COLORS);
        PieData data = new PieData(dataSet);

        //PieChartを取得する:
        PieChart piechart = (PieChart) findViewById(R.id.chart);
        piechart.setData(data);
        piechart.invalidate();
    }
}

※おまけメモ:
Fragmentに書くときは、「PieChart piechart = (PieChart) findViewById(R.id.chart);」が
「PieChart piechart = (PieChart) getView().findViewById(R.id.chart);」になる。

Layout

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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

</android.support.constraint.ConstraintLayout>

おわり!

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1