I get the following error when trying to compile a stacked bargraph. I have copied code working for a grouped bargraph and am trying to add the "stacked" functionality to this code. The error is marked in comments in the code below, in each of the bar entry lines as per following example:
"
BarEntry v1e1 = new BarEntry(0f, new float[]{10,20,20});
"
Here is the code:
public class StackedBarGraph extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.groupedbargraph);
BarChart chart = (BarChart) findViewById(R.id.mpandroidstackedbargraph );
BarData data = new BarData(getXAxisValues(),getDataSet()); //!! ignore the complaint - it compiles and works
chart.setData(data);
chart.setDescription("My Chart");
chart.animateXY(2000, 2000);
chart.invalidate();
}
private ArrayList<IBarDataSet> getDataSet() {
ArrayList<IBarDataSet> dataSets = null;
ArrayList<BarEntry> valueSet1 = new ArrayList<>();
BarEntry v1e1 = new BarEntry(0f, new float[]{10,20,20}); // ERROR
valueSet1.add(v1e1); //THESE LINES ALL GIVE ABOVE ERROR
BarEntry v1e2 = new BarEntry(1f, new float[]{20,30,30}); // ERROR
valueSet1.add(v1e2);
BarEntry v1e3 = new BarEntry(2f, new float[]{30,50,40}); // ERROR
valueSet1.add(v1e3);
BarEntry v1e4 = new BarEntry(3f, new float[]{50,70,50}); // ERROR
valueSet1.add(v1e4);
BarEntry v1e5 = new BarEntry(4f, new float[]{70,40,60}); // ERROR
valueSet1.add(v1e5);
BarEntry v1e6 = new BarEntry(5f, new float[]{40,30,70}); // Jun
valueSet1.add(v1e6);
ArrayList<BarEntry> valueSet2 = new ArrayList<>();
BarEntry v2e1 = new BarEntry(0, new float[]{10,20,30}); // ERROR
valueSet2.add(v2e1);
BarEntry v2e2 = new BarEntry(1, new float[]{10,20,30}); // ERROR
valueSet2.add(v2e2);
BarEntry v2e3 = new BarEntry(2, new float[]{10,20,30}); // ERROR
valueSet2.add(v2e3);
BarEntry v2e4 = new BarEntry(3, new float[]{10,20,30}); // ERROR
valueSet2.add(v2e4);
BarEntry v2e5 = new BarEntry(4, new float[]{10,20,30}); // ERROR
valueSet2.add(v2e5);
BarEntry v2e6 = new BarEntry(5, new float[]{10,20,30}); // ERROR
valueSet2.add(v2e6);
BarDataSet barDataSet1 = new BarDataSet(valueSet1, "Brand 1");
barDataSet1.setColor(Color.rgb(0, 155, 0)); // Green
BarDataSet barDataSet2 = new BarDataSet(valueSet2, "Brand 2");
barDataSet1.setColor(Color.rgb(0, 155, 155)); // Light Blue
dataSets = new ArrayList<>();
dataSets.add(barDataSet1);
dataSets.add(barDataSet2);
return dataSets;
}
private ArrayList<String> getXAxisValues() {
ArrayList<String> xAxis = new ArrayList<>();
xAxis.add("JAN");
xAxis.add("FEB");
xAxis.add("MAR");
xAxis.add("APR");
xAxis.add("MAY");
xAxis.add("JUN");
return xAxis;
}
BarEntry contains the following constructors:
BarEntry(float[] vals, int xIndex)
BarEntry(float val, int xIndex)
BarEntry(float[] vals, int xIndex, String label)
BarEntry(float val, int xIndex, Object data)
So, what your doing is not possible. Flip your arguments...
i.e
BarEntry v1e1 = new BarEntry(new float[]{10,20,20}, 0);
Please look into the source code for details.
Related
I'm using MPAndroidChart library, I created a simple chart example:
and I want to change to position of value X:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chart);
BarChart chart = (BarChart) findViewById(R.id.chart);
BarData data = new BarData(getXAxisValues(), getDataSet());
chart.setData(data);
chart.setDescription("My Chart");
chart.animateXY(0, 5000);
chart.invalidate();
}
private ArrayList<BarDataSet> getDataSet() {
ArrayList<BarDataSet> dataSets = null;
ArrayList<BarEntry> valueSet1 = new ArrayList<>();
BarEntry v1e1 = new BarEntry(98.000f, 0); // Jan
valueSet1.add(v1e1);
BarEntry v1e2 = new BarEntry(95.000f, 1); // Feb
valueSet1.add(v1e2);
BarEntry v1e3 = new BarEntry(102.000f, 2); // Mar
valueSet1.add(v1e3);
BarEntry v1e4 = new BarEntry(101.000f, 3); // Apr
valueSet1.add(v1e4);
BarEntry v1e5 = new BarEntry(99.000f, 4); // May
valueSet1.add(v1e5);
BarEntry v1e6 = new BarEntry(97.000f, 5); // Jun
valueSet1.add(v1e6);
BarDataSet barDataSet1 = new BarDataSet(valueSet1, "Brand 1");
barDataSet1.setColors(ColorTemplate.COLORFUL_COLORS);
dataSets = new ArrayList<>();
dataSets.add(barDataSet1);
return dataSets;
}
private ArrayList<String> getXAxisValues() {
ArrayList<String> xAxis = new ArrayList<>();
xAxis.add("Tue");
xAxis.add("Web");
xAxis.add("Thu");
xAxis.add("Fri");
xAxis.add("Sat");
xAxis.add("Sun");
return xAxis;
}
}
please help me, thanks so much!
Try
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
Use chart.getXAxis().setPosition(XAxisPosition.BOTTOM) to set the x-axis position.
As I understand from you query , you want to move your X-cordinates on bottom of the graph, please correct me if I am wrong.
If that is so then try GraphView - com.jjoe64:graphview:4.0.1.
This is how I am using it.
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,
Am trying to create BarChart (grouped DataSets) chart using mpandroid chart while am setting values using real time data from sqlite however whenver i fetch data from webservice and trying to set value to Chart am getting error like Dataset Entry are longer than x axis values of chartdata object
Here is my code:
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.android.volley.RequestQueue;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import java.util.ArrayList;
import java.util.List;
import SqliteDatabase.TimeTrackerDAO;
import model.DashboardModelCommercial;
/**
* Created by 4264 on 27-06-2016.
*/
public class DashBoardCommercial extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>{
RequestQueue mrequestqueue;
TimeTrackerDAO timeTrackerDAO;
private int dashboard = 1;
Cursor c;
List<DashboardModelCommercial>listobj;
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_commercial, container, false);
BarChart chart = (BarChart)view. findViewById(R.id.chart);
timeTrackerDAO=new TimeTrackerDAO(getContext().getApplicationContext());
listobj=new ArrayList<>();
getLoaderManager().initLoader(dashboard, null, this);
return view;
}
/*private ArrayList<IBarDataSet> getDataSet() {
ArrayList<IBarDataSet> dataSets = null;
ArrayList<BarEntry> valueSet1 = new ArrayList<>();
BarEntry v1e1 = new BarEntry(110.000f, 0); // Jan
valueSet1.add(v1e1);
BarEntry v1e2 = new BarEntry(40.000f, 1); // Feb
valueSet1.add(v1e2);
BarEntry v1e3 = new BarEntry(60.000f, 2); // Mar
valueSet1.add(v1e3);
BarEntry v1e4 = new BarEntry(30.000f, 3); // Apr
valueSet1.add(v1e4);
BarEntry v1e5 = new BarEntry(90.000f, 4); // May
valueSet1.add(v1e5);
BarEntry v1e6 = new BarEntry(100.000f, 5); // Jun
valueSet1.add(v1e6);
ArrayList<BarEntry> valueSet2 = new ArrayList<>();
BarEntry v2e1 = new BarEntry(150.000f, 0); // Jan
valueSet2.add(v2e1);
BarEntry v2e2 = new BarEntry(90.000f, 1); // Feb
valueSet2.add(v2e2);
BarEntry v2e3 = new BarEntry(120.000f, 2); // Mar
valueSet2.add(v2e3);
BarEntry v2e4 = new BarEntry(60.000f, 3); // Apr
valueSet2.add(v2e4);
BarEntry v2e5 = new BarEntry(20.000f, 4); // May
valueSet2.add(v2e5);
BarEntry v2e6 = new BarEntry(80.000f, 5); // Jun
valueSet2.add(v2e6);
ArrayList<BarEntry> valueSet3 = new ArrayList<>();
BarEntry v2e= new BarEntry(150.000f, 0); // Jan
valueSet3.add(v2e);
BarEntry v = new BarEntry(90.000f, 1); // Feb
valueSet3.add(v);
BarEntry vv = new BarEntry(120.000f, 2); // Mar
valueSet3.add(vv);
BarEntry vvv = new BarEntry(60.000f, 3); // Apr
valueSet3.add(vvv);
BarEntry vvvv = new BarEntry(20.000f, 4); // May
valueSet3.add(vvvv);
BarEntry vvvvv = new BarEntry(80.000f, 5); // Jun
valueSet3.add(vvvvv);
BarDataSet barDataSet1 = new BarDataSet(valueSet1, "Brand 1");
barDataSet1.setColor(Color.rgb(0, 155, 0));
BarDataSet barDataSet2 = new BarDataSet(valueSet2, "Brand 2");
barDataSet2.setColors(ColorTemplate.COLORFUL_COLORS);
BarDataSet barDataSet3 = new BarDataSet(valueSet3, "Brand 3");
barDataSet3.setColors(ColorTemplate.COLORFUL_COLORS);
dataSets = new ArrayList<>();
dataSets.add(barDataSet1);
dataSets.add(barDataSet2);
dataSets.add(barDataSet3);
return dataSets;
}
private ArrayList<String> getXAxisValues() {
ArrayList<String> xAxis = new ArrayList<>();
xAxis.add("JAN");
xAxis.add("FEB");
xAxis.add("MAR");
xAxis.add("APR");
xAxis.add("MAY");
xAxis.add("JUN");
return xAxis;
}
*/
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(getContext().getApplicationContext(), TimeTrackerDAO.DB_Timetracker_DashBoardCommercial, null, null, null, null){
ForceLoadContentObserver mObserver=new ForceLoadContentObserver();
#Override
public Cursor loadInBackground() {
c = timeTrackerDAO.DashBoardCommercial();
if(c==null){
Log.d("No Data", "No Data");
}
else {
c.getCount();
c.registerContentObserver(mObserver);
c.setNotificationUri(getContext().getContentResolver(), getUri());
}
return c;
}
};
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
BarChart chart = (BarChart) view.findViewById(R.id.chart);
if (getDataSet(data)
!= null) {
BarData data_commercial = new BarData(getXAxisValues(), getDataSet(data));
chart.setData(data_commercial);
chart.setDescription("My Chart");
chart.animateXY(2000, 2000);
chart.invalidate();
}
else {
Log.e("err", "err");
}
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
}
private ArrayList<IBarDataSet> getDataSet(Cursor c) {
ArrayList<IBarDataSet> dataSets = null;
ArrayList<BarEntry> valueSet1 = new ArrayList<>();
List<DashboardModelCommercial>liststatus=new ArrayList<>();
c=timeTrackerDAO.DashBoardCommercialStatus0();
if (c.moveToFirst()) {
do {
DashboardModelCommercial dashboardModelCommercial=new DashboardModelCommercial();
dashboardModelCommercial.setCount(c.getInt(c.getColumnIndex(DashboardModelCommercial.Commercial_Count)));
liststatus.add(dashboardModelCommercial);
} while (c.moveToNext());
}
for(int i=0;i<liststatus.size();i++){
valueSet1.add(new BarEntry(liststatus.get(i).getCount(),i));//Status 0
}
ArrayList<BarEntry> valueSet2 = new ArrayList<>();
List<DashboardModelCommercial>liststatus1=new ArrayList<>();
c=timeTrackerDAO.DashBoardCommercialStatus1();
if (c.moveToFirst()) {
do {
DashboardModelCommercial dashboardModelCommercial=new DashboardModelCommercial();
dashboardModelCommercial.setCount(c.getInt(c.getColumnIndex(DashboardModelCommercial.Commercial_Count)));
liststatus1.add(dashboardModelCommercial);
} while (c.moveToNext());
}
for(int i=0;i<liststatus1.size();i++){
valueSet2.add(new BarEntry(liststatus1.get(i).getCount(),i));//Status 0
}
ArrayList<BarEntry> valueSet3 = new ArrayList<>();
List<DashboardModelCommercial>liststatus2=new ArrayList<>();
c=timeTrackerDAO.DashBoardCommercialStatus2();
if(c.moveToFirst()){
do {
{
DashboardModelCommercial dashboardModelCommercial=new DashboardModelCommercial();
dashboardModelCommercial.setCount(c.getInt(c.getColumnIndex(DashboardModelCommercial.Commercial_Count)));
liststatus2.add(dashboardModelCommercial);
}
}while (c.moveToNext());
}
for (int i=0;i<liststatus2.size();i++){
valueSet3.add(new BarEntry(liststatus2.get(i).getCount(),i));
}
ArrayList<BarEntry>valueSet4=new ArrayList<>();
List<DashboardModelCommercial>liststatus3=new ArrayList<>();
c=timeTrackerDAO.DashBoardCommercialStatus3();
if(c.moveToFirst()){
do {
{
DashboardModelCommercial dashboardModelCommercial=new DashboardModelCommercial();
dashboardModelCommercial.setCount(c.getInt(c.getColumnIndex(DashboardModelCommercial.Commercial_Count)));
liststatus3.add(dashboardModelCommercial);
}
}while (c.moveToNext());
}
for (int i=0;i<liststatus3.size();i++){
valueSet4.add(new BarEntry(liststatus3.get(i).getCount(),i));
}
BarDataSet barDataSet1 = new BarDataSet(valueSet1, "Not Started");
barDataSet1.setColor(Color.rgb(0, 155, 0));
BarDataSet barDataSet2 = new BarDataSet(valueSet2, "In Progress");
barDataSet2.setColor(Color.rgb(164, 228, 251));
BarDataSet barDataSet3 = new BarDataSet(valueSet3, "Completed");
barDataSet3.setColor(Color.rgb(242, 247, 158));
BarDataSet barDataSet4 = new BarDataSet(valueSet4, " Not Completed");
barDataSet4.setColor(Color.rgb(200, 220, 170));
dataSets = new ArrayList<>();
dataSets.add(barDataSet1);
dataSets.add(barDataSet2);
dataSets.add(barDataSet3);
dataSets.add(barDataSet4);
return dataSets;
}
private ArrayList<String> getXAxisValues() {
ArrayList<String> xAxis = new ArrayList<>();
xAxis.add("Billable");
xAxis.add("Non Billable");
return xAxis;
}
}
I don't know where am making mistake how to recitify if am just banging my head in wall for hours can someone helpme out How to resolve this problem!
Bar entries count and XAxis labels count should be equal.
You are adding only 2 XAxis labels in the getXAxisValues() method.
May be you are getting more than 2 bar entries in your BarDataSet.
Add the XAxis labels that are equal to bar entries count in a BarDataSet.
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);