Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.doneit.emiltonia.ui.who.graph
- import android.graphics.*
- import android.os.Build
- import android.os.Bundle
- import android.support.annotation.RequiresApi
- import android.util.Log
- import android.view.Gravity
- import android.view.MotionEvent
- import android.view.WindowManager
- import android.widget.Toast
- 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.util.*
- import javax.inject.Inject
- class WhoGraphNewActivity : BaseInjectActivity(), WhoGraphContract.View,
- OnChartGestureListener, OnChartValueSelectedListener {
- public 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 babyId: Int = 0
- private var age = 0
- private var baby: BabyEntity? = null
- 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
- age = baby?.age!! / DAYS_IN_WEEK
- age = 26
- }
- 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()
- list.forEach {
- Timber.e("ttttt = ${it.weight}")
- percentileBabyScales.add(it.weight.toDouble()*1000)
- }
- Timber.d("percentileBabyScales ${percentileBabyScales.size}")
- Collections.sort(percentileBabyScales)
- // createSimpleGraph()
- setupChart()
- setupBabyLine()
- }
- private fun createSimpleGraph() {
- setData(percentileP3)
- setDataP3()
- setDataP15()
- setDataP50()
- setDataP85()
- setDataP97()
- val data = LineData(dataSets)
- mChart!!.data = data
- }
- @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)
- mChart = findViewById(R.id.chart1)
- 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
- /*val xLabel = ArrayList<String>()
- for (i in 0..age) {
- var weekString = ""
- // if (i == 0) weekString = "Birthday"
- if (i == 1) weekString = "week"
- else weekString = "weeks"
- xLabel.add(i.toString().plus(" ").plus(weekString))
- }*/
- val xAxis2 = mChart!!.xAxis
- xAxis2.position = XAxis.XAxisPosition.BOTTOM
- xAxis2.setDrawGridLines(true)
- xAxis2.gridColor = Color.LTGRAY
- // xAxis2.gridColor = Color.rgb(241,241,241)
- xAxis2.enableGridDashedLine(30f, 0f, 0f)
- /*xAxis2.setValueFormatter { value, axis ->
- return@setValueFormatter xLabel[value.toInt()]
- }*/
- val left = mChart!!.axisLeft
- left.setDrawGridLines(true)
- left.gridColor = Color.LTGRAY
- left.enableGridDashedLine(30f, 0f, 0f)
- //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
- // 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()*/
- }
- @RequiresApi(Build.VERSION_CODES.O)
- fun setupChart() {
- mChart!!.setBackgroundColor(Color.rgb(0.97f, 0.97f, 0.97f))
- val lineChartView = mChart!!
- // let lineChartView = self.chartView.lineChartView!
- lineChartView.setBackgroundColor(Color.rgb(0.97f, 0.97f, 0.97f))/* = chartBackgroundColor*/
- lineChartView.setBorderColor(Color.rgb(0.60f, 0.60f, 0.60f))/* = chartBorderColor*/
- //lineChartView.borderLineWidth = 1
- lineChartView.setDrawBorders(true)
- // lineChartView.dragXEnabled = true
- // lineChartView.dragYEnabled = true//false
- // lineChartView.scaleXEnabled = true
- // lineChartView.scaleYEnabled = true//false
- lineChartView.viewPortHandler.setMaximumScaleX(4f)
- lineChartView.viewPortHandler.setMaximumScaleY(2f)
- // lineChartView.delegate = self
- var xValueRenderer = XValueRenderer(
- lineChartView.viewPortHandler,
- lineChartView.xAxis,
- lineChartView.rendererXAxis.transformer
- )
- // val baby = self.dataProvider.baby
- // xValueRenderer.birthColor = Color.GREEN // = self.babyChartColor(baby).first!
- lineChartView.setXAxisRenderer(xValueRenderer)
- val xAxis = lineChartView.xAxis
- xAxis.position = XAxis.XAxisPosition.BOTTOM
- // xAxis.gridColor = Color.rgb(0.8f, 0.8f, 0.8f)
- // 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*/
- var yAxis = lineChartView.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.rgb(0.8f,0.8f,0.8f)
- // 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()
- }*/
- 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) {
- 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 set2 = LineDataSet(values2, "P3")
- set2.setDrawIcons(false)
- // set the line to be drawn like this "- - - - - -"
- set2.enableDashedLine(10f, 5f, 0f)
- set2.enableDashedHighlightLine(10f, 5f, 0f)
- set2.color = Color.MAGENTA
- set2.setCircleColor(Color.MAGENTA)
- 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*/
- // 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, 5f, 0f)
- line.enableDashedHighlightLine(10f, 5f, 0f)
- line.color = Color.rgb(152,210,212)
- line.setCircleColor(Color.rgb(152,210,212))
- // 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()
- }
- 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
- }
- }
- private fun setDataP3() : LineDataSet {
- val values2 = ArrayList<Entry>()
- // val age = percentileBabyScales[0]
- // val age = 20
- for (i in 0 until /*percentileBabyScales.size*/age) {
- // 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, 5f, 0f)
- set2.enableDashedHighlightLine(10f, 5f, 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) {
- 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, 5f, 0f)
- set2.enableDashedHighlightLine(10f, 5f, 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.formLineDashEffect = DashPathEffect(floatArrayOf(10f, 5f), 0f)
- 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) {
- 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, 5f, 0f)
- set2.enableDashedHighlightLine(10f, 5f, 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.formLineDashEffect = DashPathEffect(floatArrayOf(10f, 5f), 0f)
- 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) {
- 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, 5f, 0f)
- set2.enableDashedHighlightLine(10f, 5f, 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 setDataP97() : LineDataSet {
- val values2 = ArrayList<Entry>()
- for (i in 0 until age) {
- 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, 5f, 0f)
- set2.enableDashedHighlightLine(10f, 5f, 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 setData(list: MutableList<Double>) {
- Timber.e("setData count = ${list.size}")
- val values = ArrayList<Entry>()
- for (i in 0 until age) {
- values.add(Entry(i.toFloat(), percentileBabyScales[i].toFloat(), resources.getDrawable(R.drawable.star)))
- }
- val set1: LineDataSet
- if (mChart!!.data != null && mChart!!.data.dataSetCount > 0) {
- set1 = mChart!!.data.getDataSetByIndex(0) as LineDataSet
- set1.values = values
- mChart!!.data.notifyDataChanged()
- mChart!!.notifyDataSetChanged()
- } else {
- set1 = LineDataSet(values, "Baby Name")
- set1.setDrawIcons(false)
- // set the line to be drawn like this "- - - - - -"
- set1.enableDashedLine(10f, 5f, 0f)
- set1.enableDashedHighlightLine(10f, 5f, 0f)
- set1.color = Color.BLACK
- set1.setCircleColor(Color.BLACK)
- set1.lineWidth = 1f
- set1.circleRadius = 3f
- // set1.setDrawCircles(true)
- set1.setDrawCircleHole(false)
- set1.setDrawValues(false)
- set1.setDrawFilled(false)
- set1.formLineWidth = 1f
- set1.formLineDashEffect = DashPathEffect(floatArrayOf(10f, 5f), 0f)
- set1.formSize = 5f
- /*set1.lineWidth = 1f
- set1.circleRadius = 3f
- set1.setDrawCircleHole(false)
- set1.valueTextSize = 9f
- set1.setDrawFilled(true)
- set1.formLineWidth = 1f
- set1.formLineDashEffect = DashPathEffect(floatArrayOf(10f, 5f), 0f)
- set1.formSize = 15f*/
- // dataSets.add(set1)
- }
- mChart!!.invalidate()
- }
- 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()
- }
- 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