|
21 | 21 | import android.util.Log; |
22 | 22 | import android.view.MotionEvent; |
23 | 23 | import android.view.View; |
24 | | -import android.widget.RelativeLayout; |
25 | 24 |
|
26 | 25 | import com.github.mikephil.charting.data.ChartData; |
27 | 26 | import com.github.mikephil.charting.data.DataSet; |
28 | 27 | import com.github.mikephil.charting.data.Entry; |
29 | 28 | import com.github.mikephil.charting.interfaces.OnChartValueSelectedListener; |
30 | 29 | import com.github.mikephil.charting.utils.ColorTemplate; |
31 | 30 | import com.github.mikephil.charting.utils.Highlight; |
| 31 | +import com.github.mikephil.charting.utils.MarkerView; |
32 | 32 | import com.github.mikephil.charting.utils.SelInfo; |
33 | 33 | import com.github.mikephil.charting.utils.Utils; |
34 | 34 |
|
@@ -649,48 +649,51 @@ public void highlightValues(Highlight[] highs) { |
649 | 649 | */ |
650 | 650 | /** BELOW CODE IS FOR THE MARKER VIEW */ |
651 | 651 |
|
652 | | - /** the x-position the marker appears on */ |
653 | | - protected float mMarkerPosX = 0f; |
654 | | - |
655 | | - /** the y-postion the marker appears on */ |
656 | | - protected float mMarkerPosY = 0f; |
657 | | - |
658 | 652 | /** if set to true, the marker view is drawn when a value is clicked */ |
659 | | - protected boolean mDrawMarkerView = true; |
| 653 | + protected boolean mDrawMarkerViews = true; |
660 | 654 |
|
661 | 655 | /** the view that represents the marker */ |
662 | | - protected RelativeLayout mMarkerView; |
663 | | - |
| 656 | + protected MarkerView mMarkerView; |
| 657 | + |
664 | 658 | /** |
665 | | - * draws the view that is displayed when the chart is clicked |
| 659 | + * draws all MarkerViews on the highlighted positions |
666 | 660 | */ |
667 | | - protected void drawMarkerView() { |
668 | | - |
669 | | - // if there is no marker view or no values are to highlight, return |
670 | | - if (mMarkerView == null || !mDrawMarkerView || !valuesToHighlight()) |
| 661 | + protected void drawMarkers() { |
| 662 | + |
| 663 | + // if there is no marker view or drawing marker is disabled |
| 664 | + if (mMarkerView == null || !mDrawMarkerViews || !valuesToHighlight()) |
671 | 665 | return; |
| 666 | + |
| 667 | + for(int i = 0; i < mIndicesToHightlight.length; i++) { |
| 668 | + |
| 669 | + int xIndex = mIndicesToHightlight[i].getXIndex(); |
| 670 | + |
| 671 | + drawMarkerView(xIndex, getYValueByDataSetIndex(xIndex, mIndicesToHightlight[i].getDataSetIndex())); |
| 672 | + } |
| 673 | + } |
672 | 674 |
|
673 | | - // int index = mIndicesToHightlight[0]; |
674 | | - // float value = getYValue(index); |
675 | | - // |
676 | | - // // position of the marker depends on selected value index and value |
677 | | - // float[] pts = new float[] { |
678 | | - // index, value |
679 | | - // }; |
680 | | - // transformPointArray(pts); |
681 | | - // |
682 | | - // mMarkerPosX = pts[0] - mMarkerView.getWidth() / 2f; |
683 | | - // mMarkerPosY = pts[1] - mMarkerView.getHeight(); |
684 | | - // |
685 | | - // Log.i("", "h: " + mMarkerView.getHeight() + ", w: " + |
686 | | - // mMarkerView.getWidth()); |
687 | | - // |
688 | | - // // translate to marker position |
689 | | - // mDrawCanvas.translate(mMarkerPosX, mMarkerPosY); |
690 | | - // mMarkerView.draw(mDrawCanvas); |
691 | | - // |
692 | | - // // translate back |
693 | | - // mDrawCanvas.translate(-mMarkerPosX, -mMarkerPosY); |
| 675 | + /** |
| 676 | + * Draws the view that is displayed when a value is highlighted. |
| 677 | + * @param xIndex the selected x-index |
| 678 | + * @param value the selected value |
| 679 | + */ |
| 680 | + private void drawMarkerView(int xIndex, float value) { |
| 681 | + |
| 682 | + // position of the marker depends on selected value index and value |
| 683 | + float[] pts = new float[] { |
| 684 | + xIndex, value |
| 685 | + }; |
| 686 | + transformPointArray(pts); |
| 687 | + |
| 688 | + float posX = pts[0] - mMarkerView.getWidth() / 2f; |
| 689 | + float posY = pts[1] - mMarkerView.getHeight(); |
| 690 | + |
| 691 | + // translate to marker position |
| 692 | +// mDrawCanvas.translate(posX, posY); |
| 693 | +// mMarkerView.draw(mDrawCanvas); |
| 694 | + mMarkerView.draw(mDrawCanvas, posX, posY); |
| 695 | + // translate back |
| 696 | +// mDrawCanvas.translate(-posX, -posY); |
694 | 697 | } |
695 | 698 |
|
696 | 699 | /** |
@@ -953,45 +956,19 @@ public ColorTemplate getColorTemplate() { |
953 | 956 | * |
954 | 957 | * @param v |
955 | 958 | */ |
956 | | - public void setMarkerView(View v) { |
957 | | - |
958 | | - mMarkerView = new RelativeLayout(getContext()); |
959 | | - mMarkerView.setLayoutParams(new RelativeLayout.LayoutParams( |
960 | | - RelativeLayout.LayoutParams.WRAP_CONTENT, |
961 | | - RelativeLayout.LayoutParams.WRAP_CONTENT)); |
962 | | - mMarkerView.addView(v); |
963 | | - |
964 | | - mMarkerView.measure(getWidth(), getHeight()); |
965 | | - mMarkerView.layout(0, 0, getWidth(), getHeight()); |
| 959 | + public void setMarkerView(MarkerView v) { |
| 960 | + mMarkerView = v; |
966 | 961 | } |
967 | 962 |
|
968 | 963 | /** |
969 | 964 | * returns the view that is set as a marker view for the chartv |
970 | 965 | * |
971 | 966 | * @return |
972 | 967 | */ |
973 | | - public View getMarkerView() { |
| 968 | + public MarkerView getMarkerView() { |
974 | 969 | return mMarkerView; |
975 | 970 | } |
976 | 971 |
|
977 | | - /** |
978 | | - * returns the x-position of the marker view |
979 | | - * |
980 | | - * @return |
981 | | - */ |
982 | | - public float getMarkerPosX() { |
983 | | - return mMarkerPosX; |
984 | | - } |
985 | | - |
986 | | - /** |
987 | | - * returns the y-position of the marker view |
988 | | - * |
989 | | - * @return |
990 | | - */ |
991 | | - public float getMarkerPosY() { |
992 | | - return mMarkerPosY; |
993 | | - } |
994 | | - |
995 | 972 | /** paint for the grid lines (only line and barchart) */ |
996 | 973 | public static final int PAINT_GRID = 3; |
997 | 974 |
|
@@ -1064,18 +1041,18 @@ public void setPaint(Paint p, int which) { |
1064 | 1041 | * @return |
1065 | 1042 | */ |
1066 | 1043 | public boolean isDrawMarkerViewEnabled() { |
1067 | | - return mDrawMarkerView; |
| 1044 | + return mDrawMarkerViews; |
1068 | 1045 | } |
1069 | 1046 |
|
1070 | 1047 | /** |
1071 | | - * set this to true to draw a user specified marker-view when tapping on |
1072 | | - * chart values (use the setMarkerView(View v) method to specify a marker |
1073 | | - * view) |
| 1048 | + * Set this to true to draw a user specified marker-view when tapping on |
| 1049 | + * chart values (use the setMarkerView(MarkerView mv) method to specify a marker |
| 1050 | + * view). Default: true |
1074 | 1051 | * |
1075 | 1052 | * @param enabled |
1076 | 1053 | */ |
1077 | | - public void setDrawMarkerView(boolean enabled) { |
1078 | | - mDrawMarkerView = enabled; |
| 1054 | + public void setDrawMarkerViews(boolean enabled) { |
| 1055 | + mDrawMarkerViews = enabled; |
1079 | 1056 | } |
1080 | 1057 |
|
1081 | 1058 | /** |
|
0 commit comments