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);

  • Android ScrollView doesn't scroll when the keyboard is up
  • Textview on(above) Button, doesn't work in Android 5(API 21)
  • Android Support Repo 46.0.0 with Android Studio 2.3
  • java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference
  • What is the equivalent of ActionBar in earlier sdk versions?
  • Re-using Android Custom enum xml attributes
  • Related posts:

    Change Image of ImageView programmaticallyā€ˇ Android
    How can I find the maximum texture size for different phones?
    Is there any other payment module other than Paypal?
    Butterknife View injection
    The roadmap to an Android development expert
    Native Android WebRTC application development
  • Is it possible to use WebSockets or similar with a native Android application?
  • Android, AsyncTask, check status?
  • How to change proxy settings in Android (especially in Chrome)
  • “Conversion to Dalvik format failed with error 1” — on export only
  • Android AsyncTask for Long Running Operations
  • cmd : react-native run-android on every file change
  • 4 Solutions collect form web for “Bar chart using achartengine”

    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 );
                }
            }
        }
    }
    

    I made a hack to achieve this effect.
    Org.achartengine.SimpleSeriesRenderer changed the class, I added an int[] colors and a boolean multipleColorsEnabled with its getters and setters.
    So, I changed, org.achartengine.BarChart in class, the method drawSeries, where is set the color of each bar in a loop, as follows:

    int j = startIndex;
    for (int i = 0; i < length; i += 2) {
        if (seriesRenderer.isMultipleColorsEnabled()) {
            paint.setColor(seriesRenderer.getColors()[j++]);
        } else {
            paint.setColor(seriesRenderer.getColor());
        }
        float x = points[i];
        float y = points[i + 1];
        drawBar(canvas, x, yAxisValue, x, y, halfDiffX, seriesNr,
            seriesIndex, paint);
    }
    

    In the class that loads the data used:

    seriesRenderer.setMultipleColorsEnabled(true);
    seriesRenderer.setColors(myColors);
    

    It’s been a while since I use achartengine, but I think each series has to have its own colour. As a workaround, you could make the bar that you want to differentiate a member of its own series, and set a different colour for that series. Perhaps someone else has a better way though.

    You have to use different SimpleSeriesRenderer inside buildBarRenderer() call, and define as many serie as the desired number of colors, this way (replacing your first two lines of code):

    values.add(new double[] {21});  
    values.add(new double[] {56});  
    values.add(new double[] {33});  
    //ETC.
    int[] colors = new int[] { Color.rgb(227, 121, 15),
                               Color.rgb(227, 121, 227), 
                               Color.rgb(127, 121, 127) };
    

    The rest of code should be same as yours, but I haven’t tested it. AFAIK you need different series, because every serie can only have one color.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.