|
| 1 | +package info.appdev.chartexample |
| 2 | + |
| 3 | +import android.content.Intent |
| 4 | +import android.graphics.Color |
| 5 | +import android.os.Bundle |
| 6 | +import android.view.Menu |
| 7 | +import android.view.MenuItem |
| 8 | +import androidx.core.net.toUri |
| 9 | +import info.appdev.chartexample.DataTools.Companion.generateSineWaves |
| 10 | +import info.appdev.chartexample.databinding.ActivityLinechartNoseekbarBinding |
| 11 | +import info.appdev.chartexample.formatter.UnixTimeAxisValueFormatter |
| 12 | +import info.appdev.chartexample.notimportant.DemoBase |
| 13 | +import info.appdev.charting.components.Description |
| 14 | +import info.appdev.charting.components.Legend.LegendForm |
| 15 | +import info.appdev.charting.components.XAxis.XAxisPosition |
| 16 | +import info.appdev.charting.components.YAxis |
| 17 | +import info.appdev.charting.data.Entry |
| 18 | +import info.appdev.charting.data.LineData |
| 19 | +import info.appdev.charting.data.LineDataSet |
| 20 | +import info.appdev.charting.formatter.IFillFormatter |
| 21 | +import info.appdev.charting.interfaces.dataprovider.LineDataProvider |
| 22 | +import info.appdev.charting.interfaces.datasets.ILineDataSet |
| 23 | + |
| 24 | +class TimeLineActivity : DemoBase() { |
| 25 | + private lateinit var binding: ActivityLinechartNoseekbarBinding |
| 26 | + |
| 27 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 28 | + super.onCreate(savedInstanceState) |
| 29 | + binding = ActivityLinechartNoseekbarBinding.inflate(layoutInflater) |
| 30 | + setContentView(binding.root) |
| 31 | + |
| 32 | + binding.chart1.setBackgroundColor(Color.WHITE) |
| 33 | + binding.chart1.setDrawGridBackground(true) |
| 34 | + |
| 35 | + binding.chart1.setDrawBorders(true) |
| 36 | + |
| 37 | + // no description text |
| 38 | + binding.chart1.description = Description().apply { |
| 39 | + text = "Sinus Time Line" |
| 40 | + } |
| 41 | + binding.chart1.description.isEnabled = true |
| 42 | + |
| 43 | + binding.chart1.xAxis.apply { |
| 44 | +// enableGridDashedLine(10f, 10f, 0f) |
| 45 | +// this.position = XAxis.XAxisPosition.BOTH_SIDED |
| 46 | + isEnabled = true |
| 47 | + position = XAxisPosition.BOTTOM |
| 48 | + typeface = tfLight |
| 49 | + labelRotationAngle = 45f |
| 50 | + setDrawGridLines(false) |
| 51 | +// granularity = 1f // only intervals of 1 day |
| 52 | + labelCount = 7 |
| 53 | + valueFormatter = UnixTimeAxisValueFormatter("yyyy-MM-dd HH:mm:ss") |
| 54 | + } |
| 55 | + |
| 56 | + // if disabled, scaling can be done on x- and y-axis separately |
| 57 | + binding.chart1.setPinchZoom(false) |
| 58 | + |
| 59 | + binding.chart1.legend.apply { |
| 60 | + isEnabled = false |
| 61 | + form = LegendForm.LINE |
| 62 | + } |
| 63 | + |
| 64 | + binding.chart1.axisLeft.apply { |
| 65 | + axisMaximum = 150f |
| 66 | + axisMinimum = -50f |
| 67 | + setDrawAxisLine(true) |
| 68 | + setDrawZeroLine(true) |
| 69 | + setDrawGridLines(true) |
| 70 | + } |
| 71 | + |
| 72 | + binding.chart1.axisRight.isEnabled = false |
| 73 | + |
| 74 | + setData(60f, TIME_OFFSET) |
| 75 | + |
| 76 | + binding.chart1.invalidate() |
| 77 | + } |
| 78 | + |
| 79 | + @Suppress("SameParameterValue") |
| 80 | + private fun setData(range: Float, timeOffset: Long) { |
| 81 | + |
| 82 | + val sampleEntries = generateSineWaves(3, 30) |
| 83 | + .mapIndexed { index, data -> |
| 84 | + val valueY = (data.toFloat() * range) + 50 |
| 85 | + Entry(timeOffset + index.toFloat() * 1000, valueY) |
| 86 | + }.toMutableList() |
| 87 | + |
| 88 | + val set1: LineDataSet |
| 89 | + |
| 90 | + if (binding.chart1.lineData.dataSetCount > 0) { |
| 91 | + set1 = binding.chart1.lineData.getDataSetByIndex(0) as LineDataSet |
| 92 | + set1.entries = sampleEntries |
| 93 | + binding.chart1.lineData.notifyDataChanged() |
| 94 | + binding.chart1.notifyDataSetChanged() |
| 95 | + } else { |
| 96 | + // create a dataset and give it a type |
| 97 | + set1 = LineDataSet(sampleEntries, "DataSet 1") |
| 98 | + |
| 99 | + set1.axisDependency = YAxis.AxisDependency.LEFT |
| 100 | + set1.color = Color.rgb(255, 241, 46) |
| 101 | + set1.isDrawCirclesEnabled = false |
| 102 | + set1.lineWidth = 2f |
| 103 | + set1.circleRadius = 3f |
| 104 | + set1.fillAlpha = 255 |
| 105 | + set1.isDrawFilledEnabled = true |
| 106 | + set1.fillColor = Color.WHITE |
| 107 | + set1.highLightColor = Color.rgb(244, 117, 117) |
| 108 | + set1.isDrawCircleHoleEnabled = false |
| 109 | + set1.fillFormatter = object : IFillFormatter { |
| 110 | + override fun getFillLinePosition(dataSet: ILineDataSet?, dataProvider: LineDataProvider): Float { |
| 111 | + // change the return value here to better understand the effect |
| 112 | + // return 0; |
| 113 | + return binding.chart1.axisLeft.axisMinimum |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + val dataSets = ArrayList<ILineDataSet>() |
| 118 | + dataSets.add(set1) // add the data sets |
| 119 | + |
| 120 | + // create a data object with the data sets |
| 121 | + val data = LineData(dataSets) |
| 122 | + data.setDrawValues(false) |
| 123 | + |
| 124 | + binding.chart1.setData(data) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + override fun onCreateOptionsMenu(menu: Menu?): Boolean { |
| 129 | + menuInflater.inflate(R.menu.only_github, menu) |
| 130 | + return true |
| 131 | + } |
| 132 | + |
| 133 | + override fun onOptionsItemSelected(item: MenuItem): Boolean { |
| 134 | + when (item.itemId) { |
| 135 | + R.id.viewGithub -> { |
| 136 | + val i = Intent(Intent.ACTION_VIEW) |
| 137 | + i.data = |
| 138 | + "https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/info/appdev/chartexample/FilledLineActivity.kt".toUri() |
| 139 | + startActivity(i) |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return true |
| 144 | + } |
| 145 | + |
| 146 | + public override fun saveToGallery() = Unit // Intentionally left empty |
| 147 | + |
| 148 | + companion object { |
| 149 | + const val TIME_OFFSET = 1767105583L |
| 150 | + } |
| 151 | +} |
0 commit comments