Skip to content

Instantly share code, notes, and snippets.

@jezinka
Created Jul 12, 2017
Embed
What would you like to do?
private void createDinnerBarChart() {
dbHelper = new CoNaObiadDbHelper(this);
DinnerContract dinnerContract = new DinnerContract();
LinkedHashMap<String, Long> mealData = dinnerContract.getDinnerStatistics(dbHelper);
List<BarEntry> entries = new ArrayList<>();
int i = 0;
for (Map.Entry<String, Long> entry : mealData.entrySet()) {
String mealName = entry.getKey();
Long quantity = entry.getValue();
entries.add(new BarEntry(i, quantity, mealName));
i++;
}
final BarDataSet set = new BarDataSet(entries, "");
BarData data = new BarData(set);
data.setBarWidth(0.2f);
HorizontalBarChart chart = (HorizontalBarChart) findViewById(R.id.chart);
chart.getLegend().setEnabled(false);
chart.getDescription().setEnabled(false);
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setGranularity(1f);
xAxis.setLabelCount(i);
xAxis.setDrawGridLines(false);
xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return (String) set.getEntryForXValue(value, 0.1f).getData();
}
});
YAxis yAxis = chart.getAxisLeft();
yAxis.setGranularity(0.5f);
chart.getAxisRight().setDrawLabels(false);
chart.getAxisRight().setDrawGridLines(false);
chart.setData(data);
chart.setVisibleXRange(0,i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment