[안드로이드] 프래그먼트에 라이브러리를 이용한 차트 그리기

2022. 1. 17. 15:06·앱 개발/Java
반응형

 


 

차트 라이브러리 모음 

https://github.com/PhilJay/MPAndroidChart

 

GitHub - PhilJay/MPAndroidChart: A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubb

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

github.com

https://github.com/HackPlan/AndroidCharts

 

GitHub - HackPlan/AndroidCharts: An easy-to-use Android charts library with animation.

An easy-to-use Android charts library with animation. - GitHub - HackPlan/AndroidCharts: An easy-to-use Android charts library with animation.

github.com

https://github.com/diogobernardino/WilliamChart

 

GitHub - diogobernardino/williamchart: Android Library to rapidly develop attractive and insightful charts in android applicatio

Android Library to rapidly develop attractive and insightful charts in android applications. - GitHub - diogobernardino/williamchart: Android Library to rapidly develop attractive and insightful ch...

github.com

https://github.com/AnyChart/AnyChart-Android

 

 

GitHub - diogobernardino/williamchart: Android Library to rapidly develop attractive and insightful charts in android applicatio

Android Library to rapidly develop attractive and insightful charts in android applications. - GitHub - diogobernardino/williamchart: Android Library to rapidly develop attractive and insightful ch...

github.com

https://github.com/blackfizz/EazeGraph

 

GitHub - blackfizz/EazeGraph: An Android chart and graph library

An Android chart and graph library. Contribute to blackfizz/EazeGraph development by creating an account on GitHub.

github.com

 

 

이 중에서 원하는 라이브러리를 골라 사용하시면 됩니다

 


 

저는 맨 위의 PhilJay/MPAndroidChart 를 사용했습니다.

 

 

들어가서 보시면 이런 내용이 나오는데 아래의 Maven Setup은 안보셔도 됩니다.

 

 

위의 repositories와 dependencies만 복사해서 build.gradle(Module)에 붙여넣기 해주시면 됩니다.

 

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

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

 

[build.gradle(Module)]

실제 코드 사용 화면입니다.

 

 

그 후 chart를 입력할 Fragment의 Layout에 차트를 입력해줍니다.

 

 

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

크기는 원하시는대로 조정하시면 됩니다.

 

[fragment_home.xml]

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeFragment">

    <com.github.mikephil.charting.charts.LineChart
        android:id="@+id/chart"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/> 
        
    <TextView
        android:layout_width="match_parent"
        android:layout_height="57dp"
        android:gravity="center"
        android:text="홈"
        android:textSize="32dp"
        android:layout_gravity="bottom"/>


</FrameLayout>

레이아웃 디자인이 이렇게 뜨시면 맞게 하신 겁니다

 

 

이제 마지막으로 Fragment 창에 그래프를 입력해 줍니다.

 

 

[HomeFragment.java]

public class HomeFragment extends Fragment {

    View v;
    LineChart lineChart;
    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        v =  inflater.inflate(R.layout.fragment_home, container, false);

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

        List<Entry> entries = new ArrayList<Entry>();
        entries.add(new Entry(1,1));
        entries.add(new Entry(2,2));
        entries.add(new Entry(3,3));
        entries.add(new Entry(4,4));
        entries.add(new Entry(5,5));


        LineDataSet dataSet = new LineDataSet(entries, "Label");
        dataSet.setLineWidth(4); //라인 두께
        dataSet.setCircleRadius(6); // 점 크기
        dataSet.setCircleColor(Color.parseColor("#FFA1B4DC")); // 점 색깔
        dataSet.setDrawCircleHole(true); // 원의 겉 부분 칠할거?
        dataSet.setColor(Color.parseColor("#FFA1B4DC")); // 라인 색깔

        LineData lineData = new LineData(dataSet);
        lineChart.setData(lineData);
        lineChart.invalidate();

        return v;
    }
}

 

주의하실 점) Fragment창이기 때문에 onCreate가 아닌 onCreateView에서 구현해주셔야 합니다.

 

 

 

이렇게 뜨시면 맞게 하신겁니다

 

 

질문은 댓글 남겨주세요!

반응형

'앱 개발 > Java' 카테고리의 다른 글

[Android] DB데이터 차트 표출 - MPAndroidChart  (0) 2022.02.24
[안드로이드] 블루투스 어플 프로젝트 공유(아두이노 통신)  (3) 2022.02.21
[안드로이드] fragment 키보드(키패드) 내리기  (0) 2022.01.13
[안드로이드] 레이아웃 배경색 어둡게  (0) 2022.01.03
[안드로이드] 권한 요청 팝업 (한번에 여러개 요청)  (0) 2022.01.03
'앱 개발/Java' 카테고리의 다른 글
  • [Android] DB데이터 차트 표출 - MPAndroidChart
  • [안드로이드] 블루투스 어플 프로젝트 공유(아두이노 통신)
  • [안드로이드] fragment 키보드(키패드) 내리기
  • [안드로이드] 레이아웃 배경색 어둡게
맨땅
맨땅
다른 개발자분들에게 도움이 되었으면 좋겠습니다
  • 맨땅
    맨땅에 코딩
    맨땅
  • 전체
    오늘
    어제
    • 분류 전체보기 (48)
      • 프로그래밍 지식 (1)
      • 즐거운 웹 개발 (7)
        • Setting (3)
        • 프로그래밍 (1)
        • HTML, CSS (1)
      • 앱 개발 (29)
        • Java (26)
        • Kotlin (0)
        • Setting (2)
        • 프로그래밍 (0)
      • AI (1)
        • Setting2 (0)
        • 프로그래밍 (0)
        • 크롤링 (1)
      • DB (9)
      • ---------------------------.. (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
맨땅
[안드로이드] 프래그먼트에 라이브러리를 이용한 차트 그리기
상단으로

티스토리툴바