use of com.github.mikephil.charting.data.PieDataSet in project carat by amplab.
the class SummaryFragment method generatePieData.
protected PieData generatePieData() {
ArrayList<Entry> entries = new ArrayList<Entry>();
ArrayList<String> xVals = new ArrayList<String>();
xVals.add(getString(R.string.chart_wellbehaved));
xVals.add(getString(R.string.chart_hogs));
xVals.add(getString(R.string.chart_bugs));
int wellbehaved = mMainActivity.mWellbehaved;
int hogs = mMainActivity.mHogs;
int bugs = mMainActivity.mBugs;
entries.add(new Entry((float) (wellbehaved), 1));
entries.add(new Entry((float) (hogs), 2));
entries.add(new Entry((float) (bugs), 3));
PieDataSet ds1 = new PieDataSet(entries, getString(R.string.summary_chart_center_text));
ds1.setColors(Constants.CARAT_COLORS);
ds1.setSliceSpace(2f);
PieData d = new PieData(xVals, ds1);
return d;
}
use of com.github.mikephil.charting.data.PieDataSet in project carat by amplab.
the class Chart method prepareLegend.
/**
* Generates an automatically prepared legend depending on the DataSets in
* the chart and their colors.
*/
public void prepareLegend() {
ArrayList<String> labels = new ArrayList<String>();
ArrayList<Integer> colors = new ArrayList<Integer>();
// loop for building up the colors and labels used in the legend
for (int i = 0; i < mData.getDataSetCount(); i++) {
DataSet<? extends Entry> dataSet = mData.getDataSetByIndex(i);
ArrayList<Integer> clrs = dataSet.getColors();
int entryCount = dataSet.getEntryCount();
// if we have a barchart with stacked bars
if (dataSet instanceof BarDataSet && ((BarDataSet) dataSet).getStackSize() > 1) {
BarDataSet bds = (BarDataSet) dataSet;
String[] sLabels = bds.getStackLabels();
for (int j = 0; j < clrs.size() && j < entryCount && j < bds.getStackSize(); j++) {
labels.add(sLabels[j % sLabels.length]);
colors.add(clrs.get(j));
}
// add the legend description label
colors.add(-2);
labels.add(bds.getLabel());
} else if (dataSet instanceof PieDataSet) {
ArrayList<String> xVals = mData.getXVals();
PieDataSet pds = (PieDataSet) dataSet;
for (int j = 0; j < clrs.size() && j < entryCount && j < xVals.size(); j++) {
labels.add(xVals.get(j));
colors.add(clrs.get(j));
}
// add the legend description label
colors.add(-2);
labels.add(pds.getLabel());
} else {
for (int j = 0; j < clrs.size() && j < entryCount; j++) {
// if multiple colors are set for a DataSet, group them
if (j < clrs.size() - 1 && j < entryCount - 1) {
labels.add(null);
} else {
// add label to the last entry
String label = mData.getDataSetByIndex(i).getLabel();
labels.add(label);
}
colors.add(clrs.get(j));
}
}
}
Legend l = new Legend(colors, labels);
if (mLegend != null) {
// apply the old legend settings to a potential new legend
l.apply(mLegend);
}
mLegend = l;
}
use of com.github.mikephil.charting.data.PieDataSet in project carat by amplab.
the class PieChart method drawData.
@Override
protected void drawData() {
float angle = mRotationAngle;
ArrayList<PieDataSet> dataSets = mData.getDataSets();
int cnt = 0;
for (int i = 0; i < mData.getDataSetCount(); i++) {
PieDataSet dataSet = dataSets.get(i);
ArrayList<Entry> entries = dataSet.getYVals();
for (int j = 0; j < entries.size(); j++) {
float newangle = mDrawAngles[cnt];
float sliceSpace = dataSet.getSliceSpace();
Entry e = entries.get(j);
// draw only if the value is greater than zero
if ((Math.abs(e.getVal()) > 0.000001)) {
if (!needsHighlight(e.getXIndex(), i)) {
mRenderPaint.setColor(dataSet.getColor(j));
mDrawCanvas.drawArc(mCircleBox, angle + sliceSpace / 2f, newangle * mPhaseY - sliceSpace / 2f, true, mRenderPaint);
}
// if(sliceSpace > 0f) {
//
// PointF outer = getPosition(c, radius, angle);
// PointF inner = getPosition(c, radius * mHoleRadiusPercent
// / 100f, angle);
// }
}
angle += newangle * mPhaseX;
cnt++;
}
}
}
use of com.github.mikephil.charting.data.PieDataSet in project carat by amplab.
the class PieChart method drawValues.
@Override
protected void drawValues() {
// if neither xvals nor yvals are drawn, return
if (!mDrawXVals && !mDrawYValues)
return;
PointF center = getCenterCircleBox();
// get whole the radius
float r = getRadius();
float off = r / 2f;
if (mDrawHole) {
off = (r - (r / 100f * mHoleRadiusPercent)) / 2f;
}
// offset to keep things inside the chart
r -= off;
ArrayList<PieDataSet> dataSets = mData.getDataSets();
int cnt = 0;
for (int i = 0; i < mData.getDataSetCount(); i++) {
PieDataSet dataSet = dataSets.get(i);
ArrayList<Entry> entries = dataSet.getYVals();
for (int j = 0; j < entries.size() * mPhaseX; j++) {
// offset needed to center the drawn text in the slice
float offset = mDrawAngles[cnt] / 2;
// calculate the text position
float x = (float) (r * Math.cos(Math.toRadians((mRotationAngle + mAbsoluteAngles[cnt] - offset) * mPhaseY)) + center.x);
float y = (float) (r * Math.sin(Math.toRadians((mRotationAngle + mAbsoluteAngles[cnt] - offset) * mPhaseY)) + center.y);
String val = "";
float value = entries.get(j).getVal();
if (mUsePercentValues)
val = mValueFormatter.getFormattedValue(Math.abs(getPercentOfTotal(value))) + " %";
else
val = mValueFormatter.getFormattedValue(value);
if (mDrawUnitInChart)
val = val + mUnit;
// draw everything, depending on settings
if (mDrawXVals && mDrawYValues) {
// use ascent and descent to calculate the new line
// position,
// 1.6f is the line spacing
float lineHeight = (mValuePaint.ascent() + mValuePaint.descent()) * 1.6f;
y -= lineHeight / 2;
mDrawCanvas.drawText(val, x, y, mValuePaint);
if (j < mData.getXValCount())
mDrawCanvas.drawText(mData.getXVals().get(j), x, y + lineHeight, mValuePaint);
} else if (mDrawXVals && !mDrawYValues) {
if (j < mData.getXValCount())
mDrawCanvas.drawText(mData.getXVals().get(j), x, y, mValuePaint);
} else if (!mDrawXVals && mDrawYValues) {
mDrawCanvas.drawText(val, x, y, mValuePaint);
}
cnt++;
}
}
}
use of com.github.mikephil.charting.data.PieDataSet in project Gadgetbridge by Freeyourgadget.
the class SleepChartFragment method refreshSleepAmounts.
private MySleepChartsData refreshSleepAmounts(GBDevice mGBDevice, List<? extends ActivitySample> samples) {
ActivityAnalysis analysis = new ActivityAnalysis();
ActivityAmounts amounts = analysis.calculateActivityAmounts(samples);
PieData data = new PieData();
List<PieEntry> entries = new ArrayList<>();
List<Integer> colors = new ArrayList<>();
// int index = 0;
long totalSeconds = 0;
for (ActivityAmount amount : amounts.getAmounts()) {
if ((amount.getActivityKind() & ActivityKind.TYPE_SLEEP) != 0) {
long value = amount.getTotalSeconds();
totalSeconds += value;
// entries.add(new PieEntry(value, index++));
entries.add(new PieEntry(value, amount.getName(getActivity())));
colors.add(getColorFor(amount.getActivityKind()));
// data.addXValue(amount.getName(getActivity()));
}
}
String totalSleep = DateTimeUtils.formatDurationHoursMinutes(totalSeconds, TimeUnit.SECONDS);
PieDataSet set = new PieDataSet(entries, "");
set.setValueFormatter(new IValueFormatter() {
@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
return DateTimeUtils.formatDurationHoursMinutes((long) value, TimeUnit.SECONDS);
}
});
set.setColors(colors);
data.setDataSet(set);
//setupLegend(pieChart);
return new MySleepChartsData(totalSleep, data);
}
Aggregations