Skip to content

Instantly share code, notes, and snippets.

@nesterchung
Created January 3, 2018 11:44
Show Gist options
  • Save nesterchung/4008bed8dfc9f9a85ff9a53e83e256a0 to your computer and use it in GitHub Desktop.
Save nesterchung/4008bed8dfc9f9a85ff9a53e83e256a0 to your computer and use it in GitHub Desktop.
BarChart
package com.nesterchung.chart
import android.content.Context
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.text.format.DateUtils
import android.util.Log
import android.view.WindowManager
import com.github.mikephil.charting.charts.BarChart
import com.github.mikephil.charting.components.AxisBase
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.data.BarData
import com.github.mikephil.charting.data.BarDataSet
import com.github.mikephil.charting.data.BarEntry
import com.github.mikephil.charting.formatter.IAxisValueFormatter
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet
import com.github.mikephil.charting.utils.ColorTemplate
import java.util.*
class MainActivity : AppCompatActivity() {
companion object {
const val TAG = "MainActivity"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN)
setContentView(R.layout.activity_main)
val mChart: BarChart = findViewById(R.id.chart1)
val xAxisFormatter = DayAxisValueFormatter(mChart, this)
val xAxis = mChart.xAxis
xAxis.position = XAxis.XAxisPosition.BOTTOM
xAxis.setDrawGridLines(false)
xAxis.granularity = 1f // only intervals of 1 day
xAxis.valueFormatter = xAxisFormatter
xAxis.setLabelCount(6, true)
val leftAxis = mChart.axisLeft
leftAxis.setDrawGridLines(false)
leftAxis.setLabelCount(10, false)
leftAxis.spaceTop = 15f
leftAxis.axisMinimum = 0f // this replaces setStartAtZero(true)
val yVals = ArrayList<BarEntry>()
val halfHour = 60.0f
for (x in 1..6) {
val minute = Math.round((x-1).times(halfHour))
Log.d(TAG, "Relative Timestamp:$minute")
yVals.add(BarEntry(minute.toFloat(), x.toFloat()))
}
var set = BarDataSet(yVals, "????")
set.setDrawIcons(false)
set.colors = ColorTemplate.MATERIAL_COLORS.toList()
val dataSets = ArrayList<IBarDataSet>()
dataSets.add(set)
val data = BarData(dataSets)
data.setValueTextSize(10f)
data.barWidth = 10f
mChart.data = data
}
class DayAxisValueFormatter(mChart: BarChart, val mContext: Context) : IAxisValueFormatter {
companion object {
const val TAG = "DayAxisValueFormatter"
}
private var currentDate = 0L
init {
val cal = Calendar.getInstance()
cal.set(Calendar.HOUR_OF_DAY, 0)
cal.set(Calendar.MINUTE, 0)
cal.set(Calendar.SECOND, 0)
cal.set(Calendar.MILLISECOND, 0)
currentDate = cal.timeInMillis
Log.d(TAG, "currentDate:$currentDate)")
}
override fun getFormattedValue(value: Float, axis: AxisBase?): String {
val date = currentDate + value.toInt() * 60 * 1000
val formatDateTime = DateUtils.formatDateTime(mContext, date, DateUtils.FORMAT_SHOW_TIME)
Log.d(TAG, "R-Value: $value, A-Value:$date, format:$formatDateTime")
return formatDateTime
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment