先引用hellocharts-library-<version>.jar
然后在layout中设置
<lecho.lib.hellocharts.view.PieChartView android:id="@+id/chart" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
package com.example.chardemo; import java.util.ArrayList; import java.util.List; import lecho.lib.hellocharts.gesture.ZoomType; import lecho.lib.hellocharts.model.PieChartData; import lecho.lib.hellocharts.model.SliceValue; import lecho.lib.hellocharts.renderer.PieChartRenderer; import lecho.lib.hellocharts.util.ChartUtils; import lecho.lib.hellocharts.view.PieChartView; import android.graphics.Typeface; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; public class MainActivity extends ActionBarActivity { private PieChartView chart; private PieChartData data; private boolean hasLabels = true;//是否在薄片上显示label private boolean hasLabelsOutside = false;//是否在薄片外显示label private boolean hasCenterCircle = false;//是否中间掏空一个圈 private boolean hasCenterText1 = true;//掏空圈是的title1 private boolean hasCenterText2 = true;//掏空圈是的title2 private boolean isExploded = false;//薄片是否分离 private boolean hasLabelForSelected = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); chart = (PieChartView)findViewById(R.id.chart); generateData(); } private void generateData() { int numValues = 3; List<SliceValue> values = new ArrayList<SliceValue>(); for (int i = 0; i < numValues; ++i) { SliceValue sliceValue = new SliceValue( (float) Math.random() * 30 + 15, ChartUtils.pickColor()); sliceValue.setLabel((int)sliceValue.getValue()+"瓶");//设置label values.add(sliceValue); } data = new PieChartData(values); data.setHasLabels(hasLabels); data.setHasLabelsOnlyForSelected(hasLabelForSelected); data.setHasLabelsOutside(hasLabelsOutside); data.setHasCenterCircle(hasCenterCircle); if (isExploded) { data.setSlicesSpacing(24);//设置分离距离 } if (hasCenterText1) { data.setCenterText1("Hello!"); // Get roboto-italic font. Typeface tf = Typeface.createFromAsset(this.getAssets(),"Roboto-Italic.ttf");//自定义的字体 data.setCenterText1Typeface(tf); // Get font size from dimens.xml and convert it to sp(library uses // sp values). data.setCenterText1FontSize(ChartUtils.px2sp(getResources() .getDisplayMetrics().scaledDensity, (int) getResources() .getDimension(R.dimen.pie_chart_text1_size))); } if (hasCenterText2) { data.setCenterText2("Charts (Roboto Italic)"); Typeface tf = Typeface.createFromAsset(this.getAssets(), "Roboto-Italic.ttf"); data.setCenterText2Typeface(tf); data.setCenterText2FontSize(ChartUtils.px2sp(getResources() .getDisplayMetrics().scaledDensity, (int) getResources() .getDimension(R.dimen.pie_chart_text1_size))); } chart.setPieChartData(data); chart.setCircleFillRatio(0.5f);//设置放大缩小范围 } }
效果图
来源:https://www.cnblogs.com/sweetculiji/p/4939651.html