HelloCharts 之餅狀圖使用
註釋很詳細,直接上程式碼,上圖
<img src="https://img-blog.csdn.net/20160615133714450?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
private PieChartView chart; private PieChartData data; private boolean hasLabels = true; private boolean hasLabelsOutside = true; private boolean hasCenterCircle = true; private boolean hasCenterText1 = true;//環形中間的文字1 private boolean hasCenterText2 = false; private boolean isExploded = true; private boolean hasLabelForSelected = true; private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* 當然也可以在java程式碼中直接建立: LineChartView chart = new LineChartView(context); layout.addView(chart);*/ initViews(); initDates(); } private void initDates() { generateData(); } private void initViews() { chart=(PieChartView)findViewById(R.id.chart); chart.setOnValueTouchListener(new ValueTouchListener());//新增點選事件 chart.setCircleFillRatio(0.9f);//設定圖所佔整個view的比例 當有外面的資料時使用,防止資料顯示不全 btn=(Button)findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { prepareDataAnimation();//更新資料,並新增動畫 } }); } /** * 生成資料 */ private void generateData() { int numValues = 10;//分成的塊數 List<SliceValue> values = new ArrayList<SliceValue>(); for (int i = 0; i < numValues; ++i) { SliceValue sliceValue = new SliceValue(20.0f,ChartUtils.pickColor());//每一塊的值和顏色,圖示根據值自動進行比例分配 values.add(sliceValue); } data = new PieChartData(values); data.setHasLabels(true);//顯示資料 data.setHasLabelsOnlyForSelected(false);//不用點選顯示佔的百分比 data.setHasLabelsOutside(true);//佔的百分比是否顯示在餅圖外面 data.setHasCenterCircle(true);;//是否是環形顯示 data.setCenterCircleScale(0.5f);////設定環形的大小級別 data.setValueLabelBackgroundColor(Color.TRANSPARENT);////設定值得背景透明 data.setValueLabelBackgroundEnabled(false);//資料背景不顯示 data.setValueLabelsTextColor(Color.BLACK); //data.setValues(values);//填充資料 if (isExploded) { data.setSlicesSpacing(1);//設定間隔為0 } if (hasCenterText1) { data.setCenterText1("Hello!"); // Get roboto-italic font. // Typeface tf = Typeface.createFromAsset(MainActivity.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))); data.setCenterText1Color(Color.BLACK);////設定值得顏色*/ } if (hasCenterText2) { data.setCenterText2("Charts (Roboto Italic)"); Typeface tf = Typeface.createFromAsset(MainActivity.this.getAssets(), "Roboto-Italic.ttf"); data.setCenterText2Typeface(tf); data.setCenterText2FontSize(ChartUtils.px2sp(getResources().getDisplayMetrics().scaledDensity, (int) getResources().getDimension(R.dimen.pie_chart_text2_size))); } chart.setPieChartData(data); } /** * To animate values you have to change targets values and then call} * method(don't confuse with View.animate()). */ private void prepareDataAnimation() { for (SliceValue value : data.getValues()) { value.setTarget((float) Math.random() * 30 + 15);//更新資料 } chart.startDataAnimation(); } private class ValueTouchListener implements PieChartOnValueSelectListener { @Override public void onValueSelected(int arcIndex, SliceValue value) { Toast.makeText(MainActivity.this, "Selected: " + value, Toast.LENGTH_SHORT).show(); } @Override public void onValueDeselected() { // TODO Auto-generated method stub }
compile 'com.github.lecho:hellocharts-library:[email protected]'
<pre style="font-family: 宋體; font-size: 9pt; background-color: rgb(255, 255, 255);">maven { url <span style="color:#008000;"><strong>"https://jitpack.io" </strong></span>}