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?
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:)
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
精彩评论