Androidでレーダーチャートを表示する方法!MPAndroidChartの導入と実装手順を解説
この記事では、Androidアプリでレーダーチャート(Radar Chart)を表示する方法を、定番のグラフ描画ライブラリ「MPAndroidChart」を使って解説します。レーダーチャートは複数の評価項目をひと目で比較できるため、能力分析やアンケート結果の可視化など、幅広い用途に活用できます。
ステップ1:新しいプロジェクトを作成する
Android Studioで「File」→「New Project」を選択し、必要な項目を入力して新しいプロジェクトを作成します。
ステップ2:モジュールレベルのbuild.gradleに依存関係を追加する
モジュールレベル(app配下)のbuild.gradleを開き、dependenciesにMPAndroidChartのライブラリを追加します。
apply plugin: 'com.android.application'
android {
packagingOptions {
exclude 'META-INF/proguard/androidx-annotations.pro'
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/notice.txt'
exclude 'META-INF/ASL2.0'
}
compileSdkVersion 28
defaultConfig {
applicationId "com.example.andy.myapplication"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0-alpha'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
ステップ3:プロジェクトレベルのbuild.gradleにJitPackを追加する
トップレベル(プロジェクトレベル)のbuild.gradleにあるallprojectsブロックに、JitPackリポジトリを追加します。MPAndroidChartはJitPack経由で配布されているため、この設定が必須です。
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
ステップ4:レイアウトにRadarChartを配置する
res/layout/activity_main.xmlに以下のコードを追加します。
<?xml version = "1.0" encoding = "utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android = "https://schemas.android.com/apk/res/android"
xmlns:app = "https://schemas.android.com/apk/res-auto"
xmlns:tools = "https://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".MainActivity">
<com.github.mikephil.charting.charts.RadarChart
android:id = "@+id/RadarChart"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent" />
</android.support.constraint.ConstraintLayout>
上記コードでは、画面いっぱいにレーダーチャートを表示するためのRadarChartビューを配置しています。
ステップ5:MainActivity.javaを実装する
src/MainActivity.javaに以下のコードを記述します。
package com.example.andy.myapplication;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.github.mikephil.charting.charts.BubbleChart;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.data.BubbleData;
import com.github.mikephil.charting.data.BubbleDataSet;
import com.github.mikephil.charting.data.BubbleEntry;
import com.github.mikephil.charting.data.RadarData;
import com.github.mikephil.charting.data.RadarDataSet;
import com.github.mikephil.charting.data.RadarEntry;
import com.github.mikephil.charting.utils.ColorTemplate;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
RadarChart RadarChart;
RadarData radarData;
RadarDataSet radarDataSet;
ArrayList radarEntries;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadarChart = findViewById(R.id.RadarChart);
getEntries();
radarDataSet = new RadarDataSet(radarEntries, "");
radarData = new RadarData(radarDataSet);
RadarChart.setData(radarData);
radarDataSet.setColors(ColorTemplate.JOYFUL_COLORS);
radarDataSet.setValueTextColor(Color.BLACK);
radarDataSet.setValueTextSize(18f);
}
private void getEntries() {
radarEntries = new ArrayList<>();
radarEntries.add(new RadarEntry(0, 0.21f));
radarEntries.add(new RadarEntry(1, 0.12f));
radarEntries.add(new RadarEntry(2, 0.20f));
radarEntries.add(new RadarEntry(2, 0.52f));
radarEntries.add(new RadarEntry(3, 0.29f));
radarEntries.add(new RadarEntry(4, 0.62f));
}
}
実装のポイントは以下の通りです。
- getEntries()メソッド内で、RadarEntryオブジェクトとしてデータポイントを生成し、ArrayListに格納します。
- RadarDataSetでデータセットを作成し、RadarDataに渡してから、チャートにsetData()でセットします。
- setColors()でJOYFUL_COLORSカラーテンプレートを適用し、値テキストの色(黒)とサイズ(18f)も指定しています。
アプリを実行して結果を確認する
それでは、実際にアプリを実行してみましょう。ここでは、実機のAndroid端末をパソコンに接続しているものとします。Android Studioでプロジェクトのアクティビティファイルを開き、ツールバーの「Run」アイコンをクリックしてください。接続した端末を選択して実行すると、端末の画面に次のようにレーダーチャートが表示されます。

上記の実行結果のように、データセットに設定した値に応じてレーダーチャートが描画されます。データの値やラベル、配色を変更すれば、目的に合わせたレーダーチャートを簡単に実装できるので、ぜひいろいろなパターンを試してみてください。
-
【Android】LocalBroadcastManagerの使い方を実例付きでわかりやすく解説
LocalBroadcastManagerとは? LocalBroadcastManagerは、アプリ内部だけで完結するブロードキャスト(Intent)の送受信を行うためのクラスです。システム全体や他アプリには通知されないため、セキュリティ面でも安全で、アプリ内のコンポーネント間でデータを受け渡したい場合に便利です。 本記事では、実際に動くサンプルプロジェクトを作りながら、LocalBroadcastManagerの基本的な実装手順をステップごとに紹介します。 Step 1:新規プロジェクトの作成 まず、Android Studioを起動し、「File → New Project」から新しい
-
【Android】ViewFlipperの使い方を徹底解説!ビュー切り替えアニメーションの実装方法
ViewFlipperとは?ViewFlipperは、複数の子ビューを重ねて保持し、一定間隔での自動切り替えやボタン操作による手動切り替えを、アニメーション効果付きで実現できるAndroidのウィジェットです。画像スライダー、オンボーディング画面、シンプルなカルーセルUIなどを作りたいときに非常に便利です。本記事では、ViewFlipperを使ってImageView・Button・TextViewをスライドアニメーションで切り替えるサンプルアプリを、ステップごとに詳しく解説します。Step 1:新規プロジェクトを作成するAndroid Studioを起動し、メニューから「File」→「New