Dataset entry are longer than x axis in MpAndroid chart? - android

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.

Related

Change position of x-axis values in MPAndroidChart

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.

Same BarData shows different in BarChart and CombinedChart

I am using MPAndroidChart2.2.5 to draw charts in my project,which contains 2 group of BarDataand 1 group of LineData.When I use the BarData in Barchart,the bars shows well.But when I use the same BarData in CombinedChart,the position of the bars differs.Here's my activity.
Here's a screenshot:
.
public class CombinedChartActivity extends BaseActivity {
private CombinedChart combinedChart;
private BarChart barChart;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_combined_chart);
combinedChart = (CombinedChart) findViewById(R.id.chart);
barChart = (BarChart) findViewById(R.id.barChart);
initChart(combinedChart);
initChart(barChart);
barChart.setData(getBarData());
CombinedData combinedData = new CombinedData();
combinedData.setXVals(getXVals());
combinedData.setData(getLineData());
combinedData.setData(getBarData());
combinedChart.setData(combinedData);
}
private void initChart(BarLineChartBase barLineChartBase) {
barLineChartBase.setDescription("");
barLineChartBase.getAxisLeft().setEnabled(false);
barLineChartBase.getAxisRight().setEnabled(false);
barLineChartBase.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
barLineChartBase.getXAxis().setGridColor(R.color.white);
barLineChartBase.getXAxis().setAvoidFirstLastClipping(true);
barLineChartBase.setBackgroundColor(Color.WHITE);
}
private BarData getBarData() {
List<IBarDataSet> iBarDataSets = new ArrayList<>();
ArrayList<BarEntry> barEntries = new ArrayList<>();
for (int i = 0; i < 7; i++) {
BarEntry barEntry = new BarEntry(i + 1, i);
barEntries.add(barEntry);
}
BarDataSet barDataSet = new BarDataSet(barEntries, "Bar1");
barDataSet.setColor(Color.rgb(60, 220, 78));
barDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
ArrayList<BarEntry> barEntries2 = new ArrayList<>();
for (int i = 0; i < 7; i++) {
BarEntry barEntry = new BarEntry((float) (i + Math.random() * 10), i);
barEntries2.add(barEntry);
}
BarDataSet barDataSet1 = new BarDataSet(barEntries2, "Bar2");
barDataSet1.setColor(R.color.level_3);
barDataSet1.setAxisDependency(YAxis.AxisDependency.RIGHT);
iBarDataSets.add(barDataSet);
iBarDataSets.add(barDataSet1);
BarData barData = new BarData(getXVals(), iBarDataSets);[![enter image description here][1]][1]
return barData;
}
private LineData getLineData() {
ArrayList<Entry> entries = new ArrayList<>();
for (int i = 0; i < 7; i++) {
Entry entry = new Entry((float) (Math.random() * 10), i);
entries.add(entry);
}
List<ILineDataSet> iLineDataSets = new ArrayList<>();
LineDataSet lds1 = new LineDataSet(entries, "Line");
lds1.setLineWidth(0.2f);
lds1.setColor(R.color.level_3);
lds1.setDrawFilled(true);
lds1.setFillAlpha(150);
lds1.setFillColor(R.color.level_2);
lds1.setDrawValues(false);
lds1.setHighlightEnabled(true);
lds1.setDrawHorizontalHighlightIndicator(false);
lds1.setHighLightColor(R.color.white);
lds1.setDrawCubic(true);
lds1.setDrawCircles(true);
lds1.setCircleColor(R.color.white);
lds1.setCircleColorHole(R.color.theme_orange);
lds1.setAxisDependency(YAxis.AxisDependency.LEFT);
iLineDataSets.add(lds1);
LineData lineData = new LineData(getXVals(), iLineDataSets);
return lineData;
}
private List<String> getXVals() {
ArrayList<String> xVals = new ArrayList<>();
xVals.add("Monday");
xVals.add("Tuesday");
xVals.add("Wednesday");
xVals.add("Thursday");
xVals.add("Friday");
xVals.add("Saturday");
xVals.add("Sunday");
return xVals;
}
}
I use same BarData in both charts.What causes the difference.How can I make the bars in Combinedchart show as in the Barchart?

How to add values left of the horizontal bar chart in android studio?

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,

MPAndroidGraph 'no suitable constructor found for BarEntry(float,float[]) '

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.

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