MPAndroidChart is a way to set different colors for different bars?

I am using MPAndroidChart in my application and I was wondering if there is a way to set a different color for the first column of a dynamically moving histogram, for example: enter image description here

Bars are added dynamically as the entire stack moves to the left as data continues to flow in, is there a way to set the color of the first bar to a different color? thank you in advance.

EDIT and solution: here is my code for adding a new entry to the chart, it happens dynamically every 500 milliseconds or so.

 private void addBarEntry(float value) {


    BarData data = mDownloadChart.getData();

    if(data != null) {

        BarDataSet set = data.getDataSetByIndex(0);
        // set.addEntry(...); // can be called as well

        if (set == null) {
            set = createBarSet();
            data.addDataSet(set);
        }

        // add a new x-value first
        data.addXValue(set.getEntryCount() + "");

        // choose a random dataSet
        //int randomDataSetIndex = (int) (Math.random() * data.getDataSetCount());

        data.addEntry(new BarEntry(value, set.getEntryCount()), 0);

        // let the chart know it data has changed
        mDownloadChart.notifyDataSetChanged();

        SLog.d(TAG, "download value: "+value);

        mDownloadChart.setVisibleXRange(10);
        mDownloadChart.moveViewToX(mDownloadChart.getHighestVisibleXIndex()-5);

        // redraw the chart
        mDownloadChart.invalidate();
    }
}

      

Thanks to @Philipp Jahoda I got it to work, just add this piece of code to your addEntry method:

int[] colors = new int[set.getEntryCount()];
            for (int i = 0; i<colors.length; i++){
                colors[i]=Color.parseColor("your-hex-color-for-all-entries");
            }
            colors[colors.length-1] = Color.parseColor("your-hex-color-for-last-entry");

            set.setColors(colors);

      

+3


source to share


1 answer


Yes, there is in the documentation.

Basically, you can set a separate color for each bar in the chart. This is a little awkward at the moment, because in your case you have to set each color to "red" and the last one to "green".



I am working to improve this.

+6


source







All Articles