|
| 1 | +package info.appdev.chartexample.fragments |
| 2 | + |
| 3 | +import android.graphics.Typeface |
| 4 | +import android.os.Bundle |
| 5 | +import android.util.Log |
| 6 | +import android.view.LayoutInflater |
| 7 | +import android.view.MotionEvent |
| 8 | +import android.view.View |
| 9 | +import android.view.ViewGroup |
| 10 | +import android.widget.FrameLayout |
| 11 | +import androidx.fragment.app.Fragment |
| 12 | +import com.github.mikephil.charting.charts.BarChart |
| 13 | +import com.github.mikephil.charting.listener.ChartTouchListener.ChartGesture |
| 14 | +import com.github.mikephil.charting.listener.OnChartGestureListener |
| 15 | +import info.appdev.chartexample.R |
| 16 | +import info.appdev.chartexample.custom.MyMarkerView |
| 17 | + |
| 18 | +class BarChartFrag : SimpleFragment(), OnChartGestureListener { |
| 19 | + private var chart: BarChart? = null |
| 20 | + |
| 21 | + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { |
| 22 | + val v = inflater.inflate(R.layout.frag_simple_bar, container, false) |
| 23 | + |
| 24 | + // create a new chart object |
| 25 | + chart = BarChart(requireActivity()) |
| 26 | + chart!!.description.isEnabled = false |
| 27 | + chart!!.onChartGestureListener = this |
| 28 | + |
| 29 | + val mv = MyMarkerView(activity, R.layout.custom_marker_view) |
| 30 | + mv.chartView = chart // For bounds control |
| 31 | + chart!!.setMarker(mv) |
| 32 | + |
| 33 | + chart!!.setDrawGridBackground(false) |
| 34 | + chart!!.setDrawBarShadow(false) |
| 35 | + |
| 36 | + val tf = Typeface.createFromAsset(requireContext().assets, "OpenSans-Light.ttf") |
| 37 | + |
| 38 | + chart!!.setData(generateBarData(1, 20000f)) |
| 39 | + |
| 40 | + val l = chart!!.legend |
| 41 | + l.typeface = tf |
| 42 | + |
| 43 | + val leftAxis = chart!!.axisLeft |
| 44 | + leftAxis.typeface = tf |
| 45 | + leftAxis.setAxisMinimum(0f) // this replaces setStartAtZero(true) |
| 46 | + |
| 47 | + chart!!.axisRight.isEnabled = false |
| 48 | + |
| 49 | + val xAxis = chart!!.xAxis |
| 50 | + xAxis.isEnabled = false |
| 51 | + |
| 52 | + // programmatically add the chart |
| 53 | + val parent = v.findViewById<FrameLayout>(R.id.parentLayout) |
| 54 | + parent.addView(chart) |
| 55 | + |
| 56 | + return v |
| 57 | + } |
| 58 | + |
| 59 | + override fun onChartGestureStart(me: MotionEvent?, lastPerformedGesture: ChartGesture?) { |
| 60 | + Log.i("Gesture", "START") |
| 61 | + } |
| 62 | + |
| 63 | + override fun onChartGestureEnd(me: MotionEvent?, lastPerformedGesture: ChartGesture?) { |
| 64 | + Log.i("Gesture", "END") |
| 65 | + chart!!.highlightValues(null) |
| 66 | + } |
| 67 | + |
| 68 | + override fun onChartLongPressed(me: MotionEvent?) { |
| 69 | + Log.i("LongPress", "Chart long pressed.") |
| 70 | + } |
| 71 | + |
| 72 | + override fun onChartDoubleTapped(me: MotionEvent?) { |
| 73 | + Log.i("DoubleTap", "Chart double-tapped.") |
| 74 | + } |
| 75 | + |
| 76 | + override fun onChartSingleTapped(me: MotionEvent?) { |
| 77 | + Log.i("SingleTap", "Chart single-tapped.") |
| 78 | + } |
| 79 | + |
| 80 | + override fun onChartFling(me1: MotionEvent?, me2: MotionEvent?, velocityX: Float, velocityY: Float) { |
| 81 | + Log.i("Fling", "Chart fling. VelocityX: $velocityX, VelocityY: $velocityY") |
| 82 | + } |
| 83 | + |
| 84 | + override fun onChartScale(me: MotionEvent?, scaleX: Float, scaleY: Float) { |
| 85 | + Log.i("Scale / Zoom", "ScaleX: $scaleX, ScaleY: $scaleY") |
| 86 | + } |
| 87 | + |
| 88 | + override fun onChartTranslate(me: MotionEvent?, dX: Float, dY: Float) { |
| 89 | + Log.i("Translate / Move", "dX: $dX, dY: $dY") |
| 90 | + } |
| 91 | + |
| 92 | + companion object { |
| 93 | + fun newInstance(): Fragment { |
| 94 | + return BarChartFrag() |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments