iPhone:在我的核心绘图上看不到任何标签
我正在构建一个带有核心图的图表,除了我在 x 轴和 y 轴上看不到任何标签之外,它工作得很好。我只能看到主要和次要刻度,但它们旁边没有任何值。一定有什么东西我错过了,但我不知道是什么。另外,是否可以为每个轴添加标签,例如:x(小时),y(值)。
我正在处理的图表是列表视图第一行的一部分。
代码是:
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"GraphCustomViewCell" owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[GraphCustomViewCell class]])
{
cell = (GraphCustomViewCell *)currentObject;
break;
}
}
CGRect frame = cell.contentView.bounds;
CPLayerHostingView *hostingView = [[CPLayerHostingView alloc] initWithFrame: frame];
[cell addSubview:hostingView];
// Create graph
CPTheme *theme = [CPTheme themeNamed:kCPPlainWhiteTheme];
graph = (CPXYGraph *)[theme newGraph];
// Add graph to view
hostingView.hostedLayer = graph;
graph.paddingLeft = 50.0;
graph.paddingTop = 5.0;
graph.paddingRight = 5.0;
graph.paddingBottom = 25.0;
// Adjust graph
float xmax = [[[self arrayFromData] objectForKey:@"data"] count];
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0)
length:CPDecimalFromFloat(xmax)];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0)
length:CPDecimalFromFloat(3000)];
CPTextStyle *blackText = [[CPTextStyle textStyle] color:[CPColor blackColor]];
CPLineStyle *lineStyle = [CPLineStyle lineStyle];
lineStyle.lineColor = [CPColor blackColor];
lineStyle.lineWidth = 1.0f;
axisSet.xAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"5"] decimalValue];
axisSet.xAxis.minorTicksPerInterval = 4;
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.minorTickLineStyle = lineStyle;
axisSet.xAxis.axisLineStyle = lineStyle;
axisSet.xAxis.minorTickLength = 4.0f;
axisSet.xAxis.majorTickLength = 8.0f;
axisSet.xAxis.labelOffset = 3.0f;
axisSet.xAxis.labelTextStyle = blackText;
axisSet.yAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"500"] decimalValue];
axisSet.yAxis.minorTicksPerInterval = 4;
axisSet.yAxis.majorTickLineStyle = lineStyle;
axisSet.yAxis.minorTickLineStyle = lineStyle;
axisSet.yAxis.axisLineStyle = lineStyle;
axisSet.yAxis.minorTickLength = 4.0f;
axisSet.yAxis.majorTickLength = 8.0f;
axisSet.yAxis.labelOffset = 1.0f;
CPScatterPlot *dataSourceLinePlot = [[[CPScatterPlot alloc] init] autorelease];
dataSourceLinePlot.identifier = @"MyGraph";
dataSourceLinePlot.dataLineStyle.lineWidth = 1.f;
dataSourceLinePlot.dataLineStyle.lineColor = [CPColor greenColor];
dataSourceLinePlot.dataSource = self;
[graph addPlot:dataSourceLinePlot];
// Put an area gradient under the plot above
CPColor *areaColor = [CPColor colorWithComponentRed:0.3
green:1.0
blue:0.3
alpha:0.3];
CPGradient *areaGradient = [CPGradient gradientWithBeginningColor:areaColor
endingColor:[CPColor clearColor]];
areaGradient.angle = -90.0f;
CPFill *areaGradientFill = [CPFill fillWithGradient:areaGradient];
dataSourceLinePlot.areaFill = areaGradientFill;
dataSourceLinePlot.areaBaseValue = CPDecimalFromString(@"1.75");
非常感谢, 问候, luc
ps:快速编辑,我已经检查了 graph.name = "my graph",但图表上没有显示任何内容
I'm building a graph with core plot, that works fine except that I do not see any labels on the x and y axis. I can only see major and minor ticks but no value next to them. There must be something I'm missing but I do not know what. Also, is it possible to add a label for each axis, for instance: x (hours), y (value).
The graph I am working on is part of the first row of a listview.
The code is:
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"GraphCustomViewCell" owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[GraphCustomViewCell class]])
{
cell = (GraphCustomViewCell *)currentObject;
break;
}
}
CGRect frame = cell.contentView.bounds;
CPLayerHostingView *hostingView = [[CPLayerHostingView alloc] initWithFrame: frame];
[cell addSubview:hostingView];
// Create graph
CPTheme *theme = [CPTheme themeNamed:kCPPlainWhiteTheme];
graph = (CPXYGraph *)[theme newGraph];
// Add graph to view
hostingView.hostedLayer = graph;
graph.paddingLeft = 50.0;
graph.paddingTop = 5.0;
graph.paddingRight = 5.0;
graph.paddingBottom = 25.0;
// Adjust graph
float xmax = [[[self arrayFromData] objectForKey:@"data"] count];
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0)
length:CPDecimalFromFloat(xmax)];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0)
length:CPDecimalFromFloat(3000)];
CPTextStyle *blackText = [[CPTextStyle textStyle] color:[CPColor blackColor]];
CPLineStyle *lineStyle = [CPLineStyle lineStyle];
lineStyle.lineColor = [CPColor blackColor];
lineStyle.lineWidth = 1.0f;
axisSet.xAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"5"] decimalValue];
axisSet.xAxis.minorTicksPerInterval = 4;
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.minorTickLineStyle = lineStyle;
axisSet.xAxis.axisLineStyle = lineStyle;
axisSet.xAxis.minorTickLength = 4.0f;
axisSet.xAxis.majorTickLength = 8.0f;
axisSet.xAxis.labelOffset = 3.0f;
axisSet.xAxis.labelTextStyle = blackText;
axisSet.yAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"500"] decimalValue];
axisSet.yAxis.minorTicksPerInterval = 4;
axisSet.yAxis.majorTickLineStyle = lineStyle;
axisSet.yAxis.minorTickLineStyle = lineStyle;
axisSet.yAxis.axisLineStyle = lineStyle;
axisSet.yAxis.minorTickLength = 4.0f;
axisSet.yAxis.majorTickLength = 8.0f;
axisSet.yAxis.labelOffset = 1.0f;
CPScatterPlot *dataSourceLinePlot = [[[CPScatterPlot alloc] init] autorelease];
dataSourceLinePlot.identifier = @"MyGraph";
dataSourceLinePlot.dataLineStyle.lineWidth = 1.f;
dataSourceLinePlot.dataLineStyle.lineColor = [CPColor greenColor];
dataSourceLinePlot.dataSource = self;
[graph addPlot:dataSourceLinePlot];
// Put an area gradient under the plot above
CPColor *areaColor = [CPColor colorWithComponentRed:0.3
green:1.0
blue:0.3
alpha:0.3];
CPGradient *areaGradient = [CPGradient gradientWithBeginningColor:areaColor
endingColor:[CPColor clearColor]];
areaGradient.angle = -90.0f;
CPFill *areaGradientFill = [CPFill fillWithGradient:areaGradient];
dataSourceLinePlot.areaFill = areaGradientFill;
dataSourceLinePlot.areaBaseValue = CPDecimalFromString(@"1.75");
Thanks a lot,
Regards,
Luc
ps: quick edit, I have checked with graph.name = "my graph", but nothing is displayed on the graph
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,这是因为要显示的标签太多,而且轴在负区域中走得不够远=>没有地方显示标签。但是,graph.name 不显示图表的名称...
OK, this was due to the fact there were too many labels to display and also because the axis did not went far enough in the negative area => no place to display the labels. But, graph.name does not display a name of the graph...