andyshon

WhoGraphActivity-pre-clean

Sep 21st, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 28.14 KB | None | 0 0
  1. package com.doneit.emiltonia.ui.who.graph
  2.  
  3. import android.Manifest
  4. import android.content.Intent
  5. import android.content.pm.PackageManager
  6. import android.graphics.*
  7. import android.net.Uri
  8. import android.os.Build
  9. import android.os.Bundle
  10. import android.provider.MediaStore
  11. import android.support.annotation.RequiresApi
  12. import android.support.design.widget.FloatingActionButton
  13. import android.support.v4.app.ActivityCompat
  14. import android.util.Log
  15. import android.view.MotionEvent
  16. import android.view.View
  17. import android.widget.Toast
  18. import com.doneit.emiltonia.Const
  19.  
  20. import com.doneit.emiltonia.R
  21. import com.doneit.emiltonia.data.entity.PercentileEntity
  22. import com.doneit.emiltonia.data.entity.ScaleEntity
  23. import com.doneit.emiltonia.ui.base.BaseContract
  24. import com.doneit.emiltonia.ui.base.inject.BaseInjectActivity
  25. import com.github.mikephil.charting.charts.LineChart
  26. import com.github.mikephil.charting.components.*
  27. import com.github.mikephil.charting.data.Entry
  28. import com.github.mikephil.charting.data.LineData
  29. import com.github.mikephil.charting.data.LineDataSet
  30. import com.github.mikephil.charting.formatter.IndexAxisValueFormatter
  31. import com.github.mikephil.charting.interfaces.datasets.ILineDataSet
  32. import com.github.mikephil.charting.listener.ChartTouchListener
  33. import com.github.mikephil.charting.listener.OnChartGestureListener
  34. import com.github.mikephil.charting.renderer.XAxisRenderer
  35. import com.github.mikephil.charting.renderer.YAxisRenderer
  36. import com.github.mikephil.charting.utils.MPPointF
  37. import com.github.mikephil.charting.utils.Transformer
  38. import com.github.mikephil.charting.utils.Utils
  39. import com.github.mikephil.charting.utils.ViewPortHandler
  40. import kotlinx.android.synthetic.main.activity_who_graph.*
  41. import timber.log.Timber
  42. import java.text.SimpleDateFormat
  43. import java.util.*
  44.  
  45. import javax.inject.Inject
  46.  
  47. class WhoGraphActivity : BaseInjectActivity(), WhoGraphContract.View,
  48.         OnChartGestureListener {
  49.  
  50.     private var mChart: LineChart? = null
  51.  
  52.     companion object {
  53.         private const val KEY_EXTRA_BABY_ID = "KEY_EXTRA_BABY_ID"
  54.         private const val DAYS_IN_WEEK = 7
  55.         private var babyGender = Const.Genders.MALE
  56.  
  57.         private var bitmap: Bitmap? = null
  58.     }
  59.  
  60.     @Inject
  61.     lateinit var presenter: WhoGraphPresenter
  62.  
  63.     private var fab: FloatingActionButton? = null
  64.  
  65.     private var babyId: Int = 0
  66.  
  67.     private var babyAge = 0
  68.     private var babyName = ""
  69.     private var scales = 0
  70.  
  71.     private val data = LineData()
  72.  
  73.     private var percentileP3: MutableList<Double> = arrayListOf()
  74.     private var percentileP15: MutableList<Double> = arrayListOf()
  75.     private var percentileP50: MutableList<Double> = arrayListOf()
  76.     private var percentileP85: MutableList<Double> = arrayListOf()
  77.     private var percentileP97: MutableList<Double> = arrayListOf()
  78.  
  79.     private val allPercentiles: MutableList<MutableList<Double>> = arrayListOf()
  80.     private val allPercentilesTitles: MutableList<String> = arrayListOf("P3", "P15", "P50", "P85", "P97")
  81.     private val allDashedLinesLength: MutableList<Float> = arrayListOf(10f,10f,20f,10f,10f)
  82.     private val allDashedLinesSpaceLength: MutableList<Float> = arrayListOf(15f,10f,20f,10f,10f)
  83.  
  84.     private var percentileBabyScales: MutableList<Double> = arrayListOf()
  85.  
  86.     override fun getPresenter(): BaseContract.Presenter<*>? {
  87.         return presenter
  88.     }
  89.  
  90.     override fun showPercentilesList(list: List<PercentileEntity>) {
  91.         list.forEach {
  92.             percentileP3.add(it.p3.toDouble()*1000)
  93.             percentileP15.add(it.p15.toDouble()*1000)
  94.             Timber.e("P15 = ${it.p15}")
  95.             percentileP50.add(it.p50.toDouble()*1000)
  96.             percentileP85.add(it.p85.toDouble()*1000)
  97.             percentileP97.add(it.p97.toDouble()*1000)
  98.         }
  99.         allPercentiles.add(percentileP3)
  100.         allPercentiles.add(percentileP15)
  101.         allPercentiles.add(percentileP50)
  102.         allPercentiles.add(percentileP85)
  103.         allPercentiles.add(percentileP97)
  104.         Timber.d("P3 ${percentileP3.size}")
  105.         Timber.d("P15 ${percentileP15.size}")
  106.         Timber.d("P50 ${percentileP50.size}")
  107.         Timber.d("P85 ${percentileP85.size}")
  108.         Timber.d("P97 ${percentileP97.size}")
  109.     }
  110.  
  111.     @RequiresApi(Build.VERSION_CODES.O)
  112.     override fun showScalesList(list: List<ScaleEntity>) {
  113.         scales = list.size
  114.         Timber.e("scales size = $scales")
  115.         if (list.size > 0) {
  116.             list.forEach {
  117.                 Timber.e("ttttt = ${it.weight}")
  118.                 var weight = it.weight.toDouble()
  119.                 if (weight < 10) {
  120.                     weight *= 1000
  121.                 }
  122.                 else if (weight >= 10 && weight < 100) {
  123.                     weight *= 100
  124.                 }
  125.                 percentileBabyScales.add(/*it.weight.toDouble()*1000*/weight)
  126.             }
  127.         }
  128.         Timber.d("percentileBabyScales ${percentileBabyScales.size}")
  129.  
  130.         Collections.reverse(percentileBabyScales)
  131.  
  132.         setupChart()
  133.         setupBabyLine()
  134.     }
  135.  
  136.  
  137.     @RequiresApi(Build.VERSION_CODES.O)
  138.     override fun onCreate(savedInstanceState: Bundle?) {
  139.         super.onCreate(savedInstanceState)
  140.         setContentView(R.layout.activity_who_graph)
  141.  
  142.         setSupportActionBar(toolbar)
  143.         supportActionBar?.setDisplayShowTitleEnabled(false)
  144.  
  145.         toolbarTitle.setText(R.string.who_graph_title)
  146.         toolbar.setNavigationOnClickListener { onBackPressed() }
  147.         toolbar.setNavigationIcon(R.drawable.ic_back)
  148.  
  149.         babyId = intent.getIntExtra(WhoGraphActivity.KEY_EXTRA_BABY_ID, 0)
  150.         babyGender = intent.getStringExtra("EXTRA_BABY_GENDER")
  151.         babyName = intent.getStringExtra("EXTRA_BABY_NAME")
  152.         babyAge = intent.getIntExtra("EXTRA_BABY_AGE", 0) / DAYS_IN_WEEK
  153.  
  154.         if (babyGender == Const.Genders.MALE) {
  155.             bitmap = BitmapFactory.decodeResource(resources, R.drawable.ic_birthday_graph_male)
  156.         }
  157.         else {
  158.             bitmap = BitmapFactory.decodeResource(resources, R.drawable.ic_birthday_graph_female)
  159.         }
  160.  
  161.         tvBabyName.text = babyName
  162.  
  163.         if (babyGender == Const.Genders.MALE) {
  164.             iv_baby1.setBackgroundResource(R.drawable.bg_dotted_baby_male)
  165.             iv_baby2.setBackgroundResource(R.drawable.bg_dotted_baby_male)
  166.         }
  167.         else {
  168.             iv_baby1.setBackgroundResource(R.drawable.bg_dotted_baby_female)
  169.             iv_baby2.setBackgroundResource(R.drawable.bg_dotted_baby_female)
  170.         }
  171.  
  172.         presentationComponent.inject(this)
  173.         presenter.attachToView(this)
  174.  
  175.         presenter.getAllPercentilesByBabyId(babyId)
  176.  
  177.  
  178.         fab = findViewById(R.id.fab)
  179.         fab?.setOnClickListener {
  180.             shareWhoGraphRequest()
  181.         }
  182.  
  183.         mChart = findViewById(R.id.chart1)
  184.         mChart!!.visibility = View.INVISIBLE
  185.         mChart!!.setNoDataText("")
  186.     }
  187.  
  188.     private fun isStoragePermissionGranted() : Boolean{
  189.         return if (Build.VERSION.SDK_INT >= 23) {
  190.             if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
  191.                     == PackageManager.PERMISSION_GRANTED) {
  192.                 true
  193.             } else {
  194.                 ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 1)
  195.                 false
  196.             }
  197.         } else { //permission is automatically granted on sdk<23 upon installation
  198.             true
  199.         }
  200. }
  201.  
  202.     private fun shareWhoGraphRequest() {
  203.         if (isStoragePermissionGranted()) {
  204.             share()
  205.         }
  206.     }
  207.  
  208.     private fun share() {
  209.         val bitmap = mChart!!.chartBitmap
  210.         val imgName = "Emiltonia_".plus(babyName).plus("_Who_Graph_".plus(getCurrentTimeStamp()))
  211.         val bitmapPath = MediaStore.Images.Media.insertImage(contentResolver, bitmap, imgName, null)
  212.         val bitmapUri = Uri.parse(bitmapPath)
  213.         val intent = Intent()
  214.         intent.action = Intent.ACTION_SEND
  215.         intent.type = "image/*"
  216.  
  217.         intent.putExtra(Intent.EXTRA_STREAM, bitmapUri)
  218.         val shareTitle = "Share ".plus(babyName).plus(" Who Graph")
  219.         startActivity(Intent.createChooser(intent, shareTitle))
  220.     }
  221.  
  222.     override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
  223.         super.onRequestPermissionsResult(requestCode, permissions, grantResults)
  224.  
  225.         if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  226.             share()
  227.         }
  228.     }
  229.  
  230.  
  231.     @RequiresApi(Build.VERSION_CODES.O)
  232.     private fun setupChart() {
  233.  
  234.         mChart!!.onChartGestureListener = this
  235.         mChart!!.setDrawGridBackground(false)
  236.  
  237.         mChart!!.description.isEnabled = false
  238.  
  239.         // enable touch gestures
  240.         mChart!!.setTouchEnabled(true)
  241.  
  242.         // enable scaling and dragging
  243.         mChart!!.isDragEnabled = true
  244.         mChart!!.setScaleEnabled(true)
  245.  
  246.         // if disabled, scaling can be done on x- and y-axis separately
  247.         mChart!!.setPinchZoom(true)
  248.  
  249.         // set an alternative background color
  250.         // mChart.setBackgroundColor(Color.GRAY);
  251.  
  252.         // create a custom MarkerView (extend MarkerView) and specify the layout
  253.         // to use for it
  254. //        val mv = MyMarkerView(this, R.layout.custom_marker_view)
  255. //        mv.setChartView(mChart) // For bounds control
  256. //        mChart!!.marker = mv // Set the marker to the chart
  257.  
  258.         // x-axis limit line
  259. //        val llXAxis = LimitLine(10f, "Index 10")
  260. //        llXAxis.lineWidth = 4f
  261. //        llXAxis.enableDashedLine(10f, 10f, 0f)
  262. //        llXAxis.labelPosition = LimitLine.LimitLabelPosition.RIGHT_BOTTOM
  263. //        llXAxis.textSize = 10f
  264.  
  265. //        val xAxis = mChart!!.xAxis
  266. //        xAxis.enableGridDashedLine(10f, 10f, 0f)
  267.         //xAxis.setValueFormatter(new MyCustomXAxisValueFormatter());
  268.         //xAxis.addLimitLine(llXAxis); // add x-axis limit line
  269.  
  270.  
  271.         val leftAxis = mChart!!.axisLeft
  272.         leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART)
  273.         leftAxis.removeAllLimitLines() // reset all limit lines to avoid overlapping lines
  274.         leftAxis.axisMaximum = 10f
  275.         leftAxis.axisMinimum = 0f
  276. //        leftAxis.yOffset = 20f;
  277.         leftAxis.enableGridDashedLine(30f, 10f, 0f)
  278.         leftAxis.setDrawZeroLine(false)
  279.  
  280.  
  281. //        mChart!!.setVisibleXRange(20f)
  282. //        mChart!!.setVisibleYRange(20f, YAxis.AxisDependency.LEFT)
  283. //        mChart!!.centerViewTo(20f, 50f, YAxis.AxisDependency.LEFT)
  284.  
  285. //        mChart!!.animateX(2500)
  286. //        mChart!!.invalidate();
  287.  
  288.         // get the legend (only possible after setting data)
  289.         val l = mChart!!.legend
  290. //        l.position = Legend.LegendPosition.LEFT_OF_CHART_INSIDE
  291.         mChart!!.xAxis.setDrawLabels(true)
  292.         mChart!!.legend.setDrawInside(false)
  293.         l.form = Legend.LegendForm.LINE
  294.         mChart!!.legend.isEnabled = false
  295. //        l.setDrawInside(true)
  296. //        l.addDashedLine(width: dashWidth, pattern: dashes[0], color: statisticColor)
  297.  
  298.  
  299.         // // dont forget to refresh the drawing
  300. //         mChart!!.invalidate();
  301.  
  302.         mChart!!.setBackgroundColor(Color.WHITE)
  303.  
  304.         mChart!!.setBorderColor(Color.TRANSPARENT)
  305.         mChart!!.isDragXEnabled = true
  306.         mChart!!.isDragYEnabled = true //false
  307.         mChart!!.isScaleXEnabled = true
  308.         mChart!!.isScaleYEnabled = true //false
  309.         mChart!!.viewPortHandler.setMaximumScaleX(5f)
  310.         mChart!!.viewPortHandler.setMaximumScaleY(2f)
  311.  
  312.         val xValueRenderer = XValueRenderer(
  313.                 mChart!!.viewPortHandler,
  314.                 mChart!!.xAxis,
  315.                 mChart!!.rendererXAxis.transformer
  316.         )
  317.         mChart!!.setXAxisRenderer(xValueRenderer)
  318.  
  319.         val xAxis = mChart!!.xAxis
  320.         xAxis.position = XAxis.XAxisPosition.BOTTOM
  321.         xAxis.gridColor = Color.LTGRAY
  322.         xAxis.setDrawGridLines(true)
  323.         xAxis.enableGridDashedLine(30f, 0f, 0f)
  324.         xAxis.textColor = Color.LTGRAY
  325.         xAxis.valueFormatter = XValueFormatter()
  326.         val light = Typeface.createFromAsset(this.assets, "OpenSans-Regular.ttf")
  327.         xAxis.typeface = light
  328. //        xAxis.labelCount = UIScreen.main.bounds.size.width > 375 ? 5 : 4
  329.         xAxis.labelCount = /*5*/4
  330.         xAxis.textColor = Color.BLACK
  331.         xAxis.yOffset = 10f
  332.         mChart!!.setExtraOffsets(0f,0f,0f,10f)
  333.         //todo find out how manipulate with limits
  334. //        xAxis.axisMinimum = 0f/*self.dataProvider.leftXLimit*/
  335. //        xAxis.axisMaximum = 0f/*self.dataProvider.rightXLimit*/
  336.  
  337.         mChart!!.axisRight.isEnabled = false
  338.  
  339. //        mChart!!.setViewPortOffsets(100f,0f,-40f,0f)
  340. //        mChart!!.offsetLeftAndRight(100)
  341. //        mChart!!.setDragOffsetX(50f)
  342.  
  343.  
  344. //        val yValueRenderer = YValueRenderer(
  345. //                mChart!!.viewPortHandler,
  346. //                mChart!!.getAxis(YAxis.AxisDependency.LEFT),
  347. //                mChart!!.rendererXAxis.transformer
  348. //        )
  349. //        mChart!!.rendererLeftYAxis = yValueRenderer
  350.  
  351.         val yAxis = mChart!!.getAxis(YAxis.AxisDependency.LEFT)
  352.         yAxis.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART)
  353.         yAxis.setDrawTopYLabelEntry(false)
  354.         yAxis.setDrawLimitLinesBehindData(false)
  355.         yAxis.axisMinimum = 0f/*self.dataProvider.lowerYLimit*/
  356.         yAxis.axisMaximum = 10000f/*self.dataProvider.upperYLimit*/
  357.         yAxis.labelCount = ((yAxis.axisMaximum - yAxis.axisMinimum).toInt()) / 1000
  358.         yAxis.gridColor = Color.LTGRAY
  359.         yAxis.setDrawGridLines(true)
  360.         yAxis.enableGridDashedLine(30f, 0f, 0f)
  361.         yAxis.textColor = Color.BLACK
  362.         yAxis.valueFormatter = YValueFormatter()
  363. //        yAxis.xOffset = yAxis.xOffset - 4f/*yLabelRectInset*/
  364. //        yAxis.xOffset -= 40f
  365.  
  366.         mChart!!.invalidate()
  367.     }
  368.  
  369.  
  370.     private fun setupBabyLine() {
  371.  
  372.         if (scales > 0) {
  373.  
  374.             val values2 = ArrayList<Entry>()
  375.  
  376.             for (i in 0 until scales) {
  377.                 Timber.e("baby value = ${percentileBabyScales[i].toFloat()}")
  378.                 values2.add(Entry(i.toFloat(), percentileBabyScales[i].toFloat()))
  379.             }
  380.  
  381.             val line = LineDataSet(values2, babyName)
  382.  
  383.             line.setDrawIcons(false)
  384.  
  385.             // set the line to be drawn like this "- - - - - -"
  386.             line.enableDashedLine(10f, 0f, 0f)
  387.             line.enableDashedHighlightLine(10f, 0f, 0f)
  388.  
  389.  
  390.             if (babyGender == Const.Genders.MALE) {
  391.                 line.color = Color.rgb(152, 210, 212)
  392.                 line.setCircleColor(Color.rgb(152, 210, 212))
  393.             } else {
  394.                 line.color = Color.rgb(221, 160, 199)
  395.                 line.setCircleColor(Color.rgb(221, 160, 199))
  396.             }
  397. //        line.setCircleColorHole(line.colors[0])
  398.             line.lineWidth = 3f
  399.             line.circleRadius = 5f
  400.             line.circleHoleRadius = line.circleRadius * 0.8f
  401.             line.axisDependency = YAxis.AxisDependency.LEFT
  402.             line.setDrawValues(true)
  403. //        line.valueTextSize = 10f
  404.             line.setDrawVerticalHighlightIndicator(false)
  405.             line.setDrawHorizontalHighlightIndicator(false)
  406.  
  407.             line.formLineWidth = 1f
  408.             line.formSize = 15f
  409.  
  410.  
  411. //            val data = LineData()
  412.  
  413.             data.addDataSet(line)
  414.             /*data.addDataSet(setDataP3())
  415.             data.addDataSet(setDataP15())
  416.             data.addDataSet(setDataP50())
  417.             data.addDataSet(setDataP85())
  418.             data.addDataSet(setDataP97())*/
  419.             setDataAll()
  420.  
  421.             mChart!!.data = data
  422.             mChart!!.description = null
  423.             mChart!!.invalidate()
  424.  
  425.             fab?.visibility = View.VISIBLE
  426.             mChart!!.visibility = View.VISIBLE
  427.         }
  428.         else {
  429.             Toast.makeText(this@WhoGraphActivity, babyName.plus(" have no scales."), Toast.LENGTH_LONG).show()
  430.         }
  431.     }
  432.  
  433.  
  434.     class YValueRenderer(viewPortHandler: ViewPortHandler?, yAxis: YAxis?, trans: Transformer?) : YAxisRenderer(viewPortHandler, yAxis, trans) {
  435.  
  436.  
  437.         override fun drawYLabels(c: Canvas?, fixedPosition: Float, positions: FloatArray?, offset: Float) {
  438.             super.drawYLabels(c, fixedPosition, positions, offset)
  439.  
  440. //            c!!.drawText("MY_TEXT_111", fixedPosition, fixedPosition, mAxisLabelPaint)
  441.         }
  442.  
  443.         override fun renderAxisLabels(context: Canvas) {
  444.  
  445.             Timber.e("renderAxisLabels go 1")
  446.  
  447.             if (mYAxis == null) return
  448.  
  449.             if (!mYAxis.isEnabled || !mYAxis.isDrawLabelsEnabled) return
  450.  
  451.             Timber.e("renderAxisLabels go 2")
  452.  
  453.             val xoffset = mYAxis.xOffset
  454.             val yoffset = Utils.calcTextHeight(mAxisLabelPaint, "A") / 2.5 + mYAxis.yOffset
  455.             val dependency = mYAxis.axisDependency
  456.             val labelPosition = mYAxis.labelPosition
  457.             var xPos = 0f
  458.             val textAlign = mAxisLabelPaint
  459.  
  460.  
  461.             if (dependency == YAxis.AxisDependency.LEFT) {
  462.                 if (labelPosition == YAxis.YAxisLabelPosition.OUTSIDE_CHART) {
  463.                     textAlign.textAlign = Paint.Align.RIGHT
  464.                     xPos = mViewPortHandler.offsetLeft() - xoffset
  465.                 } else {
  466.                     textAlign.textAlign = Paint.Align.LEFT
  467.                     xPos = mViewPortHandler.offsetLeft() + xoffset
  468.                 }
  469.             } else {
  470.                 if (labelPosition == YAxis.YAxisLabelPosition.OUTSIDE_CHART) {
  471.                     textAlign.textAlign = Paint.Align.LEFT
  472.                     xPos = mViewPortHandler.contentRight() + xoffset
  473.                 } else {
  474.                     textAlign.textAlign = Paint.Align.RIGHT
  475.                     xPos = mViewPortHandler.contentRight() - xoffset
  476.                 }
  477.             }
  478.  
  479.             val fixedPosition = xPos
  480.             val positions = transformedPositions
  481. //            val offset = yoffset - yAxis.labelFont.lineHeight
  482.             val offset = yoffset - Utils.calcTextHeight(mAxisLabelPaint, "B")
  483. //            val offset = 0f
  484.  
  485.             var from = -1
  486.             if (mYAxis.isDrawBottomYLabelEntryEnabled) from = 0
  487.             else from = 1
  488.             var to = -1
  489.             if (mYAxis.isDrawTopYLabelEntryEnabled) to = mYAxis.mEntryCount
  490.             else to = mYAxis.mEntryCount - 1
  491. //            val attributes = [NSAttributedStringKey.font: labelFont]
  492.             val col = /*yLabelBackgroundColor*/Color.LTGRAY
  493.  
  494.             for (i in from..to step 1) {
  495.  
  496.                 val text = mYAxis.getFormattedLabel(i)
  497.                 var point = /*CGPoint*/MPPointF(fixedPosition, ((positions[i] + offset).toFloat()))
  498. //                val textSize = text.size(withAttributes: attributes)
  499. //                val textSize = textAlign.textSize
  500.                 val textSize = textAlign.textSize
  501.  
  502.                 if (textAlign == Paint.Align.CENTER) {
  503.                     point.x -= textSize/*.width*/ / 2.0f
  504.                 } else if (textAlign == YAxis.AxisDependency.RIGHT) {
  505.                     point.x -= textSize/*.width*/
  506.                 }
  507.                 else if (textAlign == YAxis.AxisDependency.LEFT) {
  508.                     point.x += textSize
  509.                 }
  510.  
  511.                 val rect = RectF(point.x, point.y, 4f, 0f)
  512.                 val path = Path()
  513.                 path.addRect(rect, Path.Direction.CCW)
  514.  
  515. //                context.clipOutPath(path)
  516.                 val paint = Paint()
  517. //                paint.color = Color.RED
  518. //                paint.style = Paint.Style.FILL
  519. //                context.drawPaint(paint)
  520.  
  521.                 paint.color = Color.BLACK
  522.                 paint.textSize = 20f
  523. //                context.drawText("Some Text", 100f, 125f, paint)
  524.  
  525. //                context.drawColor(Color.RED)
  526. //                context.drawCircle(100f,100f,30f, paint)
  527. //                path.rewind()
  528.                 context.drawPath(path, paint)
  529.  
  530.  
  531. //                UIGraphicsPushContext(context)
  532. //                col.set()
  533. //                path.fill()
  534. //                UIGraphicsPopContext()
  535.  
  536. //                mAxisLabelPaint.
  537.  
  538.                 /*val rect = CGRect(point, textSize).insetBy(-yLabelRectInset, 0)
  539.                 val path = UIBezierPath.init(rect, yLabelRectInset)
  540.  
  541.                 UIGraphicsPushContext(context)
  542.                 col.set()
  543.                 path.fill()
  544.                 UIGraphicsPopContext()*/
  545.             }
  546.  
  547.             super.renderAxisLabels(context)
  548.         }
  549.     }
  550.  
  551.     class XValueRenderer(viewPortHandler: ViewPortHandler?, xAxis: XAxis?, trans: Transformer?) : XAxisRenderer(viewPortHandler, xAxis, trans) {
  552.  
  553.         override fun drawLabel(context: Canvas, formattedLabel: String, x: Float, y: Float, anchor: MPPointF, angleRadians: Float) {
  554.  
  555.             if (formattedLabel == "kBirthday") {
  556.  
  557.                 context.drawBitmap(bitmap, x-20, y-50, null)
  558.  
  559.                 setBirthdayColor()
  560.  
  561.                 super.drawLabel(context, formattedLabel, x, y, anchor, angleRadians)
  562.                 return
  563.             }
  564.             else {
  565.                 mAxisLabelPaint.color = Color.BLACK
  566.             }
  567.             super.drawLabel(context, formattedLabel, x, y, anchor, angleRadians)
  568.         }
  569.  
  570.         private fun setBirthdayColor() {
  571.             if (babyGender == Const.Genders.MALE) {
  572.                 mAxisLabelPaint.color = Color.rgb(152, 210, 212)
  573.             }
  574.             else {
  575.                 mAxisLabelPaint.color = Color.rgb(221, 160, 199)
  576.             }
  577.         }
  578.     }
  579.  
  580.  
  581.     class XValueFormatter: IndexAxisValueFormatter() {
  582.  
  583.         override fun getFormattedValue(value: Float, axis: AxisBase?): String {
  584.             if (value == 0f)
  585.                 return "kBirthday"
  586.             if (value < 1)
  587.                 return ""
  588.             val i = value.toInt()
  589.             return i.toString().plus(" weeks")
  590.         }
  591.     }
  592.  
  593.     class YValueFormatter: IndexAxisValueFormatter() {
  594.  
  595.         override fun getFormattedValue(value: Float, axis: AxisBase?): String {
  596.             var stroke = value.toInt().toString()
  597.             if (value == 0f) return ""
  598.             else {
  599.                 if (value > 1000f)
  600.                     stroke = stroke.substring(0, 1).plus(" ").plus(stroke.substring(1, stroke.length))
  601.             }
  602.             return stroke.plus(" g")
  603.         }
  604.     }
  605.  
  606.     private fun getCurrentTimeStamp(): String? {
  607.         return try {
  608.             val dateFormat = SimpleDateFormat("yyyy-MM-dd_HH:mm:ss")
  609.             dateFormat.format(Date())
  610.         } catch (e: Exception) {
  611.             e.printStackTrace()
  612.             null
  613.         }
  614.     }
  615.  
  616.  
  617.     private fun setDataP3() : LineDataSet {
  618.         val values2 = ArrayList<Entry>()
  619.  
  620.         for (i in 0 until scales) {
  621.             values2.add(Entry(i.toFloat(), percentileP3[i].toFloat()))
  622.         }
  623.  
  624.         val set = LineDataSet(values2, "P3")
  625.         set.setDrawIcons(false)
  626.  
  627.         set.enableDashedLine(10f, 15f, 0f)
  628.         set.color = Color.GRAY
  629.         set.setCircleColor(Color.GRAY)
  630.         set.lineWidth = 1.5f
  631.         set.circleRadius = 2f
  632.         set.setDrawCircles(false)
  633.         set.setDrawCircleHole(false)
  634.         set.setDrawValues(false)
  635.         set.setDrawFilled(false)
  636.         set.formLineWidth = 1f
  637.         set.formSize = 15f
  638.  
  639.         return  set
  640.     }
  641.  
  642.     private fun setDataAll() {
  643.  
  644.         for ((pos, value) in allPercentiles.withIndex()) {
  645.             val values = ArrayList<Entry>()
  646.  
  647.             for (i in 0 until scales) {
  648.                 values.add(Entry(i.toFloat(), value[i].toFloat()))
  649.             }
  650.  
  651.             val set = LineDataSet(values, allPercentilesTitles[pos])
  652.             set.setDrawIcons(false)
  653.  
  654. //            set.enableDashedLine(10f, 15f, 0f)
  655.             set.enableDashedLine(allDashedLinesLength[pos], allDashedLinesSpaceLength[pos], 0f)
  656.             set.color = Color.GRAY
  657.             set.setCircleColor(Color.GRAY)
  658.             set.lineWidth = 1.5f
  659.             set.circleRadius = 2f
  660.             set.setDrawCircles(false)
  661.             set.setDrawCircleHole(false)
  662.             set.setDrawValues(false)
  663.             set.setDrawFilled(false)
  664.             set.formLineWidth = 1f
  665.             set.formSize = 15f
  666.  
  667.             data.addDataSet(set)
  668.         }
  669.     }
  670.  
  671.     private fun setDataP15() : LineDataSet {
  672.         val values2 = ArrayList<Entry>()
  673.  
  674.         for (i in 0 until scales) {
  675.             values2.add(Entry(i.toFloat(), percentileP15[i].toFloat()))
  676.         }
  677.  
  678.         val set = LineDataSet(values2, "P15")
  679.         set.setDrawIcons(false)
  680.  
  681.         set.enableDashedLine(10f, 10f, 0f)
  682.         set.color = Color.GRAY
  683.         set.setCircleColor(Color.GRAY)
  684.         set.lineWidth = 1.5f
  685.         set.circleRadius = 2f
  686.         set.setDrawCircles(false)
  687.         set.setDrawCircleHole(false)
  688.         set.setDrawValues(false)
  689.         set.setDrawFilled(false)
  690.         set.formLineWidth = 1f
  691.         set.formSize = 15f
  692.  
  693.         return set
  694.     }
  695.  
  696.     private fun setDataP50() : LineDataSet {
  697.         val values2 = ArrayList<Entry>()
  698.  
  699.         for (i in 0 until scales) {
  700.             values2.add(Entry(i.toFloat(), percentileP50[i].toFloat()))
  701.         }
  702.  
  703.         val set = LineDataSet(values2, "P50")
  704.         set.setDrawIcons(false)
  705.  
  706.         set.enableDashedLine(20f, 20f, 0f)
  707.         set.color = Color.GRAY
  708.         set.setCircleColor(Color.GRAY)
  709.         set.lineWidth = 1.5f
  710.         set.circleRadius = 2f
  711.         set.setDrawCircles(false)
  712.         set.setDrawCircleHole(false)
  713.         set.setDrawValues(false)
  714.         set.setDrawFilled(false)
  715.         set.formLineWidth = 1f
  716.         set.formSize = 15f
  717.  
  718.         return set
  719.     }
  720.  
  721.     private fun setDataP85() : LineDataSet {
  722.         val values2 = ArrayList<Entry>()
  723.  
  724.         for (i in 0 until scales) {
  725.             values2.add(Entry(i.toFloat(), percentileP85[i].toFloat()))
  726.         }
  727.  
  728.         val set = LineDataSet(values2, "P85")
  729.         set.setDrawIcons(false)
  730.  
  731.         set.enableDashedLine(10f, 10f, 0f)
  732.         set.color = Color.GRAY
  733.         set.setCircleColor(Color.GRAY)
  734.         set.lineWidth = 1.5f
  735.         set.circleRadius = 2f
  736.         set.setDrawCircles(false)
  737.         set.setDrawCircleHole(false)
  738.         set.setDrawValues(false)
  739.         set.setDrawFilled(false)
  740.         set.formLineWidth = 1f
  741.         set.formSize = 15f
  742.  
  743.         return set
  744.     }
  745.  
  746.     private fun setDataP97() : LineDataSet {
  747.         val values2 = ArrayList<Entry>()
  748.  
  749.         for (i in 0 until scales) {
  750.             values2.add(Entry(i.toFloat(), percentileP97[i].toFloat()))
  751.         }
  752.  
  753.         val set = LineDataSet(values2, "P97")
  754.         set.setDrawIcons(false)
  755.  
  756.         set.enableDashedLine(10f, 10f, 0f)
  757.         set.color = Color.GRAY
  758.         set.setCircleColor(Color.GRAY)
  759.         set.lineWidth = 1.5f
  760.         set.circleRadius = 2f
  761.         set.setDrawCircles(false)
  762.         set.setDrawCircleHole(false)
  763.         set.setDrawValues(false)
  764.         set.setDrawFilled(false)
  765.         set.formLineWidth = 1f
  766.         set.formSize = 15f
  767.  
  768.         return set
  769.     }
  770.  
  771.     override  fun onChartFling(me1: MotionEvent, me2: MotionEvent, velocityX: Float, velocityY: Float) {
  772.         Log.i("Fling", "Chart flinged. VeloX: $velocityX, VeloY: $velocityY")
  773.     }
  774.  
  775.     override   fun onChartScale(me: MotionEvent, scaleX: Float, scaleY: Float) {
  776.         Log.i("Scale / Zoom", "ScaleX: $scaleX, ScaleY: $scaleY")
  777.         recalculateXLimits()
  778.     }
  779.  
  780.     override  fun onChartTranslate(me: MotionEvent, dX: Float, dY: Float) {
  781.         Log.i("Translate / Move", "dX: $dX, dY: $dY")
  782.         recalculateXLimits()
  783.     }
  784.  
  785.     private fun recalculateXLimits() {
  786.  
  787.         val lineChartView = mChart!!
  788.         val minX = -4.0 / mChart!!.scaleX
  789.  
  790.         if (lineChartView.lowestVisibleX < minX) {
  791.             lineChartView.moveViewToX(minX.toFloat())
  792.             lineChartView.invalidate()
  793.         }
  794.     }
  795.  
  796.     override fun onChartGestureEnd(me: MotionEvent?, lastPerformedGesture: ChartTouchListener.ChartGesture?) {
  797.         // no-opt
  798.     }
  799.  
  800.     override fun onChartSingleTapped(me: MotionEvent?) {
  801.         // no-opt
  802.     }
  803.  
  804.     override fun onChartGestureStart(me: MotionEvent?, lastPerformedGesture: ChartTouchListener.ChartGesture?) {
  805.         // no-opt
  806.     }
  807.  
  808.     override fun onChartLongPressed(me: MotionEvent?) {
  809.         // no-opt
  810.     }
  811.  
  812.     override fun onChartDoubleTapped(me: MotionEvent?) {
  813.         // no-opt
  814.     }
  815. }
Advertisement
Add Comment
Please, Sign In to add comment