[关闭]
@zifeng328573112 2021-03-18T02:15:04.000000Z 字数 5690 阅读 454

MPAndroidChart技术-与图表进行手势交互 Interaction with the Chart(二)

1. 概述

MPAndroidChart 这个库完全支持图表进行触摸和手势的交互,通过回调方法做出对应的操作。

2. 启用/ 禁止 手势交互

3. 图表的 抛掷/减速

4. 高亮

以java编程方式使得值高亮不会回调 OnChartValueSelectedListener .

5. 选择回调

MPAndroidChart 提供了许多用于交互回调的方法,其中 OnChartValueSelectedListener 在点击高亮值时回调。

  1. public interface OnChartValueSelectedListener {
  2. /**
  3. * Called when a value has been selected inside the chart.
  4. *
  5. * @param e The selected Entry.
  6. * @param dataSetIndex The index in the datasets array of the data object
  7. * the Entrys DataSet is in.
  8. * @param h the corresponding highlight object that contains information
  9. * about the highlighted position
  10. */
  11. public void onValueSelected(Entry e, int dataSetIndex, Highlight h);
  12. /**
  13. * Called when nothing has been selected or an "un-select" has been made.
  14. */
  15. public void onNothingSelected();
  16. }

让你的类实现该接口并设置对 chart 进行监听,即可接受回调。
Simply let your class that should receive the callbacks implement this interface and set it as a listener to the chart:

  1. chart.setOnChartValueSelectedListener(this);

6. 手势回调

监听器 OnChartGestureListener 可以使得 chart 与手势操作进行交互。

  1. public interface OnChartGestureListener {
  2. /**
  3. * Callbacks when a touch-gesture has started on the chart (ACTION_DOWN)
  4. *
  5. * @param me
  6. * @param lastPerformedGesture
  7. */
  8. void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture);
  9. /**
  10. * Callbacks when a touch-gesture has ended on the chart (ACTION_UP, ACTION_CANCEL)
  11. *
  12. * @param me
  13. * @param lastPerformedGesture
  14. */
  15. void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture);
  16. /**
  17. * Callbacks when the chart is longpressed.
  18. *
  19. * @param me
  20. */
  21. public void onChartLongPressed(MotionEvent me);
  22. /**
  23. * Callbacks when the chart is double-tapped.
  24. *
  25. * @param me
  26. */
  27. public void onChartDoubleTapped(MotionEvent me);
  28. /**
  29. * Callbacks when the chart is single-tapped.
  30. *
  31. * @param me
  32. */
  33. public void onChartSingleTapped(MotionEvent me);
  34. /**
  35. * Callbacks then a fling gesture is made on the chart.
  36. *
  37. * @param me1
  38. * @param me2
  39. * @param velocityX
  40. * @param velocityY
  41. */
  42. public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY);
  43. /**
  44. * Callbacks when the chart is scaled / zoomed via pinch zoom gesture.
  45. *
  46. * @param me
  47. * @param scaleX scalefactor on the x-axis
  48. * @param scaleY scalefactor on the y-axis
  49. */
  50. public void onChartScale(MotionEvent me, float scaleX, float scaleY);
  51. /**
  52. * Callbacks when the chart is moved / translated via drag gesture.
  53. *
  54. * @param me
  55. * @param dX translation distance on the x-axis
  56. * @param dY translation distance on the y-axis
  57. */
  58. public void onChartTranslate(MotionEvent me, float dX, float dY);
  59. }

让你的类实现该接口并设置对 chart 进行监听,即可接受回调。

  1. chart.setOnChartGestureListener(this);

设置了监听器后,chart 会根据你的 setXXXEnable() 进行放缩移动等操作。不用在接口方法里对图表进行放缩移动等其他操作,接口方法可以让你实现其他对应功能,比如说你要打印放缩是的 ScaleX,ScaleY:

  1. @Override
  2. public void onChartScale(MotionEvent me, float scaleX, float scaleY) {
  3. Log.i("Scale / Zoom", "ScaleX: " + scaleX + ", ScaleY: " + scaleY);
  4. }

打印的日志类似:

  1. I/Gesture: START
  2. I/Scale / Zoom: ScaleX: 1.0, ScaleY: 1.0
  3. I/Scale / Zoom: ScaleX: 1.0, ScaleY: 1.0
  4. I/Scale / Zoom: ScaleX: 1.0174584, ScaleY: 1.0174584
  5. I/Scale / Zoom: ScaleX: 1.240304, ScaleY: 1.240304
  6. I/Scale / Zoom: ScaleX: 1.4446417, ScaleY: 1.4446417
  7. I/Scale / Zoom: ScaleX: 1.5617653, ScaleY: 1.5617653
  8. I/Scale / Zoom: ScaleX: 1.0241176, ScaleY: 1.0241176
  9. I/Scale / Zoom: ScaleX: 1.1038365, ScaleY: 1.1038365
  10. I/Gesture: END, lastGesture: PINCH_ZOOM

下面是练习时写的一些 OnChartGestureListener 接口实现方法:

  1. @Override
  2. public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
  3. Log.i("Gesture", "START");
  4. }
  5. @Override
  6. public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
  7. Log.i("Gesture", "END, lastGesture: " + lastPerformedGesture);
  8. // un-highlight values after the gesture is finished and no single-tap
  9. if (lastPerformedGesture != ChartTouchListener.ChartGesture.SINGLE_TAP)
  10. mChart.highlightValues(null); // or highlightTouch(null) for callback to onNothingSelected(...)
  11. }
  12. @Override
  13. public void onChartLongPressed(MotionEvent me) {
  14. Log.i("LongPress", "Chart longpressed.");
  15. }
  16. @Override
  17. public void onChartDoubleTapped(MotionEvent me) {
  18. Log.i("DoubleTap", "Chart double-tapped.");
  19. }
  20. @Override
  21. public void onChartSingleTapped(MotionEvent me) {
  22. Log.i("SingleTap", "Chart single-tapped.");
  23. }
  24. @Override
  25. public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) {
  26. Log.i("Fling", "Chart flinged. VeloX: " + velocityX + ", VeloY: " + velocityY);
  27. }
  28. @Override
  29. public void onChartScale(MotionEvent me, float scaleX, float scaleY) {
  30. Log.i("Scale / Zoom", "ScaleX: " + scaleX + ", ScaleY: " + scaleY);
  31. }
  32. @Override
  33. public void onChartTranslate(MotionEvent me, float dX, float dY) {
  34. Log.i("Translate / Move", "dX: " + dX + ", dY: " + dY);
  35. }
  36. @Override
  37. public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
  38. Log.i("Entry selected", e.toString());
  39. Log.i("", "low: " + mChart.getLowestVisibleXIndex() + ", high: " + mChart.getHighestVisibleXIndex());
  40. }
  41. @Override
  42. public void onNothingSelected() {
  43. Log.i("Nothing selected", "Nothing selected.");
  44. }
  45. }

7. 其他章节索引

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注