zoukankan      html  css  js  c++  java
  • 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);
     
  • 相关阅读:
    关于自适应屏幕方向和大小的一些经验
    在线升级Android应用程序完善版
    H263&H264&MPEG4
    PyCharm2019 激活
    VMware Workstation下载安装破解秘钥
    linux/kali安装及更新源以及输入法等配置
    python推倒式(列表、字典、集合)
    协程
    Flask中获取参数(路径,查询,请求体,请求头)
    Flask中获取参数(路径,查询,请求体,请求头)
  • 原文地址:https://www.cnblogs.com/stevenluo/p/3859321.html
Copyright © 2011-2022 走看看