Skip to content

Instantly share code, notes, and snippets.

@reuniware
Created July 5, 2019 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reuniware/2d5ae691f5bf69b8e1ad163419479f4e to your computer and use it in GitHub Desktop.
Save reuniware/2d5ae691f5bf69b8e1ad163419479f4e to your computer and use it in GitHub Desktop.
Kotlin MPAndroidChart Basic Code
package com.activity
import android.graphics.Color
import android.graphics.DashPathEffect
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.github.mikephil.charting.charts.LineChart
import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.components.LimitLine
import com.github.mikephil.charting.components.LimitLine.LimitLabelPosition
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.formatter.IFillFormatter
import com.github.mikephil.charting.highlight.Highlight
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet
import com.github.mikephil.charting.listener.OnChartValueSelectedListener
class MPAndroidChartActivity : AppCompatActivity(), OnChartValueSelectedListener {
lateinit var chart: LineChart
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.mpandroidchart)
chart = findViewById(R.id.chart1)
chart.setBackgroundColor(Color.WHITE)
chart.description.isEnabled = false
chart.setTouchEnabled(true)
chart.setOnChartValueSelectedListener(this)
chart.setDrawGridBackground(false)
chart.isDragEnabled = true
chart.setScaleEnabled(true)
chart.setPinchZoom(true)
val xAxis = chart.xAxis
xAxis.enableGridDashedLine(10f,10f,0f)
val yAxis = chart.axisLeft
chart.axisRight.isEnabled = false
yAxis.enableGridDashedLine(10f, 10f, 0f)
yAxis.setAxisMaximum(200f)
yAxis.setAxisMinimum(-50f)
val llXAxis = LimitLine(9f, "Index 10")
llXAxis.lineWidth = 4f
llXAxis.enableDashedLine(10f, 10f, 0f)
llXAxis.labelPosition = LimitLabelPosition.RIGHT_BOTTOM
llXAxis.textSize = 10f
//llXAxis.typeface = tfRegular
val ll1 = LimitLine(150f, "Upper Limit")
ll1.lineWidth = 4f
ll1.enableDashedLine(10f, 10f, 0f)
ll1.labelPosition = LimitLabelPosition.RIGHT_TOP
ll1.textSize = 10f
//ll1.typeface = tfRegular
val ll2 = LimitLine(-30f, "Lower Limit")
ll2.lineWidth = 4f
ll2.enableDashedLine(10f, 10f, 0f)
ll2.labelPosition = LimitLabelPosition.RIGHT_BOTTOM
ll2.textSize = 10f
//ll2.typeface = tfRegular
// draw limit lines behind data instead of on top
yAxis.setDrawLimitLinesBehindData(true)
xAxis.setDrawLimitLinesBehindData(true)
// add limit lines
yAxis.addLimitLine(ll1)
yAxis.addLimitLine(ll2)
setData(45, 180f)
chart.animateX(1500)
val legend = chart.legend
legend.form = Legend.LegendForm.LINE
}
private fun setData(count: Int, range: Float) {
val values = ArrayList<Entry>()
for (i in 0 until count) {
val value = (Math.random() * range).toFloat() - 30
values.add(Entry(i.toFloat(), value/*, resources.getDrawable(R.drawable.star)*/))
}
val set1: LineDataSet
if (chart.data != null && chart.data.dataSetCount > 0) {
set1 = chart.data.getDataSetByIndex(0) as LineDataSet
set1.values = values
set1.notifyDataSetChanged()
chart.data.notifyDataChanged()
chart.notifyDataSetChanged()
} else {
// create a dataset and give it a type
set1 = LineDataSet(values, "DataSet 1")
set1.setDrawIcons(false)
// draw dashed line
set1.enableDashedLine(10f, 5f, 0f)
// black lines and points
set1.color = Color.BLACK
set1.setCircleColor(Color.BLACK)
// line thickness and point size
set1.lineWidth = 1f
set1.circleRadius = 3f
// draw points as solid circles
set1.setDrawCircleHole(false)
// customize legend entry
set1.formLineWidth = 1f
set1.formLineDashEffect = DashPathEffect(floatArrayOf(10f, 5f), 0f)
set1.formSize = 15f
// text size of values
set1.valueTextSize = 9f
// draw selection line as dashed
set1.enableDashedHighlightLine(10f, 5f, 0f)
// set the filled area
set1.setDrawFilled(true)
set1.fillFormatter = IFillFormatter { dataSet, dataProvider -> chart.axisLeft.axisMinimum }
// set color of filled area
/*if (Utils.getSDKInt() >= 18) {
// drawables only supported on api level 18 and above
val drawable = ContextCompat.getDrawable(this, R.drawable.fade_red)
set1.fillDrawable = drawable
} else {
set1.fillColor = Color.BLACK
}*/
val dataSets = ArrayList<ILineDataSet>()
dataSets.add(set1) // add the data sets
// create a data object with the data sets
val data = LineData(dataSets)
// set data
chart.data = data
}
}
override fun onNothingSelected() {
}
override fun onValueSelected(e: Entry?, h: Highlight?) {
}
}
/*XML Layout (mpandroidchart.xml to create)
<?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=".StatistiquesActivity">
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart1"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
</android.support.constraint.ConstraintLayout>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment