Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.doneit.emiltonia.ui.who.graph
- import android.Manifest
- import android.content.Intent
- import android.content.pm.PackageManager
- import android.graphics.*
- import android.net.Uri
- import android.os.Build
- import android.os.Bundle
- import android.provider.MediaStore
- import android.support.annotation.RequiresApi
- import android.support.design.widget.FloatingActionButton
- import android.support.v4.app.ActivityCompat
- import android.util.Log
- import android.view.MotionEvent
- import android.view.View
- import android.widget.Toast
- import com.doneit.emiltonia.Const
- import com.doneit.emiltonia.R
- import com.doneit.emiltonia.data.entity.BabyEntity
- import com.doneit.emiltonia.data.entity.PercentileEntity
- import com.doneit.emiltonia.data.entity.ScaleEntity
- import com.doneit.emiltonia.ui.base.BaseContract
- import com.doneit.emiltonia.ui.base.inject.BaseInjectActivity
- import com.github.mikephil.charting.charts.LineChart
- import com.github.mikephil.charting.components.*
- 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.IndexAxisValueFormatter
- import com.github.mikephil.charting.highlight.Highlight
- import com.github.mikephil.charting.interfaces.datasets.ILineDataSet
- import com.github.mikephil.charting.listener.ChartTouchListener
- import com.github.mikephil.charting.listener.OnChartGestureListener
- import com.github.mikephil.charting.listener.OnChartValueSelectedListener
- import com.github.mikephil.charting.renderer.XAxisRenderer
- import com.github.mikephil.charting.renderer.YAxisRenderer
- import com.github.mikephil.charting.utils.MPPointF
- import com.github.mikephil.charting.utils.Transformer
- import com.github.mikephil.charting.utils.Utils
- import com.github.mikephil.charting.utils.ViewPortHandler
- import kotlinx.android.synthetic.main.activity_who_graph.*
- import timber.log.Timber
- import java.text.SimpleDateFormat
- import java.util.*
- import java.util.concurrent.TimeUnit
- import javax.inject.Inject
- class WhoGraphNewActivity : BaseInjectActivity(), WhoGraphContract.View,
- OnChartGestureListener, OnChartValueSelectedListener {
- private var mChart: LineChart? = null
- private var dataSets = ArrayList<ILineDataSet>()
- companion object {
- private const val KEY_EXTRA_BABY_ID = "KEY_EXTRA_BABY_ID"
- private const val DAYS_IN_WEEK = 7
- }
- @Inject
- lateinit var presenter: WhoGraphPresenter
- private var fab: FloatingActionButton? = null
- private var babyId: Int = 0
- private var age = 0
- private var gender = Const.Genders.MALE
- private var baby: BabyEntity? = null
- private var scales = 0
- private var percentileP3: MutableList<Double> = arrayListOf()
- private var percentileP15: MutableList<Double> = arrayListOf()
- private var percentileP50: MutableList<Double> = arrayListOf()
- private var percentileP85: MutableList<Double> = arrayListOf()
- private var percentileP97: MutableList<Double> = arrayListOf()
- private var percentileBabyScales: MutableList<Double> = arrayListOf()
- override fun getPresenter(): BaseContract.Presenter<*>? {
- return presenter
- }
- override fun showBabyInfo(babyEntity: BabyEntity) {
- baby = babyEntity
- val dd = baby?.age!! / DAYS_IN_WEEK
- Timber.e("showBabyInfo = ${baby?.age} : $dd")
- age = baby?.age!! / DAYS_IN_WEEK
- // age = 26
- gender = baby?.gender!!
- }
- override fun showPercentilesList(list: List<PercentileEntity>) {
- // Toast.makeText(this@WhoGraphNewActivity, "percentiles ${list.size}", Toast.LENGTH_SHORT).show()
- list.forEach {
- percentileP3.add(it.p3.toDouble()*1000)
- percentileP15.add(it.p15.toDouble()*1000)
- Timber.e("P15 = ${it.p15}")
- percentileP50.add(it.p50.toDouble()*1000)
- percentileP85.add(it.p85.toDouble()*1000)
- percentileP97.add(it.p97.toDouble()*1000)
- }
- Timber.d("P3 ${percentileP3.size}")
- Timber.d("P15 ${percentileP15.size}")
- Timber.d("P50 ${percentileP50.size}")
- Timber.d("P85 ${percentileP85.size}")
- Timber.d("P97 ${percentileP97.size}")
- }
- @RequiresApi(Build.VERSION_CODES.O)
- override fun showScalesList(list: List<ScaleEntity>) {
- Toast.makeText(this@WhoGraphNewActivity, "baby scales ${list.size}", Toast.LENGTH_SHORT).show()
- scales = list.size
- Timber.e("scales size = $scales")
- list.forEach {
- Timber.e("ttttt = ${it.weight}")
- percentileBabyScales.add(it.weight.toDouble()*1000)
- }
- Timber.d("percentileBabyScales ${percentileBabyScales.size}")
- Collections.sort(percentileBabyScales)
- // createSimpleGraph()
- setupChart()
- setupBabyLine()
- }
- @RequiresApi(Build.VERSION_CODES.O)
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_who_graph_new)
- setSupportActionBar(toolbar)
- supportActionBar?.setDisplayShowTitleEnabled(false)
- toolbarTitle.setText(R.string.who_graph_title)
- toolbar.setNavigationOnClickListener { onBackPressed() }
- toolbar.setNavigationIcon(R.drawable.ic_back)
- babyId = intent.getIntExtra(WhoGraphNewActivity.KEY_EXTRA_BABY_ID, 0)
- presentationComponent.inject(this)
- presenter.attachToView(this)
- // presenter.getAllPercentilesByBabyId(babyId)
- presenter.getBabyInfo(babyId)
- // this@WhoGraphNewActivity.requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 101)
- fab = findViewById(R.id.fab)
- fab?.setOnClickListener {
- shareWhoGraphRequest()
- }
- mChart = findViewById(R.id.chart1)
- mChart!!.setNoDataText("")
- mChart!!.onChartGestureListener = this
- mChart!!.setOnChartValueSelectedListener(this)
- mChart!!.setDrawGridBackground(false)
- // no description text
- mChart!!.description.isEnabled = false
- // enable touch gestures
- mChart!!.setTouchEnabled(true)
- // enable scaling and dragging
- mChart!!.isDragEnabled = true
- mChart!!.setScaleEnabled(true)
- // mChart.setScaleXEnabled(true);
- // mChart.setScaleYEnabled(true);
- // if disabled, scaling can be done on x- and y-axis separately
- mChart!!.setPinchZoom(true)
- // set an alternative background color
- // mChart.setBackgroundColor(Color.GRAY);
- // create a custom MarkerView (extend MarkerView) and specify the layout
- // to use for it
- // val mv = MyMarkerView(this, R.layout.custom_marker_view)
- // mv.setChartView(mChart) // For bounds control
- // mChart!!.marker = mv // Set the marker to the chart
- // x-axis limit line
- val llXAxis = LimitLine(10f, "Index 10")
- llXAxis.lineWidth = 4f
- llXAxis.enableDashedLine(10f, 10f, 0f)
- llXAxis.labelPosition = LimitLine.LimitLabelPosition.RIGHT_BOTTOM
- llXAxis.textSize = 10f
- val xAxis = mChart!!.xAxis
- xAxis.enableGridDashedLine(10f, 10f, 0f)
- //xAxis.setValueFormatter(new MyCustomXAxisValueFormatter());
- //xAxis.addLimitLine(llXAxis); // add x-axis limit line
- val leftAxis = mChart!!.axisLeft
- leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART)
- leftAxis.removeAllLimitLines() // reset all limit lines to avoid overlapping lines
- // leftAxis.addLimitLine(ll1)
- // leftAxis.addLimitLine(ll2)
- leftAxis.axisMaximum = 10f
- leftAxis.axisMinimum = 0f
- // leftAxis.yOffset = 20f;
- leftAxis.enableGridDashedLine(30f, 10f, 0f)
- leftAxis.setDrawZeroLine(false)
- // limit lines are drawn behind data (and not on top)
- leftAxis.setDrawLimitLinesBehindData(true)
- val yLabel = ArrayList<String>()
- for (i in 0..10) {
- yLabel.add(i.toString().plus(" 000 g"))
- }
- leftAxis.setValueFormatter { value, axis ->
- return@setValueFormatter yLabel[value.toInt()]
- }
- mChart!!.axisRight.isEnabled = false
- //mChart.getViewPortHandler().setMaximumScaleY(2f);
- //mChart.getViewPortHandler().setMaximumScaleX(2f);
- // add data
- // setData(45, 2f)
- // mChart.setVisibleXRange(20);
- // mChart.setVisibleYRange(20f, AxisDependency.LEFT);
- // mChart.centerViewTo(20, 50, AxisDependency.LEFT);
- // mChart!!.animateX(2500)
- //mChart.invalidate();
- // get the legend (only possible after setting data)
- val l = mChart!!.legend
- mChart!!.xAxis.setDrawLabels(true)
- mChart!!.legend.setDrawInside(false)
- mChart!!.legend.isEnabled = false;
- // modify the legend ...
- l.form = Legend.LegendForm.LINE
- // l.setDrawInside(true)
- // l.addDashedLine(width: dashWidth, pattern: dashes[0], color: statisticColor)
- // // dont forget to refresh the drawing
- // mChart!!.invalidate();
- /*setupChart()
- setupBabyLine()*/
- }
- private fun isStoragePermissionGranted() : Boolean{
- return if (Build.VERSION.SDK_INT >= 23) {
- if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
- == PackageManager.PERMISSION_GRANTED) {
- true
- } else {
- ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 1)
- false
- }
- } else { //permission is automatically granted on sdk<23 upon installation
- true
- }
- }
- private fun shareWhoGraphRequest() {
- // mChart!!.saveToGallery("First WhoGraph 101", 100)
- if (isStoragePermissionGranted()) {
- share()
- }
- }
- private fun share() {
- val bitmap = mChart!!.chartBitmap
- val imgName = "Emiltonia_".plus(baby?.name).plus("_Who_Graph_".plus(getCurrentTimeStamp()))
- val bitmapPath = MediaStore.Images.Media.insertImage(contentResolver, bitmap, imgName, null)
- val bitmapUri = Uri.parse(bitmapPath)
- val intent = Intent()
- intent.action = Intent.ACTION_SEND
- intent.type = "image/*"
- intent.putExtra(Intent.EXTRA_STREAM, bitmapUri)
- val shareTitle = "Share ".plus(baby?.name).plus(" Who Graph")
- startActivity(Intent.createChooser(intent, shareTitle))
- }
- override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults)
- if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- share()
- }
- }
- @RequiresApi(Build.VERSION_CODES.O)
- private fun setupChart() {
- // mChart!!.setBackgroundColor(Color.rgb(0.97f, 0.97f, 0.97f))
- mChart!!.setBackgroundColor(Color.WHITE)
- // val lineChartView = mChart!!
- // let lineChartView = self.chartView.lineChartView!
- // mChart!!.setBackgroundColor(Color.rgb(0.97f, 0.97f, 0.97f))
- // mChart!!.setBorderColor(Color.rgb(0.60f, 0.60f, 0.60f))
- mChart!!.setBorderColor(Color.TRANSPARENT)
- //mChart!!.borderLineWidth = 1
- mChart!!.setDrawBorders(true)
- mChart!!.isDragXEnabled = true
- mChart!!.isDragYEnabled = true//false
- mChart!!.isScaleXEnabled = true
- mChart!!.isScaleYEnabled = true//false
- mChart!!.viewPortHandler.setMaximumScaleX(4f)
- mChart!!.viewPortHandler.setMaximumScaleY(2f)
- val xValueRenderer = XValueRenderer(
- mChart!!.viewPortHandler,
- mChart!!.xAxis,
- mChart!!.rendererXAxis.transformer
- )
- // val baby = self.dataProvider.baby
- // xValueRenderer.birthColor = Color.GREEN // = self.babyChartColor(baby).first!
- mChart!!.setXAxisRenderer(xValueRenderer)
- val xAxis = mChart!!.xAxis
- xAxis.position = XAxis.XAxisPosition.BOTTOM
- xAxis.gridColor = Color.LTGRAY
- xAxis.setDrawGridLines(true)
- xAxis.enableGridDashedLine(30f, 0f, 0f)
- // xAxis.labelFont = labelFont
- xAxis.textColor = Color.LTGRAY
- xAxis.valueFormatter = XValueFormatter()
- // xAxis.labelCount = UIScreen.main.bounds.size.width > 375 ? 5 : 4
- xAxis.labelCount = /*5*/4
- xAxis.textColor = Color.BLACK
- //todo find out how manipulate with limits
- // xAxis.axisMinimum = 0f/*self.dataProvider.leftXLimit*/
- // xAxis.axisMaximum = 0f/*self.dataProvider.rightXLimit*/
- // lineChartView.getAxis(.right).enabled = false
- // lineChartView.getAxis(YAxis.AxisDependency.RIGHT).
- /*val yValueRenderer = YValueRenderer(
- lineChartView.viewPortHandler,
- lineChartView.getAxis(YAxis.AxisDependency.LEFT),
- lineChartView.rendererXAxis.transformer
- )
- lineChartView.rendererLeftYAxis = yValueRenderer*/
- val yAxis = mChart!!.getAxis(YAxis.AxisDependency.LEFT)
- yAxis.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART)
- yAxis.setDrawTopYLabelEntry(false)//drawTopYLabelEntryEnabled = false
- yAxis.setDrawLimitLinesBehindData(false)//drawBottomYLabelEntryEnabled = false
- yAxis.axisMinimum = 0f/*self.dataProvider.lowerYLimit*/
- yAxis.axisMaximum = 10000f/*self.dataProvider.upperYLimit*/
- // yAxis.labelCount = Int((yAxis.axisMaximum - yAxis.axisMinimum) / self.dataProvider.yStep)
- yAxis.labelCount = ((yAxis.axisMaximum - yAxis.axisMinimum).toInt()) / 1000
- yAxis.gridColor = Color.LTGRAY
- yAxis.setDrawGridLines(true)
- yAxis.enableGridDashedLine(30f, 0f, 0f)
- // yAxis.labelFont = labelFont
- yAxis.textColor = Color.BLACK
- yAxis.valueFormatter = YValueFormatter()
- yAxis.xOffset = yAxis.xOffset + 4f/*yLabelRectInset*/
- }
- /*fun chartScaled(_ chartView: ChartViewBase, scaleX: Float, scaleY: Float) {
- recalculateXLimits()
- }
- fun chartTranslated(_ chartView: ChartViewBase, dX: Float, dY: Float) {
- recalculateXLimits()
- }*/
- private fun setupBabyLine() {
- // val baby = self.dataProvider.baby
- val values2 = ArrayList<Entry>()
- // val age = percentileBabyScales[0]
- // val age = 20
- Timber.e("AGEee = $age")
- for (i in 0 until /*percentileBabyScales.size*//*age*/scales) {
- val value = (Math.random() * 9000).toFloat() + 13
- // Timber.e("value = $value")
- Timber.e("baby value = ${percentileBabyScales[i].toFloat()}")
- // values2.add(Entry(i.toFloat(), /*percentileP3*/percentileBabyScales[i].toFloat()/*value*/, resources.getDrawable(R.drawable.star)))
- values2.add(Entry(i.toFloat(), percentileBabyScales[i].toFloat()))
- }
- // val line = LineDataSet(values: self.dataProvider.babyDataEntry, "Vasya")
- val line = LineDataSet(values2, baby?.name)
- line.setDrawIcons(false)
- // set the line to be drawn like this "- - - - - -"
- line.enableDashedLine(10f, 0f, 0f)
- line.enableDashedHighlightLine(10f, 0f, 0f)
- if (gender == Const.Genders.MALE) {
- line.color = Color.rgb(152, 210, 212)
- line.setCircleColor(Color.rgb(152, 210, 212))
- }
- else {
- line.color = Color.rgb(221,160,199)
- line.setCircleColor(Color.rgb(221,160,199))
- }
- // line.setCircleColorHole(line.colors[0])
- line.lineWidth = 3f/*babyLineWidth*/
- line.circleRadius = /*line.lineWidth*/5f /** 3*/
- line.circleHoleRadius = line.circleRadius * 0.8f
- line.axisDependency = YAxis.AxisDependency.LEFT
- line.setDrawValues(true)
- // line.valueTextSize = 10f
- line.setDrawVerticalHighlightIndicator(false)
- line.setDrawHorizontalHighlightIndicator(false)
- line.formLineWidth = 1f
- // line.formLineDashEffect = DashPathEffect(floatArrayOf(10f, 5f), 0f)
- line.formSize = 15f
- // val data = LineChartData()
- // statisticLines().forEach {
- // data.addDataSet($0)
- // }
- val data = LineData()
- data.addDataSet(line)
- data.addDataSet(setDataP3())
- data.addDataSet(setDataP15())
- data.addDataSet(setDataP50())
- data.addDataSet(setDataP85())
- data.addDataSet(setDataP97())
- mChart!!.data = data
- mChart!!.description = null
- mChart!!.invalidate()
- fab?.visibility = View.VISIBLE
- }
- class YValueRenderer(viewPortHandler: ViewPortHandler?, yAxis: YAxis?, trans: Transformer?) : YAxisRenderer(viewPortHandler, yAxis, trans) {
- override fun renderAxisLabels(context: Canvas) {
- /*guard let yAxis = self.axis as? YAxis else {
- return
- }
- if !yAxis.isEnabled || !yAxis.isDrawLabelsEnabled {
- return
- }*/
- val yAxis = mYAxis
- if (yAxis == null) return
- if (yAxis.isEnabled || !yAxis.isDrawLabelsEnabled) return
- val xoffset = yAxis.xOffset
- // val yoffset = yAxis.labelFont.lineHeight / 2.5 + yAxis.yOffset
- val yoffset = Utils.calcTextHeight(mAxisLabelPaint, "A") / 2.5 + yAxis.yOffset
- val dependency = yAxis.axisDependency
- val labelPosition = yAxis.labelPosition
- var xPos = 0f/*CGFloat(0.0)*/
- val textAlign = mAxisLabelPaint /*NSTextAlignment*///YAxis.AxisDependency
- if (dependency == YAxis.AxisDependency.LEFT) {
- if (labelPosition == YAxis.YAxisLabelPosition.OUTSIDE_CHART) {
- // textAlign = YAxis.AxisDependency.RIGHT
- textAlign.textAlign = Paint.Align.RIGHT
- xPos = mViewPortHandler.offsetLeft() - xoffset
- } else {
- // textAlign = YAxis.AxisDependency.LEFT
- textAlign.textAlign = Paint.Align.LEFT
- xPos = mViewPortHandler.offsetLeft() + xoffset
- }
- } else {
- if (labelPosition == YAxis.YAxisLabelPosition.OUTSIDE_CHART) {
- // textAlign = YAxis.AxisDependency.LEFT
- textAlign.textAlign = Paint.Align.LEFT
- xPos = mViewPortHandler.contentRight() + xoffset
- } else {
- // textAlign = YAxis.AxisDependency.RIGHT
- textAlign.textAlign = Paint.Align.RIGHT
- xPos = mViewPortHandler.contentRight() - xoffset
- }
- }
- val fixedPosition = xPos
- val positions = transformedPositions
- // val offset = yoffset - yAxis.labelFont.lineHeight
- val offset = 0
- // var from = yAxis.isDrawBottomYLabelEntryEnabled ? 0 : 1
- var from = -1
- if (yAxis.isDrawBottomYLabelEntryEnabled) from = 0
- else from = 1
- var to = -1
- if (yAxis.isDrawTopYLabelEntryEnabled) to = yAxis.mEntryCount
- else to = yAxis.mEntryCount - 1
- // val to = yAxis.isDrawTopYLabelEntryEnabled ? yAxis.entryCount : (yAxis.entryCount - 1)
- // val attributes = [NSAttributedStringKey.font: labelFont]
- val col = /*yLabelBackgroundColor*/Color.LTGRAY
- for (i in from..to step 1) {
- // for i in stride(from: from, to: to, by: 1) {
- val text = yAxis.getFormattedLabel(i)
- var point = /*CGPoint*/MPPointF(fixedPosition, positions[i] + offset)
- // val textSize = text.size(withAttributes: attributes)
- // val textSize = text.length
- // val textSize = textAlign.textSize
- val textSize = text.length
- if (textAlign == Paint.Align.CENTER) {
- point.x -= textSize/*.width*/ / 2.0f
- } else if (textAlign == YAxis.AxisDependency.RIGHT) {
- point.x -= textSize/*.width*/
- }
- val rect = RectF(point.x, point.y, 4f, 0f)
- val path = Path()
- path.addRect(rect, Path.Direction.CW)
- path.rewind()
- // UIGraphicsPushContext(context)
- // col.set()
- // path.fill()
- // UIGraphicsPopContext()
- /*val rect = CGRect(point, textSize).insetBy(-yLabelRectInset, 0)
- val path = UIBezierPath.init(rect, yLabelRectInset)
- UIGraphicsPushContext(context)
- col.set()
- path.fill()
- UIGraphicsPopContext()*/
- }
- super.renderAxisLabels(context)
- }
- }
- class XValueRenderer(viewPortHandler: ViewPortHandler?, xAxis: XAxis?, trans: Transformer?) : XAxisRenderer(viewPortHandler, xAxis, trans) {
- val birthStr = "Birthday"/*CommonStrings.Units.birthday.localized*/
- val birthImgSize = 15f/*CGFloat(15)*/
- // val birthImg = R.drawable.star
- // birthImg
- // private(set) var birthImg:UIImage? = nil
- // var birthColor: UIColor? = nil {
- // didSet {
- // if let col = birthColor, let img = UIImage(named: "birth_image") {
- // birthImg = img.imageWithColor(color: col)
- // } else {
- // birthImg = nil
- // }
- // }
- // }
- override fun drawLabel(context: Canvas, formattedLabel: String, x: Float, y: Float, anchor: MPPointF, angleRadians: Float) {
- if (formattedLabel == "kBirthday") {
- Timber.e("drawLabel formattedLabel == kBirthday")
- // var mAttributes = attributes
- //
- // if let col = birthColor {
- // mAttributes[NSAttributedStringKey.foregroundColor] = col
- // }
- // if let font = mAttributes[NSAttributedStringKey.font] as? UIFont {
- // let name = font.fontName.replacingOccurrences(of: "Regular", with: "SemiBold")
- // mAttributes[NSAttributedStringKey.font] = UIFont(name: name, size: font.pointSize) ?? font
- // }
- //
- // if let img = birthImg {
- // UIGraphicsPushContext(context)
- // img.draw(in: CGRect(x: x - birthImgSize/2, y: viewPortHandler.contentBottom - birthImgSize/2, width: birthImgSize, height: birthImgSize))
- // UIGraphicsPopContext()
- // }
- super.drawLabel(context, formattedLabel, x, y, anchor, angleRadians)
- return
- }
- super.drawLabel(context, formattedLabel, x, y, anchor, angleRadians)
- }
- }
- class XValueFormatter: IndexAxisValueFormatter() {
- override fun getFormattedValue(value: Float, axis: AxisBase?): String {
- if (value == 0f)
- return "kBirthday"
- if (value < 1)
- return ""
- val i = value.toInt()
- return i.toString().plus(" weeks")
- // return value.toString()
- }
- }
- class YValueFormatter: IndexAxisValueFormatter() {
- override fun getFormattedValue(value: Float, axis: AxisBase?): String {
- var s = value.toInt().toString()
- // if (s.count() > 3) {
- // s.plus(" 000 g")
- // }
- s = s.plus(" g")
- var stroke = ""
- if (value != 0f) {
- stroke = s.substring(0, 1).plus(".").plus(s.substring(1, s.length))
- }
- else {
- stroke = s.substring(0,1).plus(s.substring(1, s.length))
- }
- // return s.plus(" g")
- return stroke
- }
- }
- /**
- *
- * @return yyyy-MM-dd HH:mm:ss format date as string
- */
- private fun getCurrentTimeStamp(): String? {
- return try {
- val dateFormat = SimpleDateFormat("yyyy-MM-dd_HH:mm:ss")
- dateFormat.format(Date())
- } catch (e: Exception) {
- e.printStackTrace()
- null
- }
- }
- private fun setDataP3() : LineDataSet {
- val values2 = ArrayList<Entry>()
- // val age = percentileBabyScales[0]
- // val age = 20
- for (i in 0 until /*percentileBabyScales.size*//*age*/scales) {
- // val value = (Math.random() * range).toFloat() + 13
- values2.add(Entry(i.toFloat(), percentileP3[i].toFloat(), resources.getDrawable(R.drawable.star)))
- }
- val set2 = LineDataSet(values2, "P3")
- set2.setDrawIcons(false)
- // set the line to be drawn like this "- - - - - -"
- set2.enableDashedLine(10f, 10f, 0f)
- set2.enableDashedHighlightLine(10f, 10f, 0f)
- set2.color = Color.LTGRAY
- set2.setCircleColor(Color.LTGRAY)
- set2.lineWidth = 1f
- set2.circleRadius = 2f
- set2.setDrawCircles(false)
- set2.setDrawCircleHole(false)
- set2.setDrawValues(false)
- set2.setDrawFilled(false)
- set2.formLineWidth = 1f
- // set2.formLineDashEffect = DashPathEffect(floatArrayOf(10f, 5f), 0f)
- set2.formSize = 15f
- // dataSets.add(set2) // add the datasets
- return set2
- }
- private fun setDataP15() : LineDataSet {
- val values2 = ArrayList<Entry>()
- for (i in 0 until /*age*/scales) {
- values2.add(Entry(i.toFloat(), percentileP15[i].toFloat(), resources.getDrawable(R.drawable.star)))
- }
- val set2 = LineDataSet(values2, "P15")
- set2.setDrawIcons(false)
- // set the line to be drawn like this "- - - - - -"
- set2.enableDashedLine(10f, 10f, 0f)
- set2.enableDashedHighlightLine(10f, 10f, 0f)
- set2.color = Color.GRAY
- set2.setCircleColor(Color.GRAY)
- set2.lineWidth = 1f
- set2.circleRadius = 2f
- set2.setDrawCircles(false)
- set2.setDrawCircleHole(false)
- set2.setDrawValues(false)
- set2.setDrawFilled(false)
- set2.formLineWidth = 1f
- set2.formSize = 15f
- // dataSets.add(set2) // add the datasets
- return set2
- }
- private fun setDataP50() : LineDataSet {
- val values2 = ArrayList<Entry>()
- for (i in 0 until /*age*/scales) {
- values2.add(Entry(i.toFloat(), percentileP50[i].toFloat(), resources.getDrawable(R.drawable.star)))
- }
- val set2 = LineDataSet(values2, "P50")
- set2.setDrawIcons(false)
- // set the line to be drawn like this "- - - - - -"
- set2.enableDashedLine(10f, 10f, 0f)
- set2.enableDashedHighlightLine(10f, 10f, 0f)
- set2.color = Color.DKGRAY
- set2.setCircleColor(Color.DKGRAY)
- set2.lineWidth = 1f
- set2.circleRadius = 2f
- set2.setDrawCircles(false)
- set2.setDrawCircleHole(false)
- set2.setDrawValues(false)
- set2.setDrawFilled(false)
- set2.formLineWidth = 1f
- set2.formSize = 15f
- // dataSets.add(set2) // add the datasets
- return set2
- }
- private fun setDataP85() : LineDataSet {
- val values2 = ArrayList<Entry>()
- for (i in 0 until /*age*/scales) {
- values2.add(Entry(i.toFloat(), percentileP85[i].toFloat(), resources.getDrawable(R.drawable.star)))
- }
- val set2 = LineDataSet(values2, "P85")
- set2.setDrawIcons(false)
- // set the line to be drawn like this "- - - - - -"
- set2.enableDashedLine(10f, 10f, 0f)
- set2.enableDashedHighlightLine(10f, 10f, 0f)
- set2.color = Color.LTGRAY
- set2.setCircleColor(Color.LTGRAY)
- set2.lineWidth = 1f
- set2.circleRadius = 2f
- set2.setDrawCircles(false)
- set2.setDrawCircleHole(false)
- set2.setDrawValues(false)
- set2.setDrawFilled(false)
- set2.formLineWidth = 1f
- set2.formSize = 15f
- // dataSets.add(set2) // add the datasets
- return set2
- }
- private fun setDataP97() : LineDataSet {
- val values2 = ArrayList<Entry>()
- for (i in 0 until /*age*/scales) {
- values2.add(Entry(i.toFloat(), percentileP97[i].toFloat(), resources.getDrawable(R.drawable.star)))
- }
- val set2 = LineDataSet(values2, "P97")
- set2.setDrawIcons(false)
- // set the line to be drawn like this "- - - - - -"
- set2.enableDashedLine(10f, 10f, 0f)
- set2.enableDashedHighlightLine(10f, 10f, 0f)
- set2.color = Color.LTGRAY
- set2.setCircleColor(Color.LTGRAY)
- set2.lineWidth = 1f
- set2.circleRadius = 2f
- set2.setDrawCircles(false)
- set2.setDrawCircleHole(false)
- set2.setDrawValues(false)
- set2.setDrawFilled(false)
- set2.formLineWidth = 1f
- set2.formSize = 15f
- // dataSets.add(set2) // add the datasets
- return set2
- }
- override fun onChartGestureStart(me: MotionEvent, lastPerformedGesture: ChartTouchListener.ChartGesture) {
- Log.i("Gesture", "START, x: " + me.x + ", y: " + me.y)
- }
- override fun onChartGestureEnd(me: MotionEvent, lastPerformedGesture: ChartTouchListener.ChartGesture) {
- Log.i("Gesture", "END, lastGesture: " + lastPerformedGesture)
- // un-highlight values after the gesture is finished and no single-tap
- if (lastPerformedGesture != ChartTouchListener.ChartGesture.SINGLE_TAP)
- mChart!!.highlightValues(null) // or highlightTouch(null) for callback to onNothingSelected(...)
- }
- override fun onChartLongPressed(me: MotionEvent) {
- Log.i("LongPress", "Chart longpressed.")
- }
- override fun onChartDoubleTapped(me: MotionEvent) {
- Log.i("DoubleTap", "Chart double-tapped.")
- }
- override fun onChartSingleTapped(me: MotionEvent) {
- Log.i("SingleTap", "Chart single-tapped.")
- }
- override fun onChartFling(me1: MotionEvent, me2: MotionEvent, velocityX: Float, velocityY: Float) {
- Log.i("Fling", "Chart flinged. VeloX: $velocityX, VeloY: $velocityY")
- }
- override fun onChartScale(me: MotionEvent, scaleX: Float, scaleY: Float) {
- Log.i("Scale / Zoom", "ScaleX: $scaleX, ScaleY: $scaleY")
- recalculateXLimits()
- }
- override fun onChartTranslate(me: MotionEvent, dX: Float, dY: Float) {
- Log.i("Translate / Move", "dX: $dX, dY: $dY")
- recalculateXLimits()
- }
- private fun recalculateXLimits() {
- val lineChartView = mChart!!
- // val minX = -Double(4.0 / lineChartView.scaleX)
- val minX = -4.0/mChart!!.scaleX
- if (lineChartView.lowestVisibleX < minX) {
- lineChartView.moveViewToX(minX.toFloat())
- // lineChartView.setNeedsDisplay()
- lineChartView.invalidate()
- }
- }
- override fun onValueSelected(e: Entry, h: Highlight) {
- Log.i("Entry selected", e.toString())
- Log.i("LOWHIGH", "low: " + mChart!!.lowestVisibleX + ", high: " + mChart!!.highestVisibleX)
- Log.i("MIN MAX", "xmin: " + mChart!!.xChartMin + ", xmax: " + mChart!!.xChartMax + ", ymin: " + mChart!!.yChartMin + ", ymax: " + mChart!!.yChartMax)
- }
- override fun onNothingSelected() {
- Log.i("Nothing selected", "Nothing selected.")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement