0
votes

(As for I got the solution now, it is being shared at the bottom)

Fact is I have been struggling a while about this and I believe quite a lot of discussions I found are related to older versions of CorePlot or unanswered.

Firstly, I am using CorePlot 1.5.1.

I am able to plot a PieChart already and now I would like the user to be able to rotate it by dragging on the screen ( doesn't really matter touch directly the pieChart or the host View).

Using these delegates at the moment:

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

Got a hostView,

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

Made a graph, set as, self.hostView.hostedGraph = graph

and made a PieChart, put into the graph, [graph addPlot:self.mainPieChart];

(I set the pieChart with a strong property to let me refer it anytime)

So, here is my first attempt, and fact is, it is responding, (though not in a desirable way)

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

[plotSpace setDelegate:self];

(only works by setting plotSpace delegate to self, not sure why, i guess it's about finding a way to receive user's interaction, anyway, then I overwrite these two functions)

Using this value: static float deltaAngle;

-(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;
}

This, in order to save the first touching point

Then sense the dragging do use the difference to make the rotation

( at least I wanted so )

-(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;

}

And here is an image about it, though i think I covered most of the details already.

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

It is in landscape mode though.


Fact is I think this would be the most appropriate function to call, but somehow I cannot call it out (pretty sure I set self.mainPieChart delegate/ datasource to self already)

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

(after further testing)

Interesting, after trying to print out different values, by the shouldHandlePointingDevice function (simply clicking), I think i got some ideas now.

the self.mainPieChart.centerAnchor.x / y values always return 0.5 (both)

However, point x, point y are returning values vary from 1-500+,

it seems more like I am comparing two things, though they are on top of each other, from different perspective.

Likely the PlotSpace set delegate part messed that up.

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

So, as for now I still don't know how to call -(BOOL)pointingDeviceDraggedEvent:(id)event atPoint:(CGPoint)interactionPoint{, I tried to put it into a if loop like

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

under my touched function but nothing happened, never mind.

Back to the point, my current solution works well now, even after applying padding.

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);

these lines are all same for both press / drag functions, as for drag function,

float angleDifference = deltaAngle - a;

self.mainPieChart.startAngle = angleDifference;

are added before the end

However, the case is slightly different when the Pie Chart is not at the middle, or, in other words, the graph holding the Pie Chart is padded. ( my example somehow is mid centre just to make it easy)

you simply have to mortify the x y float value above, it's easier than I expected.

For example if I have, graph.paddingLeft = -300.0f;

the value of float x in both press/drag will become

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

1 Answers

0
votes

The pie chart centerAnchor is given as fractions of the width and height. Be sure to multiply the anchor values by the corresponding dimension of the graph before computing dx and dy.