안드로이드 노트

[안드로이드][프로젝트1] MPAndroidChart를 활용한 LineChart 사용

devRobin 2022. 3. 23. 12:36

1. settings.gradle에 다음 코드를 추가하고 싱크를 맞춰준다.

maven { url 'https://jitpack.io' }

위치 참고

2. build.gradle(모듈)에 다음 코드를 추가하고 싱크를 맞춰준다.

implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'

3. layout의 xml 파일에 다음 코드를 추가한다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">

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

</LinearLayout>

4. activity에 다음 코드를 추가한다.

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

import androidx.appcompat.app.AppCompatActivity;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private LineChart chart;

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

        chart = findViewById(R.id.linechart);


        ArrayList<Entry> values1 = new ArrayList<>();//샘플1
        ArrayList<Entry> values2 = new ArrayList<>();//샘플2


        //랜덤 데이터 추출
        for (int i = 0; i < 10; i++) {
            float val1 = (float) (Math.random() * 10);
            float val2 = (float) (Math.random() * 10);
            values1.add(new Entry(i, val1));
            values2.add(new Entry(i, val2));
        }




        LineDataSet set1 = new LineDataSet(values1, "Dataset 1");//샘플1
        LineDataSet set2 = new LineDataSet(values2, "Dataset 2");//샘플2


        ArrayList<ILineDataSet> dataSets = new ArrayList<>();
        dataSets.add(set1); // add the data sets
        dataSets.add(set2); // add the data sets

        // create a data object with the data sets
        LineData data = new LineData(dataSets);

        // 샘플1: black lines and points
        set1.setColor(Color.BLACK);
        set1.setCircleColor(Color.BLACK);

        //샘플2: Red lines and points
        set2.setColor(Color.RED);
        set2.setCircleColor(Color.RED);

        // set data
        chart.setData(data);
    }

}

위는 Dataset 1, Dataset 2가 각각 '기쁨', '슬픔'으로 적용되었을 때의 모습이다.

 

참고

https://velog.io/@hyehyes/%EC%95%88%EB%93%9C%EB%A1%9C%EC%9D%B4%EB%93%9C-MPAndroidChart%EB%A5%BC-%ED%99%9C%EC%9A%A9%ED%95%9C-LineChart-%EC%82%AC%EC%9A%A9

 

[안드로이드] MPAndroidChart를 활용한 LineChart 사용

지난 글이었던 HorizontalBarChart에 이어서 MPAnroidChart의 LineChart에 대하여 설명할 것이다. LineChart View 추가 xml 레이아웃에 com.github.mikephil.charting.charts.LineChart 를 추

velog.io

https://popcorn16.tistory.com/68

 

[Android] 차트 그리기 MPAndroidChart - LineChart

예전에 해커톤에서 어플리케이션을 개발할 때 MPAndroidChart를 이용해서 간단하게 안드로이드 그래프를 만든적이 있습니다. MPAndroidChart는 GitHub에 공개되어있는 오픈소스 라이브러리입니다. 라이

popcorn16.tistory.com