Ale*_*exR 2 objective-c uiview core-plot uipopovercontroller ios
我正在使用Core Plot(1.1)来绘制一个条形图,我想在条形图下方提供一个popover,其中有更多细节由用户选择(点击).
我的代码看起来像这样:
- (void)barPlot:(CPTBarPlot *)plot barWasSelectedAtRecordIndex:(NSUInteger)idx {
NSNumber *yValue = self.values[idx];
NSLog(@"barWasSelectedAtRecordIndex x: %i, y: %@",idx,yValue);
NSDecimal plotPoint[2];
NSNumber *plotXvalue = [self numberForPlot:plot
field:CPTScatterPlotFieldX
recordIndex:idx];
plotPoint[CPTCoordinateX] =
CPTDecimalFromFloat(plotXvalue.floatValue);
NSNumber *plotYvalue = [self numberForPlot:plot
field:CPTScatterPlotFieldY
recordIndex:idx];
plotPoint[CPTCoordinateY] =
CPTDecimalFromFloat(plotYvalue.floatValue);
CPTXYPlotSpace *plotSpace =
(CPTXYPlotSpace *)graph.defaultPlotSpace;
CGPoint dataPoint =
[plotSpace plotAreaViewPointForPlotPoint:plotPoint];
NSLog(@"datapoint (CGPoint) coordinates tapped: %@",
NSStringFromCGPoint(dataPoint));
GrowthChartInfoTableViewController *infoViewController =
[self.storyboard instantiateViewControllerWithIdentifier:
@"GrowthChartInfo"];
self.popover = [[UIPopoverController alloc]
initWithContentViewController:infoViewController];
self.popover.popoverContentSize = CGSizeMake(320, 300);
self.popover.delegate = self;
CGRect popoverAnchor =
CGRectMake(dataPoint.x + graph.paddingLeft,
dataPoint.y - graph.paddingTop + graph.paddingBottom,
(CGFloat)1.0f, (CGFloat)1.0f);
[self.popover presentPopoverFromRect:popoverAnchor
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
弹出视图控制器的y位置不正确,对于每个条形图都是不同的,对于具有负y值的第一个条形图,它显示在条形图的上限而不是下限并隐藏条形图.x位似乎是正确的.
关于为什么我的代码不能按预期工作的任何想法?
谢谢!
根据Eric的回答,我更新了我的代码如下:
- (void)barPlot:(CPTBarPlot *)plot barWasSelectedAtRecordIndex:(NSUInteger)idx {
NSNumber *plotXvalue = [self numberForPlot:plot
field:CPTScatterPlotFieldX
recordIndex:idx];
NSNumber *plotYvalue = [self numberForPlot:plot
field:CPTScatterPlotFieldY
recordIndex:idx];
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
CGPoint cgPlotPoint =
CGPointMake(plotXvalue.floatValue,
plotYvalue.floatValue);
CGPoint cgPlotAreaPoint =
[graph convertPoint:cgPlotPoint toLayer:graph.plotAreaFrame.plotArea];
NSDecimal plotAreaPoint[2];
plotAreaPoint[CPTCoordinateX] =
CPTDecimalFromFloat(cgPlotAreaPoint.x);
plotAreaPoint[CPTCoordinateY] =
CPTDecimalFromFloat(cgPlotAreaPoint.y);
CGPoint dataPoint = [plotSpace
plotAreaViewPointForPlotPoint:plotAreaPoint];
NSLog(@"datapoint (CGPoint) coordinates tapped: %@",NSStringFromCGPoint(dataPoint));
GrowthChartInfoTableViewController *infoViewController = [self.storyboard
instantiateViewControllerWithIdentifier:@"GrowthChartInfo"];
self.popover = nil;
self.popover = [[UIPopoverController alloc]
initWithContentViewController:infoViewController];
self.popover.popoverContentSize = CGSizeMake(250, 200);
self.popover.delegate = self;
CGRect popoverAnchor =
CGRectMake(dataPoint.x + graph.paddingLeft,
dataPoint.y - graph.paddingTop + graph.paddingBottom,
(CGFloat)1.0f, (CGFloat)1.0f);
[self.popover presentPopoverFromRect:popoverAnchor
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
但是,我得到的结果比以前更加错误.我相信我一定会误解一些事情.以下是NSLog命令的一些结果:
barWasSelectedAtRecordIndex x: 0, y: -0.03920931548773198848
datapoint (CGPoint) coordinates tapped: {-6388.88, -67024.8}
barWasSelectedAtRecordIndex x: 2, y: 0.174494288286651392
datapoint (CGPoint) coordinates tapped: {-6073.38, -66790}
barWasSelectedAtRecordIndex x: 2, y: 0.174494288286651392
datapoint (CGPoint) coordinates tapped: {-6073.38, -66790}
barWasSelectedAtRecordIndex x: 0, y: -0.03920931548773198848
datapoint (CGPoint) coordinates tapped: {-6388.88, -67024.8}
Run Code Online (Sandbox Code Playgroud)
在我的代码中,从条形坐标到视图坐标的转换似乎完全错误.
再次感谢您的代码示例,Eric!
我已经更新了我的代码,并使用以下选项对其进行了测试(y-value = 0 : plotPoint[CPTCoordinateY] = CPTDecimalFromFloat(0.0f)
,这导致弹出窗口的锚点,其y值似乎不正确(请参阅下面的屏幕截图).锚点的y -value应该等于0
y轴上的位置.
然后我尝试将锚点的y值设置为数据的当前y值.(plotPoint[CPTCoordinateY] = plotYvalue.decimalValue
).这导致锚点接近y轴的0值而不是靠近柱的顶部.但是,对于每个公司(x值),y值会稍微变化,表明y值的偏移可能存在问题:
我还尝试了Eric建议的额外转换dataPoint = [self.view convertPoint:dataPoint fromView:graph.hostingView]
,但这并没有改变结果.另外,我试图添加- graph.paddingTop + graph.paddingBottom
到最终的y值,但没有成功.
我会很感激我的错误可能是什么.
谢谢!
该-plotAreaViewPointForPlotPoint:
方法返回绘图区域坐标系中的一个点.您需要将其转换为图形的坐标系:
[graph convertPoint:dataPoint fromLayer:graph.plotAreaFrame.plotArea];
Run Code Online (Sandbox Code Playgroud)
图形及其托管视图共享一个坐标系.如果self.view
不是托管视图,则需要从托管视图将坐标转换为其坐标系.
更完整的代码示例:
NSDecimal plotPoint[2];
NSNumber *plotXvalue = [self numberForPlot:plot
field:CPTScatterPlotFieldX
recordIndex:idx];
plotPoint[CPTCoordinateX] = plotXvalue.decimalValue;
NSNumber *plotYvalue = [self numberForPlot:plot
field:CPTScatterPlotFieldY
recordIndex:idx];
plotPoint[CPTCoordinateY] = plotYvalue.decimalValue;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
// convert from data coordinates to plot area coordinates
CGPoint dataPoint = [plotSpace plotAreaViewPointForPlotPoint:plotPoint];
// convert from plot area coordinates to graph (and hosting view) coordinates
dataPoint = [graph convertPoint:dataPoint fromLayer:graph.plotAreaFrame.plotArea];
// convert from hosting view coordinates to self.view coordinates (if needed)
dataPoint = [self.view convertPoint:dataPoint fromView:hostingView];
NSLog(@"barWasSelectedAtRecordIndex x: %@, y: %@", plotXvalue, plotYvalue);
NSLog(@"datapoint coordinates tapped: %@", NSStringFromCGPoint(dataPoint));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2761 次 |
最近记录: |