한 걸음 두 걸음

Android / 안드로이드] MPAndroidChart 꺾은선그래프 / 막대그래프 그리기 본문

FrontEnd/Android

Android / 안드로이드] MPAndroidChart 꺾은선그래프 / 막대그래프 그리기

언제나 변함없이 2019. 7. 30. 22:51
반응형

implementation 'com.github.PhilJay:MPAndroidChart:v2.2.4'

1. 꺾은선그래프

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <Button
        android:id="@+id/home_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/back_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

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



</LinearLayout>

MainActivity.java

package com.onsemiro.hanpinetree.removethis;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.YAxis;
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 com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    ArrayList<Integer> jsonList = new ArrayList<>(); // ArrayList 선언
    ArrayList<String> labelList = new ArrayList<>(); // ArrayList 선언

    //두 개의 버튼 임시 추가 (해당코드엔 없어야맞음)
    Button backButton;
    Button homeButton;

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

        init();

        LineChartGraph(labelList, jsonList);


    }

    public void init(){

      backButton =(Button) findViewById(R.id.home_btn);
      homeButton =(Button) findViewById(R.id.back_btn);

      backButton.setOnClickListener(this);
      homeButton.setOnClickListener(this);


        jsonList.add(10);
        jsonList.add(3);
        jsonList.add(1);
        jsonList.add(2);

        labelList.add("1");
        labelList.add("2");
        labelList.add("3");
        labelList.add("4");

    }


    private void LineChartGraph(ArrayList labelList, ArrayList valList){

        // LineChart 메소드

        LineChart lineChart = (LineChart) findViewById(R.id.chart);



        ArrayList<Entry> entries = new ArrayList<>();

        for(int i=0; i < valList.size();i++){

            entries.add(new Entry((Integer) valList.get(i), i));

        }



        LineDataSet depenses = new LineDataSet(entries, "# of Calls");

        depenses.setAxisDependency(YAxis.AxisDependency.LEFT);



        ArrayList<String> labels = new ArrayList<String>();

        for(int i=0; i < labelList.size(); i++){

            labels.add((String) labelList.get(i));

        }



        ArrayList<ILineDataSet> dataSets = new ArrayList<>();

        dataSets.add((ILineDataSet)depenses);

        LineData data = new LineData(labels,dataSets); // 라이브러리 v3.x 사용하면 에러 발생함

        depenses.setColors(ColorTemplate.COLORFUL_COLORS); //

        //depenses.setDrawCubic(true); //선 둥글게 만들기

        //depenses.setDrawFilled(true); //그래프 밑부분 색칠



        lineChart.setData(data);

        lineChart.animateXY(1000,1000);

        lineChart.invalidate();

    }



    private String DateChange(String sysdate){

        String year = sysdate.split("-")[0];

        String month = sysdate.split("-")[1];

        String day = sysdate.split("-")[2];



        StringBuilder finalString = new StringBuilder();

        finalString.append(year.substring(2,4));

        finalString.append(".");

        finalString.append(month);

        //finalString.append(".");

        //finalString.append(day);

        return finalString.toString();

    }



    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.back_btn:

                finish();

               // overridePendingTransition(R.anim.rightin, R.anim.rightout);

                break;

            case R.id.home_btn:

                Intent intent = new Intent(this, MainActivity.class);

                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

                startActivity(intent);

                break;

            default:

                break;

        }

    }

}

2. 막대 그래프

위랑 똑같고,
<com.github.mikephil.charting.charts.BarChart로
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/chart" />

    이거 BarChart로 씁니당 
    private void BarChartGraph(ArrayList<String> labelList, ArrayList<Integer> valList) {
        // BarChart 메소드


        ArrayList<BarEntry> entries = new ArrayList<>();
        for(int i=0; i < valList.size();i++){
            entries.add(new BarEntry((Integer) valList.get(i), i));
        }

        BarDataSet depenses = new BarDataSet (entries, "일일 사용사간"); // 변수로 받아서 넣어줘도 됨
        depenses.setAxisDependency(YAxis.AxisDependency.RIGHT);
        barChart.setDescription(" ");

        ArrayList<String> labels = new ArrayList<String>();
        for(int i=0; i < labelList.size(); i++){
            labels.add((String) labelList.get(i));
        }

        BarData data = new BarData(labels,depenses); // 라이브러리 v3.x 사용하면 에러 발생함
        depenses.setColors(ColorTemplate.LIBERTY_COLORS); //

        barChart.setData(data);
        barChart.animateXY(1000,1000);
        barChart.invalidate();
    }

막대그래프 최댓값 세팅해놓기>

        barChart.getAxisRight().setAxisMaxValue(50);
        barChart.getAxisLeft().setAxisMaxValue(50);

MPAndroidChart 공식 문서
https://github.com/PhilJay/MPAndroidChart/wiki

반응형