Why my diagram pie chart is so small. I use the library mpandroidchartlibrary-2-1-2.jar and I have activity_main.xml android:id="@+id/mainLayout". Anyone have any idea what the problem can be?
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();
}}
I had the same issue manto, you can simply resolve it by adding another layout into RelativeLayout
ie <FrameLayout>
and try to draw chart in FrameLayout
insted of directly in RelativeLayout
layout.
Changes in your XML : It should look like this :)
<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>
Changes in Java file :
1. private RelativeLayout mainLayout;
to --> private FrameLayout mainLayout;
mainLayout = (RelativeLayout)findViewById(R.id.mainLayout);
to --> mainLayout = (FrameLayout)findViewById(R.id.mainLayout);
i resolved with :
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();
barchart.setData(data); barchart.groupBars(0, 0.03f, 0.03f); barchart.invalidate();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.