Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

android - Bar chart using achartengine

I want to draw a bar chart which contains five individual bars - I have used Achartengine. I am able to display all five bars in the same color but I want to differentiate one bar with a different color, but I cant display more than one color. Please show me how to display different colors. My code...

         values.add(new double[] {21,56,33,10,20});         
        int[] colors = new int[] { Color.rgb(227, 121, 15) };
        XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
        setChartSettings(renderer, "", "", "", 0,5, 0,100, Color.WHITE, Color.WHITE);
        renderer.setXLabels(8);
        renderer.setYLabels(10);
        renderer.setDisplayChartValues(true);
       mChartView= ChartFactory.getBarChartView(context, buildBarDataset(titles, values), renderer,
            Type.DEFAULT);
        layout.addView(mChartView, 350, 500);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Can be achieved by extending the SimpleSeriesRenderer and BarChart classes. Here is my solution for RangeBarChart (all thanks to gilenodm, wish I had some reputation to upvote your answer):

import org.achartengine.renderer.SimpleSeriesRenderer;
public class AdvancedSeriesRenderer extends SimpleSeriesRenderer
{
    private int []  colors;

    public void AdvancedSeriesRenderer ()
    {
    }

    public int getColor ( int position )
    {
        return colors[position];
    }
}

import org.achartengine.chart.RangeBarChart;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;

public class AdvancedRangeBarChart extends RangeBarChart
{
    private int []  barChartColors;

    public AdvancedRangeBarChart ( XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, Type type )
    {
        super ( dataset, renderer, type );
    }

    public void setColors ( int [] colorsIn )
    {
        barChartColors = colorsIn;
    }

    @Override
    public void drawSeries ( Canvas canvas, Paint paint, float [] points, SimpleSeriesRenderer seriesRenderer, float yAxisValue, int seriesIndex, int startIndex )
    {
        int seriesNr = mDataset.getSeriesCount ();
        int length = points.length;
        paint.setStyle ( Style.FILL );
        float halfDiffX = getHalfDiffX ( points, length, seriesNr );
        int start = 0;
        if ( startIndex > 0 )
        {
            start = 2;
        }
        for ( int i = start; i < length; i += 4 )
        {
            int colorIndex = (int) ( i / 4 ) % barChartColors.length;
            paint.setColor ( barChartColors[colorIndex] );
            if ( points.length > i + 3 )
            {
                float xMin = points[i];
                float yMin = points[i + 1];
                // xMin = xMax
                float xMax = points[i + 2];
                float yMax = points[i + 3];
                drawBar ( canvas, xMin, yMin, xMax, yMax, halfDiffX, seriesNr, seriesIndex, paint );
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...