首页 > 代码库 > 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);
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。