CorePlotでグラフ作成！plotSymbolをカスタマイズ

#はじめに
CorePlot上で使えるPlotSymbolの機能を紹介します。

##PlotSymbolに用意されているFactoryMethod
CPTPlotSymbol.h

```objc
/// @name Factory Methods
/// @{
+(CPTPlotSymbol *)plotSymbol;
+(CPTPlotSymbol *)crossPlotSymbol;
+(CPTPlotSymbol *)ellipsePlotSymbol;
+(CPTPlotSymbol *)rectanglePlotSymbol;
+(CPTPlotSymbol *)plusPlotSymbol;
+(CPTPlotSymbol *)starPlotSymbol;
+(CPTPlotSymbol *)diamondPlotSymbol;
+(CPTPlotSymbol *)trianglePlotSymbol;
+(CPTPlotSymbol *)pentagonPlotSymbol;
+(CPTPlotSymbol *)hexagonPlotSymbol;
+(CPTPlotSymbol *)dashPlotSymbol;
+(CPTPlotSymbol *)snowPlotSymbol;
+(CPTPlotSymbol *)customPlotSymbolWithPath:(CGPathRef)aPath;
/// @}
```
##使い方
### cross〜snowまで

YFViewController.h

```objc
#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"

@interface YFViewController : UIViewController<CPTPlotDataSource,CPTScatterPlotDelegate>
@property(weak,nonatomic) IBOutlet CPTGraphHostingView * graphView;
@end
```

YFViewController.m

```objc
-(CPTPlotSymbol *)symbolForScatterPlot:(CPTScatterPlot *)plot recordIndex:(NSUInteger)idx
{
    CPTPlotSymbol *symbol ;
    symbol = [CPTPlotSymbol snowPlotSymbol];//この部分を変える
        
    symbol.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
    CPTMutableLineStyle *symbolLineStyle = [CPTMutableLineStyle lineStyle];
    symbolLineStyle.lineColor = [CPTColor redColor];
    symbol.lineStyle = symbolLineStyle;

    return symbol;
}
```
### pathをplotSymbolで使う

YFViewController.h

```objc
#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"

@interface YFViewController : UIViewController<CPTPlotDataSource,CPTScatterPlotDelegate>
@property(weak,nonatomic) IBOutlet CPTGraphHostingView * graphView;
@end
```

YFViewController.m

```objc
-(CPTPlotSymbol *)symbolForScatterPlot:(CPTScatterPlot *)plot recordIndex:(NSUInteger)idx
{
    CPTPlotSymbol *symbol ;
    CGMutablePathRef letters = CGPathCreateMutable();
        
    CTFontRef font = CTFontCreateWithName(CFSTR("Helvetica-Bold"), 50.0f, NULL);
    NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
                               (__bridge id)font, kCTFontAttributeName,
                               nil];
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"a"
                                                                         attributes:attrs];
    CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)attrString);
    CFArrayRef runArray = CTLineGetGlyphRuns(line);
        
    // for each RUN
    for (CFIndex runIndex = 0; runIndex < CFArrayGetCount(runArray); runIndex++)
    {
        // Get FONT for this run
        CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runArray, runIndex);
        CTFontRef runFont = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName);
            
        // for each GLYPH in run
        for (CFIndex runGlyphIndex = 0; runGlyphIndex < CTRunGetGlyphCount(run); runGlyphIndex++)
        {
            // get Glyph & Glyph-data
            CFRange thisGlyphRange = CFRangeMake(runGlyphIndex, 1);
            CGGlyph glyph;
            CGPoint position;
            CTRunGetGlyphs(run, thisGlyphRange, &glyph);
            CTRunGetPositions(run, thisGlyphRange, &position);
                
            // Get PATH of outline
            {
                CGPathRef letter = CTFontCreatePathForGlyph(runFont, glyph, NULL);
                CGAffineTransform t = CGAffineTransformMakeTranslation(position.x, position.y);
                CGPathAddPath(letters, &t, letter);
                CGPathRelease(letter);
            }
        }
    }
    CFRelease(line);
        
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointZero];
    [path appendPath:[UIBezierPath bezierPathWithCGPath:letters]];
     
    symbol = [CPTPlotSymbol customPlotSymbolWithPath:letters];
    symbol.usesEvenOddClipRule = YES;
    CGPathRelease(letters);
    CFRelease(font);
    
    symbol.symbolType = CPTPlotSymbolTypeCustom;
    symbol.size = CGSizeMake(4.0f*idx, 4.0f*idx);
    return symbol;
}
```
![スクリーンショット 2013-12-31 13.13.47.png](https://qiita-image-store.s3.amazonaws.com/0/12362/23c7b13d-83be-6fed-d747-8d6f48fa980d.png)

##参考URL
http://stackoverflow.com/questions/9976454/cgpathref-from-string

#最後に
フォントからPathへの変更は個人的にまだまだ学習が足りず書ききれていませんが
文字以外にピクチャ的なものもあり、使うとぐっと面白くなりそうです