use of com.github.mikephil.charting.data.Entry in project MPAndroidChart by PhilJay.
the class DataSetTest method testAddRemoveEntry.
@Test
public void testAddRemoveEntry() {
List<Entry> entries = new ArrayList<Entry>();
entries.add(new Entry(10, 10));
entries.add(new Entry(15, 2));
entries.add(new Entry(21, 5));
ScatterDataSet set = new ScatterDataSet(entries, "");
assertEquals(3, set.getEntryCount());
set.addEntryOrdered(new Entry(5, 1));
assertEquals(4, set.getEntryCount());
assertEquals(5, set.getXMin(), 0.01f);
assertEquals(21, set.getXMax(), 0.01f);
assertEquals(1f, set.getYMin(), 0.01f);
assertEquals(10f, set.getYMax(), 0.01f);
assertEquals(5, set.getEntryForIndex(0).getX(), 0.01f);
assertEquals(1, set.getEntryForIndex(0).getY(), 0.01f);
set.addEntryOrdered(new Entry(20, 50));
assertEquals(5, set.getEntryCount());
assertEquals(20, set.getEntryForIndex(3).getX(), 0.01f);
assertEquals(50, set.getEntryForIndex(3).getY(), 0.01f);
assertTrue(set.removeEntry(3));
assertEquals(4, set.getEntryCount());
assertEquals(21, set.getEntryForIndex(3).getX(), 0.01f);
assertEquals(5, set.getEntryForIndex(3).getY(), 0.01f);
assertEquals(5, set.getEntryForIndex(0).getX(), 0.01f);
assertEquals(1, set.getEntryForIndex(0).getY(), 0.01f);
assertTrue(set.removeFirst());
assertEquals(3, set.getEntryCount());
assertEquals(10, set.getEntryForIndex(0).getX(), 0.01f);
assertEquals(10, set.getEntryForIndex(0).getY(), 0.01f);
set.addEntryOrdered(new Entry(15, 3));
assertEquals(4, set.getEntryCount());
assertEquals(15, set.getEntryForIndex(1).getX(), 0.01f);
assertEquals(3, set.getEntryForIndex(1).getY(), 0.01f);
assertEquals(21, set.getEntryForIndex(3).getX(), 0.01f);
assertEquals(5, set.getEntryForIndex(3).getY(), 0.01f);
assertTrue(set.removeLast());
assertEquals(3, set.getEntryCount());
assertEquals(15, set.getEntryForIndex(2).getX(), 0.01f);
assertEquals(2, set.getEntryForIndex(2).getY(), 0.01f);
assertTrue(set.removeLast());
assertEquals(2, set.getEntryCount());
assertTrue(set.removeLast());
assertEquals(1, set.getEntryCount());
assertEquals(10, set.getEntryForIndex(0).getX(), 0.01f);
assertEquals(10, set.getEntryForIndex(0).getY(), 0.01f);
assertTrue(set.removeLast());
assertEquals(0, set.getEntryCount());
assertFalse(set.removeLast());
assertFalse(set.removeFirst());
}
use of com.github.mikephil.charting.data.Entry in project MPAndroidChart by PhilJay.
the class DataSetTest method testGetEntryForXValue.
@Test
public void testGetEntryForXValue() {
List<Entry> entries = new ArrayList<Entry>();
entries.add(new Entry(10, 10));
entries.add(new Entry(15, 5));
entries.add(new Entry(21, 5));
ScatterDataSet set = new ScatterDataSet(entries, "");
Entry closest = set.getEntryForXValue(17, Float.NaN, DataSet.Rounding.CLOSEST);
assertEquals(15, closest.getX(), 0.01f);
assertEquals(5, closest.getY(), 0.01f);
closest = set.getEntryForXValue(17, Float.NaN, DataSet.Rounding.DOWN);
assertEquals(15, closest.getX(), 0.01f);
assertEquals(5, closest.getY(), 0.01f);
closest = set.getEntryForXValue(15, Float.NaN, DataSet.Rounding.DOWN);
assertEquals(15, closest.getX(), 0.01f);
assertEquals(5, closest.getY(), 0.01f);
closest = set.getEntryForXValue(14, Float.NaN, DataSet.Rounding.DOWN);
assertEquals(10, closest.getX(), 0.01f);
assertEquals(10, closest.getY(), 0.01f);
closest = set.getEntryForXValue(17, Float.NaN, DataSet.Rounding.UP);
assertEquals(21, closest.getX(), 0.01f);
assertEquals(5, closest.getY(), 0.01f);
closest = set.getEntryForXValue(21, Float.NaN, DataSet.Rounding.UP);
assertEquals(21, closest.getX(), 0.01f);
assertEquals(5, closest.getY(), 0.01f);
closest = set.getEntryForXValue(21, Float.NaN, DataSet.Rounding.CLOSEST);
assertEquals(21, closest.getX(), 0.01f);
assertEquals(5, closest.getY(), 0.01f);
}
use of com.github.mikephil.charting.data.Entry in project MPAndroidChart by PhilJay.
the class Transformer method generateTransformedValuesLine.
/**
* Transforms an List of Entry into a float array containing the x and
* y values transformed with all matrices for the LINECHART.
*
* @param data
* @return
*/
public float[] generateTransformedValuesLine(ILineDataSet data, float phaseX, float phaseY, int min, int max) {
final int count = ((int) ((max - min) * phaseX) + 1) * 2;
if (valuePointsForGenerateTransformedValuesLine.length != count) {
valuePointsForGenerateTransformedValuesLine = new float[count];
}
float[] valuePoints = valuePointsForGenerateTransformedValuesLine;
for (int j = 0; j < count; j += 2) {
Entry e = data.getEntryForIndex(j / 2 + min);
if (e != null) {
valuePoints[j] = e.getX();
valuePoints[j + 1] = e.getY() * phaseY;
} else {
valuePoints[j] = 0;
valuePoints[j + 1] = 0;
}
}
getValueToPixelMatrix().mapPoints(valuePoints);
return valuePoints;
}
use of com.github.mikephil.charting.data.Entry in project Weather by Sparker0i.
the class GraphsFragment method createEntries.
public void createEntries() {
ArrayList<WeatherFort.WeatherList> list;
try {
list = mDescribable;
for (int i = 0; i < 10; ++i) {
long day = list.get(i).getDt();
long temp = list.get(i).getTemp().getDay();
long pressure = (long) list.get(i).getPressure();
long rain;
try {
rain = (long) list.get(i).getRain();
} catch (Exception ex) {
rain = 0;
}
long snow;
try {
snow = (long) list.get(i).getSnow();
} catch (Exception ex) {
snow = 0;
}
long wind = (long) list.get(i).getSpeed();
tempEntries.add(new Entry(i, temp));
rainEntries.add(new Entry(i, rain));
pressureEntries.add(new Entry(i, pressure));
snowEntries.add(new Entry(i, snow));
windEntries.add(new Entry(i, wind));
Log.i("Added", "Entry : " + i + " " + temp);
dates[i] = getDay(day);
Log.i("Added", "Day : " + dates[i]);
}
} catch (Exception ex) {
ex.printStackTrace();
Log.i("Caught", "JSON Ex");
}
}
use of com.github.mikephil.charting.data.Entry in project Gadgetbridge by Freeyourgadget.
the class AbstractChartFragment method refresh.
protected DefaultChartsData<CombinedData> refresh(GBDevice gbDevice, List<? extends ActivitySample> samples) {
// Calendar cal = GregorianCalendar.getInstance();
// cal.clear();
TimestampTranslation tsTranslation = new TimestampTranslation();
// Date date;
// String dateStringFrom = "";
// String dateStringTo = "";
// ArrayList<String> xLabels = null;
LOG.info("" + getTitle() + ": number of samples:" + samples.size());
CombinedData combinedData;
if (samples.size() > 1) {
boolean annotate = true;
boolean use_steps_as_movement;
int last_type = ActivityKind.TYPE_UNKNOWN;
int numEntries = samples.size();
List<BarEntry> activityEntries = new ArrayList<>(numEntries);
boolean hr = supportsHeartrate(gbDevice);
List<Entry> heartrateEntries = hr ? new ArrayList<Entry>(numEntries) : null;
// this is kinda inefficient...
List<Integer> colors = new ArrayList<>(numEntries);
int lastHrSampleIndex = -1;
for (int i = 0; i < numEntries; i++) {
ActivitySample sample = samples.get(i);
int type = sample.getKind();
int ts = tsTranslation.shorten(sample.getTimestamp());
// System.out.println(ts);
// ts = i;
// determine start and end dates
// if (i == 0) {
// cal.setTimeInMillis(ts * 1000L); // make sure it's converted to long
// date = cal.getTime();
// dateStringFrom = dateFormat.format(date);
// } else if (i == samples.size() - 1) {
// cal.setTimeInMillis(ts * 1000L); // same here
// date = cal.getTime();
// dateStringTo = dateFormat.format(date);
// }
float movement = sample.getIntensity();
float value = movement;
switch(type) {
case ActivityKind.TYPE_DEEP_SLEEP:
value += SleepUtils.Y_VALUE_DEEP_SLEEP;
colors.add(akDeepSleep.color);
break;
case ActivityKind.TYPE_LIGHT_SLEEP:
colors.add(akLightSleep.color);
break;
case ActivityKind.TYPE_NOT_WORN:
//a small value, just to show something on the graphs
value = SleepUtils.Y_VALUE_DEEP_SLEEP;
colors.add(akNotWorn.color);
break;
default:
// short steps = sample.getSteps();
// if (use_steps_as_movement && steps != 0) {
// // I'm not sure using steps for this is actually a good idea
// movement = steps;
// }
// value = ((float) movement) / movement_divisor;
colors.add(akActivity.color);
}
activityEntries.add(createBarEntry(value, ts));
if (hr && isValidHeartRateValue(sample.getHeartRate())) {
if (lastHrSampleIndex > -1 && ts - lastHrSampleIndex > 60 * HeartRateUtils.MAX_HR_MEASUREMENTS_GAP_MINUTES) {
heartrateEntries.add(createLineEntry(0, lastHrSampleIndex + 1));
heartrateEntries.add(createLineEntry(0, ts - 1));
}
heartrateEntries.add(createLineEntry(sample.getHeartRate(), ts));
lastHrSampleIndex = ts;
}
String xLabel = "";
if (annotate) {
// cal.setTimeInMillis((ts + tsOffset) * 1000L);
// date = cal.getTime();
// String dateString = annotationDateFormat.format(date);
// xLabel = dateString;
// if (last_type != type) {
// if (isSleep(last_type) && !isSleep(type)) {
// // woken up
// LimitLine line = new LimitLine(i, dateString);
// line.enableDashedLine(8, 8, 0);
// line.setTextColor(Color.WHITE);
// line.setTextSize(15);
// chart.getXAxis().addLimitLine(line);
// } else if (!isSleep(last_type) && isSleep(type)) {
// // fallen asleep
// LimitLine line = new LimitLine(i, dateString);
// line.enableDashedLine(8, 8, 0);
// line.setTextSize(15);
// line.setTextColor(Color.WHITE);
// chart.getXAxis().addLimitLine(line);
// }
// }
// last_type = type;
}
}
BarDataSet activitySet = createActivitySet(activityEntries, colors, "Activity");
// create a data object with the datasets
// combinedData = new CombinedData(xLabels);
combinedData = new CombinedData();
List<IBarDataSet> list = new ArrayList<>();
list.add(activitySet);
BarData barData = new BarData(list);
barData.setBarWidth(100f);
// barData.setGroupSpace(0);
combinedData.setData(barData);
if (hr && heartrateEntries.size() > 0) {
LineDataSet heartrateSet = createHeartrateSet(heartrateEntries, "Heart Rate");
LineData lineData = new LineData(heartrateSet);
combinedData.setData(lineData);
}
// chart.setDescription(getString(R.string.sleep_activity_date_range, dateStringFrom, dateStringTo));
// chart.setDescriptionPosition(?, ?);
} else {
combinedData = new CombinedData();
}
IAxisValueFormatter xValueFormatter = new SampleXLabelFormatter(tsTranslation);
return new DefaultChartsData(combinedData, xValueFormatter);
}
Aggregations