首页 > 代码库 > Android AChartEngine 饼图渐变效果

Android AChartEngine 饼图渐变效果

二话不说,先上图

核心代码如下:

private void initLabelChat(View rootView) {        NumberFormat nf = new DecimalFormat("#.# 小时");                final DefaultRenderer renderer = new DefaultRenderer();        final CategorySeries series = new CategorySeries("");        LinearLayout layout = (LinearLayout) rootView                .findViewById(R.id.pie_statistic_by_label);        final GraphicalView chartView = ChartFactory.getPieChartView(                getActivity(), series, renderer);        chartView.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                SeriesSelection seriesSelection = chartView                        .getCurrentSeriesAndPoint();                if (seriesSelection == null) {                    /*                     * Toast.makeText(getActivity(),                     * "No chart element selected", Toast.LENGTH_SHORT) .show();                     */                } else {                    for (int i = 0; i < series.getItemCount(); i++) {                        renderer.getSeriesRendererAt(i).setHighlighted(                                i == seriesSelection.getPointIndex());                    }                    chartView.repaint();                }            }        });        renderer.setClickEnabled(true);        renderer.setPanEnabled(false);        renderer.setDisplayValues(false);        layout.addView(chartView, new LayoutParams(700, 700));        renderer.setZoomButtonsVisible(false);        renderer.setZoomEnabled(false);        renderer.setDisplayValues(true);        renderer.setShowLegend(false);        renderer.setLabelsTextSize(30f);        renderer.setLabelsColor(Color.DKGRAY);        renderer.setShowGrid(true);        renderer.setAntialiasing(true);        series.add("样本1", 5);        SimpleSeriesRenderer seriesRenderer = new SimpleSeriesRenderer();        seriesRenderer.setGradientEnabled(true);        seriesRenderer.setChartValuesFormat(nf);        seriesRenderer.setGradientStart(0, Color.rgb(0x04, 0xa3, 0xff));        seriesRenderer.setGradientStop(0.8, Color.rgb(0x00, 0x89, 0xd8));        seriesRenderer.setDisplayChartValues(false);        renderer.addSeriesRenderer(seriesRenderer);        series.add("样本2", 8);        seriesRenderer = new SimpleSeriesRenderer();        seriesRenderer.setGradientEnabled(true);        seriesRenderer.setChartValuesFormat(nf);        seriesRenderer.setGradientStart(0, Color.rgb(0xff, 0xb0, 0x0d));        seriesRenderer.setGradientStop(0.8, Color.rgb(0xce, 0x97, 0x00));        seriesRenderer.setDisplayChartValues(false);        renderer.addSeriesRenderer(seriesRenderer);        series.add("样本3", 6);        seriesRenderer = new SimpleSeriesRenderer();        seriesRenderer.setGradientEnabled(true);        seriesRenderer.setChartValuesFormat(nf);        seriesRenderer.setGradientStart(0, Color.rgb(0x00, 0xbc, 0x66));        seriesRenderer.setGradientStop(0.8, Color.rgb(0x01, 0x80, 0x46));        seriesRenderer.setDisplayChartValues(false);        renderer.addSeriesRenderer(seriesRenderer);        series.add("样本4", 3);        seriesRenderer = new SimpleSeriesRenderer();        seriesRenderer.setGradientEnabled(true);        seriesRenderer.setChartValuesFormat(nf);        seriesRenderer.setGradientStart(0, Color.rgb(0xff, 0x6e, 0xf6));        seriesRenderer.setGradientStop(0.8, Color.rgb(0xff, 0x00, 0xde));        seriesRenderer.setDisplayChartValues(false);        renderer.addSeriesRenderer(seriesRenderer);        chartView.repaint();    }
其实很简单,设置渐变开启,然后设置渐变颜色即可

seriesRenderer.setGradientEnabled(true);

seriesRenderer.setGradientStart(0, Color.rgb(0x04, 0xa3, 0xff));
seriesRenderer.setGradientStop(0.8, Color.rgb(0x00, 0x89, 0xd8));


其中还有一个设置饼图值的方法,需要先创建一个NumberFormat
NumberFormat nf = new DecimalFormat("#.# 小时");


然后调用下面的方法即可
seriesRenderer.setChartValuesFormat(nf);