Skip to content

Commit 3894a17

Browse files
authored
Merge pull request #387 from AppDevNext/markers-list
Modified and expanded, made more flexible marker interaction interface
2 parents e99aea2 + 7089877 commit 3894a17

File tree

4 files changed

+47
-36
lines changed

4 files changed

+47
-36
lines changed

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

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252
import java.io.IOException;
5353
import java.io.OutputStream;
5454
import java.util.ArrayList;
55-
56-
import androidx.annotation.NonNull;
55+
import java.util.Collections;
56+
import java.util.List;
5757

5858
/**
5959
* Baseclass of all Chart-Views.
@@ -172,11 +172,6 @@ public abstract class Chart<T extends ChartData<? extends IDataSet<? extends Ent
172172
*/
173173
private float mExtraTopOffset = 0.f, mExtraRightOffset = 0.f, mExtraBottomOffset = 0.f, mExtraLeftOffset = 0.f;
174174

175-
/**
176-
* Tag for logging accessibility related content
177-
*/
178-
private String TAG = "abilityTag";
179-
180175
/**
181176
* Additional data on top of dynamically generated description. This can be set by the user.
182177
*/
@@ -543,12 +538,17 @@ public void highlightValues(Highlight[] highs) {
543538
invalidate();
544539
}
545540

