为什么我的图饼图这么小。我使用的是mpandroidchartlibrary-2-1-2.jar库,并且我具有activity_main.xml android:id =“ @ + id / mainLayout”。任何人都知道问题可能是什么?
public class MainActivity extends Activity{
private RelativeLayout mainLayout;
private PieChart mChart;
DatabaseHelper datahelper = new DatabaseHelper(this);
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.activity_main);
mainLayout = (RelativeLayout)findViewById(R.id.mainLayout);
mChart = new PieChart(this);
mainLayout.addView(mChart);
mainLayout.setBackgroundColor(Color.LTGRAY);
mChart.setUsePercentValues(true);
mChart.setDescription("Smartphones Market Share");
mChart.setDrawHoleEnabled(true);
mChart.setHoleColorTransparent(true);
mChart.setHoleRadius(7);
mChart.setTransparentCircleRadius(10);
mChart.setRotationAngle(0);
mChart.setRotationEnabled(true);
mChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
if (e == null)
return;
Toast.makeText(Main6Activity.this, xData[e.getXIndex()] + " = " + e.getVal() + "%", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected() {
}
});
addData();
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]);
PieDataSet dataSet = new PieDataSet(yVals1,"Market Share");
dataSet.setSliceSpace(3);
dataSet.setSelectionShift(5);
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);
PieData data = new PieData(xVals,dataSet);
data.setValueFormatter(new PercentFormatter());
data.setValueTextSize(11f);
data.setValueTextColor(Color.GRAY);
mChart.setData(data);
mChart.highlightValues(null);
mChart.invalidate();
}}
最佳答案
我遇到了同样的问题manto,您可以通过在RelativeLayout
中添加另一个布局来简单地解决它,即<FrameLayout>
并尝试在直接安装在FrameLayout
布局中的RelativeLayout
中绘制图表。
XML中的更改:应如下所示:)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<FrameLayout
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
>
</FrameLayout>
</RelativeLayout>
Java文件中的更改:
1。
private RelativeLayout mainLayout;
至-> private FrameLayout mainLayout;
mainLayout = (RelativeLayout)findViewById(R.id.mainLayout);
至-> mainLayout = (FrameLayout)findViewById(R.id.mainLayout);
关于java - Android-饼图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35641191/