Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Class OverviewFRagment:
- is Result.Success -> {
- val priceList = result.data.prices.mapNotNull {
- it.getOrNull(1)?.toFloat()
- }
- Log.d("ChartData", "Processed priceList: $priceList")
- if (priceList.isNotEmpty()) {
- ChartFormatter.setupMarketChartNew(
- requireContext(),
- binding.chartView,
- priceList,
- result.data.priceChangePercentage24h,
- binding.tvPrice,
- binding.tvPriceChange
- )
- } else {
- binding.chartView.clear()
- Log.w("ChartData", "Chart data kosong")
- }
- }
- Class Utils ChartFormatter :
- fun setupMarketChartNew(
- context: Context,
- chart: LineChart,
- priceList: List<Float>,
- change7d: Double?,
- priceTextView: TextView, // TextView yang akan di-update saat disentuh
- percentageTextView: TextView
- ) {
- val entries = priceList.mapIndexed { index, price ->
- Entry(index.toFloat(), price)
- }
- val color = if ((change7d ?: 0.0) >= 0.0) Color.GREEN else Color.RED
- val dataSet = LineDataSet(entries, "Price").apply {
- this.color = color
- setDrawCircles(false)
- lineWidth = 1.5f
- setDrawValues(false)
- mode = LineDataSet.Mode.CUBIC_BEZIER
- setDrawFilled(true)
- this.fillColor = color
- fillAlpha = 80
- setDrawHighlightIndicators(false)
- }
- val lineData = LineData(dataSet)
- chart.data = lineData
- // Tambah ATH dan ATL
- val athPrice = priceList.maxOrNull()
- val atlPrice = priceList.minOrNull()
- val athLine = LimitLine(athPrice!!, "ATH: $athPrice").apply {
- lineColor = Color.TRANSPARENT
- textColor = if (isUsingDarkTheme(context)) Color.WHITE else Color.BLACK
- textSize = 10f
- labelPosition = LimitLine.LimitLabelPosition.RIGHT_TOP
- }
- val atlLine = LimitLine(atlPrice!!, "ATL: $atlPrice").apply {
- lineColor = Color.TRANSPARENT
- textColor = if (isUsingDarkTheme(context)) Color.WHITE else Color.BLACK
- textSize = 10f
- labelPosition = LimitLine.LimitLabelPosition.RIGHT_BOTTOM
- }
- chart.axisLeft.removeAllLimitLines()
- chart.axisLeft.addLimitLine(athLine)
- chart.axisLeft.addLimitLine(atlLine)
- // Legend dan deskripsi
- chart.legend.isEnabled = false
- chart.description.isEnabled = false
- // Aktifkan interaksi
- chart.setTouchEnabled(true)
- chart.isDragEnabled = true
- chart.setScaleEnabled(false)
- chart.setPinchZoom(false)
- chart.isHighlightPerTapEnabled = true
- // Listener untuk update harga saat disentuh
- chart.setOnChartValueSelectedListener(object : OnChartValueSelectedListener {
- override fun onValueSelected(e: Entry?, h: Highlight?) {
- if (e != null) {
- val selectedPrice = e.y
- val selectedPricePercentage = (selectedPrice / priceList.last()) * 100
- priceTextView.text = "$${String.format("%.2f", selectedPrice)}"
- percentageTextView.text = "%.2f%%".format(selectedPricePercentage)
- }
- }
- override fun onNothingSelected() {
- priceTextView.text = "$${String.format("%.2f", priceList.last())}"
- }
- })
- // Axis kiri (harga)
- chart.axisLeft.apply {
- isEnabled = true
- textColor = ContextCompat.getColor(
- context,
- if (isUsingDarkTheme(context)) android.R.color.white else R.color.detailToolbarDescText
- )
- setDrawGridLines(false)
- setDrawAxisLine(false)
- }
- // Axis kanan dan bawah
- chart.axisRight.isEnabled = false
- chart.xAxis.isEnabled = false
- chart.invalidate()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement