반응형
* Git
https://github.com/PhilJay/MPAndroidChart
* 소스코드 출처 블로그
http://blog.naver.com/PostView.nhn?blogId=skyvvv624&logNo=221062100445
* 속성 정리 블로그
build.gradle (Project)
allprojects {
repositories {
..
maven { url "https://jitpack.io" }
}
}
build.gradle (App)
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.0'
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">
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import com.github.mikephil.charting.charts.LineChart;
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;
public class MainActivity extends AppCompatActivity {
private LineChart chart;
private Thread thread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chart = (LineChart) findViewById(R.id.chart);
chart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
chart.getAxisRight().setEnabled(false);
chart.getLegend().setTextColor(Color.WHITE);
chart.animateXY(2000, 2000);
chart.invalidate();
LineData data = new LineData();
chart.setData(data);
feedMultiple();
}
private void addEntry() {
LineData data = chart.getData();
if (data != null) {
ILineDataSet set = data.getDataSetByIndex(0);
if (set == null) {
set = createSet();
data.addDataSet(set);
}
data.addEntry(new Entry(set.getEntryCount(), (float) (Math.random() * 40) + 30f), 0);
data.notifyDataChanged();
chart.notifyDataSetChanged();
chart.setVisibleXRangeMaximum(10);
chart.moveViewToX(data.getEntryCount());
}
}
private LineDataSet createSet() {
LineDataSet set = new LineDataSet(null, "Dynamic Data");
set.setFillAlpha(110);
set.setFillColor(Color.parseColor("#d7e7fa"));
set.setColor(Color.parseColor("#0B80C9"));
set.setCircleColor(Color.parseColor("#FFA1B4DC"));
set.setCircleColorHole(Color.BLUE);
set.setValueTextColor(Color.WHITE);
set.setDrawValues(false);
set.setLineWidth(2);
set.setCircleRadius(6);
set.setDrawCircleHole(false);
set.setDrawCircles(false);
set.setValueTextSize(9f);
set.setDrawFilled(true);
set.setAxisDependency(YAxis.AxisDependency.LEFT);
set.setHighLightColor(Color.rgb(244, 117, 117));
return set;
}
private void feedMultiple() {
if (thread != null)
thread.interrupt();
final Runnable runnable = new Runnable() {
@Override
public void run() {
addEntry();
}
};
thread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
runOnUiThread(runnable);
try {
Thread.sleep(3000);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
});
thread.start();
}
@Override
protected void onPause() {
super.onPause();
if (thread != null)
thread.interrupt();
}
}
반응형
'프로그래밍 > App 개발' 카테고리의 다른 글
[android] 안드로이드 wav 레코딩 (20.06.28 깨달은점) (0) | 2020.08.06 |
---|---|
[android] 구글맵 선긋기 (2) | 2020.08.06 |
[android] MVVM의 편리함 (20.05.28 깨달은 점) (0) | 2020.08.06 |
[android] FCM 푸시 (node.js, android) (0) | 2020.08.06 |
[android] 데이터바인딩 (데이터결합 라이브러리) (0) | 2020.08.06 |