PlotSpaceとXYAxisとPlot

英語の読解力の無さとグラフの知識の無さで、なかなか理解できなかったのだけど、なんとなくこんなことだろうな〜と。

  • グラフにはいくつもプロットスペース、軸、プロットを乗せられる。
  • プロットスペースは XY軸のスケールを決めてる。
  • 軸(XYAxis)は軸を描く時の書き方を知っていて、関係できるのは1つのプロットスペースだけ。
  • 逆に1つのプロットスペースに軸はたくさん乗せることができる。
  • プロットは棒とか線とかプロットの仕方を知っていて、関係できるのは1つのプロットスペースだけ。
  • 逆にプロットスペースにプロットはたくさん乗せることができる。

で、今回は棒グラフ用のY軸(用のプロットスペース)、線グラフ用のY軸(用のプロットスペース)、どちらでも使う(というかどちらにも関係ない)X軸(用のプロットスペース)を作ってやってみた。


PlotSpace

	// Plot Spaces
	CPTXYPlotSpace *barPlotSpace = [[[CPTXYPlotSpace alloc] init] autorelease];
	barPlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(0) length:CPTDecimalFromUnsignedInteger(20)];
	barPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0) length:CPTDecimalFromInteger(100)];

	CPTXYPlotSpace *scatterPlotSpace = [[[CPTXYPlotSpace alloc] init] autorelease];
	scatterPlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(0) length:CPTDecimalFromUnsignedInteger(20)];
	scatterPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0) length:CPTDecimalFromInteger(20)];

	CPTXYPlotSpace *xPlotSpace = [[[CPTXYPlotSpace alloc] init] autorelease];
	xPlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(0) length:CPTDecimalFromInteger(20)];
	xPlotSpace.yRange = barPlotSpace.yRange;

	[graph removePlotSpace:graph.defaultPlotSpace];
	[graph addPlotSpace:barPlotSpace];
	[graph addPlotSpace:xPlotSpace];
	[graph addPlotSpace:scatterPlotSpace];

barPlotSpace が棒グラフ用。scatterPlotSpaceが線グラフ用。xPlotSpaceが X軸用。


それぞれのプロットスペースに X軸、Y軸の範囲(スケール)を設定するんだけど、棒グラフも線グラフも X軸も、X軸の範囲は同じ。これは棒グラフも線グラフも同じ X軸の範囲(スケール)だから。


で、棒グラフは Y軸が 0-100 まで、線グラフは Y軸が 0-20 までで範囲(スケール)が違ってる。


X軸用スペースの Y軸はとりあえず、棒グラフと同じ範囲(スケール)にしてある。


最後にグラフからデフォルトのプロットスペースを削除して、今作ったプロットスペースを順番に加えてる。


