Coreplot - selecting area below line - ios

I am using coreplot to draw a scatter plot like that:
I use 4 different plots (as coloured) and create a "bar like" feeling by adding 0-value points at the beginning/end of each "bar".
I want to determine which bar was selected by the user and then change alphas of other plots.
I've tried using
-(BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceDownEvent:(id)event atPoint:(CGPoint)point {
//here I translate the selected point to Data points coordinates
//and check which of the plot sources has value >0 (that means bar is visible)
//and is closest to the selected point
}
This method works, but when I want to scroll the data, the method above is called as well.
There must be some easier solution to do that. Thanks.

Return YES from your delegate method to inform the plot space that you've taken action on the event and it does not need to start the scroll. You may need to handle the other events, too, so your app can differentiate between a tap and a drag.

Related

iOS Core Plot Get visible labels X axis

I am using Core Plot and I am really still new to it. I have searched but could not find the answer. I have allowsUserInteraction enabled. I have implemented zoom in/out with that. I want to be able to get first and last visible label on X axis every time when user has zoomed in/out. I know that there is willChangePlotRangeTo I have called there expandRangeByFactor method on my CPTMutablePlotRange. I want to be able to get every time text from first and last visible label on X axis. Is there any delegate method that I have missed?
Whenever you need to find out what the current axis labels are, call -layoutIfNeeded on the axis to make sure the labels are up-to-date and get the set of labels from the axisLabels property. Since it's a set, the collection is unordered. You'll have to search the whole set to find the first and last ones. Each label has a tickLocation that is the location along the axis and a contentLayer that is the label displayed at the location. The automatic axis labels are always CPTTextLayer objects, so you can extract the text property from there.

CPTXYScatterPlot - Can I set the Z order of plot symbols?

Using Core Plot's CPTScatterPlot, I've made a bubble chart that looks like the above.
I've implemented plotSymbolWasSelectedAtRecordIndex in my delegate so I can detect when the user touches one of the bubbles. Using this screenshot as an example, I want to be able to highlight the yellow bubble and bring it in front of the red one when the user touches it. In graphical terms, I want to set the Z order of the selected CPTPlotSymbol to be higher than the others. How can I accomplish that? I'm able to highlight the selected bubble by drawing it in a different color in my symbolForScatterPlot method, but I can't seem to find a way to make it draw in front of the overlapping symbol.
Core Plot draws the data points in the order they appear in the datasource. You'll need to reorder the data so the top-most bubble is at the last index and call -reloadData on the plot to tell it to refresh its data. If you have the data points in an array, sort it by the desired z-index (back to front) and use the sorted array to feed the datasource.

How to detect the coordinates of a custom UIView on CPTPlotspace?

In my App, I am using Coreplot to draw a CPTScatterPlot. Now i want to do a scanner on the chart and this animation of UIView (a line) should be like a Line aligned with Y-axis and travels through x=0 to whatever the last value of x is.
I want to perform an action based on the y value while this custom UIView (scanner) travels.
Any help would be appreciated.
Thanks,
i have something similar in an app of mine, the way i went about it was to add a bar graph on top of the scatter plot, i used that bar graph to represent the line that tracks the user's touch.
then i have a UIView that is overlaid on the plot and tracks the scatter plot touches and displays details of the point being touched.
if you just want to display a line, adding a bar graph is probably the easiest, then you don't have to worry about plot space coordinates vs superview coordinates.
the bar graph data source is only returning a value for the location the user is touching, with a height to match the size of the scatter plot.
the quick version is you can use handlePlotTouchEventInSpace:atPoint:isNewEvent to track the user's touches on the graph.
from there you can use the plotPoint:forPlotAreaViewPoint method on the plot space to get the coordinates of the touch.
something like this is a good starting point. it's a bit of a journey to get it all working, but its not too bad once you get the puzzle started.
-(void)handlePlotTouchEventInSpace:(CPTPlotSpace *)space atPoint:(CGPoint)point isNewEvent:(BOOL)newEvent {
NSDecimal plotPoint[2];
CPTXYPlotSpace *xySpace = (CPTXYPlotSpace *)space;
[xySpace plotPoint:plotPoint forPlotAreaViewPoint:point];
after that plotPoint will contain the x and y location of the touch inside the plot space.

Core Plot: Pan and zoom two plot spaces along x-axis and auto-scaling both y-axes

I have a Core Plot chart with two line plots (plot 1 uses the y-axis on the LHS, plot 2 uses the y2-axis on the RHS) and two plot spaces (lhsPlotSpace and rhsPlotSpace).
For the initial plot set-up, I use scaleToFitPlotsto autoscale the two plots, which works as expected:
[lhsPlotSpace scaleToFitPlots:lhsPlots];
[rhsPlotSpace scaleToFitPlots:rhsPlots];
The plot then looks similar to the screen shot below.
In order to show more details, I would like to allow the user to pan horizontally and to zoom horizontally as long there is more data to be shown on the left and right (panning and zooming should be user driven only along the x-axis).
The y-axis and y2-axis should be scaled automatically depending on the range visible after panning and zooming.
Could you please help me a bit with this problem? I tried to use the delegate
Based on the code example I have found in the Core Plot forums and on StackOverFlow I tried to do this using the delegate method plotSpace:willChangePlotRangeTo:forCoordinate
- (CPTPlotRange *)plotSpace:(CPTPlotSpace *)space
willChangePlotRangeTo:(CPTPlotRange *)newRange
forCoordinate:(CPTCoordinate)coordinate
{
if (coordinate == CPTCoordinateY) {
newRange = ((CPTXYPlotSpace*)space).yRange;
return newRange
}
but besides getting very basic zooming, I did not found a solution which is even close to my problem.
Could you please point me to which delegates methods to use, how to restrict scrolling and how and where to set the new y-scale factor after panning and zooming?
Thank you very much!
Set the globalYRange of each plot space to the corresponding yRange to prevent scaling and scrolling in the y-direction.
-scaleToFitPlots: uses all plot data for the given plots when computing the new ranges. If you want to scale the plot space yRange based on the visible data, you'll have to do that yourself in the plot space delegate. The -plotSpace:willChangePlotRangeTo:forCoordinate: delegate method is a good choice for this.

How do i add label to the pie chart slice when a particular slice is touched using coreplot?

I had drawn a pie chart using core-plot. I need to display the item name when a particular slice is touched. can anyone suggest me a solution to achieve this?
You can handle individual slice events by implementing CPPieChartDelegate methods. Declare CPPieChartDelegate protocol and implement the method (void)pieChart:sliceWasSelectedAtRecordIndex: . Set the delegate of piechart to self(controller). This method gives you which slice(index) is selected. And now for drawing the label, you have to re-draw the pie chart, specifying which slice to be labeled. You can set empty string to labels for other slices.
The iPhone version of CPTTestApp (in the Core Plot examples folder) shows how to display the selected slice index. It displays the index in the graph title, but you just as easily display it in a Core Plot annotation or in a separate UI control.

Resources