Skip to content

Commit 1fbfd0d

Browse files
committed
Improved the code. Added highlight function for Linechart. TODO: Highlight on touch.
1 parent 4c0e3dd commit 1fbfd0d

9 files changed

Lines changed: 266 additions & 151 deletions

File tree

‎MPChartExample/res/menu/pie.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
3-
<item android:id="@+id/actionToggleValues" android:title="Toggle Values"></item>
3+
<item android:id="@+id/actionToggleValues" android:title="Toggle Y-Values"></item>
44
<item android:id="@+id/actionToggleXVals" android:title="Toggle X-Values"></item>
55
<item android:id="@+id/actionTogglePercent" android:title="Toggle Percent"></item>
66
<item android:id="@+id/actionToggleHole" android:title="Toggle Hole"></item>

‎MPChartExample/src/com/example/mpchartexample/LineChartActivity.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ protected void onCreate(Bundle savedInstanceState) {
4242
// mChart.setRoundedYLegend(false);
4343
// mChart.setStartAtZero(true);
4444
mChart.setDrawYValues(false);
45-
mChart.setLineWidth(5f);
46-
mChart.setCircleSize(5f);
45+
mChart.setLineWidth(4f);
46+
mChart.setCircleSize(4f);
4747
// mChart.setSpacePercent(20, 10);
4848
mChart.setYLegendCount(6);
4949
mChart.setTouchEnabled(true);
50+
mChart.setHighlightEnabled(true);
51+
// mChart.highlightValues(new int[] {2, 6});
5052

5153
mSeekBarX.setProgress(45);
5254
mSeekBarY.setProgress(100);

‎MPChartLib/src/com/github/mikephil/charting/BarChart.java‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,19 @@ protected void calcMinMax() {
102102
// increase deltax by 1 because the bars have a width of 1
103103
mDeltaX++;
104104
}
105+
106+
@Override
107+
protected void drawHighlights() {
108+
109+
// if there are values to highlight and highlighnting is enabled, do it
110+
if(mHighlightEnabled && valuesToHighlight()) {
111+
112+
for(int i = 0; i < mIndicesToHightlight.length; i++) {
113+
114+
115+
}
116+
}
117+
}
105118

106119
private RectF mBarRect = new RectF();
107120

@@ -224,11 +237,6 @@ protected void drawValues() {
224237
}
225238
}
226239

227-
@Override
228-
public void highlightValues(int[] indices) {
229-
super.highlightValues(indices);
230-
}
231-
232240
/**
233241
* sets the skew (default 0.3f), the skew indicates how much the 3D effect
234242
* of the chart is turned to the right

‎MPChartLib/src/com/github/mikephil/charting/BarLineChartBase.java‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ protected void onDraw(Canvas canvas) {
145145

146146
drawHorizontalGrid();
147147
drawVerticalGrid();
148+
drawHighlights();
148149
drawData();
149150
drawAdditional();
150151
drawValues();
@@ -682,10 +683,26 @@ public void setGridWidth(float width) {
682683
width = 3.0f;
683684
mGridWidth = width;
684685
}
685-
686-
@Override
687-
protected void highlightValues(int[] indices) {
688-
super.highlightValues(indices);
686+
687+
/**
688+
* returns the index of the value at the given touch point
689+
* @param x
690+
* @param y
691+
* @return
692+
*/
693+
public int getIndexByTouchPoint(float x, float y) {
694+
695+
return -1;
696+
}
697+
698+
/**
699+
* returns the value displayed at the touched position of the chart
700+
* @param x
701+
* @param y
702+
* @return
703+
*/
704+
public float getValueByTouchPoint(float x, float y) {
705+
return mYVals.get(getIndexByTouchPoint(x, y));
689706
}
690707

691708
@Override

