Mpandroidchart: LineChart: how to change the circle color according to the yVals??

Created on 9 Oct 2014  路  14Comments  路  Source: PhilJay/MPAndroidChart

Hello Phil,
I have to display a line chart, the following is my app design diagram:
1

Some features are required in my app:

  1. I have to change the circle color according to yVals, for example, if the yVal is between 0 and 50, then the circle has to be green.
  2. I have to change the background color according to the yVal value range, i.e. 0-50 is green, 50-100 is yellow...etc.

I've cloned your repo. and included it to my app project, I would like to modify the source code of this project directly to achieve my requirement.
Could you help me out?? Thank a lot.

Regrads,
Engine Bai.

Most helpful comment

Hi Phil, I've tried the following code in my project to achieve the first feature, but it does not work.

LineData lineData = new LineData( new ArrayList<String>( dataMap.keySet() ) );
ArrayList<Entry> yVals = new ArrayList<Entry>();
int xIndex = 0;
ArrayList<Integer> colors = new ArrayList<Integer>();
for ( String time : dataMap.keySet() )
{
    float value = dataMap.get( time );
    Entry entry = new Entry( value, xIndex++ );
    yVals.add( entry );

    if ( value <= 50 )
        colors.add( this.getResources().getColor( R.color.infoview_air_good ) );
    else if ( value >= 51 && value <= 100 )
        colors.add( this.getResources().getColor( R.color.infoview_air_normal ) );
    else if ( value >= 101 && value <= 199 )
        colors.add( this.getResources().getColor( R.color.infoview_air_bad ) );
    else if ( value >= 200 && value <= 299 )
        colors.add( this.getResources().getColor( R.color.infoview_air_verybad ) );
    else
        colors.add( this.getResources().getColor( R.color.infoview_air_hazardous ) );
}
LineDataSet lineDataSet = new LineDataSet( yVals, this.service );
lineDataSet.setCircleColors( colors );
lineData.addDataSet( lineDataSet );

Thank for your help.

All 14 comments

Hi Phil, I've tried the following code in my project to achieve the first feature, but it does not work.

LineData lineData = new LineData( new ArrayList<String>( dataMap.keySet() ) );
ArrayList<Entry> yVals = new ArrayList<Entry>();
int xIndex = 0;
ArrayList<Integer> colors = new ArrayList<Integer>();
for ( String time : dataMap.keySet() )
{
    float value = dataMap.get( time );
    Entry entry = new Entry( value, xIndex++ );
    yVals.add( entry );

    if ( value <= 50 )
        colors.add( this.getResources().getColor( R.color.infoview_air_good ) );
    else if ( value >= 51 && value <= 100 )
        colors.add( this.getResources().getColor( R.color.infoview_air_normal ) );
    else if ( value >= 101 && value <= 199 )
        colors.add( this.getResources().getColor( R.color.infoview_air_bad ) );
    else if ( value >= 200 && value <= 299 )
        colors.add( this.getResources().getColor( R.color.infoview_air_verybad ) );
    else
        colors.add( this.getResources().getColor( R.color.infoview_air_hazardous ) );
}
LineDataSet lineDataSet = new LineDataSet( yVals, this.service );
lineDataSet.setCircleColors( colors );
lineData.addDataSet( lineDataSet );

Thank for your help.

What exactly do you mean by "it does not work"? What do you see?

Hi, Phil, this is the screenshot when executing the above code.
1

The size of colors and yVals is the same.

Your value is always below 50, that is why all circles are the same color:

if ( value <= 50 )
        colors.add( this.getResources().getColor( R.color.infoview_air_good ) );

Or am I missing something?

Oops! Sorry, it's my mistake, you don't miss something!

And how about the second feature?? Is it possible to change the backgroud color according to yVal value range?? Thank you.

At the moment, that is not supported.

Ok, thank you, Phil.

Hi Phil,

How to set the circle hole colors.

Is it possible to use drawable for circle.

Hey Phil.
Thanks for this library.
Is this feature supported yet?

I have to change the background color according to the yVal value range, i.e. 0-50 is green, 50-100 is yellow...etc.

Hi Phil, how can we do this for realtime data?

Hi Phil, Instead of setCircleColors I am trying to pass setColors() //List of colors but default color is being drawn. I wan red, yellow, green to be drawn based on values. But my graph is like heatmap not a circle. Any help?

Hi Phil, is there a way to display image, in place of circle, that too not in all points, in some points I need to show?

Hey there..
I want to increase size of circle how I'll be able to achieve that.?

set1.setCircleSize(10f); // by using this you can increase the size of circle

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andreyfel picture andreyfel  路  3Comments

AiTheAnswer picture AiTheAnswer  路  3Comments

JungYongWook picture JungYongWook  路  3Comments

SutharRohit picture SutharRohit  路  3Comments

thanhcly920 picture thanhcly920  路  3Comments