use of com.github.mikephil.charting.data.BarData in project MPAndroidChart by PhilJay.
the class HorizontalBarHighlighter method getHighlight.
@Override
public Highlight getHighlight(float x, float y) {
BarData barData = mChart.getBarData();
MPPointD pos = getValsForTouch(y, x);
Highlight high = getHighlightForX((float) pos.y, y, x);
if (high == null)
return null;
IBarDataSet set = barData.getDataSetByIndex(high.getDataSetIndex());
if (set.isStacked()) {
return getStackedHighlight(high, set, (float) pos.y, (float) pos.x);
}
MPPointD.recycleInstance(pos);
return high;
}
use of com.github.mikephil.charting.data.BarData in project MPAndroidChart by PhilJay.
the class BarDataTest method testGroupBars.
@Test
public void testGroupBars() {
float groupSpace = 5f;
float barSpace = 1f;
List<BarEntry> values1 = new ArrayList<>();
List<BarEntry> values2 = new ArrayList<>();
for (int i = 0; i < 5; i++) {
values1.add(new BarEntry(i, 50));
values2.add(new BarEntry(i, 60));
}
BarDataSet barDataSet1 = new BarDataSet(values1, "Set1");
BarDataSet barDataSet2 = new BarDataSet(values2, "Set2");
BarData data = new BarData(barDataSet1, barDataSet2);
data.setBarWidth(10f);
float groupWidth = data.getGroupWidth(groupSpace, barSpace);
assertEquals(27f, groupWidth, 0.01f);
assertEquals(0f, values1.get(0).getX(), 0.01f);
assertEquals(1f, values1.get(1).getX(), 0.01f);
data.groupBars(1000, groupSpace, barSpace);
// 1000 + 2.5 + 0.5 + 5
assertEquals(1008f, values1.get(0).getX(), 0.01f);
assertEquals(1019f, values2.get(0).getX(), 0.01f);
assertEquals(1035f, values1.get(1).getX(), 0.01f);
assertEquals(1046f, values2.get(1).getX(), 0.01f);
data.groupBars(-1000, groupSpace, barSpace);
assertEquals(-992f, values1.get(0).getX(), 0.01f);
assertEquals(-981f, values2.get(0).getX(), 0.01f);
assertEquals(-965f, values1.get(1).getX(), 0.01f);
assertEquals(-954f, values2.get(1).getX(), 0.01f);
data.setBarWidth(20f);
groupWidth = data.getGroupWidth(groupSpace, barSpace);
assertEquals(47f, groupWidth, 0.01f);
data.setBarWidth(10f);
data.groupBars(-20, groupSpace, barSpace);
assertEquals(-12f, values1.get(0).getX(), 0.01f);
assertEquals(-1f, values2.get(0).getX(), 0.01f);
assertEquals(15f, values1.get(1).getX(), 0.01f);
assertEquals(26f, values2.get(1).getX(), 0.01f);
}
use of com.github.mikephil.charting.data.BarData 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);
}
use of com.github.mikephil.charting.data.BarData in project Gadgetbridge by Freeyourgadget.
the class LiveActivityFragment method setupCommonChart.
private BarDataSet setupCommonChart(CustomBarChart chart, BarEntry entry, String title) {
chart.setSinglAnimationEntry(entry);
// chart.getXAxis().setPosition(XAxis.XAxisPosition.TOP);
chart.getXAxis().setDrawLabels(false);
chart.getXAxis().setEnabled(false);
chart.getXAxis().setTextColor(CHART_TEXT_COLOR);
chart.getAxisLeft().setTextColor(CHART_TEXT_COLOR);
chart.setBackgroundColor(BACKGROUND_COLOR);
chart.getDescription().setTextColor(DESCRIPTION_COLOR);
chart.getDescription().setText(title);
// chart.setNoDataTextDescription("");
chart.setNoDataText("");
chart.getAxisRight().setEnabled(false);
List<BarEntry> entries = new ArrayList<>();
List<Integer> colors = new ArrayList<>();
entries.add(new BarEntry(0, 0));
entries.add(entry);
entries.add(new BarEntry(2, 0));
colors.add(akActivity.color);
colors.add(akActivity.color);
colors.add(akActivity.color);
// //we don't want labels
// xLabels.add("");
// xLabels.add("");
// xLabels.add("");
BarDataSet set = new BarDataSet(entries, "");
set.setDrawValues(false);
set.setColors(colors);
BarData data = new BarData(set);
// data.setGroupSpace(0);
chart.setData(data);
chart.getLegend().setEnabled(false);
return set;
}
use of com.github.mikephil.charting.data.BarData in project Gadgetbridge by Freeyourgadget.
the class AbstractWeekChartFragment method refreshWeekBeforeData.
private DefaultChartsData<BarData> refreshWeekBeforeData(DBHandler db, BarChart barChart, Calendar day, GBDevice device) {
// do not modify the caller's argument
day = (Calendar) day.clone();
day.add(Calendar.DATE, -7);
List<BarEntry> entries = new ArrayList<>();
ArrayList<String> labels = new ArrayList<String>();
for (int counter = 0; counter < 7; counter++) {
ActivityAmounts amounts = getActivityAmountsForDay(db, day, device);
entries.add(new BarEntry(counter, getTotalsForActivityAmounts(amounts)));
labels.add(day.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, mLocale));
day.add(Calendar.DATE, 1);
}
BarDataSet set = new BarDataSet(entries, "");
set.setColors(getColors());
set.setValueFormatter(getBarValueFormatter());
BarData barData = new BarData(set);
//prevent tearing other graph elements with the black text. Another approach would be to hide the values cmpletely with data.setDrawValues(false);
barData.setValueTextColor(Color.GRAY);
LimitLine target = new LimitLine(mTargetValue);
barChart.getAxisLeft().removeAllLimitLines();
barChart.getAxisLeft().addLimitLine(target);
return new DefaultChartsData(barData, new PreformattedXIndexLabelFormatter(labels));
}
Aggregations