iPhone:如何更改核心绘图中负值的渐变颜色?

iPhone:如何更改核心绘图中负值的渐变颜色?,iphone,objective-c,cocoa-touch,ios4,core-plot,Iphone,Objective C,Cocoa Touch,Ios4,Core Plot,如何更改iPhone Core Plot中渐变散点图中负值的区域渐变颜色 我想要的渐变色如下: 正值为绿色时 将负值设置为红色 我该怎么做呢?嗯,我不知道它是否太漂亮,但我想你可以用同一个数据源做两个单独的绘图,比如说正片绘图和负片绘图 CPScatterPlot *positivePlot = [[[CPScatterPlot alloc] init] autorelease]; positivePlot.identifier = @"PositivePlot"; positivePlot

如何更改iPhone Core Plot中渐变散点图中负值的区域渐变颜色

我想要的渐变色如下:

  • 正值为绿色时

  • 将负值设置为红色


我该怎么做呢?

嗯,我不知道它是否太漂亮,但我想你可以用同一个数据源做两个单独的绘图,比如说正片绘图和负片绘图

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];
现在,如果正确配置绘图空间,则可以分离正值和负值,并正确配置每个绘图,包括面积渐变:

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;
注意:这是我脑子里想不出来的,因为我这里没有核心绘图文档,也没有测试它的工作配置,所以语法或其他东西可能不正确,但我认为一般的概念应该可以工作


干杯

只需实现以下CPBarPlotDataSource方法即可:

- (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;
}
-(CPFill*)条fillforbarplot:(CPBarPlot*)条图记录索引:(NSUInteger)索引
{
if(barPlot.identifier=@“利润”){
id项=[self.probacts objectAtIndex:index];
双重利润=[[item objectForKey:@“利润”]双重价值];
如果(利润<0.0){
返回[CPFill fillWithGradient:[CPGradient GradientWithBeginingColor:[CPColor redColor]endingColor:[CPColor blackColor]];
}
}
返回零;
}

希望有所帮助:)

您可能需要规范化您的值。据我所见,CPGradient只接受
CGFloat 0-1
。因此,如果值的范围为-1024到+1024,则需要添加一个偏移量并除以总范围。这会给你一个0-1之间的数字。我不能肯定地回答,b/c我从来没有和CorePlot一起工作过。@Stephen Furlani:谢谢你的意见。我不清楚你说了什么。请您提前或查看您的数据集,详细解释一下,所有值
n
都是
j≤ N≤ k
。然后,您可以将梯度值设置为
(n-j)/(k-j)
,其范围为
[0-1]
。这在
j
k
之间线性插值。我不确定为什么这个答案被标记为已接受,因为问题是关于散点图的。当我使用散点图实现
-barFillForBarPlot:recordIndex:
时,永远不会调用该方法。这是如何解决问题的?