iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 6
0
Software Development

Kotlin 2018連續開發30天系列 第 6

Kotlin 2018連續開發30天 day 5 android 第三方工具 mpandroidchart-pieChart

android 畫圖表的第三方工具

(三)pieChart(圓餅圖)
導入
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
XML中建立view
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/bar_pie"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></com.github.mikephil.charting.charts.PieChart>

Activity中設定
val piechart = bar_pie

建立資料

  val entries = ArrayList<PieEntry>()
    entries.add(PieEntry(22F,"aaa"))
    entries.add(PieEntry(78F,"bbb"))
    

PieEntry格式為<所佔範圍100%,標籤>

建立顏色分類

    val colors = ArrayList<Int>()
    colors.add(resources.getColor(R.color.colorPrimaryDark))
    colors.add(resources.getColor(R.color.colorPrimary))

建立dataset

    val dataSet =PieDataSet(entries,"label")
    dataSet.setColors(colors)
    

建立data 加入piechart

    val pieData = PieData(dataSet)
    pieData.setDrawValues(true)

    piechart.setData(pieData)
    piechart.invalidate()
    

完工


上一篇
Kotlin 2018連續開發30天 day 4 android 第三方工具 mpandroidchart-lineChar
下一篇
Kotlin 2018連續開發30天 day 6 android 第三方工具 mpandroidchart-scatterhart
系列文
Kotlin 2018連續開發30天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言