한 걸음 두 걸음
MPandroidchart 간단 예제 / 코드 첨부 본문
반응형
mpandroidchart 막대그래프 예제 간단하게 첨부.
xml은 차트 하나만 넣음.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/fragment_bluetooth_chat_barchart"
android:layout_width="300dp"
android:layout_height="180dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.471" />
</android.support.constraint.ConstraintLayout>
gradle
repositories {
maven { url "https://jitpack.io" }
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:2.0.4'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.github.PhilJay:MPAndroidChart:v2.2.4'
}
MainActivity 하나로만 구성해봄
package com.onsemiro.hanpinetree.mpandroidtest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.utils.ColorTemplate;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<Integer> jsonList = new ArrayList<>(); // ArrayList 선언
ArrayList<String> labelList = new ArrayList<>(); // ArrayList 선언
BarChart barChart;
TextView minuteTextview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
barChart = (BarChart) findViewById(R.id.fragment_bluetooth_chat_barchart);
graphInitSetting(); //그래프 기본 세팅
BarChartGraph(labelList, jsonList);
barChart.setTouchEnabled(false); //확대하지못하게 막아버림! 별로 안좋은 기능인 것 같아~
barChart.getAxisRight().setAxisMaxValue(80);
barChart.getAxisLeft().setAxisMaxValue(80);
}
public void graphInitSetting(){
labelList.add("일");
labelList.add("월");
labelList.add("화");
labelList.add("수");
labelList.add("목");
labelList.add("금");
labelList.add("토");
jsonList.add(10);
jsonList.add(20);
jsonList.add(30);
jsonList.add(40);
jsonList.add(50);
jsonList.add(60);
jsonList.add(70);
BarChartGraph(labelList, jsonList);
barChart.setTouchEnabled(false); //확대하지못하게 막아버림! 별로 안좋은 기능인 것 같아~
//barChart.setRendererLeftYAxis();
// barChart.setMaxVisibleValueCount(50);
// barChart.setTop(50);
// barChart.setBottom(0);
// barChart.setAutoScaleMinMaxEnabled(true);
barChart.getAxisRight().setAxisMaxValue(80);
barChart.getAxisLeft().setAxisMaxValue(80);
}
/**
* 그래프함수
*/
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.LEFT);
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();
}
}
2. 꺾은선 그래프로 나타내기
repositories {
maven { url "https://jitpack.io" }
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:2.0.4'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
}
xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/fragment_bluetooth_chat_linechart"
android:layout_width="300dp"
android:layout_height="180dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.471" />
</android.support.constraint.ConstraintLayout>
package com.onsemiro.hanpinetree.mpandroidtest;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
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;
import java.util.LinkedList;
import java.util.List;
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.fragment_bluetooth_chat_linechart);
ArrayList<Entry> values = new ArrayList<>();
for (int i = 0; i < 100; i++) {
float val = (float) (Math.random() * 10);
values.add(new Entry(i, (int)val));
}
LineDataSet set1;
set1 = new LineDataSet(values, "DataSet 1");
ArrayList<ILineDataSet> dataSets = new ArrayList<>();
dataSets.add(set1); // add the data sets
// create a data object with the data sets
LineData data = new LineData(dataSets);
// black lines and points
set1.setColor(Color.BLACK);
set1.setCircleColor(Color.BLACK);
set1.setDrawCircles(false);
set1.setDrawValues(false);
// set data
chart.setData(data);
}
}
..........
..
반응형
'FrontEnd > Android' 카테고리의 다른 글
안드로이드 APK파일 만들기 (0) | 2021.08.02 |
---|---|
Android MPAndroid Chart Circle 삭제하기 (0) | 2021.03.24 |
Retrofit expected begin_object but was string at line 1 column 1 path $ 에러 해결 (0) | 2021.02.23 |
no usb devices or running emulators detected (0) | 2021.02.21 |
개발 트랜드 (0) | 2020.01.01 |