I want to group the data coming form Rest Api as monthly basis like Jan, Feb, Mar..., but it doesn't plotting or grouping the value like I desired.
Now Where did I go wrong?
Or should I follow another approach?
If I have to then what is it?
Here is my code:
private void graphData(Response<List<MontlyPatientAnalysisModel>> response) {
List<BarEntry> entriesJan = new ArrayList<>();
List<BarEntry> entriesFeb = new ArrayList<>();
List<BarEntry> entriesMar = new ArrayList<>();
List<BarEntry> entriesApr = new ArrayList<>();
List<BarEntry> entriesMay = new ArrayList<>();
List<BarEntry> entriesJun = new ArrayList<>();
List<BarEntry> entriesJuly = new ArrayList<>();
List<BarEntry> entriesAug = new ArrayList<>();
List<BarEntry> entriesSep = new ArrayList<>();
List<BarEntry> entriesOct = new ArrayList<>();
List<BarEntry> entriesNov = new ArrayList<>();
List<BarEntry> entriesDec = new ArrayList<>();
List<BarEntry> entriesTotal = new ArrayList<>();
final List<String> xLabel = new ArrayList<>();
if (response.body() != null) {
for (MontlyPatientAnalysisModel montlyPatientAnalysisModel : response.body()) {
entriesJan.add(new BarEntry(1, montlyPatientAnalysisModel.getJan(), "January"));
entriesFeb.add(new BarEntry(2, montlyPatientAnalysisModel.getFeb(), "February"));
entriesMar.add(new BarEntry(3, montlyPatientAnalysisModel.getMar(),"March"));
entriesApr.add(new BarEntry(4, montlyPatientAnalysisModel.getApr(), "April"));
entriesMay.add(new BarEntry(5, montlyPatientAnalysisModel.getMay(), "May"));
entriesJun.add(new BarEntry(6, montlyPatientAnalysisModel.getJun(), "Jun"));
entriesJuly.add(new BarEntry(7, montlyPatientAnalysisModel.getJul(), "July"));
entriesAug.add(new BarEntry(8, montlyPatientAnalysisModel.getAug(), "August"));
entriesSep.add(new BarEntry(9, montlyPatientAnalysisModel.getSep(), "September"));
entriesOct.add(new BarEntry(10, montlyPatientAnalysisModel.getOct(), "October"));
entriesNov.add(new BarEntry(11, montlyPatientAnalysisModel.getNov(), "November"));
entriesDec.add(new BarEntry(12, montlyPatientAnalysisModel.getDec(), "December"));
entriesTotal.add(new BarEntry(13, montlyPatientAnalysisModel.getTotal(), "Total"));
xLabel.add(montlyPatientAnalysisModel.getName());
}
}
BarDataSet barDataSetJan, barDataSetFeb, barDataSetMar,
barDataSetApr, barDataSetMay, barDataSetJun, barDataSetJuly,
barDataSetAug, barDataSetSep, barDataSetOct, barDataSetNov, barDataSetDec, barDataSetTotal;
barDataSetJan = new BarDataSet(entriesJan, "Jan");
barDataSetJan.setColor(randomAndroidColor1);
barDataSetJan.setValueTextColor(randomAndroidColor1);
barDataSetJan.setValueTextSize(5f);
barDataSetFeb = new BarDataSet(entriesFeb, "Feb");
barDataSetFeb.setColor(randomAndroidColor2);
barDataSetFeb.setValueTextColor(randomAndroidColor2);
barDataSetFeb.setValueTextSize(5f);
barDataSetMar = new BarDataSet(entriesMar, "Mar");
barDataSetMar.setColor(randomAndroidColor3);
barDataSetMar.setValueTextColor(randomAndroidColor3);
barDataSetMar.setValueTextSize(5f);
barDataSetApr = new BarDataSet(entriesApr, "Apr");
barDataSetApr.setColor(randomAndroidColor4);
barDataSetApr.setValueTextColor(randomAndroidColor4);
barDataSetApr.setValueTextSize(5f);
barDataSetMay = new BarDataSet(entriesMay, "May");
barDataSetMay.setColor(randomAndroidColor5);
barDataSetMay.setValueTextColor(randomAndroidColor5);
barDataSetMay.setValueTextSize(5f);
barDataSetJun = new BarDataSet(entriesJun, "Jun");
barDataSetJun.setColor(randomAndroidColor6);
barDataSetJun.setValueTextColor(randomAndroidColor6);
barDataSetJun.setValueTextSize(5f);
barDataSetJuly = new BarDataSet(entriesJuly, "July");
barDataSetJuly.setColor(randomAndroidColor1);
barDataSetJuly.setValueTextColor(randomAndroidColor1);
barDataSetJuly.setValueTextSize(5f);
barDataSetAug = new BarDataSet(entriesAug, "Aug");
barDataSetAug.setColor(randomAndroidColor2);
barDataSetAug.setValueTextColor(randomAndroidColor2);
barDataSetAug.setValueTextSize(5f);
barDataSetSep = new BarDataSet(entriesSep, "Sep");
barDataSetSep.setColor(randomAndroidColor3);
barDataSetSep.setValueTextColor(randomAndroidColor3);
barDataSetSep.setValueTextSize(5f);
barDataSetOct = new BarDataSet(entriesOct, "Oct");
barDataSetOct.setColor(randomAndroidColor4);
barDataSetOct.setValueTextColor(randomAndroidColor4);
barDataSetOct.setValueTextSize(5f);
barDataSetNov = new BarDataSet(entriesNov, "Nov");
barDataSetNov.setColor(randomAndroidColor5);
barDataSetNov.setValueTextColor(randomAndroidColor5);
barDataSetNov.setValueTextSize(5f);
barDataSetDec = new BarDataSet(entriesDec, "Dec");
barDataSetDec.setColor(randomAndroidColor6);
barDataSetDec.setValueTextColor(randomAndroidColor6);
barDataSetDec.setValueTextSize(5f);
barDataSetTotal = new BarDataSet(entriesTotal, "Total");
barDataSetTotal.setColor(randomAndroidColor3);
barDataSetTotal.setValueTextColor(randomAndroidColor3);
barDataSetTotal.setValueTextSize(5f);
//add the data
ArrayList<IBarDataSet> barDataSets = new ArrayList<IBarDataSet>();
barDataSets.add(barDataSetJan);
barDataSets.add(barDataSetFeb);
barDataSets.add(barDataSetMar);
barDataSets.add(barDataSetApr);
barDataSets.add(barDataSetMay);
barDataSets.add(barDataSetJun);
barDataSets.add(barDataSetJuly);
barDataSets.add(barDataSetAug);
barDataSets.add(barDataSetSep);
barDataSets.add(barDataSetOct);
barDataSets.add(barDataSetNov);
barDataSets.add(barDataSetDec);
barDataSets.add(barDataSetTotal);
BarData barData = new BarData(barDataSets);
mBarChart.setData(barData);
//barData.setValueFormatter(new LargeValueFormatter());
Description description = new Description();
description.setText("Growth rate per month");
description.setTextSize(12);
mBarChart.setDescription(description);
final XAxis xAxis = mBarChart.getXAxis();
xAxis.setLabelCount(entriesJan.size());
xAxis.setValueFormatter(new IndexAxisValueFormatter(xLabel) {
#Override
public String getFormattedValue(float value) {
if (value >= 0) {
if (value <= xLabel.size() - 1) {
return xLabel.get((int) value);
}
return "";
}
return "";
}
});
xAxis.setCenterAxisLabels(true);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setGranularity(1);
//xAxis.setGranularityEnabled(true);
xAxis.setTextSize(9);
xAxis.setTextColor(R.color.colorBlack);
xAxis.setLabelRotationAngle(-13);
mBarChart.getAxisRight().setEnabled(false);
YAxis leftAxis = mBarChart.getAxisLeft();
mBarChart.setVisibleYRangeMaximum(3000, YAxis.AxisDependency.LEFT);
leftAxis.setTextColor(Color.BLACK);
leftAxis.setTextSize(12);
leftAxis.setAxisLineColor(Color.WHITE);
leftAxis.setDrawGridLines(true);
leftAxis.setGranularity(1);
leftAxis.setLabelCount(12, true);
mBarChart.animateY(1000);
mBarChart.setDragEnabled(true);
mBarChart.setVisibleXRangeMaximum(15);
float groupSpace = 0.060f;
float barSpace = 0.011f;
barData.setBarWidth((1 - groupSpace) / barDataSets.size() - barSpace);
mBarChart.getXAxis().setAxisMinimum(0);
mBarChart.getXAxis().setAxisMaximum(0 + mBarChart.getBarData().getGroupWidth(groupSpace, barSpace) * 18);
mBarChart.getAxisLeft().setAxisMinimum(0);
mBarChart.setScaleEnabled(false);
mBarChart.groupBars(0f, groupSpace, barSpace);
mBarChart.fitScreen();
mBarChart.zoom(4.5f, 1.2f, 0, 0);
barData.setDrawValues(true);
barData.notifyDataChanged();
mBarChart.getData().notifyDataChanged();
mBarChart.notifyDataSetChanged();
mBarChart.invalidate();
mBarChart.refreshDrawableState();
}
Any idea guys where did I go wrong?
Should I follow another approach?
Thanks in advance
https://github.com/PhilJay/MPAndroidChart/tree/master/MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample
Check this link, While if you are creating group chart your groupspace , barWidth is more important , in my example.
float barWidth = 0.25f;
float barSpace = 0.05f;
float groupSpace = 0.40f;
barChart.setOnChartValueSelectedListener(this);
barChart.setHighlightPerTapEnabled(true);
barChart.setDescription(null);
barChart.setPinchZoom(false);
barChart.setFitBars(true);
barChart.setScaleEnabled(false);
barChart.setDrawBarShadow(false);
barChart.setDrawGridBackground(false);
BarDataSet set1, set2;
set1 = new BarDataSet(mViewModel.getBalList(), "Remaining");
set1.setColor(ContextCompat.getColor(getContext(), R.color.darkRed));
set1.setDrawIcons(false);
set1.setDrawValues(false);
set2 = new BarDataSet(mViewModel.getCollectedList(), "Collected");
set2.setColor(ContextCompat.getColor(getContext(), R.color.primaryYellow));
set2.setDrawIcons(false);
set2.setDrawValues(false)
BarData data = new BarData(set1, set2);
barChart.setData(data);
data.setValueFormatter(new LargeValueFormatter());
barChart.getBarData().setBarWidth(barWidth);
barChart.getXAxis().setAxisMinimum(0f);
barChart.getXAxis().setAxisMaximum(mViewModel.getxVals().size());
barChart.groupBars(0f, groupSpace, barSpace);
// barChartView.setFitBars(true)
barChart.getData().setHighlightEnabled(true);
XAxis xAxis = barChart.getXAxis();
xAxis.setGranularity(1f);
xAxis.setGranularityEnabled(true);
xAxis.setCenterAxisLabels(true);
xAxis.setDrawGridLines(false);
xAxis.setTextSize(10f);
xAxis.setTextColor(ContextCompat.getColor(getContext(), R.color.black));
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setValueFormatter(new IndexAxisValueFormatter(mViewModel.getxVals()));
binding.className.setText(mViewModel.getxVals().get(0));
xAxis.setLabelCount(mViewModel.getxVals().size());
xAxis.mAxisMaximum = mViewModel.getxVals().size();
xAxis.setCenterAxisLabels(true);
xAxis.setAvoidFirstLastClipping(true);
xAxis.setSpaceMin(4f);
xAxis.setSpaceMax(4f);
YAxis leftAxis = barChart.getAxisLeft();
leftAxis.setValueFormatter(new LargeValueFormatter());
leftAxis.setDrawGridLines(true);
leftAxis.setSpaceTop(1f);
leftAxis.setTextColor(ContextCompat.getColor(getContext(), R.color.black));
leftAxis.setTextSize(10f);
leftAxis.setAxisMinimum(0f);
barChart.getAxisRight().setEnabled(false);
barChart.setVisibleXRange(1f, 4.5f);
barChart.animateXY(1000, 1000);
barChart.getLegend().setEnabled(true);
Legend legend = barChart.getLegend();
legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
legend.setDrawInside(false);
barChart.invalidate();
Related
I use MpAndroidChart-CombinedChart in my project,but I met a problem,please look the follow screenshot.
As you saw,the X axis right part didn't filled.In other words,I want set the bar space,but I can't.
I try barData.setBarWidth(0.4f),it didn't work,it will make the bar width Larger,but it will not make bar space lager.
public static void initCombinedChart(List<FieldFragment.CombinedBean> list, CombinedChart combinedChart) {
if (list == null || list.size() == 0) return;
combinedChart.setPinchZoom(false);
combinedChart.setDragEnabled(false);
combinedChart.setTouchEnabled(false);
combinedChart.getLegend().setEnabled(false);
combinedChart.getDescription().setEnabled(false);
XAxis xAxis = combinedChart.getXAxis();
xAxis.setDrawGridLines(false);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setYOffset(5);
xAxis.setAvoidFirstLastClipping(true);
YAxis leftYAxis = combinedChart.getAxisLeft();
leftYAxis.setLabelCount(6, true);
leftYAxis.setDrawAxisLine(false);
leftYAxis.setDrawGridLines(true);
leftYAxis.setGridColor(Color.parseColor("#DDDDDD"));
leftYAxis.setXOffset(10f);
leftYAxis.setAxisMinimum(0);
leftYAxis.setSpaceBottom(0);
leftYAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
YAxis rightYAxis = combinedChart.getAxisRight();
rightYAxis.setSpaceBottom(0);
rightYAxis.setDrawAxisLine(false);
rightYAxis.setDrawGridLines(true);
rightYAxis.setAxisMinimum(0);
rightYAxis.setXOffset(10);
rightYAxis.setLabelCount(6, true);
rightYAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
List<Entry> lineEntries = new ArrayList<>();
List<BarEntry> barEntries = new ArrayList<>();
List<String> xValues = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
lineEntries.add(new Entry(i, list.get(i).getRightYValue()));
barEntries.add(new BarEntry(i, list.get(i).getLeftYValue()));
xValues.add(list.get(i).getxValue());
}
xAxis.setValueFormatter(new IAxisValueFormatter() {
#Override
public String getFormattedValue(float value, AxisBase axisBase) {
try {
int position = (int) value;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = dateFormat.parse(xValues.get(position));
SimpleDateFormat format = new SimpleDateFormat("MM.dd");
return format.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
});
// 折线数据
LineDataSet lineDataSet = new LineDataSet(lineEntries, "");
lineDataSet.setDrawValues(false);
lineDataSet.setDrawCircles(false);
lineDataSet.setLineWidth(3f);
lineDataSet.setColor(Color.parseColor("#00c2bd"));
lineDataSet.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
lineDataSet.setDrawFilled(true); //这个不要忘了
lineDataSet.setFillColor(Color.parseColor("#00c2bd"));
lineDataSet.setFillAlpha(50);
lineDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
List<ILineDataSet> lineDataSets = new ArrayList<>();
lineDataSets.add(lineDataSet);
LineData lineData = new LineData(lineDataSets);
// 柱子数据
BarDataSet barDataSet = new BarDataSet(barEntries, "");
barDataSet.setDrawValues(false);
barDataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
barDataSet.setColor(Color.parseColor("#1490ee"));
BarData barData = new BarData(barDataSet);
barData.setBarWidth(0.4f); //值越大柱子越宽
// 解决bar显示不全问题
float barWidth = barData.getBarWidth() / 2;
xAxis.setAxisMinimum(-barWidth);
xAxis.setAxisMaximum(xValues.size() - barWidth);
CombinedData combinedData = new CombinedData();
combinedData.setData(lineData);
combinedData.setData(barData);
combinedChart.setData(combinedData);
combinedChart.invalidate();
}
Drawing a horizontal bar chart using MPAndroidChart 3.0.2. the values are shown on the right of the bars. I could use setValueFormatter and use IAxisValueFormatter interface to display the labels on the right. But the values are not displayed now.
{
HorizontalBarChart barChart = (HorizontalBarChart) itemView.findViewById(R.id.barChart);
BarData data = new BarData();
ArrayList<BarEntry> valueSet1 = new ArrayList<>();
ArrayList<String> labels = new ArrayList<>();
labels.add("January");
labels.add("February");
labels.add("March");
labels.add("April");
labels.add("May");
labels.add("June");
ArrayList<String> ylabels = new ArrayList<>();
int dataCount=0;
for (int i=0;i<6;++i) {
BarEntry entry = new BarEntry(dataCount,(i+1)*2);
valueSet1.add(entry);
ylabels.add(" "+i);
dataCount++;
}
List<IBarDataSet> dataSets = new ArrayList<>();
BarDataSet bds = new BarDataSet(valueSet1, " ");
bds.setColors(ColorTemplate.MATERIAL_COLORS);
String[] xAxisLabels = labels.toArray(new String[0]);
String[] yAxisLabels = ylabels.toArray(new String[0]);
bds.setStackLabels(xAxisLabels);
dataSets.add(bds);
data.addDataSet(bds);
data.setDrawValues(true);
data.setBarWidth(0.4f);
XAxis xaxis = barChart.getXAxis();
xaxis.setDrawGridLines(false);
xaxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xaxis.setGranularityEnabled(true);
xaxis.setGranularity(1);
xaxis.setDrawLabels(true);
xaxis.setLabelCount(dataCount+1);
xaxis.setXOffset(10);
xaxis.setDrawAxisLine(false);
CategoryBarChartXaxisFormatter xaxisFormatter = new CategoryBarChartXaxisFormatter(xAxisLabels);
xaxis.setValueFormatter(xaxisFormatter);
YAxis yAxisLeft = barChart.getAxisLeft();
yAxisLeft.setEnabled(false);
YAxis yAxisRight = barChart.getAxisRight();
yAxisRight.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
yAxisRight.setDrawGridLines(false);
yAxisRight.setDrawAxisLine(false);
Legend legend = barChart.getLegend();
legend.setEnabled(false);
barChart.setFitBars(true);
barChart.setData(data);
barChart.setDescription(null);
}
public class CategoryBarChartXaxisFormatter implements IAxisValueFormatter {
private String[] mValues;
public CategoryBarChartXaxisFormatter(String[] values) {
this.mValues = values;
}
#Override
public String getFormattedValue(float value, AxisBase axis) {
int val = (int)value;
String label="";
if(val>=0 && val<mValues.length) {
label= mValues[val];
}
else {
label= "";
}
return label;
}
}
requirement is to display label strings on the right and a number on the left corresponding to each bar.
I did check some stack overflow and google it but didn't find anything that works so far.
i get this
but this is an example of my requirement
Do appreciate some help.
Thanks for your time
I implemented something similar just a few days back. Here's the code...
public class MainActivity extends AppCompatActivity {
protected HorizontalBarChart mChart;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> labels = new ArrayList<>();
labels.add("January");
labels.add("February");
labels.add("March");
labels.add("April");
labels.add("May");
labels.add("June");
mChart = (HorizontalBarChart) findViewById(R.id.barChart);
mChart.setDrawBarShadow(false);
mChart.setDrawValueAboveBar(true);
mChart.getDescription().setEnabled(false);
mChart.setPinchZoom(false);
mChart.setDrawGridBackground(false);
XAxis xl = mChart.getXAxis();
xl.setPosition(XAxis.XAxisPosition.BOTTOM);
xl.setDrawAxisLine(true);
xl.setDrawGridLines(false);
CategoryBarChartXaxisFormatter xaxisFormatter = new CategoryBarChartXaxisFormatter(labels);
xl.setValueFormatter(xaxisFormatter);
xl.setGranularity(1);
YAxis yl = mChart.getAxisLeft();
yl.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
yl.setDrawGridLines(false);
yl.setEnabled(false);
yl.setAxisMinimum(0f);
YAxis yr = mChart.getAxisRight();
yr.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
yr.setDrawGridLines(false);
yr.setAxisMinimum(0f);
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
for (int i = 0; i < 6; i++) {
yVals1.add(new BarEntry(i, (i+1)*10));
}
BarDataSet set1;
set1 = new BarDataSet(yVals1, "DataSet 1");
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
data.setValueTextSize(10f);
data.setBarWidth(.9f);
mChart.setData(data);
mChart.getLegend().setEnabled(false);
}
public class CategoryBarChartXaxisFormatter implements IAxisValueFormatter {
ArrayList<String> mValues;
public CategoryBarChartXaxisFormatter(ArrayList<String> values) {
this.mValues = values;
}
#Override
public String getFormattedValue(float value, AxisBase axis) {
int val = (int) value;
String label = "";
if (val >= 0 && val < mValues.size()) {
label = mValues.get(val);
} else {
label = "";
}
return label;
}
}
}
RESULT
Hope this helps.
I have a problem using BarData. My code shows an error at :
BarData Data = new BarData(Date,depenses);
I only want to show the date under every bar.
My Code is..
public class statistique extends AppCompatActivity {
BarChart barChart;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_statistique);
barChart = (BarChart)findViewById(R.id.stats_chart);
ArrayList<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(4f, 0));
entries.add(new BarEntry(8f, 1));
entries.add(new BarEntry(6f, 2));
entries.add(new BarEntry(12f, 3));
entries.add(new BarEntry(18f, 4));
entries.add(new BarEntry(9f, 5));
BarDataSet depenses = new BarDataSet(entries, "depenses");
ArrayList<String> Date = new ArrayList<>();
Date.add("2/7/2016");
Date.add("3/7/2016");
Date.add("5/7/2016");
Date.add("6/7/2016");
Date.add("23/7/2016");
Date.add("30/7/2016");
BarData Data = new BarData(Date,depenses);
barChart.setData(Data);
barChart.setTouchEnabled(true);
barChart.setDragEnabled(true);
barChart.setScaleEnabled(true);
barChart.animateXY(3000, 3000);
barChart.setHorizontalScrollBarEnabled(true);
barChart.setDoubleTapToZoomEnabled(true);
barChart.setDescription("Dépenses");
depenses.setColors(ColorTemplate.COLORFUL_COLORS);
}
}
Which version of the library are you using. The new v3.0 works slightly differently. If your using any v2 version the following will work. The error is misleading you need to cast your DataSets to IBarData sets, and it needs to be a list I believe.
This should work for you.
ArrayList<IBarDataSet> dataSets = new ArrayList<>();
dataSets.add((IBarDataSet) depenses);
BarData Data = new BarData(Date, dataSets);
If you're using the new version 3 library, you need to set your XAxis differently. In that case you just need to pass
ArrayList<IBarDataSet> dataSets = new ArrayList<>();
dataSets.add((IBarDataSet) depenses);
BarData Data = new BarData(dataSets);
You'll then need to setup your XAxis.
IAxisValueFormatter formatter = new IAxisValueFormatter() {
#Override
public String getFormattedValue(float value, AxisBase axis) {
return Date.get((int)value);
}
// we don't draw numbers, so no decimal digits needed
#Override
public int getDecimalDigits() { return 0; }
};
XAxis xAxis = barChart.getXAxis();
xAxis.setGranularity(1f); // minimum axis-step (interval) is 1
xAxis.setValueFormatter(formatter);
implementation 'com.github.PhilJay:MPAndroidChart:3.0.3'
List<BarEntry> grafdata = new ArrayList<>();
Create a method:
public void barchardata(){
grafdata.add(new BarEntry( 0, Math.round(Float.valueOf(5.5f))));
grafdata.add(new BarEntry( 1, Math.round(Float.valueOf(1.5f))));
grafdata.add(new BarEntry( 2, Math.round(Float.valueOf(7.5f))));
grafdata.add(new BarEntry( 3, Math.round(Float.valueOf(4.5f))));
grafdata.add(new BarEntry( 4, Math.round(Float.valueOf(3.5f))));
InitilizationBarChart();
}
private void InitilizationBarChart() {
BarDataSet bardataset = new BarDataSet(grafdata, "");
barChartView.animateY(2000);
BarData data = new BarData(bardataset);
bardataset.setColors(ColorTemplate.COLORFUL_COLORS);
bardataset.setValueTextColor(Color.GREEN);
barChartView.getAxisLeft().setTextColor(this.getResources().getColor(R.color.green));
barChartView.getXAxis().setTextColor(this.getResources().getColor(R.color.green));
barChartView.getAxisRight().setDrawGridLines(false);
barChartView.getAxisLeft().setDrawGridLines(false);
barChartView.getXAxis().setDrawGridLines(false);
barChartView.getLegend().setTextColor(this.getResources().getColor(R.color.green));
barChartView.setData(data);
final XAxis xAxis = barChartView.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
xAxis.setValueFormatter(new IndexAxisValueFormatter(){
#Override
public String getFormattedValue(float value, AxisBase axis) {
return getXAxisValues().get((int) value);
}
});
barChartView.getAxisRight().setEnabled(false);
xAxis.setGranularityEnabled(true);
}
Initialize name:
private List<String> getXAxisValues() {
ArrayList<String> xAxis = new ArrayList<>();
UserNameList.clear();
UserNameList.add("Rampal");
UserNameList.add("sonu");
UserNameList.add("Amber");
UserNameList.add("Viresh");
UserNameList.add("Pal");
return UserNameList;
}
You can do something like this:
barChart = (BarChart)findViewById(R.id.stats_chart);
ArrayList<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(4f, 0));
entries.add(new BarEntry(8f, 1));
entries.add(new BarEntry(6f, 2));
entries.add(new BarEntry(12f, 3));
entries.add(new BarEntry(18f, 4));
entries.add(new BarEntry(9f, 5));
ArrayList<String> Date = new ArrayList<>();
Date.add("2/7/2016");
Date.add("3/7/2016");
Date.add("5/7/2016");
Date.add("6/7/2016");
Date.add("23/7/2016");
Date.add("30/7/2016");
barChart.setTouchEnabled(true);
barChart.setDragEnabled(true);
barChart.setScaleEnabled(true);
barChart.animateXY(3000, 3000);
barChart.setHorizontalScrollBarEnabled(true);
barChart.setDoubleTapToZoomEnabled(true);
barChart.setDescription("Dépenses");
depenses.setColors(ColorTemplate.COLORFUL_COLORS);
ArrayList<IBarDataSet> dataSets = null;
BarDataSet barDataSet1 = new BarDataSet(entries "your title");
dataSets=new ArrayList<>();
dataSets.add(barDataSet)
BarData Data = new BarData(Date,barDataSet1);
barChart.setData(Data);
I have implemented horizontal barChart successfully by using MPAndroidChart library. here is my code,
HorizontalBarChart barChart = (HorizontalBarChart)view.findViewById(R.id.chart);
BarData data = new BarData(getXAxisValues(), getDataSet());
barChart.setData(data);
barChart.invalidate();
barChart.setDescription(""); // Hide the description
barChart.getAxisLeft().setDrawLabels(false);
barChart.getAxisRight().setDrawLabels(false);
barChart.getXAxis().setDrawLabels(false);
barChart.setDrawGridBackground(false);
barChart.setDrawBorders(false);
barChart.getAxisLeft().setDrawGridLines(false);
barChart.getXAxis().setDrawGridLines(false);
barChart.getAxisLeft().setDrawAxisLine(false);
barChart.getAxisRight().setDrawAxisLine(false);
barChart.getXAxis().setDrawAxisLine(false);
barChart.getXAxis().setEnabled(false);
barChart.setDoubleTapToZoomEnabled(false);
barChart.setPinchZoom(false);
//YAxis
YAxis leftAxis = barChart.getAxisLeft();
leftAxis.setEnabled(false);
barChart.getAxisRight().setEnabled(false);
barChart.setClickable(false);
barChart.getLegend().setEnabled(false);
private ArrayList<BarDataSet> getDataSet() {
ArrayList<BarDataSet> dataSets = null;
ArrayList<BarEntry> valueSet1 = new ArrayList<>();
BarEntry v1e1 = new BarEntry(1110f, 0);
valueSet1.add(v1e1);
BarEntry v1e2 = new BarEntry(4075f, 1);
valueSet1.add(v1e2);
BarEntry v1e3 = new BarEntry(6080f, 2);
valueSet1.add(v1e3);
BarEntry v1e4 = new BarEntry(6563f, 3);
valueSet1.add(v1e4);
BarEntry v1e5 = new BarEntry(6000f, 4);
valueSet1.add(v1e5);
BarDataSet barDataSet1 = new BarDataSet(valueSet1, "");
barDataSet1.setColors(ColorTemplate.COLORFUL_COLORS);
dataSets = new ArrayList<>();
dataSets.add(barDataSet1);
return dataSets;
}
private ArrayList<String> getXAxisValues() {
ArrayList<String> xAxis = new ArrayList<>();
xAxis.add("1");
xAxis.add("2");
xAxis.add("3");
xAxis.add("4");
xAxis.add("5");
return xAxis;
}
but my question is, i want to be add red circle rated values with icon in barChart like the image below,
I am trying to add two sets of data i.e. BarDataSet to Bar graph, independently both the sets work just fine, but when I try adding the second the graph is showing incorrect. Following are the data sets with
(0 to 9 as x-axis values)
Set1-> 15:5
Set2-> 16:5
Set1-> 0:2
Set2-> 0:2
Set1-> 0:1
Set2-> 22:1
Set1-> 40:0
Set2-> 10:0
//DATA SET CREATION
ArrayList<BarDataSet> dataSets = null;
ArrayList<BarEntry> valueSet1 = new ArrayList<>();
ArrayList<BarEntry> valueSet2 = new ArrayList<>();
for(int i=0;i<arrayList.size();i++) {
if(Loop through size of arrayList)
{
BarEntry v1e1 = new BarEntry(getData1, valueFromArrayList);
if(!valueSet1.contains(v1e1)) {
valueSet1.add(v1e1);
}
BarEntry v2e2 = new BarEntry(getData2, valueFromArrayList);
if(!valueSet2.contains(v2e2)) {
valueSet2.add(v2e2);
}
}
}
BarDataSet barDataSet1 = new BarDataSet(valueSet1, TITLE1);
barDataSet1.setColor(getResources().getColor(R.color.Dark_Blue));
barDataSet1.setDrawValues(false);
BarDataSet barDataSet2 = new BarDataSet(valueSet2, TITLE2);
barDataSet2.setColor(getResources().getColor(R.color.Orange));
barDataSet2.setDrawValues(false);
dataSets = new ArrayList<>();
dataSets.add(barDataSet1);
dataSets.add(barDataSet2);
//Plotting graph
BarChart barChart = (BarChart) findViewById(ID OF GRAPH);
XAxis xAxis = barChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTextColor(Color.DKGRAY);
xAxis.setValues(xVals);
xAxis.setAdjustXLabels(false);
xAxis.setDrawAxisLine(true);
xAxis.setDrawGridLines(true);
YAxis yAxis = barChart.getAxisLeft();
yAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
yAxis.setDrawAxisLine(true);
yAxis.setDrawGridLines(false);
yAxis.setAxisMinValue(0);
yAxis.setValueFormatter(new ValueFormatter() {
#Override
public String getFormattedValue(float value) {
return String.valueOf((int) Math.floor(value));
}
});
yAxis.setDrawLabels(true);
YAxis yAxisRight = barChart.getAxisRight();
yAxisRight.setDrawAxisLine(true);
yAxisRight.setDrawGridLines(true);
yAxisRight.setDrawLabels(false);
BarData data = new BarData(setUpXAxisLabels(), getDataSet());
data.setGroupSpace(0f);
barChart.setData(data);
barChart.setPinchZoom(false);
barChart.setDoubleTapToZoomEnabled(false);
barChart.setScaleEnabled(false);
barChart.getLegend().setEnabled(true);
barChart.setEnabled(false);
barChart.setDrawBarShadow(false);
barChart.setDrawGridBackground(false);
barChart.getLegend().setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);
barChart.setDescription("");
barChart.invalidate();
barChart.animateXY(1000, 1000);