5

我正在使用 AchartEngine 绘制折线图。

我所做的:我想使用 Achart 引擎制作 LineGraph。但是像我们在条形图中那样启用不同的颜色渐变。

我有无忧无虑地制作折线图。

我知道渐变仅受 Achart 引擎中的范围条形图支持。但是,如果有人有的话,应该有一些解决方法。

我有我的代码如下:

public GraphicalView graph(Context context) {
        String[] titles = new String[] { "Nike Line Graph" };
        List<double[]> values = new ArrayList<double[]>();

        values.add(new double[] { 14230, 12300, 14240, 15244, 14900, 12200,
                11030, 12000, 12500, 15500, 14600, 15000 });


        int length = values.get(0).length;

        Log.d("****length*********", "" + length);

        int[] colors = new int[] { Color.GREEN };
        PointStyle[] styles = new PointStyle[] { PointStyle.POINT };

        XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
        setChartSettings(renderer, "Monthly sales in the last 2 years",
                "Month", "Units sold", 0.75, 12.25, 10000, 19000,
                Color.argb(0x00, 0x01, 0x01, 0x01),
                Color.argb(0x00, 0x01, 0x01, 0x01));

        renderer.setChartTitleTextSize(20);
        renderer.setTextTypeface("sans_serif", Typeface.BOLD);
        renderer.setLabelsTextSize(0f);
        renderer.setLabelsColor(Color.argb(0x00, 0x01, 0x01, 0x01));
        renderer.setAxisTitleTextSize(15);
        renderer.setApplyBackgroundColor(true);
        renderer.setBackgroundColor(Color.argb(0x00, 0x01, 0x01, 0x01));
        renderer.setLegendTextSize(15);
        renderer.setScale(10);
        renderer.setShowAxes(false);
        renderer.setXLabelsColor(Color.argb(0x00, 0x01, 0x01, 0x01));
        renderer.setYLabelsColor(0, Color.argb(0x00, 0x01, 0x01, 0x01));
        renderer.setMarginsColor(Color.argb(0x00, 0x01, 0x01, 0x01));

        renderer.setPanEnabled(false, false);
        renderer.setZoomEnabled(false, false);
        length = renderer.getSeriesRendererCount();
        SimpleSeriesRenderer r1 = new SimpleSeriesRenderer();

        r1.setGradientEnabled(true);
        r1.setGradientStart(-50, Color.BLACK);
        r1.setGradientStop(50, Color.GREEN);



        Log.d("****length*********", "" + length);

        for (int i = 0; i < length; i++) {
            XYSeriesRenderer r = (XYSeriesRenderer) renderer
                    .getSeriesRendererAt(i);
            r.setLineWidth(5f);
        }

        GraphicalView v = ChartFactory.getCubeLineChartView(context,
                buildBarDataset(titles, values), renderer, 0.5f);
        return v;

    }

有了方法:

