我在我的应用程序中使用Mp饼图.它显示非常小的尺寸,我试图增加它的大小但它没有增加它的大小.我无法找出问题所在.请告诉我们如何增加尺寸.
这是我的代码:
public class MPpiechart extends Activity {
private LinearLayout mainLayout;
private PieChart mChart;
// we're going to display pie chart for smartphones martket shares
private float[] yData = { 5, 10, 15, 30, 40 };
private String[] xData = { "Sony", "Huawei", "LG", "Apple", "Samsung" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.piegraph);
mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
mChart = new PieChart(this);
// add pie chart to main layout
mainLayout.addView(mChart);
mainLayout.setBackgroundColor(Color.parseColor("#55656C"));
// configure pie chart
mChart.setUsePercentValues(true);
mChart.setDescription("Smartphones Market Share");
// enable hole and configure
mChart.setDrawHoleEnabled(true);
//mChart.setHoleColorTransparent(true);
mChart.setHoleRadius(7);
mChart.setTransparentCircleRadius(10);
// enable rotation of the chart by touch
mChart.setRotationAngle(0);
mChart.setRotationEnabled(true);
// set a chart value selected listener
mChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
// display msg when value selected
if (e == null)
return;
Toast.makeText(MPpiechart.this,
xData[e.getXIndex()] + " = " + e.getVal() + "%", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected() {
}
});
// add data
addData();
// customize legends
Legend l = mChart.getLegend();
l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
l.setXEntrySpace(7);
l.setYEntrySpace(5);
}
private void addData() {
ArrayList<Entry> yVals1 = new ArrayList<Entry>();
for (int i = 0; i < yData.length; i++)
yVals1.add(new Entry(yData[i], i));
ArrayList<String> xVals = new ArrayList<String>();
for (int i = 0; i < xData.length; i++)
xVals.add(xData[i]);
// create pie data set
PieDataSet dataSet = new PieDataSet(yVals1, "Market Share");
dataSet.setSliceSpace(3);
dataSet.setSelectionShift(5);
// 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);
// instantiate pie data object now
PieData data = new PieData(xVals, dataSet);
data.setValueFormatter(new PercentFormatter());
data.setValueTextSize(11f);
data.setValueTextColor(Color.GRAY);
mChart.setData(data);
// undo all highlights
mChart.highlightValues(null);
// update pie chart
mChart.invalidate();
}
Piegraph.java
public class PieGraph extends View {
private Paint piePaint;
private RectF rectF;
private float[] data;
public PieGraph(Context context, AttributeSet attrs){
super(context,attrs);
piePaint = new Paint();
piePaint.setAntiAlias(true);
piePaint.setDither(true);
piePaint.setStyle(Paint.Style.FILL);
/* mShadowPaint = new Paint(0);
mShadowPaint.setColor(0xff101010);
mShadowPaint.setMaskFilter(new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL));*/
}
public float[] pieSegment(){
float[] segValues = new float[this.data.length];
float Total = getTotal();
for (int i = 0; i < this.data.length; i++){
segValues[i] = (this.data[i]/Total) * 360;
}
// x = ( radius of pie chart /2)*cos(angle in RADIANS) [angle in radians = Math.toRadians(half the sweep angle in degrees)
return segValues;
}
public float getTotal(){
float total = 0;
for (float val : this.data){
total +=val;
}
return total;
}
@Override
protected void onDraw(Canvas canvas){
if (data != null){
int top = 0;
int left = 0;
int endBottom = getHeight();
int endRight = endBottom;
rectF = new RectF(left, top, endRight, endBottom);
float[] segment = pieSegment();
float segStartPoint = 0;
for (int i = 0; i < segment.length; i++){
Random rnd = new Random();
int[] color = {getResources().getColor(R.color.blue),getResources().getColor(R.color.yellow),
getResources().getColor(R.color.red),getResources().getColor(R.color.gray)};
// int color = Color.argb(255, (int)segment[i], rnd.nextInt(256), rnd.nextInt(256));
String[] name ={(String) getResources().getText(R.string.Energy), (String) getResources().getText(R.string.Fat),
(String) getResources().getText(R.string.Fiber), (String) getResources().getText(R.string.Methi)};
piePaint.setColor(color[i]);
canvas.drawText(name[i],segStartPoint,segment[i], piePaint);
canvas.drawArc(rectF, segStartPoint, segment[i], true, piePaint);
segStartPoint += segment[i];
}
}
}
public void setData(float[] data){
this.data = data;
invalidate();
}
}
piegraph.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:gravity="center"
android:orientation="vertical"
android:id="@+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</RelativeLayout>
</LinearLayout>
最佳答案 内部饼图数据大小
.setValueTextSize(12);
中心数据大小
.setCenterTextSize(30);