Advertisement
eveeeeef21

Note TouchedChart

May 16th, 2025 (edited)
722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.27 KB | Source Code | 0 0
  1. Class OverviewFRagment:
  2. is Result.Success -> {
  3.                     val priceList = result.data.prices.mapNotNull {
  4.                         it.getOrNull(1)?.toFloat()
  5.                     }
  6.                     Log.d("ChartData", "Processed priceList: $priceList")
  7.                     if (priceList.isNotEmpty()) {
  8.                         ChartFormatter.setupMarketChartNew(
  9.                             requireContext(),
  10.                             binding.chartView,
  11.                             priceList,
  12.                             result.data.priceChangePercentage24h,
  13.                             binding.tvPrice,
  14.                             binding.tvPriceChange
  15.                         )
  16.                     } else {
  17.                         binding.chartView.clear()
  18.                         Log.w("ChartData", "Chart data kosong")
  19.                     }
  20.                 }
  21. Class Utils ChartFormatter :
  22. fun setupMarketChartNew(
  23.         context: Context,
  24.         chart: LineChart,
  25.         priceList: List<Float>,
  26.         change7d: Double?,
  27.         priceTextView: TextView, // TextView yang akan di-update saat disentuh
  28.         percentageTextView: TextView
  29.     ) {
  30.         val entries = priceList.mapIndexed { index, price ->
  31.             Entry(index.toFloat(), price)
  32.         }
  33.  
  34.         val color = if ((change7d ?: 0.0) >= 0.0) Color.GREEN else Color.RED
  35.  
  36.         val dataSet = LineDataSet(entries, "Price").apply {
  37.             this.color = color
  38.             setDrawCircles(false)
  39.             lineWidth = 1.5f
  40.             setDrawValues(false)
  41.             mode = LineDataSet.Mode.CUBIC_BEZIER
  42.             setDrawFilled(true)
  43.             this.fillColor = color
  44.             fillAlpha = 80
  45.             setDrawHighlightIndicators(false)
  46.         }
  47.  
  48.         val lineData = LineData(dataSet)
  49.         chart.data = lineData
  50.  
  51.         // Tambah ATH dan ATL
  52.         val athPrice = priceList.maxOrNull()
  53.         val atlPrice = priceList.minOrNull()
  54.  
  55.         val athLine = LimitLine(athPrice!!, "ATH: $athPrice").apply {
  56.             lineColor = Color.TRANSPARENT
  57.             textColor = if (isUsingDarkTheme(context)) Color.WHITE else Color.BLACK
  58.             textSize = 10f
  59.             labelPosition = LimitLine.LimitLabelPosition.RIGHT_TOP
  60.         }
  61.  
  62.         val atlLine = LimitLine(atlPrice!!, "ATL: $atlPrice").apply {
  63.             lineColor = Color.TRANSPARENT
  64.             textColor = if (isUsingDarkTheme(context)) Color.WHITE else Color.BLACK
  65.             textSize = 10f
  66.             labelPosition = LimitLine.LimitLabelPosition.RIGHT_BOTTOM
  67.         }
  68.  
  69.         chart.axisLeft.removeAllLimitLines()
  70.         chart.axisLeft.addLimitLine(athLine)
  71.         chart.axisLeft.addLimitLine(atlLine)
  72.  
  73.         // Legend dan deskripsi
  74.         chart.legend.isEnabled = false
  75.         chart.description.isEnabled = false
  76.  
  77.         // Aktifkan interaksi
  78.         chart.setTouchEnabled(true)
  79.         chart.isDragEnabled = true
  80.         chart.setScaleEnabled(false)
  81.         chart.setPinchZoom(false)
  82.         chart.isHighlightPerTapEnabled = true
  83.  
  84.         // Listener untuk update harga saat disentuh
  85.         chart.setOnChartValueSelectedListener(object : OnChartValueSelectedListener {
  86.             override fun onValueSelected(e: Entry?, h: Highlight?) {
  87.                 if (e != null) {
  88.                     val selectedPrice = e.y
  89.                     val selectedPricePercentage = (selectedPrice / priceList.last()) * 100
  90.                     priceTextView.text = "$${String.format("%.2f", selectedPrice)}"
  91.                     percentageTextView.text = "%.2f%%".format(selectedPricePercentage)
  92.                 }
  93.             }
  94.  
  95.             override fun onNothingSelected() {
  96.                 priceTextView.text = "$${String.format("%.2f", priceList.last())}"
  97.             }
  98.  
  99.         })
  100.  
  101.         // Axis kiri (harga)
  102.         chart.axisLeft.apply {
  103.             isEnabled = true
  104.             textColor = ContextCompat.getColor(
  105.                 context,
  106.                 if (isUsingDarkTheme(context)) android.R.color.white else R.color.detailToolbarDescText
  107.             )
  108.             setDrawGridLines(false)
  109.             setDrawAxisLine(false)
  110.         }
  111.  
  112.         // Axis kanan dan bawah
  113.         chart.axisRight.isEnabled = false
  114.         chart.xAxis.isEnabled = false
  115.  
  116.         chart.invalidate()
  117.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement