I have followed the MpAndroid Library for chart creation.I wanted to set Dynamic text on the center of the Pie chart .I am creating a BMI calculator ,so i wanted to display the BMI value inside the center of the Pie chart . This is the code which i used to display the text inside the Pie chart . `
mChart.setDrawCenterText(true);
mChart.setCenterTextTypeface(mTfLight);
mChart.setCenterTextColor(Color.WHITE);
mChart.setCenterTextSize(15f);
mChart.setCenterText("BMI:"+bmi);
`
Please have a look at the full code .In the Oncreate()
method i have set the above code for setting the text in the center but this is showing a static text , how to set the text inside the center dynamically (i.e. the calculated value of the BMI).????
MainActivity.java
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
private int selectedId;
private EditText age;
private EditText height;
private EditText weight;
private TextView result;
private Spinner height_spinner;
private Spinner weight_spinner;
private Button calculate;
private PieChart mChart;
String spinner_height; //Height Spinner
String spinner_weight; //Weight Spinner
String selected_item1;
String selected_item2;
float bmi=0f;
protected String[] BMIcategory = new String[]
{
"Very Severly Underweight", "Severly Underweight", "Underweight", "Normal",
"Overweight", "Obese Class I", "Obese Class II", "Obese Class III",
};
protected Typeface mTfRegular;
protected Typeface mTfLight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setTitle("BMI Calculator");
mTfRegular=Typeface.MONOSPACE;
mTfLight = Typeface.MONOSPACE;
radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
selectedId = radioSexGroup.getCheckedRadioButtonId();
radioSexButton = (RadioButton) findViewById(selectedId);
mChart=(PieChart)findViewById(R.id.chart);
age=(EditText) findViewById(R.id.age);
height=(EditText) findViewById(R.id.height);
weight=(EditText) findViewById(R.id.weight);
// result=(TextView) findViewById(R.id.result);
height_spinner=(Spinner) findViewById(R.id.height_spinner);
weight_spinner=(Spinner) findViewById(R.id.weight_spinner);
calculate=(Button) findViewById(R.id.calculate_button);
mChart.setBackgroundColor(Color.rgb(76,0,0));
// moveOffScreen();
mChart.setUsePercentValues(false);
mChart.getDescription().setEnabled(false);
mChart.setDrawCenterText(true);
mChart.setCenterTextTypeface(mTfLight);
mChart.setCenterTextColor(Color.WHITE);
mChart.setCenterTextSize(15f);
mChart.setCenterText("BMI:"+bmi);
mChart.setDrawHoleEnabled(true);
mChart.setHoleColor(Color.rgb(76,0,0));
mChart.setTransparentCircleColor(Color.rgb(76,0,0));
mChart.setTransparentCircleAlpha(110);
mChart.setHoleRadius(65f);
mChart.setTransparentCircleRadius(68f);
mChart.setDrawSliceText(false);
mChart.setDrawEntryLabels(false);
mChart.setRotationEnabled(false);
mChart.setHighlightPerTapEnabled(false);
mChart.setMaxAngle(270f); // HALF CHART
mChart.setRotationAngle(135f);
// mChart.setRotation(180f);
// mChart.setCenterTextOffset(0, -20);
setData(8, 80);
mChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);
mChart.getLegend().setEnabled(false);
calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculate();
}
});
set_spinner();
}
private void setData(int count, float range) {
ArrayList<PieEntry> values = new ArrayList<>();
values.add(new PieEntry(10f,BMIcategory[0]));
values.add(new PieEntry(15f,BMIcategory[1]));
values.add(new PieEntry(8f,BMIcategory[2]));
values.add(new PieEntry(12f,BMIcategory[3]));
values.add(new PieEntry(5f,BMIcategory[4]));
values.add(new PieEntry(5f,BMIcategory[5]));
values.add(new PieEntry(15f,BMIcategory[6]));
values.add(new PieEntry(10f,BMIcategory[7]));
PieDataSet dataSet = new PieDataSet(values, "BMI Category");
dataSet.setSliceSpace(1f);
dataSet.setSelectionShift(5f);
// add many colors
ArrayList<Integer> colors = new ArrayList<Integer>();
for (int c : ColorTemplate.VORDIPLOM_COLORS)
colors.add(c);
for (int c : ColorTemplate.JOYFUL_COLORS)
colors.add(c);
for (int c : ColorTemplate.COLORFUL_COLORS)
colors.add(c);
for (int c : ColorTemplate.LIBERTY_COLORS)
colors.add(c);
for (int c : ColorTemplate.PASTEL_COLORS)
colors.add(c);
colors.add(ColorTemplate.getHoloBlue());
dataSet.setColors(colors);
//dataSet.setSelectionShift(0f);
PieData data = new PieData(dataSet);
data.setDrawValues(false);
mChart.setData(data);
mChart.invalidate();
}
public void set_spinner(){
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this, R.array.height_spinner_array, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
height_spinner.setAdapter(adapter1);
height_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
int a = parent.getSelectedItemPosition();
spinner_height = height_spinner.getSelectedItem().toString();
if (a == 0) {
selected_item1="Meters";
}
if (a == 1) {
selected_item1="Inches";
}
if(a==2)
{
selected_item1="Centimeters";
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
});
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.weight_spinner_array, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
weight_spinner.setAdapter(adapter2);
weight_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
int b = parent.getSelectedItemPosition();
spinner_weight = weight_spinner.getSelectedItem().toString();
if(b==0)
{
selected_item2="Kgs";
}
if(b==1)
{
selected_item2="Lbs";
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {} } );
}
public void calculate() {
float getHeight, getWeight;
DecimalFormat df = new DecimalFormat("#0.00");
// for height in m and weight in kgs
if (height.getText().toString().equals("")) {
getHeight = 0f;
} else {
getHeight = Float.parseFloat(height.getText().toString());
}
if (weight.getText().toString().equals("")) {
getWeight = 0f;
} else {
getWeight = Float.parseFloat(weight.getText().toString());
}
if(height.getText().toString().equals("") || weight.getText().toString().equals("") || age.getText().toString().equals(""))
{
Toast.makeText(this,"Please Enter All the Values",Toast.LENGTH_SHORT).show();
return;
}
if(selected_item1.equals("Meters"))
{
if(selected_item2.equals("Kgs"))
{
bmi = getWeight / (getHeight * getHeight);
}
if(selected_item2.equals("Lbs"))
{
bmi = (getWeight*0.453592f) / (getHeight * getHeight);
}
}
if(selected_item1.equals("Inches"))
{
if(selected_item2.equals("Kgs"))
{
bmi = (getWeight*318.87517f) / (getHeight * getHeight);
}
if(selected_item2.equals("Lbs"))
{
bmi = (getWeight*703f) / (getHeight * getHeight);
}
}
if(selected_item1.equals("Centimeters"))
{
if(selected_item2.equals("Kgs"))
{
bmi = (getWeight*10000) / (getHeight * getHeight);
}
if(selected_item2.equals("Lbs"))
{
bmi=(getWeight*0.453592f*10000) / (getHeight * getHeight);
}
}
// result.setText("Your BMI is " + df.format((double)bmi));
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
One more thing i wanted to ask ,How do i decrease the width of the Slices of my Pie chart .There is a option to adjust the radius of the Inner circle ,but how do i adjust the width of the Slices .??
Any Kind of Help is most Welcome .Thanks !!
You have asked two questions:
how to set the text inside the center dynamically?
Just call again setCenterText
and then invalidate
method of Chart
:
mChart.setCenterText("BMI:" + bmi);
mChart.invalidate();
How do i decrease the width of the Slices of my Pie chart
There is a method of PieChart
:
You are using it incorrectly. It's parameter should a percent of the `PieChart' radius. So the call:
mChart.setHoleRadius(0.65f);
means, the hole should take 65% of chart radius.