546-
/**
547-
* Highlights any y-value at the given x-value in the given DataSet.
548-
* Provide -1 as the dataSetIndex to undo all highlighting.
549-
* This method will call the listener.
550-
*
551-
* @param x The x-value to highlight
541+
public void highlightValues(List<Highlight> highs, List<IMarker> markers) {
542+
if (highs.size() != markers.size()) throw new IllegalArgumentException("Markers and highs must be mutually corresponding. High size = " + highs.size() + " Markers size = " + markers.size());
543+
setMarkers(markers);
544+
highlightValues(highs.toArray(new Highlight[0]));
545+
}
546+
547+
/**
548+
* Highlights any y-value at the given x-value in the given DataSet.
549+
* Provide -1 as the dataSetIndex to undo all highlighting.
550+
* This method will call the listener.
551+
** @param x The x-value to highlight
552552
* @param dataSetIndex The dataset index to search in
553553
* @param dataIndex The data index to search in (only used in CombinedChartView currently)
554554
*/
@@ -747,19 +747,21 @@ public ChartTouchListener getOnTouchListener() {
747747
/**
748748
* the view that represents the marker
749749
*/
750-
protected IMarker mMarker;
750+
protected List<IMarker> mMarkers = new ArrayList<>();
751751

752752
/**
753753
* draws all MarkerViews on the highlighted positions
754754
*/
755755
protected void drawMarkers(Canvas canvas) {
756756

757757
// if there is no marker view or drawing marker is disabled
758-
if (mMarker == null || !isDrawMarkersEnabled() || !valuesToHighlight()) {
758+
if (mMarkers == null || !isDrawMarkersEnabled() || !valuesToHighlight()) {
759759
return;
760760
}
761761

762-
for (Highlight highlight : mIndicesToHighlight) {
762+
for (int i = 0; i < mIndicesToHighlight.length; i++) {
763+
764+
Highlight highlight = mIndicesToHighlight[i];
763765

764766
// When changing data sets and calling animation functions, sometimes an erroneous highlight is generated
765767
// on the dataset that is removed. Null check to prevent crash
@@ -771,7 +773,7 @@ protected void drawMarkers(Canvas canvas) {
771773
Entry e = mData.getEntryForHighlight(highlight);
772774

773775
// make sure entry not null before using it
774-
if (e == null || set == null) {
776+
if (e == null) {
775777
continue;
776778
}
777779

@@ -789,10 +791,12 @@ protected void drawMarkers(Canvas canvas) {
789791
}
790792

791793
// callbacks to update the content
792-
mMarker.refreshContent(e, highlight);
794+
int markerIndex = i % mMarkers.size();
795+
IMarker markerItem = mMarkers.get(markerIndex);
796+
markerItem.refreshContent(e, highlight);
793797

794798
// draw the marker
795-
mMarker.draw(canvas, pos[0], pos[1]);
799+
markerItem.draw(canvas, pos[0], pos[1]);
796800
}
797801
}
798802

@@ -1136,18 +1140,22 @@ public void setTouchEnabled(boolean enabled) {
11361140
this.mTouchEnabled = enabled;
11371141
}
11381142

1139-
/**
1140-
* sets the marker that is displayed when a value is clicked on the chart
1141-
*/
1142-
public void setMarker(IMarker marker) {
1143-
mMarker = marker;
1144-
}
1143+
public void setMarkers(List<IMarker> marker) {
1144+
mMarkers = marker;
1145+
}
11451146

1146-
/**
1147+
/**
1148+
* sets the marker that is displayed when a value is clicked on the chart
1149+
*/
1150+
public void setMarker(IMarker marker) {
1151+
setMarkers(Collections.singletonList(marker));
1152+
}
1153+
/**
11471154
* returns the marker that is set as a marker view for the chart
1155+
11481156
*/
1149-
public IMarker getMarker() {
1150-
return mMarker;
1157+
public List<IMarker> getMarker() {
1158+
return mMarkers;
11511159
}
11521160

11531161
@Deprecated
@@ -1156,7 +1164,7 @@ public void setMarkerView(IMarker v) {
11561164
}
11571165

11581166
@Deprecated
1159-
public IMarker getMarkerView() {
1167+
public List<IMarker> getMarkerView() {
11601168
return getMarker();
11611169
}
11621170

@@ -1690,7 +1698,7 @@ public void setAccessibilitySummaryDescription(String accessibilitySummaryDescri
16901698
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
16911699

16921700
boolean completed = super.dispatchPopulateAccessibilityEvent(event);
1693-
Log.d(TAG, "Dispatch called for Chart <View> and completed as " + completed);
1701+
Log.d(LOG_TAG, "Dispatch called for Chart <View> and completed as " + completed);
16941702

16951703
event.getText().add(getAccessibilityDescription());
16961704

MPChartLib/src/main/java/com/github/mikephil/charting/charts/CombinedChart.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.util.AttributeSet;
77
import android.util.Log;
88

9+
import com.github.mikephil.charting.components.IMarker;
910
import com.github.mikephil.charting.data.BarData;
1011
import com.github.mikephil.charting.data.BubbleData;
1112
import com.github.mikephil.charting.data.CandleData;
@@ -231,12 +232,13 @@ public void setDrawOrder(DrawOrder[] order) {
231232
protected void drawMarkers(Canvas canvas) {
232233

233234
// if there is no marker view or drawing marker is disabled
234-
if (mMarker == null || !isDrawMarkersEnabled() || !valuesToHighlight()) {
235+
if (mMarkers == null || !isDrawMarkersEnabled() || !valuesToHighlight()) {
235236
return;
236237
}
237238

238-
for (Highlight highlight : mIndicesToHighlight) {
239+
for (int i = 0; i < mIndicesToHighlight.length; i++) {
239240

241+
Highlight highlight = mIndicesToHighlight[i];
240242
IDataSet set = mData.getDataSetByHighlight(highlight);
241243

242244
Entry e = mData.getEntryForHighlight(highlight);
@@ -259,10 +261,11 @@ protected void drawMarkers(Canvas canvas) {
259261
}
260262

261263
// callbacks to update the content
262-
mMarker.refreshContent(e, highlight);
264+
IMarker markerItem = mMarkers.get(i % mMarkers.size());
265+
markerItem.refreshContent(e, highlight);
263266

264267
// draw the marker
265-
mMarker.draw(canvas, pos[0], pos[1]);
268+
markerItem.draw(canvas, pos[0], pos[1]);
266269
}
267270
}
268271

app/src/main/java/info/appdev/chartexample/LineChartActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class LineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelec
6262

6363
// Set the marker to the chart
6464
mv.chartView = binding.chart1
65-
binding.chart1.marker = mv
65+
binding.chart1.marker.add(mv)
6666

6767
// enable scaling and dragging
6868
binding.chart1.isDragEnabled = true

app/src/main/java/info/appdev/chartexample/SpecificPositionsLineChartActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener,
8181
// to use for it
8282
val mv = MyMarkerView(this, R.layout.custom_marker_view)
8383
mv.chartView = mChart // For bounds control
84-
mChart!!.marker = mv // Set the marker to the chart
84+
mChart!!.marker.add(mv) // Set the marker to the chart
8585

8686
// x-axis limit line
8787
val llXAxis = LimitLine(10f, "Index 10")

0 commit comments

Comments
 (0)