Skip to content

Commit db0c921

Browse files
committed
MarkerView issue resolved. MarkerView can now be drawn when selecting values. Added possibility to set custom marker layout.
1 parent 30e54a3 commit db0c921

10 files changed

Lines changed: 233 additions & 101 deletions

File tree

12.2 KB
Loading
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="60dp"
4+
android:layout_height="50dp"
5+
android:background="@drawable/marker" >
6+
7+
<TextView
8+
android:id="@+id/textView1"
9+
android:layout_width="wrap_content"
10+
android:layout_height="wrap_content"
11+
android:layout_centerHorizontal="true"
12+
android:layout_centerVertical="true"
13+
android:text="Selected."
14+
android:textSize="10dp"
15+
android:textColor="@android:color/white"
16+
android:textAppearance="?android:attr/textAppearanceSmall" />
17+
18+
</RelativeLayout>

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.github.mikephil.charting.interfaces.OnChartValueSelectedListener;
1818
import com.github.mikephil.charting.utils.ColorTemplate;
1919
import com.github.mikephil.charting.utils.Highlight;
20+
import com.github.mikephil.charting.utils.MarkerView;
2021

2122
import java.util.ArrayList;
2223

@@ -70,6 +71,21 @@ protected void onCreate(Bundle savedInstanceState) {
7071

7172
// if disabled, scaling can be done on x- and y-axis separately
7273
mChart.setPinchZoom(true);
74+
75+
// create a MarkerView
76+
MarkerView mv = new MarkerView(this);
77+
78+
// set a custom markerview
79+
mv.setCustomViewResource(R.layout.custom_marker_view);
80+
81+
// define an offset to change the original position of the marker
82+
mv.setOffsets(0, -mv.getMeasuredHeight());
83+
84+
// set the marker to the chart
85+
mChart.setMarkerView(mv);
86+
87+
// enable/disable highlight indicators
88+
// mChart.setHighlightIndicatorEnabled(false);
7389

7490
mSeekBarX.setProgress(45);
7591
mSeekBarY.setProgress(100);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ protected void calcMinMax(boolean fixedValues) {
147147
protected void drawHighlights() {
148148

149149
// if there are values to highlight and highlighnting is enabled, do it
150-
if (mHighlightEnabled && valuesToHighlight()) {
150+
if (mHighlightEnabled && mHighLightIndicatorEnabled && valuesToHighlight()) {
151151

152152
// distance between highlight arrow and bar
153153
float offsetY = mDeltaY * 0.04f;

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22
package com.github.mikephil.charting.charts;
33

4-
import android.annotation.SuppressLint;
54
import android.content.Context;
65
import android.graphics.Canvas;
76
import android.graphics.Color;
@@ -140,6 +139,12 @@ public abstract class BarLineChartBase extends Chart {
140139
/** paint used for highlighting values */
141140
protected Paint mHighlightPaint;
142141

142+
/**
143+
* if set to true, the highlight indicator (lines for linechart, dark bar
144+
* for barchart) will be drawn upon selecting values.
145+
*/
146+
protected boolean mHighLightIndicatorEnabled = true;
147+
143148
/**
144149
* boolean to indicate if user drawing on chart should automatically be
145150
* finished
@@ -240,14 +245,15 @@ protected void onDraw(Canvas canvas) {
240245
mDrawCanvas.restoreToCount(clipRestoreCount);
241246

242247
drawAdditional();
248+
249+
drawMarkers();
250+
243251
drawValues();
244252

245253
drawXLegend();
246254

247255
drawYLegend();
248256

249-
drawMarkerView();
250-
251257
drawDescription();
252258

253259
canvas.drawBitmap(mDrawBitmap, 0, 0, mDrawPaint);
@@ -359,10 +365,6 @@ protected void prepareXLegend() {
359365

360366
if (mData.getXVals().get(0).length() <= 3)
361367
length *= 2;
362-
// else if(mXVals.get(0).length() <= 5) length *= 1;
363-
// else if(mXVals.get(0).length() <= 7) length = (int) (length / 1.5f);
364-
// else if(mXVals.get(0).length() <= 9) length = (int) (length / 2.5f);
365-
// else if(mXVals.get(0).length() > 9) length = (int) (length / 4f);
366368

367369
for (int i = 0; i < length; i++) {
368370
a.append("h");
@@ -378,7 +380,6 @@ protected void prepareXLegend() {
378380
*
379381
* @return
380382
*/
381-
@SuppressLint("NewApi")
382383
private void prepareYLegend() {
383384

384385
// calculate the currently visible extremes
@@ -461,15 +462,6 @@ protected void drawXLegend() {
461462

462463
if (position[0] >= mOffsetLeft && position[0] <= getWidth() - mOffsetRight + 10) {
463464

464-
// Rect rect = new Rect();
465-
// mXLegendPaint.getTextBounds(mXVals.get(i), 0,
466-
// mXVals.get(i).length(), rect);
467-
//
468-
// float toRight = rect.width() / 2;
469-
//
470-
// // make sure
471-
// if(i == 0) toRight = rect.width();
472-
473465
mDrawCanvas.drawText(mData.getXVals().get(i), position[0], mOffsetTop - 5,
474466
mXLegendPaint);
475467
}
@@ -991,6 +983,16 @@ public void setDrawGrid(boolean enabled) {
991983
this.mDrawGrid = enabled;
992984
}
993985

986+
/**
987+
* If set to true, the highlight indicators (two lines for linechart, dark
988+
* bar for barchart) will be drawn upon selecting values. Default: true
989+
*
990+
* @param enabled
991+
*/
992+
public void setHighlightIndicatorEnabled(boolean enabled) {
993+
mHighLightIndicatorEnabled = enabled;
994+
}
995+
994996
/**
995997
* enable this to force the y-legend to always start at zero
996998
*

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

Lines changed: 48 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
import android.util.Log;
2222
import android.view.MotionEvent;
2323
import android.view.View;
24-
import android.widget.RelativeLayout;
2524

2625
import com.github.mikephil.charting.data.ChartData;
2726
import com.github.mikephil.charting.data.DataSet;
2827
import com.github.mikephil.charting.data.Entry;
2928
import com.github.mikephil.charting.interfaces.OnChartValueSelectedListener;
3029
import com.github.mikephil.charting.utils.ColorTemplate;
3130
import com.github.mikephil.charting.utils.Highlight;
31+
import com.github.mikephil.charting.utils.MarkerView;
3232
import com.github.mikephil.charting.utils.SelInfo;
3333
import com.github.mikephil.charting.utils.Utils;
3434

@@ -649,48 +649,51 @@ public void highlightValues(Highlight[] highs) {
649649
*/
650650
/** BELOW CODE IS FOR THE MARKER VIEW */
651651

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-
658652
/** if set to true, the marker view is drawn when a value is clicked */
659-
protected boolean mDrawMarkerView = true;
653+
protected boolean mDrawMarkerViews = true;
660654

661655
/** the view that represents the marker */
662-
protected RelativeLayout mMarkerView;
663-
656+
protected MarkerView mMarkerView;
657+
664658
/**
665-
* draws the view that is displayed when the chart is clicked
659+
* draws all MarkerViews on the highlighted positions
666660
*/
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())
671665
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+
}
672674

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);
694697
}
695698

696699
/**
@@ -953,45 +956,19 @@ public ColorTemplate getColorTemplate() {
953956
*
954957
* @param v
955958
*/
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;
966961
}
967962

968963
/**
969964
* returns the view that is set as a marker view for the chartv
970965
*
971966
* @return
972967
*/
973-
public View getMarkerView() {
968+
public MarkerView getMarkerView() {
974969
return mMarkerView;
975970
}
976971

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-
995972
/** paint for the grid lines (only line and barchart) */
996973
public static final int PAINT_GRID = 3;
997974

@@ -1064,18 +1041,18 @@ public void setPaint(Paint p, int which) {
10641041
* @return
10651042
*/
10661043
public boolean isDrawMarkerViewEnabled() {
1067-
return mDrawMarkerView;
1044+
return mDrawMarkerViews;
10681045
}
10691046

10701047
/**
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
10741051
*
10751052
* @param enabled
10761053
*/
1077-
public void setDrawMarkerView(boolean enabled) {
1078-
mDrawMarkerView = enabled;
1054+
public void setDrawMarkerViews(boolean enabled) {
1055+
mDrawMarkerViews = enabled;
10791056
}
10801057

10811058
/**

MPChartLib/src/com/github/mikephil/charting/charts/LineChart.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void setData(ChartData data) {
8282
protected void drawHighlights() {
8383

8484
// if there are values to highlight and highlighnting is enabled, do it
85-
if (mHighlightEnabled && valuesToHighlight()) {
85+
if (mHighlightEnabled && mHighLightIndicatorEnabled && valuesToHighlight()) {
8686

8787
for (int i = 0; i < mIndicesToHightlight.length; i++) {
8888

MPChartLib/src/com/github/mikephil/charting/charts/ScatterChart.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ protected void drawValues() {
170170
protected void drawHighlights() {
171171

172172
// if there are values to highlight and highlighnting is enabled, do it
173-
if (mHighlightEnabled && valuesToHighlight()) {
173+
if (mHighlightEnabled && mHighLightIndicatorEnabled && valuesToHighlight()) {
174174

175175
for (int i = 0; i < mIndicesToHightlight.length; i++) {
176176

0 commit comments

Comments
 (0)