2011-02-25 93 views
7

如何更改iPhone中Core-Plot中漸變散點圖中負值的區域漸變顏色?iPhone:如何更改Core繪圖中負值的漸變顏色?

欲如下梯度的顏色:

  • 對於正值爲綠色

  • 對於負值爲

我應該怎麼做?

+0

你可能需要正常化你的價值觀。從我所看到的,CPGradient只接受'CGFloat 0 - 1'。所以如果你的數值範圍從-1024到+1024,你需要添加一個偏移量併除以整個範圍。這會給你一個0-1的數字。我不能肯定地回答,B/C我從來沒有與CorePlot合作過。 – 2011-02-28 14:41:52

+0

@Stephen Furlani:感謝您的意見。我不清楚你說的。能否請你詳細解釋一下 – 2011-02-28 16:40:06

+0

你知道,無論是提前,還是通過查看你的數據集,所有值'n'都是這樣的:'j≤n≤k'。然後,你可以得到梯度值爲'(n - j)/(k - j)',它將在'[0-1]'範圍內。這在'j'和'k'之間進行線性插值。 – tJener 2011-03-18 04:23:39

回答

3

只需實現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; 
} 

希望幫助:)

+0

我不確定爲什麼這個答案被標記爲接受,因爲問題是關於散點圖。當我使用散點圖實現'-barFillForBarPlot:recordIndex:'時,該方法永遠不會被調用。這是如何解決這個問題的? – simonbs 2013-03-11 16:11:30

2

嗯,我不知道它是否太漂亮了,但我想你可以用相同的數據源做兩個單獨的圖,比方說positivePlot和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]; 

,如果你正確配置plotSpaces,你可以單獨積極和消極的價值觀和正確配置每個橋段,包括區域梯度:

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; 

注:這關的頂部我的頭,因爲我沒有Core-Plot文檔,或者是一個工作配置來測試它,所以語法或某些東西可能會關閉,但我認爲一般概念應該起作用。

乾杯