Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 15, 2013, 07:42 AM   #1
spaniard89
macrumors newbie
 
Join Date: Jan 2013
Generating plots on Iphone

Hi,

I am working on an app, that would that would parse JSON and fetch the values from JSON into an array. Now I would like to plot this array into a plot(scatter).

I have until now, parsed the JSON and have the values in an array, now how can i plot the graph? I have heard a lot about COREPLOT, but it too complicated and I want my graph to be as simple as possible.

I would really appreciate any help or any example project, that would help me start.
spaniard89 is offline   0 Reply With Quote
Old Jan 15, 2013, 08:20 AM   #2
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
I've looked some but haven't found anything I like.

I think I'm just going to roll my own... drawing a simple plot with iOS's drawing commands doesn't seem that difficult.

I'm not quite certain, but I think for you, drawing a bunch of points should be as easy as:

Code:
- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor blueColor] CGColor]));
    CGContextFillEllipseInRect(ctx, CGRectMake(x, y, width, height));
    // repeat FillEllipseInRect as many times as necessary.
}
All that's left at that point is drawing some axes and you're done, right?

Here's the documentation you'll want:

CGContext:
https://developer.apple.com/library/...reference.html

CGGeometry:
https://developer.apple.com/library/...reference.html
__________________
Don't tell me Macs don't last: 2007 iMac, 2007 Mac Mini, 2008 MacBook Air, all Vintage.
(iMac obsoletion: April 28, 2015, MBA: October 14, 2015, Mac Mini: March 9, 2016)

Last edited by ArtOfWarfare; Jan 15, 2013 at 08:27 AM.
ArtOfWarfare is offline   0 Reply With Quote
Old Jan 15, 2013, 09:51 AM   #3
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
You could also consider using a UIWebView and Google Chart Tools.
__________________
dejo is offline   0 Reply With Quote
Old Jan 15, 2013, 12:07 PM   #4
chown33
macrumors 603
 
Join Date: Aug 2009
Core Plot:
http://code.google.com/p/core-plot/


Google search terms ios plotting and you'll find more info, such as articles on using Core Plot and comparisons with other libraries.
chown33 is offline   0 Reply With Quote
Old Jan 15, 2013, 12:25 PM   #5
KoolStar
macrumors Demi-God
 
KoolStar's Avatar
 
Join Date: Oct 2006
Location: Kentucky
Send a message via AIM to KoolStar Send a message via Skype™ to KoolStar
The OP said they wanted something easier than coreplot, even though that would be my recommendation too.
Quote:
Originally Posted by chown33 View Post
Core Plot:
http://code.google.com/p/core-plot/


Google search terms ios plotting and you'll find more info, such as articles on using Core Plot and comparisons with other libraries.
__________________
"Innovation distinguishes between a leader and a follower." : S.J.
KoolStar is offline   0 Reply With Quote
Old Jan 15, 2013, 01:10 PM   #6
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by KoolStar View Post
The OP said they wanted something easier than coreplot, even though that would be my recommendation too.
Sorry, I posted in a pre-caffeinated state. I should have written "Just use Core Plot".

Here's a Ray Wenderlich tutorial that shows a scatter plot:
http://www.raywenderlich.com/13271/h...re-plot-part-2

It's part 2, and part 1 has the fundamentals (and a downloadable ready-to-use project).


Plotting and charting aren't usually written as isolated components, but as part of a multi-component library of various kinds of plots/charts. So I really don't think there's much possibility of finding a simpler component. If there's a simpler plot/chart library, I don't know what it is.

One could write a component from scratch, but that's not exactly a trivial process. And if one already thinks Core Plot is too complex, I don't see how writing it from scratch would be any less so. Perhaps if someone is already skilled at iOS graphics it might be simpler, but that seems unlikely given the question (a skilled poster wouldn't be expected to ask the question at all).


So, short answer: Just use Core Plot, and search for tutorials and examples.
chown33 is offline   0 Reply With Quote
Old Jan 15, 2013, 04:06 PM   #7
larswik
macrumors 65816
 
Join Date: Sep 2006
A couple weeks ago I decided to write my own. It took a couple evenings but it was not all that hard. I found it more difficult to try and download and incorporate other pre built ones. I built the background in Photoshop and added it as a subView to the view first.

You then set up a context like so with the size you want returned.
Code:
UIGraphicsBeginImageContext(CGSizeMake(300, 200));
CGContextRef context = UIGraphicsGetCurrentContext();
Then you set up colors and line widths with these methods

Code:
CGContextSetRGBStrokeColor();
CGContextSetLineWidth();
The main part of creating the points and adding lines to the points is this code which I iterated through using a for loop for each index of my array. The trick here was to first set the first point outside of the for loop, for the starting point. Then basically get the next index value and use that for the AddLineToPoint(). Then shift the MoveToPoint to the point the AddLineToPoint ended so it starts to draw the next line where the last one left ended. The next time thought the loop you get the new numbers from the index and the cycle starts again till the loop is all done.

Code:
CGContextMoveToPoint(); // outside of loop

CGContextAddLineToPoint(); // in loop
CGContextMoveToPoint(); // in loop
Once all of your paths have been set you stoke the path basically with this

Code:
CGContextStrokePath();
Once I created the lines I start with the dots. I changed the color, like I set it above but to green and then used this method in my for loop to draw the dots above.

Code:
CGContextAddArc();
Then when you finish with all of your drawing you add it to a UIImage and then end the image context like so

Code:
    UIImage *imageContext = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
At that point I just added subView over my Photoshop graphic.

I was doing a 30 day history so my Photoshop file had 30 lines spaced by 10 pixels, so every loop I multiplied i x 10 to move the graph horizontally. Vertical was easy to find too. I sorted my array so highest number was index 0 and lowest was last index. Then I subtracted the lowest from the highest number and divided that by my total pixel range which was 200 vertically.

Code:
    float dif = highNum - lowNum;
    float range = 200.0 / dif;
That number, range became my multiplier to find the vertical position with this code. When you get into it and start testing it it became pretty intuitive.

Code:
float vSpace = ((highNumber - currentNumber) * myRange);
So in the end I had 3 sections in my method that drew to the context, the lines, the dots and the text for the numbers. The only thing I would have done differently is build a time traveling machine to take me back 20 years and tell my younger self to take math more seriously since that is what I struggled with the most!
Attached Thumbnails
Click image for larger version

Name:	ploter.jpg
Views:	6
Size:	29.5 KB
ID:	390476  
__________________
I know more than yesterday.
Lars
larswik is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Disable Finder Generating Thumbnails in a Specific Folder energy23 Mac OS X Server, Xserve, and Networking 2 Apr 23, 2014 10:36 AM
trying to draw multiple plots based on core data entity, in the same graph isianto iPhone/iPad Programming 0 Mar 30, 2014 12:05 PM
OSX generating duplicate files Dagless OS X 2 Apr 14, 2013 09:37 AM
iMovie 9.0.8 crashes when generating thumbnails devonxo Mac Applications and Mac App Store 1 Dec 9, 2012 07:21 PM
Air generating too much network traffic? tdomil MacBook Air 11 Aug 21, 2012 03:20 AM

Forum Jump

All times are GMT -5. The time now is 07:42 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC