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

}

+ Recent posts