如何在Android中使用AchartEngine在条形图中为单条应用多种颜色?

时间:2013-06-14 07:24:27

标签: android achartengine

enter image description here
 我想根据图像制作条形图,但我不知道如何在y轴的某个点之后更改条形颜色我也想在y轴的特定点绘制一条线(图像中的红线),如图像请帮帮我怎么办呢?我的图表视图代码是

private void ChartView() {

            String[] titles = new String[] { " " };

        //  int[] colors = new int[] { Color.BLUE, Color.BLUE };
            multiRenderer = buildBarRenderer(colors);
            multiRenderer.setApplyBackgroundColor(true);

            multiRenderer.setMarginsColor(Color.WHITE);
            multiRenderer.setBackgroundColor(Color.parseColor("#DCDCDC"));
            setChartSettings(multiRenderer, "Calorie-Consume-Chart", chatX_lable,chatY_lable, -4,
                    6, 0, 240, Color.BLACK, Color.BLACK);
            multiRenderer.getSeriesRendererAt(0).setDisplayChartValues(true);
            //multiRenderer.getSeriesRendererAt(1).setDisplayChartValues(true);

            multiRenderer.setXLabels(0);
            multiRenderer.setYLabels(10);
            multiRenderer.clearXTextLabels();
            multiRenderer.setXLabelsAlign(Align.LEFT);
            multiRenderer.setYLabelsAlign(Align.LEFT);
            multiRenderer.setPanEnabled(false, false);
            // renderer.setZoomEnabled(false);
            multiRenderer.setZoomRate(1.1f);
            multiRenderer.setBarSpacing(0.7);
            multiRenderer.setZoomEnabled(true);
            multiRenderer.


            LinearLayout layout = (LinearLayout) findViewById(R.id.weightlinl);
            layout.removeAllViews();
            if (mChartView == null) {
                mChartView = ChartFactory.getBarChartView(this,
                        buildBarDataset(titles, weekly_data_int)/* dataset */,
                        multiRenderer,org.achartengine.chart.BarChart.Type.DEFAULT);
                layout.addView(mChartView);
            } else {
                // mChartView.repaint();

                mChartView = ChartFactory.getBarChartView(this,
                        buildBarDataset(titles, weekly_data_int), multiRenderer,
                        org.achartengine.chart.BarChart.Type.DEFAULT);
                mChartView.setBackgroundColor(Color.YELLOW);
                layout.addView(mChartView);

                // mChartView.repaint();
            }
            mChartView.invalidate();
        }

        protected void setChartSettings(XYMultipleSeriesRenderer renderer,
                String title, String xTitle, String yTitle, double xMin,
                double xMax, double yMin, double yMax, int axesColor,
                int labelsColor) {
            renderer.setChartTitle(title);
            renderer.setXTitle(xTitle);
            renderer.setYTitle(yTitle);
            renderer.setXAxisMin(xMin);
            renderer.setXAxisMax(xMax);
            renderer.setYAxisMin(yMin);
            renderer.setYAxisMax(yMax);
            renderer.setInitialRange(range);
            renderer.clearXTextLabels();
            renderer.setAxesColor(axesColor);

            renderer.setLabelsColor(labelsColor);
        }

        protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) {
            XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
            renderer.setAxisTitleTextSize(12);
            renderer.setChartTitleTextSize(16);
            renderer.setLabelsTextSize(12);
            renderer.setLegendTextSize(12);
            int length = colors.length;
            for (int i = 0; i < length; i++) {
                SimpleSeriesRenderer r = new SimpleSeriesRenderer();
                r.setColor(colors[i]);
                renderer.addSeriesRenderer(r);
            }
            return renderer;
        }

        protected XYMultipleSeriesDataset buildBarDataset(String[] titles, int j) {
            XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
            int length = titles.length;
            for (int i = 0; i < length; i++) {
                CategorySeries series = new CategorySeries(titles[i]);
                series.add(j);

                dataset.addSeries(series.toXYSeries());
            }
            return dataset;
        }
![enter image description here][1]


  [1]: http://i.stack.imgur.com/rKyjg.png

2 个答案:

答案 0 :(得分:1)

我认为以下黑客攻击对您有用:

  • 根据您的第一个要求,尝试使用堆积条形图。管理它可能有点棘手,但应该有效
  • 为了绘制直线,我不确定AchartEngine库是否提供任何此类方法。您可以使用折线图绘制直线,也可以在库中进行更改以添加功能。

答案 1 :(得分:0)

这可能会解决您的问题:

renderer.getSeriesRendererAt(0).setGradientEnabled(真);

renderer.getSeriesRendererAt(0).setGradientStart(0,                 Color.rgb(025,117,025));

renderer.getSeriesRendererAt(0).setGradientStop(yMaxVal / 2,                 Color.rgb(94,158,94));

使用setGradientStart并停止在栏中的某个点之后定义颜色。