Suresh Jagnani
Suresh Jagnani

Reputation: 330

Coreplot Library - Extract entire graph image

i am creating image for graph using this code

UIImage *newImage=[graph imageOfLayer]
NSData *newPNG= UIImageJPEGRepresentation(newImage, 1.0); 
NSString *filePath=[NSString stringWithFormat:@"%@/graph.jpg",     [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]];           
if([newPNG writeToFile:filePath atomically:YES])
NSLog(@"Created new file successfully");

But i get only visible area(320*460) in image, How can i get whole graph image with axis. Please provide some code snippet, how can i do it with coreplot.

Thanks In Advance...

Upvotes: 3

Views: 798

Answers (2)

Andrew S
Andrew S

Reputation: 2987

My approach to this problem was to create a method to extract the image.

In that method I momentarily make the hosting view, scroll view, graph bounds, and plot area frame bounds momentarily bigger. I then convert the graph to a an image.

I then remove the hosting view from its container to remove it from screen. I then call the doPlot method to reinitialise the plot and its data via the DoPlot method. My code is below.

There might be a visual jitter whilst this is carried out. However, you could always disguise this by using a alert either to enter an email for export, or just a simple alert saying image exported.

//=============================================================================
/**
 Gets the image of the chart to export via email.
 */
//=============================================================================
-(UIImage *) getImage
{

    //Temprorarilty make plot bigger.
    // CGRect rect =  self.hostingView.bounds;
    CGRect rect =  self.scroller.bounds;
    rect.size.height = rect.size.height -100;

    rect.origin.x = 0;
    rect.size.width = rect.size.width + [fields count] * 100.0;
    [self.hostingView setBounds:rect];
    [scroller setContentSize:  hostingView.frame.size];
    graph.plotAreaFrame.bounds = rect;
    graph.bounds = rect;


    UIImage * image =[graph imageOfLayer];//get image of plot.


    //Redraw the plot back at its normal size;
    [self.hostingView removeFromSuperview];
    self.hostingView = nil;
    [self doPlot];

     return image;
}//============================================================================

Upvotes: 0

Eric Skroch
Eric Skroch

Reputation: 27381

Make a new graph the size of the desired output image. It doesn't have to be added to a hosting view—that's only needed for displaying it on screen.

CPTXYGraph *graph = [(CPTXYGraph *)[CPTXYGraph alloc] initWithFrame:desiredFrame];
// set up the graph as usual
UIImage *newImage=[graph imageOfLayer];
// process output image

Upvotes: 4

Related Questions