如何在 Mpandroid 饼图中动态添加文本?
How to add text dynamically inside the Mpandroid Pie chart?
我已经关注了图表的 MpAndroid 库 creation.I 想在饼图的中心设置动态文本。我正在创建一个 BMI 计算器,所以我想在饼图的中心显示 BMI 值饼图。
这是我用来在饼图中显示文本的代码。
`
mChart.setDrawCenterText(true);
mChart.setCenterTextTypeface(mTfLight);
mChart.setCenterTextColor(Color.WHITE);
mChart.setCenterTextSize(15f);
mChart.setCenterText("BMI:"+bmi);
`
请查看完整代码。在 Oncreate()
方法中,我已将上述代码设置为在中心设置文本,但这显示的是静态文本,如何在中心设置文本动态居中(即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);
}
}
我还想问一件事,如何减小饼图切片的宽度。有一个选项可以调整内圆的半径,但是如何调整切片的宽度.??
欢迎任何形式的帮助。谢谢!!
您问了两个问题:
how to set the text inside the center dynamically?
再次调用setCenterText
然后调用Chart
的invalidate
方法:
mChart.setCenterText("BMI:" + bmi);
mChart.invalidate();
How do i decrease the width of the Slices of my Pie chart
有个方法PieChart
:
您使用不当。它的参数应该是“PieChart”半径的百分比。所以调用:
mChart.setHoleRadius(0.65f);
表示孔应占图表半径的 65%。
我已经关注了图表的 MpAndroid 库 creation.I 想在饼图的中心设置动态文本。我正在创建一个 BMI 计算器,所以我想在饼图的中心显示 BMI 值饼图。 这是我用来在饼图中显示文本的代码。 `
mChart.setDrawCenterText(true);
mChart.setCenterTextTypeface(mTfLight);
mChart.setCenterTextColor(Color.WHITE);
mChart.setCenterTextSize(15f);
mChart.setCenterText("BMI:"+bmi);
`
请查看完整代码。在 Oncreate()
方法中,我已将上述代码设置为在中心设置文本,但这显示的是静态文本,如何在中心设置文本动态居中(即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);
}
}
我还想问一件事,如何减小饼图切片的宽度。有一个选项可以调整内圆的半径,但是如何调整切片的宽度.??
欢迎任何形式的帮助。谢谢!!
您问了两个问题:
how to set the text inside the center dynamically?
再次调用setCenterText
然后调用Chart
的invalidate
方法:
mChart.setCenterText("BMI:" + bmi);
mChart.invalidate();
How do i decrease the width of the Slices of my Pie chart
有个方法PieChart
:
您使用不当。它的参数应该是“PieChart”半径的百分比。所以调用:
mChart.setHoleRadius(0.65f);
表示孔应占图表半径的 65%。