Issue
I am trying to populate a vertical bar chart with some values. I still want to keep as entry the values that are equal to 0. However, the chart looks suspicious when there is an existing entry that is equal to 0.
The first image shows how the chart looks if all the entries are not zero.enter image description here The second image shows how the chart looks if there is an entry equal to 0.enter image description here (the chart does not exactly start from 0 on the vertical axis, it goes below) The third image shows how the chart looks like if all entries are equal to zero.enter image description here (as you can see, the chart starts from -1 on the vertical axis)
Is there a way to make it look like in the first image, despite the 0 values?
Solution
I found out how to achieve to start at zero even every value is 0. See in the code below, I commented everything:
public class MainActivity extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BarChart barChart = findViewById(R.id.bar_chart);
ArrayList<BarEntry> inventory = new ArrayList<>();
inventory.add(new BarEntry(1f,0));
inventory.add(new BarEntry(2f,10));
inventory.add(new BarEntry(3f,20));
inventory.add(new BarEntry(4f,30));
inventory.add(new BarEntry(5f,40));
//Colors & Textsize
BarDataSet barDataSet = new BarDataSet(inventory,"Inventory");
barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
barDataSet.setValueTextColor(Color.BLACK);
barDataSet.setValueTextSize(16f);
//Start at zero even all values are 0 for left YAxis.
YAxis yAxis = barChart.getAxis(YAxis.AxisDependency.LEFT);
yAxis.setStartAtZero(true);
//Start at zero even all values are 0 for right YAxis.
YAxis yAxis1 = barChart.getAxis(YAxis.AxisDependency.RIGHT);
yAxis1.setStartAtZero(true);
//Define every space between the scales you want to.
XAxis xAxis = barChart.getXAxis();
xAxis.setGranularity(1.0f);
xAxis.setGranularityEnabled(true);
/*
//Disables the right YAxis (Optional)
YAxis rightYAxis = barChart.getAxisRight();
rightYAxis.setEnabled(false);*/
//Other Settings
BarData barData = new BarData(barDataSet);
barChart.setData(barData);
barChart.getDescription().setEnabled(false);
barChart.getLegend().setEnabled(false);
barChart.animateX(500);
}
}
If you want to customized further I suggest to take a look at the documentation.
RESULT
Answered By - DEX7RA
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.