2

私はコアプロットにまったく慣れていないので、ビューに凡例を追加できません。私はあらゆる種類のチュートリアルを試し、多くの情報を読みましたが、凡例を表示する方法がわかりません。

私はこれを機能させるためにかなりの時間を費やしてきたので、私は本当にいくつかの助けをいただければ幸いです。

この時点での私の目的は、次のような凡例を表示することであることに注意してください。

私が欲しいもの

これが私が得るものです:

今見ている伝説

表示されるのは、右側の少し太い線だけです。私はそれを動かすことはできますが、伝説を得ることができません。

コード:

// Create pieChart from theme
pieChart = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
[pieChart applyTheme:theme];

//Create host view
self.hostingView = [[CPTGraphHostingView alloc] 
                    initWithFrame:[[UIScreen mainScreen]bounds]];
[self.view addSubview:self.hostingView];

hostingView_.hostedGraph = pieChart;
pieChart.axisSet = nil;
pieChart.plotAreaFrame.borderLineStyle = nil; 

pieChart.paddingLeft   = 10.0;
pieChart.paddingTop    = 120.0;
pieChart.paddingRight  = 10.0;
pieChart.paddingBottom = 80.0;

pieChart.fill = [CPTFill fillWithColor:[CPTColor clearColor]];
pieChart.plotAreaFrame.fill = [CPTFill fillWithColor:[CPTColor clearColor]];

CPTMutableTextStyle *whiteText = [CPTMutableTextStyle textStyle];
whiteText.color = [CPTColor whiteColor];
whiteText.fontSize = 14;
whiteText.fontName = @"Helvetica";

pieChart.titleTextStyle = whiteText;




//==================================================//
// LEGENDS //

// 1 - Get graph instance
CPTGraph *graph = self.hostingView.hostedGraph;
// 2 - Create legend
CPTLegend *theLegend = [CPTLegend legendWithGraph:graph];
// 3 - Configure legen
theLegend.numberOfColumns = 1;
theLegend.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
theLegend.borderLineStyle = [CPTLineStyle lineStyle];
theLegend.cornerRadius = 5.0;
// 4 - Add legend to graph
graph.legend = theLegend;     
graph.legendAnchor = CPTRectAnchorRight;
CGFloat legendPadding = -(self.view.bounds.size.width / 8);
graph.legendDisplacement = CGPointMake(legendPadding, 0.0);




//==================================================//





// Add pie chart
CPTPieChart *piePlot = [[CPTPieChart alloc] init];
piePlot.dataSource      = self;
piePlot.pieRadius       = 70.0; //140.0;  //131.0;
piePlot.identifier      = @"Pie Chart 1";
piePlot.startAngle      = M_PI_4;
piePlot.sliceDirection  = CPTPieDirectionCounterClockwise;   //CPTPieDirectionCounterClockwise;
piePlot.centerAnchor    = CGPointMake(0.5, 0.5); //(0.5, 0.38);
piePlot.borderLineStyle = [CPTLineStyle lineStyle];


piePlot.delegate        = self;
[pieChart addPlot:piePlot];

xx

-(NSString *)legendTitleForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)index {
return [NSString stringWithFormat:@"Pie Slice %lu", (unsigned long)index];
}
4

1 に答える 1

9

凡例を設定する前に、プロットをグラフに追加してください。凡例のメソッドを直接使用することもできますが-addPlot:、グラフにそれを実行させる方が簡単です。

于 2012-08-21T01:29:12.327 に答える