Android - 饼图

时间:2016-02-26 00:34:25

标签: java android pie-chart

enter image description here

为什么我的图表饼图太小了。我使用库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();


}}

2 个答案:

答案 0 :(得分:0)

我有同样的问题,您可以通过在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; 指向 - &gt; private FrameLayout mainLayout;

  1. mainLayout = (RelativeLayout)findViewById(R.id.mainLayout); 指向 - &gt; mainLayout = (FrameLayout)findViewById(R.id.mainLayout);

答案 1 :(得分:0)

我决定:

 ViewGroup.LayoutParams lp = barchart.getLayoutParams();
    lp.height=Extra.convertDpToPixel(yVals1.size()*150,this);
    barchart.setLayoutParams(lp);

befor barchart.setData(data); barchart.groupBars(0, 0.03f, 0.03f); barchart.invalidate();