티스토리 뷰
안드로이드 원 그래프 만들기
build.gradle
repositories{ maven {url "https://jitpack.io"} }
dependencies { compile 'com.github.PhilJay:MpAndroidChart:v3.0.2' }
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="org.androidtown.graph.MainActivity">
<com.github.mikephil.charting.charts.PieChart android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/piechart"> </com.github.mikephil.charting.charts.PieChart>
</RelativeLayout>
Java
public class MainActivity extends AppCompatActivity {
PieChart pieChart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pieChart = (PieChart)findViewById(R.id.piechart);
pieChart.setUsePercentValues(true);
pieChart.getDescription().setEnabled(false);
pieChart.setExtraOffsets(5,10,5,5);
pieChart.setDragDecelerationFrictionCoef(0.95f);
pieChart.setDrawHoleEnabled(false);
pieChart.setHoleColor(Color.WHITE);
pieChart.setTransparentCircleRadius(61f);
ArrayList<PieEntry> yValues = new ArrayList<PieEntry>();
yValues.add(new PieEntry(34f,"Japen"));
yValues.add(new PieEntry(23f,"USA"));
yValues.add(new PieEntry(14f,"UK"));
yValues.add(new PieEntry(35f,"India"));
yValues.add(new PieEntry(40f,"Russia"));
yValues.add(new PieEntry(40f,"Korea"));
Description description = new Description();
description.setText("세계 국가"); //라벨
description.setTextSize(15);
pieChart.setDescription(description);
pieChart.animateY(1000, Easing.EasingOption.EaseInOutCubic); //애니메이션
PieDataSet dataSet = new PieDataSet(yValues,"Countries");
dataSet.setSliceSpace(3f);
dataSet.setSelectionShift(5f);
dataSet.setColors(ColorTemplate.JOYFUL_COLORS);
PieData data = new PieData((dataSet));
data.setValueTextSize(10f);
data.setValueTextColor(Color.YELLOW);
pieChart.setData(data);
}
}
안드로이드 꺽은선 그래프 만들기
build.gradle
repositories {
maven { url "https://jitpack.io" }
}
dependencies {
compile 'com.github.PhilJay:MPAndroidChart:v3.0.2'
}
XML
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent" />
JAVA
public class MainActivity extends Activity{
private LineChart lineChart;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lineChart = (LineChart)findViewById(R.id.chart);
List<Entry> entries = new ArrayList<>();
entries.add(new Entry(1, 1));
entries.add(new Entry(2, 2));
entries.add(new Entry(3, 0));
entries.add(new Entry(4, 4));
entries.add(new Entry(5, 3));
LineDataSet lineDataSet = new LineDataSet(entries, "속성명1");
lineDataSet.setLineWidth(2);
lineDataSet.setCircleRadius(6);
lineDataSet.setCircleColor(Color.parseColor("#FFA1B4DC"));
lineDataSet.setCircleColorHole(Color.BLUE);
lineDataSet.setColor(Color.parseColor("#FFA1B4DC"));
lineDataSet.setDrawCircleHole(true);
lineDataSet.setDrawCircles(true);
lineDataSet.setDrawHorizontalHighlightIndicator(false);
lineDataSet.setDrawHighlightIndicators(false);
lineDataSet.setDrawValues(false);
LineData lineData = new LineData(lineDataSet);
lineChart.setData(lineData);
XAxis xAxis = lineChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTextColor(Color.BLACK);
xAxis.enableGridDashedLine(8, 24, 0);
YAxis yLAxis = lineChart.getAxisLeft();
yLAxis.setTextColor(Color.BLACK);
YAxis yRAxis = lineChart.getAxisRight();
yRAxis.setDrawLabels(false);
yRAxis.setDrawAxisLine(false);
yRAxis.setDrawGridLines(false);
Description description = new Description();
description.setText("");
lineChart.setDoubleTapToZoomEnabled(false);
lineChart.setDrawGridBackground(false);
lineChart.setDescription(description);
lineChart.animateY(2000, Easing.EasingOption.EaseInCubic);
lineChart.invalidate();
}
}
Create a real time line graph in Android with GraphView
XML Code
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.ssaurel.testgraphview.MainActivity" >
<!-- we add graph view to display -->
<com.jjoe64.graphview.GraphView
android:id="@+id/graph"
android:layout_width="match_parent"
android:layout_height="300dp" />
</RelativeLayout>
JAVA Code
package com.ssaurel.testgraphview;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.Viewport;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
public class MainActivity extends Activity {
private static final Random RANDOM = new Random();
private LineGraphSeries<DataPoint> series;
private int lastX = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// we get graph view instance
GraphView graph = (GraphView) findViewById(R.id.graph);
// data
series = new LineGraphSeries<DataPoint>();
graph.addSeries(series);
// customize a little bit viewport
Viewport viewport = graph.getViewport();
viewport.setYAxisBoundsManual(true);
viewport.setMinY(0);
viewport.setMaxY(10);
viewport.setScrollable(true);
}
@Override
protected void onResume() {
super.onResume();
// we're going to simulate real time with thread that append data to the graph
new Thread(new Runnable() {
@Override
public void run() {
// we add 100 new entries
for (int i = 0; i < 100; i++) {
runOnUiThread(new Runnable() {
@Override
public void run() {
addEntry();
}
});
// sleep to slow down the add of entries
try {
Thread.sleep(600);
} catch (InterruptedException e) {
// manage error ...
}
}
}
}).start();
}
// add random data to graph
private void addEntry() {
// here, we choose to display max 10 points on the viewport and we scroll to end
series.appendData(new DataPoint(lastX++, RANDOM.nextDouble() * 10d), true, 10);
}
}
'프로그래밍 > 안드로이드 앱프로그래밍' 카테고리의 다른 글
Importing Image From Camera (0) | 2020.06.24 |
---|---|
서버/클라이언트 소켓(SOCKET) 통신하기 (3) | 2020.06.24 |
안드로이드 String 변수를 숫자(int나 float)로 변환 (0) | 2020.02.04 |
안드로이드 차트 만들기 (0) | 2020.02.04 |
안드로이드 텍스트뷰에 스크롤 추가하기 (1) | 2020.01.30 |
댓글