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.Context
- import android.content.Intent
- import android.content.pm.PackageManager
- import android.graphics.*
- import android.graphics.drawable.Drawable
- import android.net.Uri
- import android.os.Build
- import android.os.Bundle
- import android.provider.MediaStore
- import android.support.annotation.RequiresApi
- import android.support.v4.app.ActivityCompat
- import android.text.SpannableStringBuilder
- import android.text.method.LinkMovementMethod
- 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.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.doneit.emiltonia.utils.extensions.addClickableSpannable
- import com.doneit.emiltonia.utils.extensions.color
- import com.doneit.emiltonia.utils.extensions.drawable
- import com.doneit.emiltonia.utils.extensions.string
- 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.listener.ChartTouchListener
- import com.github.mikephil.charting.listener.OnChartGestureListener
- 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 javax.inject.Inject
- class WhoGraphActivity : BaseInjectActivity(), WhoGraphContract.View,
- OnChartGestureListener {
- companion object {
- private const val KEY_EXTRA_IS_SHARED = "KEY_EXTRA_IS_SHARED"
- private const val KEY_EXTRA_BABY_NAME = "KEY_EXTRA_BABY_NAME"
- private const val KEY_EXTRA_BABY_AGE = "KEY_EXTRA_BABY_AGE"
- private const val KEY_EXTRA_BABY_GENDER = "KEY_EXTRA_BABY_GENDER"
- private const val KEY_EXTRA_BABY_WEIGHT = "KEY_EXTRA_BABY_WEIGHT"
- private const val KEY_EXTRA_BABY_ID = "KEY_EXTRA_BABY_ID"
- private const val DAYS_IN_WEEK = 7
- private var babyGender = Const.Genders.MALE
- private var babyWeight = 0f
- private var bitmap: Bitmap? = null
- private var birthday = ""
- private var weeks = ""
- private var isDE = false
- }
- @Inject
- lateinit var presenter: WhoGraphPresenter
- private var babyId: Int = 0
- private var babyAge = 0
- private var babyName = ""
- private var scales = 0
- private var isShared = false
- private val data = LineData()
- 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 val allPercentiles: MutableList<MutableList<Double>> = arrayListOf()
- private val allDashedLinesLength: MutableList<Float> = arrayListOf(10f,10f,20f,10f,10f)
- private val allDashedLinesSpaceLength: MutableList<Float> = arrayListOf(15f,10f,20f,10f,10f)
- private var percentileBabyScales: MutableList<Double> = arrayListOf()
- private var percentileBabyScales2: MutableList<Double> = arrayListOf()
- override fun getPresenter(): BaseContract.Presenter<*>? {
- return presenter
- }
- override fun showPercentilesList(list: List<PercentileEntity>) {
- list.forEach {
- percentileP3.add(it.p3.toDouble())
- percentileP15.add(it.p15.toDouble())
- percentileP50.add(it.p50.toDouble())
- percentileP85.add(it.p85.toDouble())
- percentileP97.add(it.p97.toDouble())
- }
- Timber.e("SIZE ${percentileP3.size} : ${percentileP3[0]} : ${percentileP15.size} : ${percentileP15[0]}")
- allPercentiles.add(percentileP3)
- allPercentiles.add(percentileP15)
- allPercentiles.add(percentileP50)
- allPercentiles.add(percentileP85)
- allPercentiles.add(percentileP97)
- }
- @RequiresApi(Build.VERSION_CODES.O)
- override fun showScalesList(list: List<ScaleEntity>) {
- scales = list.size
- if (list.isNotEmpty()) {
- list.forEach {
- var weight = it.weight.toDouble()
- if (weight < 10) {
- weight *= 1000
- }
- else if (weight >= 10 && weight < 100) {
- weight *= 10
- }
- percentileBabyScales.add(weight)
- Timber.e("showScalesList date: ${it.createdAt}")
- Timber.e("showScalesList age days: ${it.age} weeks: ${it.age!!/ DAYS_IN_WEEK}")
- percentileBabyScales2.add((it.age!! / DAYS_IN_WEEK).toDouble())
- }
- // percentileBabyScales.add(0.0)
- // percentileBabyScales2.add(0.0)
- Collections.reverse(percentileBabyScales)
- Collections.reverse(percentileBabyScales2)
- }
- if (babyWeight != -1f) {
- // percentileBabyScales.add(0.0)
- // percentileBabyScales2.add(0.0)
- percentileBabyScales2.add(/*percentileBabyScales[0]*/0.0)
- }
- setupChart()
- setupBabyLine()
- }
- @RequiresApi(Build.VERSION_CODES.O)
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_who_graph)
- setSupportActionBar(toolbar)
- supportActionBar?.setDisplayShowTitleEnabled(false)
- toolbarTitle.setText(R.string.who_graph_title)
- toolbar.setNavigationOnClickListener { onBackPressed() }
- toolbar.setNavigationIcon(R.drawable.ic_back)
- babyId = intent.getIntExtra(WhoGraphActivity.KEY_EXTRA_BABY_ID, 0)
- babyGender = intent.getStringExtra(KEY_EXTRA_BABY_GENDER)
- babyWeight = intent.getFloatExtra(KEY_EXTRA_BABY_WEIGHT, -1f)
- babyName = intent.getStringExtra(KEY_EXTRA_BABY_NAME)
- babyAge = intent.getIntExtra(KEY_EXTRA_BABY_AGE, 0) / DAYS_IN_WEEK
- isShared = intent.getBooleanExtra(KEY_EXTRA_IS_SHARED, false)
- if (babyGender == Const.Genders.MALE) {
- bitmap = BitmapFactory.decodeResource(resources, R.drawable.ic_birthday_graph_male)
- }
- else {
- bitmap = BitmapFactory.decodeResource(resources, R.drawable.ic_birthday_graph_female)
- }
- tvBabyName.text = babyName
- // birthday = this string R.string.who_graph_birthday
- weeks = this string R.string.who_graph_weeks
- // weeks = resources.getQuantityString(R.plurals.who_graph_weeks, i)
- isDE = false
- if (tvTip.text.equals("Tip")) {
- birthday = " Birthday"
- }
- else {
- isDE = true
- birthday = " Geburtstag"
- }
- if (babyGender == Const.Genders.MALE) {
- iv_baby_line.setBackgroundResource(R.drawable.bg_dotted_baby_male)
- iv_baby_line2.setBackgroundResource(R.drawable.bg_dotted_baby_male)
- }
- else {
- iv_baby_line.setBackgroundResource(R.drawable.bg_dotted_baby_female)
- iv_baby_line2.setBackgroundResource(R.drawable.bg_dotted_baby_female)
- }
- presentationComponent.inject(this)
- presenter.attachToView(this)
- presenter.getAllPercentilesByBabyId(babyId, isShared)
- fab?.visibility = View.INVISIBLE
- fab?.setOnClickListener {
- shareWhoGraphRequest()
- }
- tvTip.visibility = View.INVISIBLE
- mChart.visibility = View.INVISIBLE
- mChart.setNoDataText("")
- legend1.visibility = View.INVISIBLE
- legend2.visibility = View.INVISIBLE
- legend3.visibility = View.INVISIBLE
- setTipSpan()
- }
- 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() {
- if (isStoragePermissionGranted()) {
- share()
- }
- }
- // Method to draw watermark
- private fun mark(src:Bitmap, watermark:String, location:Point, alpha: Int, size: Int, typeface: Typeface): Bitmap {
- val w = src.width
- val h = src.height
- val result = Bitmap.createBitmap(w, h, src.config)
- val canvas = Canvas(result)
- canvas.drawBitmap(src, 0f, 0f, null)
- val paint = Paint()
- paint.color = this color R.color.colorBlueLight
- paint.alpha = alpha
- paint.style = Paint.Style.FILL
- paint.textSize = size.toFloat()
- paint.isAntiAlias = true
- paint.typeface = typeface
- canvas.drawText(watermark, location.x.toFloat(), location.y.toFloat(), paint)
- var b = BitmapFactory.decodeResource(resources, R.drawable.ic_app_logo)
- val paint2 = Paint()
- paint2.alpha = 200
- b = Bitmap.createScaledBitmap(b, 120, 120, false)
- canvas.drawBitmap(b, 110f, 25f, paint2)
- return result;
- }
- private fun share() {
- val mBitmap = mChart.chartBitmap
- // Bitmap bitmap = createBitmapFromUri(uri);
- val point = Point(250, 100)
- // Creating type face
- val plain = Typeface.createFromAsset(assets, "OpenSans-Regular.ttf")
- val bold = Typeface.create(plain, Typeface.BOLD)
- val newBitmap = mark(mBitmap, "www.emiltonia.de", point, 200, 50, bold)
- // val bitmap = mChart.chartBitmap
- val imgName = "Emiltonia_".plus(babyName).plus("_Who_Graph_".plus(getCurrentTimeStamp()))
- val bitmapPath = MediaStore.Images.Media.insertImage(contentResolver, /*bitmap*/newBitmap, 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(babyName).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.onChartGestureListener = this
- mChart.setDrawGridBackground(false)
- mChart.description.isEnabled = false
- // enable touch gestures
- mChart.setTouchEnabled(true)
- // enable scaling and dragging
- mChart.isDragEnabled = true
- mChart.setScaleEnabled(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
- // 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.yOffset = 20f;
- leftAxis.enableGridDashedLine(30f, 10f, 0f)
- leftAxis.setDrawZeroLine(false)
- // leftAxis.
- // mChart.setVisibleXRange(20f)
- // mChart.setVisibleYRange(20f, YAxis.AxisDependency.LEFT)
- // mChart.centerViewTo(20f, 50f, YAxis.AxisDependency.LEFT)
- // mChart.animateX(2500)
- // mChart.invalidate();
- // get the legend (only possible after setting data)
- val l = mChart.legend
- // l.position = Legend.LegendPosition.LEFT_OF_CHART_INSIDE
- mChart.xAxis.setDrawLabels(true)
- mChart.legend.setDrawInside(false)
- l.form = Legend.LegendForm.LINE
- mChart.legend.isEnabled = false
- // l.setDrawInside(true)
- // l.addDashedLine(width: dashWidth, pattern: dashes[0], color: statisticColor)
- // // dont forget to refresh the drawing
- // mChart.invalidate();
- mChart.setBackgroundColor(Color.WHITE)
- mChart.setBorderColor(Color.TRANSPARENT)
- mChart.isDragXEnabled = true
- mChart.isDragYEnabled = true //false
- mChart.isScaleXEnabled = true
- mChart.isScaleYEnabled = true //false
- mChart.viewPortHandler.setMaximumScaleX(/*5*//*8*/16f)
- mChart.viewPortHandler.setMaximumScaleY(/*10*/2f)
- val xValueRenderer = XValueRenderer(
- mChart.viewPortHandler,
- mChart.xAxis,
- mChart.rendererXAxis.transformer
- )
- 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.textColor = Color.LTGRAY
- xAxis.valueFormatter = XValueFormatter(this@WhoGraphActivity)
- val light = Typeface.createFromAsset(this.assets, "OpenSans-Regular.ttf")
- xAxis.typeface = light
- // xAxis.labelCount = UIScreen.main.bounds.size.width > 375 ? 5 : 4
- xAxis.labelCount = /*5*/4
- xAxis.textColor = Color.BLACK
- xAxis.yOffset = 10f
- xAxis.textSize = 9f
- mChart.setExtraOffsets(0f,0f,0f,10f)
- //todo find out how manipulate with limits
- // xAxis.axisMinimum = 0f/*self.dataProvider.leftXLimit*/
- // xAxis.axisMaximum = 0f/*self.dataProvider.rightXLimit*/
- mChart.axisRight.isEnabled = false
- // mChart.setViewPortOffsets(100f,0f,-40f,0f)
- // mChart.offsetLeftAndRight(100)
- // mChart.setDragOffsetX(50f)
- val yValueRenderer = YValueRenderer(
- mChart.viewPortHandler,
- mChart.getAxis(YAxis.AxisDependency.LEFT),
- mChart.rendererXAxis.transformer
- )
- mChart.rendererLeftYAxis = yValueRenderer
- val yAxis = mChart.getAxis(YAxis.AxisDependency.LEFT)
- yAxis.setPosition(YAxis.YAxisLabelPosition./*INSIDE_CHART*/OUTSIDE_CHART)
- yAxis.setDrawTopYLabelEntry(false)
- yAxis.setDrawLimitLinesBehindData(false)
- yAxis.axisMinimum = 0f/*self.dataProvider.lowerYLimit*/
- yAxis.axisMaximum = 10000f/*self.dataProvider.upperYLimit*/
- yAxis.labelCount = ((yAxis.axisMaximum - yAxis.axisMinimum).toInt()) / 1000
- yAxis.gridColor = Color.LTGRAY
- yAxis.setDrawGridLines(true)
- yAxis.enableGridDashedLine(30f, 0f, 0f)
- yAxis.textColor = Color.BLACK
- yAxis.valueFormatter = YValueFormatter()
- // yAxis.xOffset = yAxis.xOffset - 4f/*yLabelRectInset*/
- // yAxis.xOffset -= 40f
- // mChart.setVisibleXRange(1f, 10f)
- mChart.zoom(3.692f, 1f, 1f, 1f, yAxis.axisDependency)
- // mChart.centerViewTo(10f, 10f, yAxis.axisDependency)
- }
- private fun setTipSpan() {
- val spannableText = SpannableStringBuilder()
- spannableText.append(this string R.string.who_graph_tip_text)
- val registration = this string R.string.who_graph_tip
- spannableText.addClickableSpannable(registration,
- this color R.color.colorAccent
- ) {
- // RegistrationActivity.startActivity(this)
- }
- tvTip.text = spannableText
- tvTip.movementMethod = LinkMovementMethod.getInstance()
- }
- private fun setupBabyLine() {
- if (scales > 0 || babyWeight != -1f) {
- val values2 = ArrayList<Entry>()
- // Timber.e("setupBabyLine scales = $scales")
- var icon: Drawable? = null
- if (babyGender == Const.Genders.MALE) {
- icon = resources.getDrawable(R.drawable.ic_birthday_graph_male)
- }
- else {
- icon = resources.getDrawable(R.drawable.ic_birthday_graph_female)
- }
- if (babyWeight != -1f) {
- // values2.add(Entry(0f, 0f))
- values2.add(Entry(percentileBabyScales2[0].toFloat() ,babyWeight, icon))
- }
- else {
- // values2.add(Entry(percentileBabyScales2[0].toFloat(), percentileBabyScales[0].toFloat(), icon))
- for (i in 0 until scales) {
- // Timber.e("baby value = X: ${percentileBabyScales2[i].toFloat()} Y: ${percentileBabyScales[i].toFloat()}")
- values2.add(Entry(percentileBabyScales2[i].toFloat(), percentileBabyScales[i].toFloat()))
- // values2.add(Entry(i.toFloat(), percentileBabyScales[i].toFloat()))
- }
- values2[0] = Entry(percentileBabyScales2[0].toFloat(), percentileBabyScales[0].toFloat(), icon)
- }
- // Timber.e("BBBB: ${percentileBabyScales2[0].toFloat()} : $babyWeight")
- val line = LineDataSet(values2, babyName)
- // set the line to be drawn like this "- - - - - -"
- line.enableDashedLine(10f, 0f, 0f)
- line.enableDashedHighlightLine(10f, 0f, 0f)
- if (babyGender == 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
- line.circleRadius = 5f
- line.circleHoleRadius = line.circleRadius * 0.8f
- line.axisDependency = YAxis.AxisDependency.LEFT
- line.setDrawValues(false)
- line.setDrawIcons(true)
- // line.valueTextSize = 10f
- line.setDrawVerticalHighlightIndicator(false)
- line.setDrawHorizontalHighlightIndicator(false)
- // line.formLineWidth = 1f
- // line.formSize = 15f
- // line.getEntryForIndex(0).icon = drawable(R.drawable.ic_access)
- // line.mode = LineDataSet.Mode.CUBIC_BEZIER
- data.addDataSet(line)
- setPercentilesData()
- // mChart.moveViewToX(scales.toFloat())
- // mChart.setScaleMinima(10f,1f)
- mChart.data = data
- mChart.data.isHighlightEnabled = false
- mChart.description = null
- // mChart.renderer = MyLineChartRender(mChart, mChart.animator, mChart.viewPortHandler, this@WhoGraphActivity)
- mChart.invalidate()
- fab?.visibility = View.VISIBLE
- tvTip.visibility = View.VISIBLE
- mChart.visibility = View.VISIBLE
- legend1.visibility = View.VISIBLE
- legend2.visibility = View.VISIBLE
- legend3.visibility = View.VISIBLE
- }
- else {
- Toast.makeText(this@WhoGraphActivity, babyName.plus(" have no scales."), Toast.LENGTH_LONG).show()
- }
- }
- class YValueRenderer(viewPortHandler: ViewPortHandler?, yAxis: YAxis?, trans: Transformer?) : YAxisRenderer(viewPortHandler, yAxis, trans) {
- override fun drawYLabels(c: Canvas?, fixedPosition: Float, positions: FloatArray?, offset: Float) {
- super.drawYLabels(c, fixedPosition, positions, offset)
- // c!!.drawText("MY_TEXT_111", fixedPosition, fixedPosition, mAxisLabelPaint)
- // Timber.e("YValueRenderer: Y: ${positions!![0]} : ${positions!![1]} : ${positions!![2]} : ${positions!![3]}")
- for ((key, v) in positions!!.withIndex()) {
- // Timber.e("POS: $v")
- }
- }
- override fun renderAxisLabels(context: Canvas) {
- // Timber.e("renderAxisLabels go 1")
- if (mYAxis == null) return
- if (!mYAxis.isEnabled || !mYAxis.isDrawLabelsEnabled) return
- // Timber.e("renderAxisLabels go 2")
- val xoffset = mYAxis.xOffset
- val yoffset = Utils.calcTextHeight(mAxisLabelPaint, "A") / 2.5 + mYAxis.yOffset
- val dependency = mYAxis.axisDependency
- val labelPosition = mYAxis.labelPosition
- var xPos = 0f
- val textAlign = mAxisLabelPaint
- if (dependency == YAxis.AxisDependency.LEFT) {
- if (labelPosition == YAxis.YAxisLabelPosition.OUTSIDE_CHART) {
- textAlign.textAlign = Paint.Align.RIGHT
- xPos = mViewPortHandler.offsetLeft() - xoffset
- } else {
- textAlign.textAlign = Paint.Align.LEFT
- xPos = mViewPortHandler.offsetLeft() + xoffset
- }
- } else {
- if (labelPosition == YAxis.YAxisLabelPosition.OUTSIDE_CHART) {
- textAlign.textAlign = Paint.Align.LEFT
- xPos = mViewPortHandler.contentRight() + xoffset
- } else {
- textAlign.textAlign = Paint.Align.RIGHT
- xPos = mViewPortHandler.contentRight() - xoffset
- }
- }
- val fixedPosition = xPos
- val positions = transformedPositions
- // val offset = yoffset - yAxis.labelFont.lineHeight
- val offset = yoffset - Utils.calcTextHeight(mAxisLabelPaint, "B")
- // val offset = 0f
- var from = -1
- if (mYAxis.isDrawBottomYLabelEntryEnabled) from = 0
- else from = 1
- var to = -1
- if (mYAxis.isDrawTopYLabelEntryEnabled) to = mYAxis.mEntryCount
- else to = mYAxis.mEntryCount - 1
- // val attributes = [NSAttributedStringKey.font: labelFont]
- val col = Color.LTGRAY
- for (i in from..to step 1) {
- val text = mYAxis.getFormattedLabel(i)
- val point = /*CGPoint*/MPPointF(fixedPosition, ((positions[i] + offset).toFloat()))
- // val textSize = text.size(withAttributes: attributes)
- // val textSize = textAlign.textSize
- val textSize = textAlign.textSize
- if (textAlign == Paint.Align.CENTER) {
- // Timber.e("textAlign == Paint.Align.CENTER")
- point.x -= textSize/*.width*/ / 2.0f
- } else if (textAlign == YAxis.AxisDependency.RIGHT) {
- point.x -= textSize/*.width*/
- }
- else if (textAlign == YAxis.AxisDependency.LEFT) {
- point.x += textSize
- }
- val rect = RectF(point.x, point.y, 4f, 10f)
- val path = Path()
- path.addRect(rect, Path.Direction.CW)
- // context.clipOutPath(path)
- // var paint = Paint()
- // paint.color = Color.RED
- // paint.style = Paint.Style.FILL
- // context.drawPaint(paint)
- // paint.color = Color.GREEN
- // paint.textSize = 20f
- // val paint2 = Paint()
- // paint2.color = Color.GRAY
- // paint2.strokeWidth = 40f
- // paint2.color = Color.argb(0.75f, 0.8f, 0.8f, 0.8f)
- // context.drawLine(point.x-5, point.y+5, point.x+95, point.y+5, paint2)
- // context.drawCircle(point.x+50, point.y, 50f, paint2)
- // context.drawText("Some Text", point.x, point.y, paint)
- // context.drawColor(Color.RED)
- // context.drawCircle(100f,100f,30f, paint)
- // path.rewind()
- // context.drawPath(path, paint)
- // UIGraphicsPushContext(context)
- // col.set()
- // path.fill()
- // UIGraphicsPopContext()
- // mAxisLabelPaint.
- /*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) {
- override fun drawLabel(context: Canvas, formattedLabel: String, x: Float, y: Float, anchor: MPPointF, angleRadians: Float) {
- var bday = ""
- if (!isDE) {
- bday = " Birthday"
- } else bday = " Geburtstag"
- // if (formattedLabel == /*" Birthday"*/bday) {
- // Timber.e("DDD: $bday : $formattedLabel")
- if (formattedLabel == bday) {
- // context.drawBitmap(bitmap, x-20, y-50, null)
- setBirthdayColor()
- // val paint = Paint()
- // paint.color = Color.RED
- // paint.color = Color.argb(0.75f, 0.8f, 0.8f, 0.8f)
- // paint.strokeWidth = 4f
- // context.drawLine(x-5, y+5, x+95, y+5, paint)
- // context.drawLine(x-5, y+5, X1.toFloat(), Y1.toFloat(), paint)
- // Timber.e("POSSS: $X1 : $Y1")
- // context.drawLine(x-5, y+5, X1.toFloat(), Y1.toFloat()*10, paint)
- super.drawLabel(context, formattedLabel, x, y, anchor, angleRadians)
- return
- }
- else {
- mAxisLabelPaint.color = Color.BLACK
- }
- // Timber.e("XValueRenderer: X: $x : Y: $y")
- super.drawLabel(context, formattedLabel, x, y, anchor, angleRadians)
- }
- private fun setBirthdayColor() {
- if (babyGender == Const.Genders.MALE) {
- mAxisLabelPaint.color = Color.rgb(152, 210, 212)
- }
- else {
- mAxisLabelPaint.color = Color.rgb(221, 160, 199)
- }
- }
- }
- class XValueFormatter(val context: Context): IndexAxisValueFormatter() {
- override fun getFormattedValue(value: Float, axis: AxisBase?): String {
- if (value == 0f)
- // return R.string.who_graph_birthday
- // return " Birthday"dd
- return birthday
- if (value < 1)
- return ""
- val i = value.toInt()
- // return i.toString().plus(" weeks")
- // var weeks = ""
- // if (birthday.equals(" Birthday")) {
- // weeks =
- // }
- return context.resources.getQuantityString(R.plurals.who_graph_weeks, i, i)
- // return i.toString().plus(" ").plus(weeks)
- }
- }
- class YValueFormatter: IndexAxisValueFormatter() {
- override fun getFormattedValue(value: Float, axis: AxisBase?): String {
- var stroke = value.toInt().toString()
- if (value == 0f) return ""
- else {
- if (value > 1000f)
- stroke = stroke.substring(0, 1).plus(" ").plus(stroke.substring(1, stroke.length))
- }
- return stroke.plus(" g")
- }
- }
- 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 setPercentilesData() {
- for ((pos, value) in allPercentiles.withIndex()) {
- val values = ArrayList<Entry>()
- // Timber.e("SetAllData until scales = $scales")
- for (i in 0 until /*scales*/96) {
- values.add(Entry(i.toFloat(), value[i].toFloat()))
- }
- val set = LineDataSet(values,"")
- set.setDrawIcons(false)
- set.enableDashedLine(allDashedLinesLength[pos], allDashedLinesSpaceLength[pos], 0f)
- set.color = Color.GRAY
- set.setCircleColor(Color.GRAY)
- set.lineWidth = 1.5f
- set.circleRadius = 2f
- set.setDrawCircles(false)
- set.setDrawCircleHole(false)
- set.setDrawValues(false)
- set.setDrawFilled(false)
- set.formLineWidth = 1f
- set.formSize = 15f
- // set.fillColor = Color.RED
- // set.fillDrawable = this drawable R.drawable.ic_access
- // data.setValueTextColor(Color.RED)
- data.addDataSet(set)
- }
- }
- override fun onChartFling(me1: MotionEvent, me2: MotionEvent, velocityX: Float, velocityY: Float) {
- }
- override fun onChartScale(me: MotionEvent, scaleX: Float, scaleY: Float) {
- recalculateXLimits()
- }
- override fun onChartTranslate(me: MotionEvent, dX: Float, dY: Float) {
- recalculateXLimits()
- }
- private fun recalculateXLimits() {
- val lineChartView = mChart
- val minX = -4.0 / mChart.scaleX
- if (lineChartView.lowestVisibleX < minX) {
- lineChartView.moveViewToX(minX.toFloat())
- lineChartView.invalidate()
- }
- }
- override fun onChartGestureEnd(me: MotionEvent?, lastPerformedGesture: ChartTouchListener.ChartGesture?) {
- // no-opt
- }
- override fun onChartSingleTapped(me: MotionEvent?) {
- // no-opt
- }
- override fun onChartGestureStart(me: MotionEvent?, lastPerformedGesture: ChartTouchListener.ChartGesture?) {
- // no-opt
- }
- override fun onChartLongPressed(me: MotionEvent?) {
- // no-opt
- }
- override fun onChartDoubleTapped(me: MotionEvent?) {
- // no-opt
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement