먼지 쌓인 키보드

[안드로이드] MPAndroidChart 꺾은선 그래프 만들기 본문

안드로이드 스튜디오

[안드로이드] MPAndroidChart 꺾은선 그래프 만들기

Under_Desk 2020. 3. 29. 00:10
반응형

 

 

 

PhilJay/MPAndroidChart

A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, dragging and animations. - PhilJay/MPAndroidChart

github.com

MPAndroidChart 링크

 

 

 

build.gradle (module:app)

여기에 추가하면 되고, repositories 없으면 추가하시면 됨.

(버전은 추후에 바뀔수있으니 github 링크에서 확인 후 하시면 됩니다.)

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

dependencies {
    implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
}

 

 

 

main.xml에 추가할 linechart layout추가

( layout 설정은 알아서)

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

 

 

 

main.java 에 추가할 import

//MPAndroidChart import 
import com.github.mikephil.charting.charts.LineChart; 
import com.github.mikephil.charting.components.Description; 
import com.github.mikephil.charting.components.XAxis; 
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 com.github.mikephil.charting.charts.LineChart; 

 

 

정말 기본적인 선 그래프 선언,

여기서 응용해서 사용하시면 됩니다.

   public class MainActivity extends AppCompatActivity {

   
   //선 그래프
    private LineChart lineChart;
    
    ArrayList<Entry> entry_chart = new ArrayList<>();
    
    
    lineChart = (LineChart) findViewById(R.id.chart);//layout의 id
    LineData chartData = new LineData()
    
    entry_chart.add(new Entry(x값, y값));
    /* 만약 (2, 3) add하고 (2, 5)한다고해서
    기존 (2, 3)이 사라지는게 아니라 x가 2인곳에 y가 3, 5의 점이 찍힘 */
    
    LineDataSet lineDataSet = new LineDataSet(entry_chart, "꺽은선1");
    chartData.addDataSet(lineDataSet);
    
    lineChart.setData(chartData);
    
    lineChart.invalidate();
            
   }

 

 

 

 

다음은 차트 하나에 2개의 그래프 넣는 방법

 

[안드로이드] 차트 하나에 2개의 라인 그래프 만들기

MPAndroidChart 기본 예제 참고 [안드로이드] MPAndroidChart 꺾은선 그래프 만들기 PhilJay/MPAndroidChart A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble-..

under-desk.tistory.com

 

 

 

 

반응형
Comments