CorePlot 允许用户拖动旋转饼图

CorePlot allow user drag rotate PieChart

(至于我现在得到解决方案,在底部分享)

事实上,我为此苦苦挣扎了一段时间,我相信我发现的很多讨论都与旧版本的 CorePlot 相关或未得到答复。

首先,我使用的是CorePlot 1.5.1。

我已经能够绘制饼图了,现在我希望用户能够通过在屏幕上拖动来旋转它(直接触摸饼图或主机视图并不重要)。

目前使用这些代表:

@interface MyChartViewController : UIViewController<CPTPieChartDataSource,CPTPlotSpaceDelegate,CPTPieChartDelegate>

得到一个hostView,

@property (strong, nonatomic) IBOutlet CPTGraphHostingView *hostView;

做了一个图表,设置为,self.hostView.hostedGraph = graph

并做了一个PieChart,放入图中,[graph addPlot:self.mainPieChart];

(我给pieChart设置了强属性让我随时参考)

所以,这是我的第一次尝试,事实是,它正在响应,(虽然不是以理想的方式)

CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) self.hostView.hostedGraph.defaultPlotSpace;

[plotSpace setDelegate:self];

(只能通过将plotSpace delegate设置为self来工作,不知道为什么,我猜是为了找到一种方法来接收用户的交互,不管怎样,然后我覆盖了这两个函数)

使用这个值: 静态浮动增量角度;

-(BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceDownEvent:(UIEvent *)event atPoint:(CGPoint)point
{

  float dx = point.x - self.mainPieChart.centerAnchor.x;

  float dy = point.y - self.mainPieChart.centerAnchor.y;

  deltaAngle = atan2(dy,dx);

  return YES;
}

这个,为了挽救第一个接触点

然后感应拖动,利用差值做旋转

(至少我是这么想的)

-(BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceDraggedEvent:(UIEvent *)event atPoint:(CGPoint)point
{

    int x = self.mainPieChart.centerAnchor.x;
    int y = self.mainPieChart.centerAnchor.y;

    float dx = point.x - x;
    float dy = point.y - y;


    double a = atan2(dx,dy);

    float angleDifference = deltaAngle - a;

    self.mainPieChart.startAngle = -angleDifference;

    return YES;

}

这是一张关于它的图片,不过我想我已经涵盖了大部分细节。

http://postimg.org/image/bey0fosqj/

不过是横屏模式。


事实是我认为这将是最适合调用的函数,但不知何故我无法调用它(很确定我已经将 self.mainPieChart 委托/数据源设置为 self)

-(BOOL)pointingDeviceDraggedEvent:(id)event atPoint:(CGPoint)interactionPoint{

(进一步测试后)

有趣的是,在尝试通过 shouldHandlePointingDevice 函数(只需单击)打印出不同的值之后,我想我现在有了一些想法。

self.mainPieChart.centerAnchor.x / y 值总是 return 0.5(两者)

但是,x 点和 y 点 returning 值在 1-500+ 之间变化,

从不同的角度来看,我似乎更像是在比较两件事,尽管它们彼此重叠。

可能是 PlotSpace 设置委托部分搞砸了。

============================================= ===============

所以,至于现在我仍然不知道如何调用 -(BOOL)pointingDeviceDraggedEvent:(id)event atPoint:(CGPoint)interactionPoint{,我试着把它放到一个 if 循环中,比如

if([self.mainPieChart pointingDeviceDownEvent:event atPoint:self.mainPieChart.centerAnchor] == YES)

在我的touched函数下没有任何反应,没关系。

回到重点,我当前的解决方案现在运行良好,即使在应用填充之后也是如此。

float x = (self.hostView.bounds.size.width + self.hostView.hostedGraph.paddingLeft)*self.mainPieChart.centerAnchor.x;

float y = self.hostView.bounds.size.height * self.mainPieChart.centerAnchor.y;

float dx = point.x - x;
float dy = point.y - y;

double a = atan2(dx,dy);

这些行对于按下/拖动功能都是相同的,至于拖动功能,

float angleDifference = deltaAngle - a;

self.mainPieChart.startAngle = angleDifference;

在结束前添加

但是,当饼图不在中间时,或者换句话说,填充饼图的图形时,情况会略有不同。 (为了方便起见,我的示例在某种程度上居中)

你只需要修改上面的 x y 浮点值,这比我预期的要容易。

例如,如果我有, graph.paddingLeft = -300.0f;

两个press/drag中float x的值都会变成

float x = (self.hostView.bounds.size.width + self.hostView.hostedGraph.paddingLeft)*self.mainPieChart.centerAnchor.x;

饼图 centerAnchor 以宽度和高度的分数形式给出。在计算 dxdy.

之前,请务必将锚点值乘以图形的相应维度