1 package com.xxmassdeveloper.mpchartexample.fragments; 2 import android.graphics.Typeface; 3 import android.os.Bundle; 4 import androidx.annotation.NonNull; 5 import androidx.fragment.app.Fragment; 6 import android.util.Log; 7 import android.view.LayoutInflater; 8 import android.view.MotionEvent; 9 import android.view.View; 10 import android.view.ViewGroup; 11 import android.widget.FrameLayout; 12 13 import com.github.mikephil.charting.charts.BarChart; 14 import com.github.mikephil.charting.components.Legend; 15 import com.github.mikephil.charting.components.XAxis; 16 import com.github.mikephil.charting.components.YAxis; 17 import com.github.mikephil.charting.listener.ChartTouchListener; 18 import com.github.mikephil.charting.listener.OnChartGestureListener; 19 import com.xxmassdeveloper.mpchartexample.R; 20 import com.xxmassdeveloper.mpchartexample.custom.MyMarkerView; 21 22 23 public class BarChartFrag extends SimpleFragment implements OnChartGestureListener { 24 25 @NonNull newInstance()26 public static Fragment newInstance() { 27 return new BarChartFrag(); 28 } 29 30 private BarChart chart; 31 32 @Override onCreateView(@onNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)33 public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 34 View v = inflater.inflate(R.layout.frag_simple_bar, container, false); 35 36 // create a new chart object 37 chart = new BarChart(getActivity()); 38 chart.getDescription().setEnabled(false); 39 chart.setOnChartGestureListener(this); 40 41 MyMarkerView mv = new MyMarkerView(getActivity(), R.layout.custom_marker_view); 42 mv.setChartView(chart); // For bounds control 43 chart.setMarker(mv); 44 45 chart.setDrawGridBackground(false); 46 chart.setDrawBarShadow(false); 47 48 Typeface tf = Typeface.createFromAsset(context.getAssets(), "OpenSans-Light.ttf"); 49 50 chart.setData(generateBarData(1, 20000, 12)); 51 52 Legend l = chart.getLegend(); 53 l.setTypeface(tf); 54 55 YAxis leftAxis = chart.getAxisLeft(); 56 leftAxis.setTypeface(tf); 57 leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true) 58 59 chart.getAxisRight().setEnabled(false); 60 61 XAxis xAxis = chart.getXAxis(); 62 xAxis.setEnabled(false); 63 64 // programmatically add the chart 65 FrameLayout parent = v.findViewById(R.id.parentLayout); 66 parent.addView(chart); 67 68 return v; 69 } 70 71 @Override onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture)72 public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) { 73 Log.i("Gesture", "START"); 74 } 75 76 @Override onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture)77 public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) { 78 Log.i("Gesture", "END"); 79 chart.highlightValues(null); 80 } 81 82 @Override onChartLongPressed(MotionEvent me)83 public void onChartLongPressed(MotionEvent me) { 84 Log.i("LongPress", "Chart long pressed."); 85 } 86 87 @Override onChartDoubleTapped(MotionEvent me)88 public void onChartDoubleTapped(MotionEvent me) { 89 Log.i("DoubleTap", "Chart double-tapped."); 90 } 91 92 @Override onChartSingleTapped(MotionEvent me)93 public void onChartSingleTapped(MotionEvent me) { 94 Log.i("SingleTap", "Chart single-tapped."); 95 } 96 97 @Override onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY)98 public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) { 99 Log.i("Fling", "Chart fling. VelocityX: " + velocityX + ", VelocityY: " + velocityY); 100 } 101 102 @Override onChartScale(MotionEvent me, float scaleX, float scaleY)103 public void onChartScale(MotionEvent me, float scaleX, float scaleY) { 104 Log.i("Scale / Zoom", "ScaleX: " + scaleX + ", ScaleY: " + scaleY); 105 } 106 107 @Override onChartTranslate(MotionEvent me, float dX, float dY)108 public void onChartTranslate(MotionEvent me, float dX, float dY) { 109 Log.i("Translate / Move", "dX: " + dX + ", dY: " + dY); 110 } 111 112 } 113