Skip to content

Commit 3398cf4

Browse files
committed
Refactoring, prepare example for testing highlight
1 parent 323329d commit 3398cf4

4 files changed

Lines changed: 41 additions & 18 deletions

File tree

‎MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity2.java‎

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
package com.xxmassdeveloper.mpchartexample;
33

44
import android.graphics.Color;
5-
import android.graphics.Paint;
65
import android.os.Bundle;
76
import android.util.Log;
87
import android.view.Menu;
@@ -16,7 +15,6 @@
1615
import com.github.mikephil.charting.charts.LineChart;
1716
import com.github.mikephil.charting.components.Legend;
1817
import com.github.mikephil.charting.components.Legend.LegendForm;
19-
import com.github.mikephil.charting.components.Legend.LegendPosition;
2018
import com.github.mikephil.charting.components.XAxis;
2119
import com.github.mikephil.charting.components.YAxis;
2220
import com.github.mikephil.charting.components.YAxis.AxisDependency;
@@ -243,6 +241,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
243241
}
244242
case R.id.animateX: {
245243
mChart.animateX(3000);
244+
//mChart.highlightValue(9.7f, 1, false);
246245
break;
247246
}
248247
case R.id.animateY: {
@@ -287,30 +286,39 @@ private void setData(int count, float range) {
287286

288287
for (int i = 0; i < count; i++) {
289288
float mult = range / 2f;
290-
float val = (float) (Math.random() * mult) + 50;// + (float)
291-
// ((mult *
292-
// 0.1) / 10);
289+
float val = (float) (Math.random() * mult) + 50;
293290
yVals1.add(new Entry(i, val));
294291
}
295292

296293
ArrayList<Entry> yVals2 = new ArrayList<Entry>();
297294

298-
for (int i = 0; i < count; i++) {
295+
for (int i = 0; i < count-1; i++) {
299296
float mult = range;
300-
float val = (float) (Math.random() * mult) + 450;// + (float)
301-
// ((mult *
302-
// 0.1) / 10);
297+
float val = (float) (Math.random() * mult) + 450;
303298
yVals2.add(new Entry(i, val));
299+
// if(i == 10) {
300+
// yVals2.add(new Entry(i, val + 50));
301+
// }
302+
}
303+
304+
ArrayList<Entry> yVals3 = new ArrayList<Entry>();
305+
306+
for (int i = 0; i < count; i++) {
307+
float mult = range;
308+
float val = (float) (Math.random() * mult) + 500;
309+
yVals3.add(new Entry(i, val));
304310
}
305311

306-
LineDataSet set1, set2;
312+
LineDataSet set1, set2, set3;
307313

308314
if (mChart.getData() != null &&
309315
mChart.getData().getDataSetCount() > 0) {
310316
set1 = (LineDataSet) mChart.getData().getDataSetByIndex(0);
311317
set2 = (LineDataSet) mChart.getData().getDataSetByIndex(1);
318+
set3 = (LineDataSet) mChart.getData().getDataSetByIndex(2);
312319
set1.setValues(yVals1);
313320
set2.setValues(yVals2);
321+
set3.setValues(yVals3);
314322
mChart.getData().notifyDataChanged();
315323
mChart.notifyDataSetChanged();
316324
} else {
@@ -344,12 +352,19 @@ private void setData(int count, float range) {
344352
set2.setHighLightColor(Color.rgb(244, 117, 117));
345353
//set2.setFillFormatter(new MyFillFormatter(900f));
346354

347-
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
348-
dataSets.add(set1); // add the datasets
349-
dataSets.add(set2);
355+
set3 = new LineDataSet(yVals3, "DataSet 3");
356+
set3.setAxisDependency(AxisDependency.RIGHT);
357+
set3.setColor(Color.YELLOW);
358+
set3.setCircleColor(Color.WHITE);
359+
set3.setLineWidth(2f);
360+
set3.setCircleRadius(3f);
361+
set3.setFillAlpha(65);
362+
set3.setFillColor(ColorTemplate.colorWithAlpha(Color.YELLOW, 200));
363+
set3.setDrawCircleHole(false);
364+
set3.setHighLightColor(Color.rgb(244, 117, 117));
350365

351366
// create a data object with the datasets
352-
LineData data = new LineData(dataSets);
367+
LineData data = new LineData(set1, set2, set3);
353368
data.setValueTextColor(Color.WHITE);
354369
data.setValueTextSize(9f);
355370

‎MPChartLib/src/main/java/com/github/mikephil/charting/data/DataSet.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,14 @@ public List<T> getEntriesForXValue(float xValue) {
351351
int m = (high + low) / 2;
352352
T entry = mValues.get(m);
353353

354+
// if we have a match
354355
if (xValue == entry.getX()) {
355356
while (m > 0 && mValues.get(m - 1).getX() == xValue)
356357
m--;
357358

358359
high = mValues.size();
360+
361+
// loop over all "equal" entries
359362
for (; m < high; m++) {
360363
entry = mValues.get(m);
361364
if (entry.getX() == xValue) {

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import com.github.mikephil.charting.utils.Utils;
1919
import com.github.mikephil.charting.utils.ViewPortHandler;
2020

21-
import java.util.List;
22-
2321
public class RadarChartRenderer extends LineRadarRenderer {
2422

2523
protected RadarChart mChart;
@@ -306,7 +304,7 @@ public void drawHighlighted(Canvas c, Highlight[] indices) {
306304
}
307305

308306
if (set.getHighlightCircleStrokeAlpha() < 255) {
309-
strokeColor = ColorTemplate.getColorWithAlphaComponent(strokeColor, set.getHighlightCircleStrokeAlpha());
307+
strokeColor = ColorTemplate.colorWithAlpha(strokeColor, set.getHighlightCircleStrokeAlpha());
310308
}
311309

312310
drawHighlightCircle(c,

‎MPChartLib/src/main/java/com/github/mikephil/charting/utils/ColorTemplate.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,14 @@ public static int getHoloBlue() {
7878
return Color.rgb(51, 181, 229);
7979
}
8080

81-
public static int getColorWithAlphaComponent(int color, int alpha) {
81+
/**
82+
* Sets the alpha component of the given color.
83+
*
84+
* @param color
85+
* @param alpha 0 - 255
86+
* @return
87+
*/
88+
public static int colorWithAlpha(int color, int alpha) {
8289
return (color & 0xffffff) | ((alpha & 0xff) << 24);
8390
}
8491

0 commit comments

Comments
 (0)