ArrayList<String> cannot be converted to IBarDataSet - android

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);

Related

Mpandroidchart fail to group data as desired?

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();

How to show labels on right and values to left side in HorizontalBarChart?

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.

Failed to run graph using PhilJay/MPAndroidChart library for android

allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
compile 'com.github.PhilJay:MPAndroidChart:v2.2.4'
public class MainActivity extends AppCompatActivity {
BarChart chart;
ArrayList<BarEntry> entries;
ArrayList<String> labels;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chart = (BarChart) findViewById(R.id.barChartId);
entries = new ArrayList<>();
labels = new ArrayList<>();
entries.add(new BarEntry(2f, 0));
entries.add(new BarEntry(4f, 1));
entries.add(new BarEntry(6f, 2));
entries.add(new BarEntry(8f, 3));
entries.add(new BarEntry(7f, 4));
entries.add(new BarEntry(3f, 5));
labels.add("January");
labels.add("February");
labels.add("March");
labels.add("April");
labels.add("May");
labels.add("June");
BarDataSet barDataSet = new BarDataSet(entries, "Projects");
BarData barData = new BarData(labels, barDataSet);
barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
chart.setData(barData);
chart.animateY(3000);
}
Error :
java.lang.NoSuchMethodError: No direct method (FI)V in class Lcom/github/mikephil/charting/data/BarEntry;

Getting error at PieData data = new PieData(labels, dataset); while adding data

In my app there is a need of pie chart for which i am using MPAndroidChart but, getting error while adding data in Piedata constructor,please help.
Using dependancy compile 'com.github.PhilJay:MPAndroidChart:v3.0.0 beta'
imageView=(PieChart) v.findViewById(R.id.result_text);
ArrayList<Entry> entries = new ArrayList<>();
entries.add(new Entry(4f, 0));
entries.add(new Entry(8f, 1));
entries.add(new Entry(6f, 2));
entries.add(new Entry(12f, 3));
entries.add(new Entry(18f, 4));
entries.add(new Entry(9f, 5));
PieDataSet dataset = new PieDataSet(entries,"uyggh");
ArrayList<Object> labels = new ArrayList<Object>();
labels.add("January");
labels.add("February");
labels.add("March");
labels.add("April");
labels.add("May");
labels.add("June");
PieData data = new PieData(labels, dataset);//getting error here
imageView.setData(data);
imageView.setDescription("Description");
Acoording to the latest documentation corresponding to com.github.PhilJay:MPAndroidChart:v3.0.3 , entries (Pie Entries) now take x and y values together.
List<PieEntry> entries = new ArrayList<>();
entries.add(new PieEntry(18.5f, "Green"));
entries.add(new PieEntry(26.7f, "Yellow"));
entries.add(new PieEntry(24.0f, "Red"));
entries.add(new PieEntry(30.8f, "Blue"));
PieDataSet set = new PieDataSet(entries, "Election Results");
PieData data = new PieData(set);
pieChart.setData(data);
pieChart.invalidate(); // refresh
So instead of degrading the depandency , use the updated code.
PieData takes only one argument and that is PieDataSet. So replace
PieData data = new PieData(labels, dataset);
with
PieData data = new PieData(dataset);
For more details, you can check this official example.
Hope, this answer is helpful to you.
When i used dependancy com.github.PhilJay:MPAndroidChart:v2.1.6' then
PieData accepts two arguments i.e String and PieDataSet. So, I think its problem with the dependancy update.
Using lower dependancy it can be solve.
You should passing labels in PieEntry methode like this.
it works for me
private float[] yData = {25.3f,10.3f,66.3f,44.3f,16.3f,46.3f,23.3f};
private String[] xData = {"med","hamza","mohammed","","","",""};
for(int i = 0 ; i <yData.length;i++){
yEntrys.add(new PieEntry(yData[i],xData[i % xData.length]));
}
it's my code , it works 100 % with
compile 'com.github.PhilJay:MPAndroidChart:v3.0.1' But higher version of gradle use implementation instead of compile
public class Main2Activity extends AppCompatActivity {
private static String TAG = "MainActivit";
private float[] yData = {25.3f,10.3f,66.3f,44.3f,16.3f,46.3f,23.3f};
private String[] xData = {"med","hamza","mohammed","","","",""};
PieChart pieChart;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Log.d(TAG, "Beach chart");
pieChart = (PieChart) findViewById(R.id.idPieChart);
//pieChart.setDescription(" Sales by employee In thousands $");
pieChart.setRotationEnabled(true);
pieChart.setHoleRadius(25f);
pieChart.setTransparentCircleAlpha(0);
pieChart.setCenterText("super cool chart");
pieChart.setCenterTextSize(10);
pieChart.setDrawEntryLabels(true);
addDataSet();
pieChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
#Override
public void onValueSelected(Entry e, Highlight h) {
Log.d(TAG, "onValueSelected: value selected from chart");
Log.d(TAG, "onValueSelected: "+ e.toString());
Log.d(TAG, "onValueSelected: "+ h.toString());
}
#Override
public void onNothingSelected() {
}
});
}
private void addDataSet() {
Log.d(TAG, "addDataSet: started");
ArrayList<PieEntry> yEntrys = new ArrayList<>();
ArrayList<String> xEntrys = new ArrayList<>();
for(int i = 0 ; i <yData.length;i++){
yEntrys.add(new PieEntry(yData[i],xData[i % xData.length]));
}
for(int i = 0 ; i <xData.length;i++){
xEntrys.add(xData[i]);
}
//create the DATA
PieDataSet pieDataSet = new PieDataSet(yEntrys,"Beachs");
pieDataSet.setSliceSpace(2);
pieDataSet.setValueTextSize(12);
//add colors to dataSet
ArrayList<Integer> colors = new ArrayList<>();
colors.add(Color.GRAY);
colors.add(Color.BLUE);
colors.add(Color.RED);
colors.add(Color.GREEN);
colors.add(Color.CYAN);
colors.add(Color.YELLOW);
colors.add(Color.MAGENTA);
pieDataSet.setColors(colors);
pieDataSet.setSliceSpace(25);
//add legend to chart
Legend legend = pieChart.getLegend();
legend.setForm(Legend.LegendForm.CIRCLE);
legend.setPosition(Legend.LegendPosition.LEFT_OF_CHART);
//create a pieData object
PieData pieData = new PieData(pieDataSet);
pieChart.setData(pieData);
pieChart.invalidate();
}
}
Your ArryayList<> should be with String, not with Object:
ArrayList<String> labels = new ArrayList<String>();
....
PieData data = new PieData(labels, dataset);//getting error here
Because new PieData needs ArrayList<< String >>

MPAndroidChart- Not able to add second set of data in Bar graph

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);

Categories

Resources