Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone : How to change gradient color for negative values in Core Plot?

How to change area gradient color for negative values in Gradient Scatter Plot in Core-Plot in iPhone?

I want gradient colors as below:

  • For positive values to be Green

  • For negative values to be Red.

How should I do that?

like image 295
Parth Bhatt Avatar asked Feb 25 '11 10:02

Parth Bhatt


2 Answers

Just implement the following method of the CPBarPlotDataSource is OK:

- (CPFill *)barFillForBarPlot:(CPBarPlot *)barPlot recordIndex:(NSUInteger)index
{
    if (barPlot.identifier == @"profit") {
        id item = [self.profits objectAtIndex:index];
        double profit = [[item objectForKey:@"profit"] doubleValue];

        if (profit < 0.0) {
            return [CPFill fillWithGradient:[CPGradient gradientWithBeginningColor:[CPColor redColor] endingColor:[CPColor blackColor]]];
        }
    }

    return nil;
}

Hope to help:)

like image 127
Shiny Avatar answered Sep 28 '22 18:09

Shiny


Well, I don't know if it's too pretty, but I suppose you could do two separate plots with the same dataSource, let's say positivePlot and negativePlot.

CPScatterPlot *positivePlot = [[[CPScatterPlot alloc] init] autorelease];
positivePlot.identifier = @"PositivePlot";
positivePlot.dataSource = self;
[graph addPlot:positivePlot];

CPScatterPlot *negativevePlot = [[[CPScatterPlot alloc] init] autorelease];
negativevePlot.identifier = @"NegativePlot";
negativePlot.dataSource = self;
[graph addPlot:negativePlot];

Now, if you configure your plotSpaces properly, you can separate positive and negative values and configure each plot properly, including area gradient:

CPXYPlotSpace *positivePlotSpace = [graph newPlotSpace];
positivePlotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0)
                                             length:CPDecimalFromFloat(100)];
positivePlotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0)
                                             length:CPDecimalFromFloat(100)];

CPXYPlotSpace *negativevePlotSpace = [graph newPlotSpace];
negativePlotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-100)
                                             length:CPDecimalFromFloat(100)];
negativePlotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0)
                                             length:CPDecimalFromFloat(100)];


// Plots configuration


//POSITIVE VALUES
positivePlot.plotSpace = positivePlotSpace;

// Green for positive
CPColor *areaColor = [CPColor colorWithComponentRed:0.0 
                                            green:1.0
                                             blue:0.0
                                            alpha:1.0];
CPGradient *areaGradient = [CPGradient gradientWithBeginningColor:areaColor 
                                                    endingColor:[CPColor clearColor]];
areaGradient.angle = -90.0f;
CPFill *areaGradientFill = [CPFill fillWithGradient:areaGradient];
positivePlot.areaFill = areaGradientFill;


//NEGATIVE VALUES
negativePlot.plotSpace = negativePlotSpace;

// Red for negative
areaColor = [CPColor colorWithComponentRed:1.0 
                                     green:0.0
                                      blue:0.0
                                     alpha:1.0];
areaGradient = [CPGradient gradientWithBeginningColor:areaColor 
                                          endingColor:[CPColor clearColor]];
areaGradient.angle = -90.0f;
areaGradientFill = [CPFill fillWithGradient:areaGradient];
negativePlot.areaFill = areaGradientFill;

NOTE: This is off the top of my head, as I dont have Core-Plot docs here, or a working configuration to test this in, so the syntax or something might be off, but I think the general concept should work.

Cheers

like image 27
jcane86 Avatar answered Sep 28 '22 18:09

jcane86