Skip to content

Commit 8d529bd

Browse files
committed
Fix markerview position when stacked-bars.
1 parent 539a894 commit 8d529bd

12 files changed

Lines changed: 116 additions & 33 deletions

File tree

‎MPChartExample/res/layout/frag_simple_line.xml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<com.github.mikephil.charting.charts.LineChart
88
android:id="@+id/lineChart1"
9-
android:layout_width="wrap_content"
10-
android:layout_height="wrap_content" />
9+
android:layout_width="match_parent"
10+
android:layout_height="match_parent" />
1111

1212
</FrameLayout>

‎MPChartExample/res/layout/frag_simple_pie.xml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<com.github.mikephil.charting.charts.PieChart
88
android:id="@+id/pieChart1"
9-
android:layout_width="wrap_content"
10-
android:layout_height="wrap_content" />
9+
android:layout_width="match_parent"
10+
android:layout_height="match_parent" />
1111

1212
</LinearLayout>

‎MPChartExample/res/layout/frag_simple_scatter.xml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<com.github.mikephil.charting.charts.ScatterChart
88
android:id="@+id/scatterChart1"
9-
android:layout_width="wrap_content"
10-
android:layout_height="wrap_content" />
9+
android:layout_width="match_parent"
10+
android:layout_height="match_parent" />
1111

1212
</LinearLayout>

‎MPChartExample/src/com/xxmassdeveloper/mpchartexample/custom/MyMarkerView.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.github.mikephil.charting.components.MarkerView;
88
import com.github.mikephil.charting.data.CandleEntry;
99
import com.github.mikephil.charting.data.Entry;
10+
import com.github.mikephil.charting.highlight.Highlight;
1011
import com.github.mikephil.charting.utils.Utils;
1112
import com.xxmassdeveloper.mpchartexample.R;
1213

