1. 그래들 코드 

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.BarChart
android:id="@+id/stacked_BarChart"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

 

3. MainActivity.java 코드

package com.jwlee.stackedbarchart;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    BarChart stackedChart;
    int[] colorArray = new int[] {Color.RED, Color.YELLOW, Color.BLUE};

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

        stackedChart = findViewById(R.id.stacked_BarChart);

        BarDataSet barDataSet = new BarDataSet(dataValues1(),"Bar Set");
        barDataSet.setColors(colorArray);


        BarData barData = new BarData(barDataSet);
        stackedChart.setData(barData);

    }

    private ArrayList dataValues1() {
        ArrayList dataVals = new ArrayList<>();

        dataVals.add(new BarEntry(0, new float[]{2,5.5f,4}));
        dataVals.add(new BarEntry(1, new float[]{2,8f,4.2f}));
        dataVals.add(new BarEntry(2, new float[]{3,5.5f,4}));
        dataVals.add(new BarEntry(3, new float[]{4,6.5f,4}));
        dataVals.add(new BarEntry(4, new float[]{2,5.5f,3.3f}));


        return dataVals;
    }
}

+ Recent posts