Axes

	// Axes
	// Bar Graph Axis
	CPTXYAxis *barAxis = [[[CPTXYAxis alloc] init] autorelease];
	barAxis.plotSpace = barPlotSpace;
	barAxis.orthogonalCoordinateDecimal = CPTDecimalFromUnsignedInteger(0);
	barAxis.majorIntervalLength = CPTDecimalFromUnsignedInteger(20);
	barAxis.minorTicksPerInterval = 9;
	barAxis.tickDirection = CPTSignNone;
	barAxis.axisLineStyle = axisLineStyle;
	barAxis.majorTickLength = majorTickLength;
	barAxis.majorTickLineStyle = majorTickLineStyle;
	barAxis.minorTickLength = minorTickLength;
	barAxis.minorTickLineStyle = minorTickLineStyle;
	barAxis.title = @"Left Y-Axis title";
	barAxis.titleTextStyle = axisTitleTextStyle;
	barAxis.titleOffset = titleOffset;
	barAxis.coordinate = CPTCoordinateY;

	// xAxis
	CPTXYAxis *xAxis = [[[CPTXYAxis alloc] init] autorelease];
	xAxis.plotSpace = xPlotSpace;
	xAxis.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
	xAxis.orthogonalCoordinateDecimal = CPTDecimalFromUnsignedInteger(0);
	xAxis.minorTicksPerInterval = 4;
	xAxis.tickDirection = CPTSignNone;
	xAxis.axisLineStyle = axisLineStyle;
	xAxis.majorTickLength = majorTickLength;
	xAxis.majorTickLineStyle = majorTickLineStyle;
	xAxis.minorTickLength = minorTickLength;
	xAxis.minorTickLineStyle = minorTickLineStyle;
	xAxis.title = @"X-Axis title";
	xAxis.titleTextStyle = axisTitleTextStyle;
	xAxis.titleOffset = titleOffset;

	// Scatter Graph Axis
	CPTXYAxis *scatterAxis = [[[CPTXYAxis alloc] init] autorelease];
	scatterAxis.plotSpace = scatterPlotSpace;
	scatterAxis.orthogonalCoordinateDecimal = CPTDecimalFromUnsignedInteger(20);
	scatterAxis.majorIntervalLength = CPTDecimalFromUnsignedInteger(5);
	scatterAxis.minorTicksPerInterval = 4;
	scatterAxis.tickDirection = CPTSignNone;
	scatterAxis.axisLineStyle = axisLineStyle;
	scatterAxis.majorTickLength = majorTickLength;
	scatterAxis.majorTickLineStyle = majorTickLineStyle;
	scatterAxis.minorTickLength = minorTickLength;
	scatterAxis.minorTickLineStyle = minorTickLineStyle;
	scatterAxis.title = @"Right Y-Axis title";
	scatterAxis.titleTextStyle = axisTitleTextStyle;
	scatterAxis.titleOffset = -50.0f;
	scatterAxis.coordinate = CPTCoordinateY;
	scatterAxis.labelOffset = -35.0f;
	scatterAxis.labelAlignment = CPTAlignmentLeft;

	graph.axisSet.axes = [NSArray arrayWithObjects:barAxis, xAxis, scatterAxis, nil];

だらだらっと長いけど、それぞれの軸の見え方(描き方)の設定。


まず、それぞれの軸はどのプロットスペースに関係する(属する)かを設定してる。


次に orthogonalCoordinateDecimal を設定してそれぞれの Y軸は X軸と、X軸は Y軸とどこで交わるかを設定してる。この場合、棒グラフの Y軸は X軸の 0の場所と交わる。線グラフの Y軸は X軸の 20の場所と交わる。X軸は Y軸の 0の場所と交わる。
これで棒グラフの Y軸はグラフの左側、線グラフの Y軸はグラフの右側に描かれる事になる。


で、軸のタイトルだけど、右側に描いた Y軸のタイトルの位置決めが微妙。もっと軸の右に描く、左に描くみたいな設定がありそうなのだけど見つけられなかった。


あ、あと、X軸のラベルは labelingPolicy = CPTAxisLabelingPolicyAutomatic;として、自動で Core-Plot に描いてもらっている。ラベルが重なったりせずに良い感じに調節してくれている。


最後に作った軸を NSArray に入れてグラフの axisSet にセットして軸は終了。


Plot

	// Bar plot
	CPTBarPlot *barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor redColor] horizontalBars:NO];
	barPlot.baseValue = CPTDecimalFromString(@"0");
	barPlot.dataSource = self;
	barPlot.identifier = @"Bar Plot 1";
	barPlot.barOffset = CPTDecimalFromFloat(0.5f);
	[graph addPlot:barPlot toPlotSpace:barPlotSpace];


	// Scatter plot
	CPTScatterPlot *scatterPlot = [[[CPTScatterPlot alloc] init] autorelease];
	scatterPlot.identifier = @"Scatter Plot 1";
	CPTMutableLineStyle *scatterLineStyle = [CPTMutableLineStyle lineStyle];
	scatterLineStyle.lineWidth = 3.0f;
	scatterLineStyle.lineColor = [CPTColor greenColor];
	scatterPlot.dataLineStyle = scatterLineStyle;
	CPTPlotSymbol *plotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
	plotSymbol.fill = [CPTFill fillWithColor:[CPTColor blackColor]];
	plotSymbol.size = CGSizeMake(5.0, 5.0);
	scatterPlot.plotSymbol = plotSymbol;
	scatterPlot.dataSource = self;
	[graph addPlot:scatterPlot toPlotSpace:scatterPlotSpace];

ここは特に新しいことはなし。
ScatterPlotの設定がちょっと面倒なことくらいかな。


あ、それぞれのプロットは対応するプロットスペースに加えている。X軸のプロットスペースにはプロットが無いけど、X軸を描くためだけにあるプロットスペースだから問題ない。