@@ -28,7 +29,7 @@ public MyMarkerView(Context context, int layoutResource) {
2829
// callbacks everytime the MarkerView is redrawn, can be used to update the
2930
// content (user-interface)
3031
@Override
31-
public void refreshContent(Entry e, int dataSetIndex) {
32+
public void refreshContent(Entry e, Highlight highlight) {
3233

3334
if (e instanceof CandleEntry) {
3435

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
package com.xxmassdeveloper.mpchartexample.custom;
3+
4+
import android.content.Context;
5+
import android.widget.TextView;
6+
7+
import com.github.mikephil.charting.components.MarkerView;
8+
import com.github.mikephil.charting.data.BarEntry;
9+
import com.github.mikephil.charting.data.CandleEntry;
10+
import com.github.mikephil.charting.data.Entry;
11+
import com.github.mikephil.charting.highlight.Highlight;
12+
import com.github.mikephil.charting.utils.Utils;
13+
import com.xxmassdeveloper.mpchartexample.R;
14+
15+
/**
16+
* Custom implementation of the MarkerView.
17+
*
18+
* @author Philipp Jahoda
19+
*/
20+
public class StackedBarsMarkerView extends MarkerView {
21+
22+
private TextView tvContent;
23+
24+
public StackedBarsMarkerView(Context context, int layoutResource) {
25+
super(context, layoutResource);
26+
27+
tvContent = (TextView) findViewById(R.id.tvContent);
28+
}
29+
30+
// callbacks everytime the MarkerView is redrawn, can be used to update the
31+
// content (user-interface)
32+
@Override
33+
public void refreshContent(Entry e, Highlight highlight) {
34+
35+
if (e instanceof BarEntry) {
36+
37+
BarEntry be = (BarEntry) e;
38+
39+
if(be.getVals() != null) {
40+
41+
// draw the stack value
42+
tvContent.setText("" + Utils.formatNumber(be.getVals()[highlight.getStackIndex()], 0, true));
43+
} else {
44+
tvContent.setText("" + Utils.formatNumber(be.getVal(), 0, true));
45+
}
46+
} else {
47+
48+
tvContent.setText("" + Utils.formatNumber(e.getVal(), 0, true));
49+
}
50+
}
51+
52+
@Override
53+
public int getXOffset() {
54+
// this will center the marker-view horizontally
55+
return -(getWidth() / 2);
56+
}
57+
58+
@Override
59+
public int getYOffset() {
60+
// this will cause the marker-view to be above the selected value
61+
return -getHeight();
62+
}
63+
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.github.mikephil.charting.components.YAxis;
2020
import com.github.mikephil.charting.components.YAxis.AxisDependency;
2121
import com.github.mikephil.charting.data.BarData;
22+
import com.github.mikephil.charting.data.BarEntry;
2223
import com.github.mikephil.charting.data.BarLineScatterCandleData;
2324
import com.github.mikephil.charting.data.BarLineScatterCandleDataSet;
2425
import com.github.mikephil.charting.data.DataSet;
@@ -522,9 +523,11 @@ protected void calcModulus() {
522523
}
523524

524525
@Override
525-
protected float[] getMarkerPosition(Entry e, int dataSetIndex) {
526+
protected float[] getMarkerPosition(Entry e, Highlight highlight) {
526527

528+
int dataSetIndex = highlight.getDataSetIndex();
527529
float xPos = e.getXIndex();
530+
float yPos = e.getVal();
528531

529532
if (this instanceof BarChart) {
530533

@@ -537,11 +540,16 @@ protected float[] getMarkerPosition(Entry e, int dataSetIndex) {
537540
/ 2f;
538541

539542
xPos += x;
543+
544+
BarEntry entry = (BarEntry) e;
545+
if(entry.getVals() != null) {
546+
yPos = highlight.getRange().to;
547+
}
540548
}
541549

542550
// position of the marker depends on selected value index and value
543551
float[] pts = new float[] {
544-
xPos, e.getVal() * mAnimator.getPhaseY()
552+
xPos, yPos * mAnimator.getPhaseY()
545553
};
546554

547555
getTransformer(mData.getDataSetByIndex(dataSetIndex).getAxisDependency())

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,9 @@ protected void drawMarkers(Canvas canvas) {
587587

588588
for (int i = 0; i < mIndicesToHightlight.length; i++) {
589589

590-
int xIndex = mIndicesToHightlight[i].getXIndex();
591-
int dataSetIndex = mIndicesToHightlight[i].getDataSetIndex();
590+
Highlight highlight = mIndicesToHightlight[i];
591+
int xIndex = highlight.getXIndex();
592+
int dataSetIndex = highlight.getDataSetIndex();
592593

593594
if (xIndex <= mDeltaX && xIndex <= mDeltaX * mAnimator.getPhaseX()) {
594595

@@ -598,14 +599,14 @@ protected void drawMarkers(Canvas canvas) {
598599
if (e == null || e.getXIndex() != mIndicesToHightlight[i].getXIndex())
599600
continue;
600601

601-
float[] pos = getMarkerPosition(e, dataSetIndex);
602+
float[] pos = getMarkerPosition(e, highlight);
602603

603604
// check bounds
604605
if (!mViewPortHandler.isInBounds(pos[0], pos[1]))
605606
continue;
606607

607608
// callbacks to update the content
608-
mMarkerView.refreshContent(e, dataSetIndex);
609+
mMarkerView.refreshContent(e, highlight);
609610

610611
// mMarkerView.measure(MeasureSpec.makeMeasureSpec(0,
611612
// MeasureSpec.UNSPECIFIED),
@@ -633,11 +634,11 @@ protected void drawMarkers(Canvas canvas) {
633634
* Returns the actual position in pixels of the MarkerView for the given
634635
* Entry in the given DataSet.
635636
*
636-
* @param xIndex
637-
* @param dataSetIndex
637+
* @param e
638+
* @param highlight
638639
* @return
639640
*/
640-
protected abstract float[] getMarkerPosition(Entry e, int dataSetIndex);
641+
protected abstract float[] getMarkerPosition(Entry e, Highlight highlight);
641642

642643
/**
643644
* ################ ################ ################ ################

‎MPChartLib/src/com/github/mikephil/charting/charts/PieChart.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.github.mikephil.charting.data.Entry;
1515
import com.github.mikephil.charting.data.PieData;
1616
import com.github.mikephil.charting.data.PieDataSet;
17+
import com.github.mikephil.charting.highlight.Highlight;
1718
import com.github.mikephil.charting.renderer.PieChartRenderer;
1819
import com.github.mikephil.charting.utils.Utils;
1920

@@ -144,7 +145,7 @@ protected void calcMinMax() {
144145

145146
/** PieChart does not support MarkerView */
146147
@Override
147-
protected float[] getMarkerPosition(Entry e, int dataSetIndex) {
148+
protected float[] getMarkerPosition(Entry e, Highlight highlight) {
148149
return new float[0];
149150
}
150151

‎MPChartLib/src/com/github/mikephil/charting/charts/RadarChart.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.github.mikephil.charting.components.YAxis.AxisDependency;
1414
import com.github.mikephil.charting.data.Entry;
1515
import com.github.mikephil.charting.data.RadarData;
16+
import com.github.mikephil.charting.highlight.Highlight;
1617
import com.github.mikephil.charting.renderer.RadarChartRenderer;
1718
import com.github.mikephil.charting.renderer.XAxisRendererRadarChart;
1819
import com.github.mikephil.charting.renderer.YAxisRendererRadarChart;
@@ -112,7 +113,7 @@ protected void calcMinMax() {
112113
}
113114

114115
@Override
115-
protected float[] getMarkerPosition(Entry e, int dataSetIndex) {
116+
protected float[] getMarkerPosition(Entry e, Highlight highlight) {
116117

117118
float angle = getSliceAngle() * e.getXIndex() + getRotationAngle();
118119
float val = e.getVal() * getFactor();

‎MPChartLib/src/com/github/mikephil/charting/components/MarkerView.java‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@
88
import android.widget.RelativeLayout;
99

1010
import com.github.mikephil.charting.data.Entry;
11+
import com.github.mikephil.charting.highlight.Highlight;
1112

1213
/**
1314
* View that can be displayed when selecting values in the chart. Extend this
1415
* class to provide custom layouts for your markers.
15-
*
16+
*
1617
* @author Philipp Jahoda
1718
*/
1819
public abstract class MarkerView extends RelativeLayout {
1920

2021
/**
2122
* Constructor. Sets up the MarkerView with a custom layout resource.
22-
*
23+
*
2324
* @param context
2425
* @param layoutResource the layout resource to use for the MarkerView
2526
*/
@@ -30,7 +31,7 @@ public MarkerView(Context context, int layoutResource) {
3031

3132
/**
3233
* Sets the layout resource for a custom MarkerView.
33-
*
34+
*
3435
* @param layoutResource
3536
*/
3637
private void setupLayoutResource(int layoutResource) {
@@ -49,7 +50,7 @@ private void setupLayoutResource(int layoutResource) {
4950
/**
5051
* Draws the MarkerView on the given position on the screen with the given
5152
* Canvas object.
52-
*
53+
*
5354
* @param canvas
5455
* @param posx
5556
* @param posy
@@ -69,19 +70,19 @@ public void draw(Canvas canvas, float posx, float posy) {
6970
/**
7071
* This method enables a specified custom MarkerView to update it's content
7172
* everytime the MarkerView is redrawn.
72-
*
73-
* @param e The Entry the MarkerView belongs to. This can also be any
74-
* subclass of Entry, like BarEntry or CandleEntry, simply cast
75-
* it at runtime.
76-
* @param dataSetIndex the index of the DataSet the selected value is in
73+
*
74+
* @param e The Entry the MarkerView belongs to. This can also be any
75+
* subclass of Entry, like BarEntry or CandleEntry, simply cast
76+
* it at runtime.
77+
* @param highlight the highlight object contains information about the highlighted value such as it's dataset-index, the selected range or stack-index (only stacked bar entries).
7778
*/
78-
public abstract void refreshContent(Entry e, int dataSetIndex);
79+
public abstract void refreshContent(Entry e, Highlight highlight);
7980

8081
/**
8182
* Use this to return the desired offset you wish the MarkerView to have on
8283
* the x-axis. By returning -(getWidth() / 2) you will center the MarkerView
8384
* horizontally.
84-
*
85+
*
8586
* @return
8687
*/
8788
public abstract int getXOffset();
@@ -90,7 +91,7 @@ public void draw(Canvas canvas, float posx, float posy) {
9091
* Use this to return the desired position offset you wish the MarkerView to
9192
* have on the y-axis. By returning -getHeight() you will cause the
9293
* MarkerView to be above the selected value.
93-
*
94+
*
9495
* @return
9596
*/
9697
public abstract int getYOffset();

0 commit comments

Comments
 (0)