|
| 1 | +package com.xxmassdeveloper.mpchartexample |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.graphics.Color |
| 5 | +import android.graphics.DashPathEffect |
| 6 | +import android.util.Log |
| 7 | +import androidx.core.content.ContextCompat |
| 8 | +import com.github.mikephil.charting.charts.LineChart |
| 9 | +import com.github.mikephil.charting.data.Entry |
| 10 | +import com.github.mikephil.charting.data.LineData |
| 11 | +import com.github.mikephil.charting.data.LineDataSet |
| 12 | +import com.github.mikephil.charting.formatter.IFillFormatter |
| 13 | +import com.github.mikephil.charting.interfaces.dataprovider.LineDataProvider |
| 14 | +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet |
| 15 | +import com.github.mikephil.charting.utils.Utils |
| 16 | + |
| 17 | +class DataTools { |
| 18 | + companion object { |
| 19 | + private const val VAL_COUNT = 45 |
| 20 | + private const val VAL_RANGE = 180f |
| 21 | + |
| 22 | + private val VAL_FIX = arrayOf( |
| 23 | + 94.84043, -19.610321, 34.980606, 137.20502, 3.3113098, 18.506958, |
| 24 | + 72.16055, 36.291832, 135.97142, 122.0381, 85.873055, 68.582016, -13.099461, 85.85466, |
| 25 | + 85.45009, 97.90119, 132.87332, 125.11917, 147.57323, -2.4140186, 28.75475, -17.208168, |
| 26 | + 70.36055, 10.715485, 53.00132, 49.197624, 48.25173, 117.765854, 15.96653, 114.79433, |
| 27 | + 127.782455, 14.049572, 44.2753, 106.75516, 108.96782, 75.33596, 128.22353, -12.423063, |
| 28 | + 44.768654, -25.790316, 5.9754066, 99.64748, 141.99321, -17.990795, 38.272446 |
| 29 | + ) |
| 30 | + |
| 31 | + fun setData(context: Context, lineChart: LineChart, count: Int = VAL_COUNT, range: Float = VAL_RANGE) { |
| 32 | + Log.d("setData", "$count= range=$range") |
| 33 | + val values = ArrayList<Entry>() |
| 34 | + if (count == VAL_COUNT) { |
| 35 | + VAL_FIX.forEachIndexed { index, d -> |
| 36 | + values.add(Entry(index.toFloat(), d.toFloat(), ContextCompat.getDrawable(context, R.drawable.star))) |
| 37 | + } |
| 38 | + } else { |
| 39 | + for (i in 0 until count) { |
| 40 | + val value = (Math.random() * range).toFloat() - 30 |
| 41 | + values.add(Entry(i.toFloat(), value, ContextCompat.getDrawable(context, R.drawable.star))) |
| 42 | + } |
| 43 | + } |
| 44 | + val lineDataSet0: LineDataSet |
| 45 | + if (lineChart.data != null && lineChart.data.dataSetCount > 0) { |
| 46 | + lineDataSet0 = lineChart.data.getDataSetByIndex(0) as LineDataSet |
| 47 | + lineDataSet0.entries = values |
| 48 | + lineDataSet0.notifyDataSetChanged() |
| 49 | + lineChart.data.notifyDataChanged() |
| 50 | + lineChart.notifyDataSetChanged() |
| 51 | + } else { |
| 52 | + // create a dataset and give it a type |
| 53 | + lineDataSet0 = LineDataSet(values, "DataSet 1") |
| 54 | + lineDataSet0.setDrawIcons(false) |
| 55 | + |
| 56 | + // draw dashed line |
| 57 | + lineDataSet0.enableDashedLine(10f, 5f, 0f) |
| 58 | + |
| 59 | + // black lines and points |
| 60 | + lineDataSet0.color = Color.BLACK |
| 61 | + lineDataSet0.setCircleColor(Color.BLACK) |
| 62 | + |
| 63 | + // line thickness and point size |
| 64 | + lineDataSet0.lineWidth = 1f |
| 65 | + lineDataSet0.circleRadius = 3f |
| 66 | + |
| 67 | + // draw points as solid circles |
| 68 | + lineDataSet0.setDrawCircleHole(false) |
| 69 | + |
| 70 | + // customize legend entry |
| 71 | + lineDataSet0.formLineWidth = 1f |
| 72 | + lineDataSet0.formLineDashEffect = DashPathEffect(floatArrayOf(10f, 5f), 0f) |
| 73 | + lineDataSet0.formSize = 15f |
| 74 | + |
| 75 | + // text size of values |
| 76 | + lineDataSet0.valueTextSize = 9f |
| 77 | + |
| 78 | + // draw selection line as dashed |
| 79 | + lineDataSet0.enableDashedHighlightLine(10f, 5f, 0f) |
| 80 | + |
| 81 | + // set the filled area |
| 82 | + lineDataSet0.setDrawFilled(true) |
| 83 | + lineDataSet0.fillFormatter = IFillFormatter { _: ILineDataSet?, _: LineDataProvider? -> |
| 84 | + lineChart.axisLeft.axisMinimum |
| 85 | + } |
| 86 | + |
| 87 | + // set color of filled area |
| 88 | + if (Utils.getSDKInt() >= 18) { |
| 89 | + // drawables only supported on api level 18 and above |
| 90 | + val drawable = ContextCompat.getDrawable(context, R.drawable.fade_red) |
| 91 | + lineDataSet0.fillDrawable = drawable |
| 92 | + } else { |
| 93 | + lineDataSet0.fillColor = Color.BLACK |
| 94 | + } |
| 95 | + val dataSets = ArrayList<ILineDataSet>() |
| 96 | + dataSets.add(lineDataSet0) // add the data sets |
| 97 | + |
| 98 | + // create a data object with the data sets |
| 99 | + val data = LineData(dataSets) |
| 100 | + |
| 101 | + // set data |
| 102 | + lineChart.data = data |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments