条形图上的自定义标签(CorePlot 框架)
我第一次使用 CorePlot,现在想要自定义条形图。你在这张照片中看到它。
我现在想在栏顶部的每个栏上添加数字
希望您知道我的意思。
我怎样才能意识到它?
这是我的代码
-(void) generateDataSamples
{
int rawSamples [] = {1,3,6,2,1,1,3,15,2,1};
int numSamples = sizeof(rawSamples) / sizeof(int);
samples = [[NSMutableArray alloc] initWithCapacity:numSamples];
for (int i = 0; i < numSamples; i++){
double x = i;
double y = rawSamples[i];
NSDictionary *sample = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithDouble:x],X_VAL,
[NSNumber numberWithDouble:y],Y_VAL,
nil];
[samples addObject:sample];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
[self generateDataSamples];
double xAxisStart = 0;
double xAxisLength = [samples count];
double yAxisStart = 0;
double yAxisLength = [[samples valueForKeyPath:@"@max.Y_VAL"] doubleValue];
CPGraphHostingView *hostingView = [[CPGraphHostingView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:hostingView];
CPXYGraph *graph = [[CPXYGraph alloc] initWithFrame:self.view.bounds];
hostingView.hostedGraph = graph;
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromDouble(0)
length:CPDecimalFromDouble(xAxisLength +1)];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromDouble(0)
length:CPDecimalFromDouble(yAxisLength + 0.5)];
CPBarPlot *plot = [[CPBarPlot alloc] initWithFrame:CGRectZero];
plot.plotRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromDouble(0.0)
length:CPDecimalFromDouble(xAxisLength)];
CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;
CPLineStyle *lineStyle = [CPLineStyle lineStyle];
lineStyle.lineColor = [CPColor whiteColor];
lineStyle.lineWidth = 1.0f;
CPTextStyle *cyanStyle = [CPTextStyle textStyle];
cyanStyle.color = [CPColor cyanColor];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:0];
plot.barOffset = .5;
plot.dataSource = self;
[graph addPlot:plot];
[plot release];
[graph release];
[hostingView release];
}
i have used CorePlot for the first time and want now customize the Bar Graph. You see it in this picture.
I want now add the Number over every Bar on top of the Bar
hope you know what i mean.
How can i realize it?
This is my Code
-(void) generateDataSamples
{
int rawSamples [] = {1,3,6,2,1,1,3,15,2,1};
int numSamples = sizeof(rawSamples) / sizeof(int);
samples = [[NSMutableArray alloc] initWithCapacity:numSamples];
for (int i = 0; i < numSamples; i++){
double x = i;
double y = rawSamples[i];
NSDictionary *sample = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithDouble:x],X_VAL,
[NSNumber numberWithDouble:y],Y_VAL,
nil];
[samples addObject:sample];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
[self generateDataSamples];
double xAxisStart = 0;
double xAxisLength = [samples count];
double yAxisStart = 0;
double yAxisLength = [[samples valueForKeyPath:@"@max.Y_VAL"] doubleValue];
CPGraphHostingView *hostingView = [[CPGraphHostingView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:hostingView];
CPXYGraph *graph = [[CPXYGraph alloc] initWithFrame:self.view.bounds];
hostingView.hostedGraph = graph;
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromDouble(0)
length:CPDecimalFromDouble(xAxisLength +1)];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromDouble(0)
length:CPDecimalFromDouble(yAxisLength + 0.5)];
CPBarPlot *plot = [[CPBarPlot alloc] initWithFrame:CGRectZero];
plot.plotRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromDouble(0.0)
length:CPDecimalFromDouble(xAxisLength)];
CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;
CPLineStyle *lineStyle = [CPLineStyle lineStyle];
lineStyle.lineColor = [CPColor whiteColor];
lineStyle.lineWidth = 1.0f;
CPTextStyle *cyanStyle = [CPTextStyle textStyle];
cyanStyle.color = [CPColor cyanColor];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:0];
plot.barOffset = .5;
plot.dataSource = self;
[graph addPlot:plot];
[plot release];
[graph release];
[hostingView release];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只需要简单的数字标签,请在绘图上设置
labelTextStyle
和labelFormatter
属性。如果您需要更复杂的标签,可以将-dataLabelForPlot:recordIndex:
方法添加到数据源并创建您自己的自定义标签。有关示例代码,请参阅 Core Plot 附带的示例应用程序。所有绘图类型的标记机制都是相同的。 Plot Gallery 应用程序中的“垂直条形图”演示了第一种技术。图库中的其他几个图都使用数据源技术。
If you just want simple numeric labels, set the
labelTextStyle
andlabelFormatter
properties on the plot. If you need more complex labels, you can add the-dataLabelForPlot:recordIndex:
method to your datasource and create your own custom labels.See the example apps included with Core Plot for sample code. The labeling mechanism is the same for all plot types. The "Vertical Bar Chart" in the Plot Gallery app demonstrates the first technique. Several of the other plots in the Plot Gallery use the datasource technique.