‎MPChartLib/src/com/github/mikephil/charting/BarLineChartTouchListener.java‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,6 @@ public Matrix getMatrix() {
245245

246246
@Override
247247
public boolean onSingleTapConfirmed(MotionEvent e) {
248-
249-
PointF pointF = calcImagePosition(start);
250248

251249
mChart.highlightValues(new int[] {1});
252250

‎MPChartLib/src/com/github/mikephil/charting/Chart.java‎

Lines changed: 139 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ public abstract class Chart extends View {
118118

119119
/** if true, y-values are drawn on the chart */
120120
protected boolean mDrawYValues = true;
121+
122+
/** if true, value highlightning is enabled */
123+
protected boolean mHighlightEnabled = true;
121124

122125
/** this rectangle defines the area in which graph values can be drawn */
123126
protected Rect mContentRect;
@@ -240,7 +243,7 @@ protected void calcMinMax() {
240243

241244
// calc delta
242245
mDeltaY = mYMax - mYChartMin;
243-
mYChartMax = mYChartMin + mDeltaY;
246+
mYChartMax = mYChartMin + mYMax;
244247

245248
mDeltaX = mXVals.size() - 1;
246249

@@ -420,28 +423,6 @@ protected void transformPointArrayNoTouch(float[] pts) {
420423
mMatrixOffset.mapPoints(pts);
421424
}
422425

423-
/** the x-position the marker appears on */
424-
protected int mMarkerPosX = 100;
425-
426-
/** the y-postion the marker appears on */
427-
protected int mMarkerPosY = 100;
428-
429-
/** the view that represents the marker */
430-
protected RelativeLayout mMarkerView;
431-
432-
/**
433-
* draws the view that is displayed when the chart is clicked
434-
*/
435-
protected void drawMarkerView() {
436-
437-
if (mMarkerView == null)
438-
return;
439-
440-
mDrawCanvas.translate(mMarkerPosX, mMarkerPosY);
441-
mMarkerView.draw(mDrawCanvas);
442-
mDrawCanvas.translate(-mMarkerPosX, -mMarkerPosY);
443-
}
444-
445426
/**
446427
* draws the description text in the bottom right corner of the chart
447428
*/
@@ -479,30 +460,11 @@ protected int calcTextWidth(Paint paint, String demoText) {
479460
* draws additional stuff, whatever that might be
480461
*/
481462
protected abstract void drawAdditional();
482-
463+
483464
/**
484-
* highlights the value at the given index of the values list
485-
*
486-
* @param indices
465+
* draws the values of the chart that need highlightning
487466
*/
488-
protected void highlightValues(int[] indices) {
489-
490-
if (mSelectionListener != null) {
491-
492-
if (indices[0] == -1)
493-
mSelectionListener.onNothingSelected();
494-
else {
495-
496-
float[] values = new float[indices.length];
497-
498-
for (int i = 0; i < values.length; i++)
499-
values[i] = getYValue(indices[i]);
500-
501-
// notify the listener
502-
mSelectionListener.onValuesSelected(values, indices);
503-
}
504-
}
505-
}
467+
protected abstract void drawHighlights();
506468

507469
/**
508470
* ################ ################ ################ ################
@@ -578,6 +540,102 @@ protected void limitTransAndScale(Matrix matrix) {
578540

579541
matrix.setValues(vals);
580542
}
543+
544+
/**
545+
* ################ ################ ################ ################
546+
*/
547+
/** BELOW THIS CODE FOR HIGHLIGHTING */
548+
549+
550+
/**
551+
* array of integers that reference the highlighted slices in the pie chart
552+
*/
553+
protected int[] mIndicesToHightlight = new int[0];
554+
555+
/**
556+
* checks if the given index is set for highlighting or not
557+
*
558+
* @param index
559+
* @return
560+
*/
561+
public boolean needsHighlight(int index) {
562+
563+
// no highlight
564+
if(!valuesToHighlight()) return false;
565+
566+
for (int i = 0; i < mIndicesToHightlight.length; i++)
567+
if (mIndicesToHightlight[i] == index)
568+
return true;
569+
570+
return false;
571+
}
572+
573+
/**
574+
* returns true if there are values to highlight, false if there are not
575+
* @return
576+
*/
577+
public boolean valuesToHighlight() {
578+
return mIndicesToHightlight == null || mIndicesToHightlight.length == 0 ? false : true;
579+
}
580+
581+
/**
582+
* highlights the value at the given index of the values list
583+
*
584+
* @param indices
585+
*/
586+
public void highlightValues(int[] indices) {
587+
588+
// set the indices to highlight
589+
mIndicesToHightlight = indices;
590+
591+
// redraw the chart
592+
invalidate();
593+
594+
if (mSelectionListener != null) {
595+
596+
if(indices == null) return;
597+
598+
if (indices[0] == -1)
599+
mSelectionListener.onNothingSelected();
600+
else {
601+
602+
float[] values = new float[indices.length];
603+
604+
for (int i = 0; i < values.length; i++)
605+
values[i] = getYValue(indices[i]);
606+
607+
// notify the listener
608+
mSelectionListener.onValuesSelected(values, indices);
609+
}
610+
}
611+
}
612+
613+
/**
614+
* ################ ################ ################ ################
615+
*/
616+
/** BELOW CODE IS FOR THE MARKER VIEW */
617+
618+
/** the x-position the marker appears on */
619+
protected int mMarkerPosX = 100;
620+
621+
/** the y-postion the marker appears on */
622+
protected int mMarkerPosY = 100;
623+
624+
/** the view that represents the marker */
625+
protected RelativeLayout mMarkerView;
626+
627+
/**
628+
* draws the view that is displayed when the chart is clicked
629+
*/
630+
protected void drawMarkerView() {
631+
632+
if (mMarkerView == null)
633+
return;
634+
635+
mDrawCanvas.translate(mMarkerPosX, mMarkerPosY);
636+
mMarkerView.draw(mDrawCanvas);
637+
mDrawCanvas.translate(-mMarkerPosX, -mMarkerPosY);
638+
}
581639

582640
/**
583641
* ################ ################ ################ ################
@@ -593,6 +651,21 @@ protected void limitTransAndScale(Matrix matrix) {
593651
public void setOnTouchListener(OnTouchListener l) {
594652
this.mListener = l;
595653
}
654+
/**
655+
* if set to true, value highlightning is enabled
656+
* @param enabled
657+
*/
658+
public void setHighlightEnabled(boolean enabled) {
659+
mHighlightEnabled = enabled;
660+
}
661+
662+
/**
663+
* returns true if highlightning of values is enabled, false if not
664+
* @return
665+
*/
666+
public boolean isHighlightEnabled() {
667+
return mHighlightEnabled;
668+
}
596669

597670
/**
598671
* returns the total value (sum) of all y-values
@@ -748,28 +821,6 @@ public void setDrawYValues(boolean enabled) {
748821
this.mDrawYValues = enabled;
749822
}
750823

751-
// /**
752-
// * set this to true to make the x-legend exactly fill the whole chart with
753-
// * all values being exacly correct, if the x-legend fits, it means that
754-
// all
755-
// * values use exactly the whole chart width, this can however lead to an
756-
// * increased or decreased number of x-legend grid lines -> e.g. if the
757-
// * number of x-values is a prime number only the first and last x-legend
758-
// * grid line will be created. Nevertheless, the chart will always try to
759-
// get
760-
// * as close as possible to the actually specified number of x-legend grid
761-
// * lines. if set to false, the chart will use exactly the specified number
762-
// * of x-legend grid lines. This can however lead to small incorrectness of
763-
// * the gridlines if the number of x-entries cannot be divided through the
764-
// * number of X-legend entries. default: enabled true
765-
// *
766-
// * @param enabled
767-
// */
768-
// public void setFitXLegend(boolean enabled) {
769-
// this.mFitXLegend = enabled;
770-
// prepare();
771-
// }
772-
773824
/**
774825
* sets the y- starting and ending value
775826
*
@@ -815,6 +866,24 @@ public View getMarkerView() {
815866
return mMarkerView;
816867
}
817868

869+
/**
870+
* returns the x-position of the marker view
871+
*
872+
* @return
873+
*/
874+
public int getMarkerPosX() {
875+
return mMarkerPosX;
876+
}
877+
878+
/**
879+
* returns the y-position of the marker view
880+
*
881+
* @return
882+
*/
883+
public int getMarkerPosY() {
884+
return mMarkerPosY;
885+
}
886+
818887
/** paint for the lines of the linechart */
819888
public static final int PAINT_LINE = 1;
820889

@@ -859,6 +928,9 @@ public View getMarkerView() {
859928

860929
/** paint for the text in the middle of the pie chart */
861930
public static final int PAINT_CENTER_TEXT = 14;
931+
932+
/** paint for highlightning the values of a linechart */
933+
public static final int PAINT_HIGHLIGHT_LINE = 15;
862934

863935
/**
864936
* set a new paint object for the specified parameter in the chart e.g.
@@ -882,18 +954,6 @@ public void setPaint(Paint p, int which) {
882954
}
883955
}
884956

885-
//
886-
// /**
887-
// * returns true if fitting x legend is enabled, false if not if the
888-
// x-legend
889-
// * fits, it means that all values use exactly the whole chart width
890-
// *
891-
// * @return
892-
// */
893-
// public boolean isFitXLegendEnabled() {
894-
// return mFitXLegend;
895-
// }
896-
897957
/**
898958
* returns true if y-value drawing is enabled, false if not
899959
*

0 commit comments

Comments
 (0)