2011-01-29 13 views
3

グラデーション/シェーディング効果を持つグラフを生成しようとしています。今私は線グラフを描画し、そのグラフに勾配効果を適用することができます。それはグラフではなく全体の視野に適用されています。今私はenter image description hereのようなイメージを得ています。しかし、私はenter image description hereのようにしたいです。グラフのすぐ下のグラデーション効果が必要です。iPhoneの折れ線グラフの勾配効果

私を助けてください。前もって感謝します。

私が使用しているコードは次のとおりです。

UIGraphicsBeginImageContext(self.graphView.frame.size); 
[graphView.image drawInRect:CGRectMake(0, 0, self.graphView.frame.size.width, self.graphView.frame.size.height)]; 
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
CGContextSetLineJoin(UIGraphicsGetCurrentContext(), kCGLineJoinRound); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0); 
CGContextSetRGBFillColor(UIGraphicsGetCurrentContext(), 225, 48, 48, 1.0); 
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 225, 225, 0.0, 1.0); 
CGContextBeginPath(UIGraphicsGetCurrentContext()); 

float xCordinate, yCordinate; 

for (int i = 0; i < [graphValues count]; i++) { 
    int val = [[graphValues objectAtIndex: i] intValue]/5; 
    float diff = [[graphValues objectAtIndex: i] floatValue]/5 - val; 
    yCordinate = val * 120 + 120 * diff; 
    xCordinate = graphWidth * i/[graphValues count] + 60; 
    if (i == 0) 
     CGContextMoveToPoint(UIGraphicsGetCurrentContext(), xCordinate, graphHeight + 60 - yCordinate); 
    else 
     CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), xCordinate, graphHeight + 60 - yCordinate); 
} 
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), xCordinate, graphHeight + 60); 
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 60, graphHeight + 60); 
CGContextClosePath(UIGraphicsGetCurrentContext()); 
CGContextSaveGState(UIGraphicsGetCurrentContext()); 
CGContextDrawPath(UIGraphicsGetCurrentContext(), kCGPathStroke); 

// CGContextFillPath(UIGraphicsGetCurrentContext());

CGContextClip(UIGraphicsGetCurrentContext()); 


//Draw Gradient 
UIColor *topColor = [UIColor colorWithRed: 1.0 green:1.0 blue:1.0 alpha:1.0]; 
UIColor *bottomColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.0]; 
CGColorRef colorRef[] = { [topColor CGColor], [bottomColor CGColor] }; 
CFArrayRef colors = CFArrayCreate(NULL, (const void**)colorRef, sizeof(colorRef)/sizeof(CGColorRef), &kCFTypeArrayCallBacks); 

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, colors, NULL); 
CFRelease(colorSpace); 
CFRelease(colors); 

// Draw a linear gradient from top to bottom 
CGPoint gradStartPoint = CGPointMake(50.0, graphView.bounds.size.height); 
CGPoint gradEndPoint = CGPointMake(50.0, 0.0); 
CGContextDrawLinearGradient(UIGraphicsGetCurrentContext(), gradient, gradStartPoint, gradEndPoint, 0); 

CFRelease(gradient); 

// Cleanup 
CGColorSpaceRelease(colorSpace); 

CGContextRestoreGState(UIGraphicsGetCurrentContext()); 

graphView.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
+0

完全な赤色(または白色)の色を、グラフの一番上の点ではなく、ビューの上端にする場合は、グラフの最も高い点のy座標を上に指定する必要があります勾配の –

答えて

4

トリックは、あなたがグラデーションを描く前に、クリッピングパスを設定することです。詳細については、CGContextRefドキュメントを参照してください。

+0

ありがとうございますが、私はクリッピングパスを使用しています。 CGContextClip(UIGraphicsGetCurrentContext());しかし、現在のコンテキストは、バックイメージview.UIGraphicsBeginImageContext(self.graphView.frame.size)で定義されています。 \t [graphView.image drawInRect:CGRectMake(0、0、self.graphView.frame.size.width、self.graphView.frame.size.height)]; – MohanVydehi

+1

@MohanVydehi:イメージを描いたものとは別の場所でクリッピングパスを設定していると言っていますか?もしそうなら、それが理由です。それ以外の場合は、クリッピングと描画を行っているビューのどのようなメソッドの完全なコードを含めるように質問を編集すると、事柄が大幅に明確になります。 –

+0

ありがとうございます。私はその問題を理解した。私は自分のコードを添付して、いくつかの変更を加え、アプリケーションを終了するのを助けてください。 – MohanVydehi

関連する問題