Skip to content

Commit 8a37bbd

Browse files
committed
Work on highlight rendering and markerview
1 parent e853a80 commit 8a37bbd

12 files changed

Lines changed: 215 additions & 210 deletions

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ protected void drawMarkers(Canvas canvas) {
760760
* @return
761761
*/
762762
protected float[] getMarkerPosition(Highlight high) {
763-
return new float[]{high.getXPx(), high.getYPx()};
763+
return new float[]{high.getDrawX(), high.getDrawY()};
764764
}
765765

766766
/**

‎MPChartLib/src/main/java/com/github/mikephil/charting/charts/HorizontalBarChart.java‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.github.mikephil.charting.charts;
22

33
import android.content.Context;
4-
import android.graphics.Matrix;
54
import android.graphics.PointF;
65
import android.graphics.RectF;
76
import android.util.AttributeSet;
@@ -126,9 +125,14 @@ public void calculateOffsets() {
126125
@Override
127126
protected void prepareValuePxMatrix() {
128127
mRightAxisTransformer.prepareMatrixValuePx(mAxisRight.mAxisMinimum, mAxisRight.mAxisRange, mXAxis.mAxisRange,
129-
mXAxis.mAxisMinimum);
128+
mXAxis.mAxisMinimum);
130129
mLeftAxisTransformer.prepareMatrixValuePx(mAxisLeft.mAxisMinimum, mAxisLeft.mAxisRange, mXAxis.mAxisRange,
131-
mXAxis.mAxisMinimum);
130+
mXAxis.mAxisMinimum);
131+
}
132+
133+
@Override
134+
protected float[] getMarkerPosition(Highlight high) {
135+
return new float[]{high.getDrawY(), high.getDrawX()};
132136
}
133137

134138
@Override
@@ -181,7 +185,7 @@ public PointF getPosition(Entry e, AxisDependency axis) {
181185
public Highlight getHighlightByTouchPoint(float x, float y) {
182186

183187
if (mData == null) {
184-
if(mLogEnabled)
188+
if (mLogEnabled)
185189
Log.e(LOG_TAG, "Can't select by touch. No data set.");
186190
return null;
187191
} else
@@ -191,14 +195,14 @@ public Highlight getHighlightByTouchPoint(float x, float y) {
191195
@Override
192196
public float getLowestVisibleX() {
193197
PointD pos = getTransformer(AxisDependency.LEFT).getValuesByTouchPoint(mViewPortHandler.contentLeft(),
194-
mViewPortHandler.contentBottom());
198+
mViewPortHandler.contentBottom());
195199
return (float) Math.max(mXAxis.mAxisMinimum, pos.y);
196200
}
197201

198202
@Override
199203
public float getHighestVisibleX() {
200204
PointD pos = getTransformer(AxisDependency.LEFT).getValuesByTouchPoint(mViewPortHandler.contentLeft(),
201-
mViewPortHandler.contentTop());
205+
mViewPortHandler.contentTop());
202206
return (float) Math.min(mXAxis.mAxisMaximum, pos.y);
203207
}
204208
}

‎MPChartLib/src/main/java/com/github/mikephil/charting/highlight/Highlight.java‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ public class Highlight {
5555
*/
5656
private YAxis.AxisDependency axis;
5757

58+
/**
59+
* the x-position (pixels) on which this highlight object was last drawn
60+
*/
61+
private float mDrawX;
62+
63+
/**
64+
* the y-position (pixels) on which this highlight object was last drawn
65+
*/
66+
private float mDrawY;
67+
5868
public Highlight(float x, int dataSetIndex) {
5969
this.mX = x;
6070
this.mDataSetIndex = dataSetIndex;
@@ -179,6 +189,35 @@ public YAxis.AxisDependency getAxis() {
179189
return axis;
180190
}
181191

192+
/**
193+
* Sets the x- and y-position (pixels) where this highlight was last drawn.
194+
*
195+
* @param x
196+
* @param y
197+
*/
198+
public void setDraw(float x, float y) {
199+
this.mDrawX = x;
200+
this.mDrawY = y;
201+
}
202+
203+
/**
204+
* Returns the x-position in pixels where this highlight object was last drawn.
205+
*
206+
* @return
207+
*/
208+
public float getDrawX() {
209+
return mDrawX;
210+
}
211+
212+
/**
213+
* Returns the y-position in pixels where this highlight object was last drawn.
214+
*
215+
* @return
216+
*/
217+
public float getDrawY() {
218+
return mDrawY;
219+
}
220+
182221
/**
183222
* Returns true if this highlight object is equal to the other (compares
184223
* xIndex and dataSetIndex)

‎MPChartLib/src/main/java/com/github/mikephil/charting/highlight/RadarHighlighter.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ protected List<Highlight> getHighlightsAtIndex(int index) {
7777
y * factor * phaseY,
7878
sliceangle * index * phaseX + mChart.getRotationAngle());
7979

80-
vals.add(new Highlight(entry.getX(), entry.getY(), p.x, p.y, i, dataSet.getAxisDependency()));
80+
vals.add(new Highlight(index, entry.getY(), p.x, p.y, i, dataSet.getAxisDependency()));
8181
}
8282

8383
return vals;

‎MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.java‎

Lines changed: 30 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import java.util.List;
2121

22-
public class BarChartRenderer extends DataRenderer {
22+
public class BarChartRenderer extends BarLineScatterCandleBubbleRenderer {
2323

2424
protected BarDataProvider mChart;
2525

@@ -328,82 +328,48 @@ public void drawHighlighted(Canvas c, Highlight[] indices) {
328328

329329
for (Highlight high : indices) {
330330

331-
final int minDataSetIndex = high.getDataSetIndex() == -1
332-
? 0
333-
: high.getDataSetIndex();
334-
final int maxDataSetIndex = high.getDataSetIndex() == -1
335-
? barData.getDataSetCount()
336-
: (high.getDataSetIndex() + 1);
337-
if (maxDataSetIndex - minDataSetIndex < 1) continue;
331+
IBarDataSet set = barData.getDataSetByIndex(high.getDataSetIndex());
338332

339-
for (int dataSetIndex = minDataSetIndex;
340-
dataSetIndex < maxDataSetIndex;
341-
dataSetIndex++) {
333+
if (set == null || !set.isHighlightEnabled())
334+
continue;
342335

343-
IBarDataSet set = barData.getDataSetByIndex(dataSetIndex);
336+
BarEntry e = set.getEntryForXPos(high.getX());
344337

345-
if (set == null || !set.isHighlightEnabled())
346-
continue;
347-
348-
Transformer trans = mChart.getTransformer(set.getAxisDependency());
338+
if (!isInBoundsX(e, set))
339+
continue;
349340

350-
mHighlightPaint.setColor(set.getHighLightColor());
351-
mHighlightPaint.setAlpha(set.getHighLightAlpha());
341+
Transformer trans = mChart.getTransformer(set.getAxisDependency());
352342

353-
float x = high.getX();
343+
mHighlightPaint.setColor(set.getHighLightColor());
344+
mHighlightPaint.setAlpha(set.getHighLightAlpha());
354345

355-
BarEntry e = set.getEntryForXPos(x);
356-
float entryIndex = set.getEntryIndex(e);
346+
boolean isStack = high.getStackIndex() < 0 ? false : true;
357347

358-
if (e == null || entryIndex > set.getEntryCount() * mAnimator.getPhaseX())
359-
continue;
348+
final float y1;
349+
final float y2;
360350

361-
boolean isStack = high.getStackIndex() < 0 ? false : true;
351+
if (isStack) {
352+
y1 = high.getRange().from;
353+
y2 = high.getRange().to;
354+
} else {
355+
y1 = e.getY();
356+
y2 = 0.f;
357+
}
362358

363-
final float y1;
364-
final float y2;
359+
prepareBarHighlight(e.getX(), y1, y2, barData.getBarWidth() / 2f, trans);
365360

366-
if (isStack) {
367-
y1 = high.getRange().from;
368-
y2 = high.getRange().to;
369-
} else {
370-
y1 = e.getY();
371-
y2 = 0.f;
372-
}
361+
setHighlightDrawPos(high, mBarRect);
373362

374-
prepareBarHighlight(e.getX(), y1, y2, barData.getBarWidth() / 2f, trans);
375-
376-
c.drawRect(mBarRect, mHighlightPaint);
377-
378-
// if (mChart.isDrawHighlightArrowEnabled()) {
379-
//
380-
// mHighlightPaint.setAlpha(255);
381-
//
382-
// // distance between highlight arrow and bar
383-
// float offsetY = mAnimator.getPhaseY() * 0.07f;
384-
//
385-
// float[] values = new float[9];
386-
// trans.getPixelToValueMatrix().getValues(values);
387-
// final float xToYRel = Math.abs(
388-
// values[Matrix.MSCALE_Y] / values[Matrix.MSCALE_X]);
389-
//
390-
// final float arrowWidth = barData.getBarWidth();
391-
// final float arrowWidthHalf = arrowWidth / 2f;
392-
// final float arrowHeight = arrowWidth * xToYRel;
393-
//
394-
// final float yArrow = (y1 > -y2 ? y1 : y1) * mAnimator.getPhaseY();
395-
//
396-
// Path arrow = new Path();
397-
// arrow.moveTo(e.getX() - arrowWidthHalf, yArrow + offsetY + arrowHeight);
398-
// arrow.lineTo(e.getX(), yArrow + offsetY);
399-
// arrow.lineTo(e.getX() + arrowWidthHalf, yArrow + offsetY + arrowHeight);
400-
//
401-
// trans.pathValueToPixel(arrow);
402-
// c.drawPath(arrow, mHighlightPaint);
403-
// }
404-
}
363+
c.drawRect(mBarRect, mHighlightPaint);
405364
}
365+
}
406366

367+
/**
368+
* Sets the drawing position of the highlight object based on the riven bar-rect.
369+
* @param high
370+
*/
371+
protected void setHighlightDrawPos(Highlight high, RectF bar) {
372+
high.setDraw(bar.centerX(), bar.top);
407373
}
408374

409375
@Override

‎MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarLineScatterCandleBubbleRenderer.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.mikephil.charting.renderer;
22

33
import com.github.mikephil.charting.animation.ChartAnimator;
4+
import com.github.mikephil.charting.data.BarEntry;
45
import com.github.mikephil.charting.data.DataSet;
56
import com.github.mikephil.charting.data.Entry;
67
import com.github.mikephil.charting.interfaces.dataprovider.BarLineScatterCandleBubbleDataProvider;
@@ -17,6 +18,27 @@ public BarLineScatterCandleBubbleRenderer(ChartAnimator animator, ViewPortHandle
1718
super(animator, viewPortHandler);
1819
}
1920

21+
/**
22+
* Checks if the provided entry object is in bounds for drawing considering the current animation phase.
23+
*
24+
* @param e
25+
* @param set
26+
* @return
27+
*/
28+
protected boolean isInBoundsX(Entry e, IBarLineScatterCandleBubbleDataSet set) {
29+
30+
if (e == null)
31+
return false;
32+
33+
float entryIndex = set.getEntryIndex(e);
34+
35+
if (e == null || entryIndex >= set.getEntryCount() * mAnimator.getPhaseX()) {
36+
return false;
37+
} else {
38+
return true;
39+
}
40+
}
41+
2042
/**
2143
* Calculates and returns the x-bounds for the given DataSet in terms of index in their values array. This
2244
* includes minimum and maximum visible x, as well as range.

‎MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BubbleChartRenderer.java‎

Lines changed: 39 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -184,76 +184,62 @@ public void drawHighlighted(Canvas c, Highlight[] indices) {
184184

185185
for (Highlight high : indices) {
186186

187-
final int minDataSetIndex = high.getDataSetIndex() == -1
188-
? 0
189-
: high.getDataSetIndex();
190-
final int maxDataSetIndex = high.getDataSetIndex() == -1
191-
? bubbleData.getDataSetCount()
192-
: (high.getDataSetIndex() + 1);
193-
if (maxDataSetIndex - minDataSetIndex < 1) continue;
187+
IBubbleDataSet set = bubbleData.getDataSetByIndex(high.getDataSetIndex());
194188

195-
for (int dataSetIndex = minDataSetIndex; dataSetIndex < maxDataSetIndex; dataSetIndex++) {
196-
197-
IBubbleDataSet dataSet = bubbleData.getDataSetByIndex(dataSetIndex);
198-
199-
if (dataSet == null || !dataSet.isHighlightEnabled())
200-
continue;
201-
202-
final BubbleEntry entry = dataSet.getEntryForXPos(high.getX());
189+
if (set == null || !set.isHighlightEnabled())
190+
continue;
203191

204-
if (entry == null)
205-
continue;
192+
final BubbleEntry entry = set.getEntryForXPos(high.getX());
206193

207-
XBounds bounds = getXBounds(mChart, dataSet);
194+
if (!isInBoundsX(entry, set))
195+
continue;
208196

209-
Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
197+
Transformer trans = mChart.getTransformer(set.getAxisDependency());
210198

211-
sizeBuffer[0] = 0f;
212-
sizeBuffer[2] = 1f;
199+
sizeBuffer[0] = 0f;
200+
sizeBuffer[2] = 1f;
213201

214-
trans.pointValuesToPixel(sizeBuffer);
202+
trans.pointValuesToPixel(sizeBuffer);
215203

216-
boolean normalizeSize = dataSet.isNormalizeSizeEnabled();
204+
boolean normalizeSize = set.isNormalizeSizeEnabled();
217205

218-
// calcualte the full width of 1 step on the x-axis
219-
final float maxBubbleWidth = Math.abs(sizeBuffer[2] - sizeBuffer[0]);
220-
final float maxBubbleHeight = Math.abs(
221-
mViewPortHandler.contentBottom() - mViewPortHandler.contentTop());
222-
final float referenceSize = Math.min(maxBubbleHeight, maxBubbleWidth);
206+
// calcualte the full width of 1 step on the x-axis
207+
final float maxBubbleWidth = Math.abs(sizeBuffer[2] - sizeBuffer[0]);
208+
final float maxBubbleHeight = Math.abs(
209+
mViewPortHandler.contentBottom() - mViewPortHandler.contentTop());
210+
final float referenceSize = Math.min(maxBubbleHeight, maxBubbleWidth);
223211

224-
pointBuffer[0] = entry.getX();
225-
pointBuffer[1] = (entry.getY()) * phaseY;
226-
trans.pointValuesToPixel(pointBuffer);
212+
pointBuffer[0] = entry.getX();
213+
pointBuffer[1] = (entry.getY()) * phaseY;
214+
trans.pointValuesToPixel(pointBuffer);
227215

228-
float shapeHalf = getShapeSize(entry.getSize(),
229-
dataSet.getMaxSize(),
230-
referenceSize,
231-
normalizeSize) / 2f;
216+
high.setDraw(pointBuffer[0], pointBuffer[1]);
232217

233-
if (!mViewPortHandler.isInBoundsTop(pointBuffer[1] + shapeHalf)
234-
|| !mViewPortHandler.isInBoundsBottom(pointBuffer[1] - shapeHalf))
235-
continue;
218+
float shapeHalf = getShapeSize(entry.getSize(),
219+
set.getMaxSize(),
220+
referenceSize,
221+
normalizeSize) / 2f;
236222

237-
if (!mViewPortHandler.isInBoundsLeft(pointBuffer[0] + shapeHalf))
238-
continue;
223+
if (!mViewPortHandler.isInBoundsTop(pointBuffer[1] + shapeHalf)
224+
|| !mViewPortHandler.isInBoundsBottom(pointBuffer[1] - shapeHalf))
225+
continue;
239226

240-
if (!mViewPortHandler.isInBoundsRight(pointBuffer[0] - shapeHalf))
241-
break;
227+
if (!mViewPortHandler.isInBoundsLeft(pointBuffer[0] + shapeHalf))
228+
continue;
242229

243-
if (high.getX() < bounds.min || high.getX() > bounds.max)
244-
continue;
230+
if (!mViewPortHandler.isInBoundsRight(pointBuffer[0] - shapeHalf))
231+
break;
245232

246-
final int originalColor = dataSet.getColor((int) entry.getX());
233+
final int originalColor = set.getColor((int) entry.getX());
247234

248-
Color.RGBToHSV(Color.red(originalColor), Color.green(originalColor),
249-
Color.blue(originalColor), _hsvBuffer);
250-
_hsvBuffer[2] *= 0.5f;
251-
final int color = Color.HSVToColor(Color.alpha(originalColor), _hsvBuffer);
235+
Color.RGBToHSV(Color.red(originalColor), Color.green(originalColor),
236+
Color.blue(originalColor), _hsvBuffer);
237+
_hsvBuffer[2] *= 0.5f;
238+
final int color = Color.HSVToColor(Color.alpha(originalColor), _hsvBuffer);
252239

253-
mHighlightPaint.setColor(color);
254-
mHighlightPaint.setStrokeWidth(dataSet.getHighlightCircleWidth());
255-
c.drawCircle(pointBuffer[0], pointBuffer[1], shapeHalf, mHighlightPaint);
256-
}
240+
mHighlightPaint.setColor(color);
241+
mHighlightPaint.setStrokeWidth(set.getHighlightCircleWidth());
242+
c.drawCircle(pointBuffer[0], pointBuffer[1], shapeHalf, mHighlightPaint);
257243
}
258244
}
259245
}

0 commit comments

Comments
 (0)