Zihan S. Zihan S. - 4 months ago 63
Android Question

How to set the proportion of a bar(in y-axis) in a mpandroidchart barchart

I just started learning to code and develop for Android.

Right now, I'm trying to make something like this:
Project Fi App UI
However, I cannot find a parameter or method in the MPAndroidChart library to set the proportion of a bar in a BarChart(or a HorizontalBarChart that I'm using). The bar in the BarChart simply decide its proportion automatically. What I want is a fixed maximum range of the Y-axis, and the proportion of the bar in the graph change according to its value.

Another problem is that I cannot get rid of the label on each bar. I wrote

barChart.setDrawValueAboveBar(false);


but the Bar still have a value to the right of it.

A screenshot of the fragment that contains the BarChart:
Screenshot

Here's the full code

public class MainFragment extends android.support.v4.app.Fragment{
public MainFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main_fragment, container, false);

HorizontalBarChart barChart = (HorizontalBarChart)rootView.findViewById(R.id.time_chart);

ArrayList<BarEntry> vals = new ArrayList<BarEntry>();

BarEntry val = new BarEntry(0.20f,0);
vals.add(val);

BarDataSet setTime = new BarDataSet(vals,"time used");
setTime.setAxisDependency(YAxis.AxisDependency.LEFT);
setTime.setColor(R.color.colorAccent);

ArrayList<String> xVals = new ArrayList<String>();
xVals.add("1");

BarData data = new BarData(xVals, setTime);
barChart.setData(data);
barChart.setBackgroundColor(ContextCompat.getColor(rootView.getContext(),R.color.chartBackGround));
barChart.setDrawValueAboveBar(false);
barChart.setDrawGridBackground(false);
barChart.setDrawBarShadow(true);
barChart.setDescription("");
barChart.setVisibleYRangeMaximum(1.0f,barChart.getAxisLeft().getAxisDependency());

barChart.getLegend().setEnabled(false);
barChart.getXAxis().setEnabled(false);
barChart.getXAxis().setDrawLabels(false);
barChart.getAxisRight().setEnabled(false);
barChart.getAxisLeft().setEnabled(false);
barChart.getAxisLeft().setAxisMaxValue(1.0f);
barChart.getAxisLeft().setAxisMinValue(0);
barChart.getAxisLeft().setDrawLabels(false);

barChart.invalidate();

return rootView;
}


}

Here's my color.xml

<resources>
<color name="colorPrimary">#607d8b</color>
<color name="colorPrimaryDark">#455a64</color>
<color name="colorAccent">#64ffda</color>
<color name="windowBackground">#ffffff</color>
<color name="chartBackGround">#ffffff</color>
<color name="colorTransparent">#00ffffff</color>




Thank you for helping!! I really cannot find the answer to this anywhere.

Answer

Try using:

YAxis leftAxis = chart.getAxisLeft();
leftAxis.setAxisMaxValue(maxValue);

This should give you a fixed max value.

If you want to get rid of the labels, try:

setDrawLabels(false)

This method can be applied to either of the axes, depending up on which labels you want to skip.

All the information you need is available in the documentation.

EDIT: Use this to get rid of the values

setTime.setDrawValues(false);

In your comments, you also asked how to change the color of the dataset. For that, use this:

setTime.setColor(Color.CYAN);