CPTPlotDataSource プロトコル対応

ScatterPlot 用に CPTScatterPlotField が定義されているので、それに対応する必要がある。

typedef enum _CPTScatterPlotField {
    CPTScatterPlotFieldX,                               
    CPTScatterPlotFieldY                                
} CPTScatterPlotField;

で、実際のコードはこんな感じ。

-(NSNumber *)numberForPlot:(CPTPlot *)plot 
			field:(NSUInteger)fieldEnum 
		recordIndex:(NSUInteger)index 
{
	switch (fieldEnum) {
		case CPTBarPlotFieldBarLocation:
			return [NSNumber numberWithDouble:index];
		case CPTScatterPlotFieldX:
			return [NSNumber numberWithDouble:index+0.5f];
		case CPTScatterPlotFieldY:
			return [NSNumber numberWithInt:(20-index)];
		default:
		{
			if(plot.identifier == @"Bar Plot 1")
				return [NSNumber numberWithDouble:(index+1.0f)*5.0f];
			else
				return 0;
		}
	}
}

CPTScatterPlotFieldX の所で 0.5f を加えているのは、線グラフのポッチを描く部分を1目盛りの半分右にずらしているだけ。棒グラフもずらしてあるのでそれに合わせてる。(棒グラフは BarPlot の方でずらしてる。)


というわけで、Y軸が2つあるグラフも無事描けました!
とりあえずサンプルはココに置いておきます。

たぶん本当は X軸用のプロットスペースは必要なくて、xAxisは BarPlotSpace か ScatterPlotSpace にくっ付ければ良いんだと思うけど、今回は全部をバラバラに独立させてみた。
相変わらず配色が徹底的にダメだけど、気にしない〜。