protected XYMultipleSeriesRenderer buildRenderer(int[] colors,
            PointStyle[] styles) {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        setRenderer(renderer, colors, styles);
        return renderer;
    }

    protected void setRenderer(XYMultipleSeriesRenderer renderer, int[] colors,
            PointStyle[] styles) {
        renderer.setAxisTitleTextSize(16);
        renderer.setChartTitleTextSize(20);
        renderer.setLabelsTextSize(15);
        renderer.setLegendTextSize(15);
        renderer.setPointSize(5f);

        renderer.setMargins(new int[] { 20, 30, 15, 20 });
        int length = colors.length;
        for (int i = 0; i < length; i++) {

             XYSeriesRenderer r = new XYSeriesRenderer();
              r.setColor(colors[i]);
             r.setPointStyle(styles[i]);    
             renderer.addSeriesRenderer(r);     

        }

我得到的是如下截图:

在此处输入图像描述

我想要的是 :

在此处输入图像描述

问题:我想在此折线图中添加渐变颜色。从一种颜色到另一种颜色。

任何帮助都会受到高度赞赏。

预先感谢。

4

2 回答 2

0

如果您仍在寻找答案并且有人发现这个问题相同,那么答案是肯定的,我们可以将渐变设置为折线图。解决方案是继承 LineChart 并覆盖 drawSeries 方法,如下所示

 @Override
    public void drawSeries(Canvas canvas, Paint paint, float[] points,
                    SimpleSeriesRenderer seriesRenderer, float yAxisValue, int seriesIndex, int startIndex) {
            int length = points.length;
            XYSeriesRenderer renderer = (XYSeriesRenderer) seriesRenderer;
            float lineWidth = paint.getStrokeWidth();
            paint.setStrokeWidth(renderer.getLineWidth());
            if (renderer.isFillBelowLine() || renderer.isGradientEnabled()) {
                    int pLength = points.length;
                    float[] fillPoints = new float[pLength + 4];
                    System.arraycopy(points, 0, fillPoints, 0, length);
                    fillPoints[0] = points[0] + 1;
                    fillPoints[length] = fillPoints[length - 2];
                    fillPoints[length + 1] = yAxisValue;
                    fillPoints[length + 2] = fillPoints[0];
                    fillPoints[length + 3] = fillPoints[length + 1];
                    float max = 0;
                    for (int i = 0; i < length + 4; i += 2) {
                            if (fillPoints[i + 1] < 0) {
                                    fillPoints[i + 1] = 0;
                            }
                            else if (fillPoints[i + 1] > max) {
                                    max = fillPoints[i + 1];
                            }
                    }
                    paint.setStyle(Style.FILL);
                    if (renderer.isGradientEnabled()) {
                            Shader s = paint.getShader();
                            // Create gradient from values set in renderer
                            paint.setShader(new LinearGradient(0, (float)(renderer.getGradientStartValue() * max), 0, (float)(renderer.getGradientStopValue() * max),
                                            renderer.getGradientStartColor(), renderer.getGradientStopColor(), Shader.TileMode.MIRROR));
                            drawPath(canvas, fillPoints, paint, true);
                            // Restore paint's shader to previous value (null)
                            paint.setShader(s);
                    }
                    else {
                            paint.setColor(renderer.getFillBelowLineColor());
                            drawPath(canvas, fillPoints, paint, true);
                    }
            }
            paint.setColor(seriesRenderer.getColor());
            paint.setStyle(Style.STROKE);
            drawPath(canvas, points, paint, false);
            paint.setStrokeWidth(lineWidth);
    }

如果您想使用原始粘贴数据查看,请使用以下代码:

@Override
public void drawSeries(Canvas canvas, Paint paint, float[] points, 
        SimpleSeriesRenderer seriesRenderer, float yAxisValue, int seriesIndex, int startIndex) {
    int length = points.length;
    XYSeriesRenderer renderer = (XYSeriesRenderer) seriesRenderer;
    float lineWidth = paint.getStrokeWidth();
    paint.setStrokeWidth(renderer.getLineWidth());
    if (renderer.isFillBelowLine() || renderer.isGradientEnabled()) {
        int pLength = points.length;
        float[] fillPoints = new float[pLength + 4];
        System.arraycopy(points, 0, fillPoints, 0, length);
        fillPoints[0] = points[0] + 1;
        fillPoints[length] = fillPoints[length - 2];
        fillPoints[length + 1] = yAxisValue;
        fillPoints[length + 2] = fillPoints[0];
        fillPoints[length + 3] = fillPoints[length + 1];
        float max = 0;
        for (int i = 0; i < length + 4; i += 2) {
            if (fillPoints[i + 1] < 0) {
                fillPoints[i + 1] = 0;
            }
            else if (fillPoints[i + 1] > max) {
                max = fillPoints[i + 1];
            }
        }
        paint.setStyle(Style.FILL);
        if (renderer.isGradientEnabled()) {
            Shader s = paint.getShader();
            // Create gradient from values set in renderer
            paint.setShader(new LinearGradient(0, (float)(renderer.getGradientStartValue() * max), 0, (float)(renderer.getGradientStopValue() * max), 
                    renderer.getGradientStartColor(), renderer.getGradientStopColor(), Shader.TileMode.MIRROR));
            drawPath(canvas, fillPoints, paint, true);
            // Restore paint's shader to previous value (null)
            paint.setShader(s);
        }
        else {
            paint.setColor(renderer.getFillBelowLineColor());
            drawPath(canvas, fillPoints, paint, true);
        }
    }
    paint.setColor(seriesRenderer.getColor());
    paint.setStyle(Style.STROKE);
    drawPath(canvas, points, paint, false);
    paint.setStrokeWidth(lineWidth);
}

如果它帮助你让我知道。或者你已经解决了,然后让我知道你是怎么做到的。

于 2014-02-04T07:12:44.827 回答
0

使用多个系列,每个系列都有自己的颜色。

于 2013-03-13T15:35:05.360 回答