P Android Chart에서 PieChart
접선도, 스트라이프도, 파이도 등.
Kotlin 연습을 하기 굉장히 편해요.
MPAndroidChart
나는 PieChart를 그려 보았다.연습 중이라 이상한 점이 있을 수 있어요.
참고하도록 허락해 주세요.
배치
gradleallprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
dependencies {
〜中略〜
compile 'com.github.PhilJay:MPAndroidChart:v3.0.3'
}
스크린
layout<?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="com.example.ssuzaki.piesample.MainActivity">
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/pie"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
/>
</android.support.constraint.ConstraintLayout>
코드
kotlinpackage com.example.ssuzaki.piesample
import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.github.mikephil.charting.charts.PieChart
import com.github.mikephil.charting.components.Description
import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.data.PieData
import com.github.mikephil.charting.data.PieDataSet
import com.github.mikephil.charting.data.PieEntry
import com.github.mikephil.charting.formatter.PercentFormatter
import com.github.mikephil.charting.utils.ColorTemplate
import java.util.*
class MainActivity : AppCompatActivity() {
var mPie: PieChart? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupPieChartView()
}
fun setupPieChartView() {
mPie = findViewById(R.id.pie)
mPie?.setUsePercentValues(true)
val desc: Description = Description()
desc.text = "PieChartのサンプルだよ"
mPie?.description = desc
val legend: Legend? = mPie?.legend
legend?.horizontalAlignment = Legend.LegendHorizontalAlignment.LEFT
/**
* サンプルデータ
*/
val value = Arrays.asList(10f, 20f, 30f, 40f)
val label = Arrays.asList("A", "B", "C", "D")
val entry = ArrayList<PieEntry>()
for(i in value.indices) {
entry.add( PieEntry(value.get(i), label.get(i)) )
}
/**
* ラベル
*/
val dataSet = PieDataSet(entry, "チャートのラベル")
dataSet.colors = ColorTemplate.COLORFUL_COLORS.toList()
dataSet.setDrawValues(true)
val pieData = PieData(dataSet)
pieData.setValueFormatter(PercentFormatter())
pieData.setValueTextSize(20f)
pieData.setValueTextColor(Color.WHITE)
mPie?.data = pieData
}
}
결과
Reference
이 문제에 관하여(P Android Chart에서 PieChart), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/ssuzaki/items/32d538445b7ecd73cad2
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
dependencies {
〜中略〜
compile 'com.github.PhilJay:MPAndroidChart:v3.0.3'
}
layout
<?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="com.example.ssuzaki.piesample.MainActivity">
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/pie"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
/>
</android.support.constraint.ConstraintLayout>
코드
kotlinpackage com.example.ssuzaki.piesample
import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.github.mikephil.charting.charts.PieChart
import com.github.mikephil.charting.components.Description
import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.data.PieData
import com.github.mikephil.charting.data.PieDataSet
import com.github.mikephil.charting.data.PieEntry
import com.github.mikephil.charting.formatter.PercentFormatter
import com.github.mikephil.charting.utils.ColorTemplate
import java.util.*
class MainActivity : AppCompatActivity() {
var mPie: PieChart? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupPieChartView()
}
fun setupPieChartView() {
mPie = findViewById(R.id.pie)
mPie?.setUsePercentValues(true)
val desc: Description = Description()
desc.text = "PieChartのサンプルだよ"
mPie?.description = desc
val legend: Legend? = mPie?.legend
legend?.horizontalAlignment = Legend.LegendHorizontalAlignment.LEFT
/**
* サンプルデータ
*/
val value = Arrays.asList(10f, 20f, 30f, 40f)
val label = Arrays.asList("A", "B", "C", "D")
val entry = ArrayList<PieEntry>()
for(i in value.indices) {
entry.add( PieEntry(value.get(i), label.get(i)) )
}
/**
* ラベル
*/
val dataSet = PieDataSet(entry, "チャートのラベル")
dataSet.colors = ColorTemplate.COLORFUL_COLORS.toList()
dataSet.setDrawValues(true)
val pieData = PieData(dataSet)
pieData.setValueFormatter(PercentFormatter())
pieData.setValueTextSize(20f)
pieData.setValueTextColor(Color.WHITE)
mPie?.data = pieData
}
}
결과
Reference
이 문제에 관하여(P Android Chart에서 PieChart), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/ssuzaki/items/32d538445b7ecd73cad2
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
package com.example.ssuzaki.piesample
import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.github.mikephil.charting.charts.PieChart
import com.github.mikephil.charting.components.Description
import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.data.PieData
import com.github.mikephil.charting.data.PieDataSet
import com.github.mikephil.charting.data.PieEntry
import com.github.mikephil.charting.formatter.PercentFormatter
import com.github.mikephil.charting.utils.ColorTemplate
import java.util.*
class MainActivity : AppCompatActivity() {
var mPie: PieChart? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupPieChartView()
}
fun setupPieChartView() {
mPie = findViewById(R.id.pie)
mPie?.setUsePercentValues(true)
val desc: Description = Description()
desc.text = "PieChartのサンプルだよ"
mPie?.description = desc
val legend: Legend? = mPie?.legend
legend?.horizontalAlignment = Legend.LegendHorizontalAlignment.LEFT
/**
* サンプルデータ
*/
val value = Arrays.asList(10f, 20f, 30f, 40f)
val label = Arrays.asList("A", "B", "C", "D")
val entry = ArrayList<PieEntry>()
for(i in value.indices) {
entry.add( PieEntry(value.get(i), label.get(i)) )
}
/**
* ラベル
*/
val dataSet = PieDataSet(entry, "チャートのラベル")
dataSet.colors = ColorTemplate.COLORFUL_COLORS.toList()
dataSet.setDrawValues(true)
val pieData = PieData(dataSet)
pieData.setValueFormatter(PercentFormatter())
pieData.setValueTextSize(20f)
pieData.setValueTextColor(Color.WHITE)
mPie?.data = pieData
}
}
Reference
이 문제에 관하여(P Android Chart에서 PieChart), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/ssuzaki/items/32d538445b7ecd73cad2텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)