Using core-plot, how can you convert a touched point to the plot space?

为君一笑 提交于 2019-12-21 05:39:18

问题


I have a very simple graph that I want to enable touch on, I have the first part working:

plotSpace.delegate = self;

and the method:

-(BOOL)plotSpace:(CPPlotSpace *)space
shouldHandlePointingDeviceDownEvent:(id)event atPoint:(CGPoint)point
{
    NSLog(@"touched at: x: %f y: %f", point.x, point.y);
}

How do I convert the point "point" to the plot space of my graph?

I can see the methods, but Im unsure on how to use them, the doco, although large, doesnt really describe them well enough :)

Thanks a lot Mark


回答1:


This is what I'm using:

NSDecimal plotPoint[2]; 
CPXYPlotSpace *xySpace = (CPXYPlotSpace *)space; 
[xySpace plotPoint:plotPoint forPlotAreaViewPoint:point]; 

NSlog(@"plotPoint = %@, %@", 
[[NSDecimalNumber decimalNumberWithDecimal:plotPoint[CPCoordinateX]] intValue],
[[NSDecimalNumber decimalNumberWithDecimal:plotPoint[CPCoordinateY]] doubleValue]);

I'd guess that you could change CPXYPlotSpace to CPPlotSpace and In my plot the plot space is an CPXYPlotSpace so the plotPoint:forPlotAreaViewPoint method accepts an array of 2 NSDecimal values.

Also in my plot the x-values are all integers, and the y-values are all doubles.




回答2:


I have to admit, I forgot about this question, thanks for the reply Steve.

I did get it solved too, here is my implementation (im extending CPXYGraph)

-(BOOL)plotSpace:(CPPlotSpace *)space shouldHandlePointingDeviceDownEvent:(id)event atPoint:(CGPoint)point {

    CPScatterPlot *plot = (CPScatterPlot*)[[self allPlots] objectAtIndex: 0];
    CGPoint pointInPlotArea = [plot convertPoint:point toLayer:plot];

    if ([plot containsPoint:pointInPlotArea]) {
        CPXYPlotSpace *plotSpace = (CPXYPlotSpace*)[self defaultPlotSpace];
        NSDecimal touchDataPoint[2];
        [plotSpace plotPoint:touchDataPoint forPlotAreaViewPoint:pointInPlotArea];
    }
}

touchDataPoint then contains the coords that the user has touched.



来源:https://stackoverflow.com/questions/3270803/using-core-plot-how-can-you-convert-a-touched-point-to-the-plot-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!