1. 그래들 스크립트 Gradle Scripts
build.gradle (Project)
allprojects {
repositories {
google()
jcenter()
maven {url "https://jitpack.io"}
}
}
build.gradle(app)
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.android.gms:play-services-ads:15.0.1'
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
}
2. XML 코드
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/picChart"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
3. MainActivity.java 코드
package com.jwlee.piechartexample;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
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 java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
PieChart pieChart;
int[] colorArray = new int[] {Color.LTGRAY, Color.BLUE, Color.RED};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pieChart = findViewById(R.id.picChart);
PieDataSet pieDataSet = new PieDataSet(data1(),"좋음싫음 설문조사");
pieDataSet.setColors(colorArray);
PieData pieData = new PieData(pieDataSet);
pieChart.setDrawEntryLabels(true);
pieChart.setUsePercentValues(true);
pieData.setValueTextSize(30);
pieChart.setCenterText("설문조사");
pieChart.setCenterTextSize(25);
pieChart.setHoleRadius(30);
pieChart.setData(pieData);
pieChart.invalidate();
}
private ArrayList<PieEntry> data1() {
ArrayList<PieEntry> datavalue = new ArrayList<>();
datavalue.add(new PieEntry(30,"무응답"));
datavalue.add(new PieEntry(50,"좋음"));
datavalue.add(new PieEntry(20,"싫음"));
return datavalue;
}
}
'기타 ETC > Android Studio' 카테고리의 다른 글
안드로이드 네이버지도API 사용해서 네이버지도앱만들고 초보 (0) | 2020.03.12 |
---|---|
안드로이드 구글 지도앱 만들기 초보 (0) | 2020.03.12 |
안드로이드 스택바차트 Android Stacked Bar Chart (0) | 2020.02.19 |
안드로이드 수명 계산기 만들기, Android Develop Death Calculator (0) | 2020.02.17 |
안드로이드 러시아 룰렛 게임 만들기 Android Develop Russian Roulette Game (0) | 2020.02.17 |