user2701925
user2701925

Reputation:

Making a simple line graph with NO core plot

I need to make a line graph in my application to track data. I looked at core plot, and it seems WAY to complicated. Is there a simpler way to just make a line graph that can move horizontally and it will need to be able to add new segments. And not cause a huge memory overload because a lot will be added frequently, tho I could make it so they delete after a moth or something. So my question is basically : is there a easier method than core plot, and if Someone can lead me in the direction of being able to add more segments with custom data. Thanks in advance.


Edit

I tried this

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    [super drawRect:rect];
    // find min and max values of data
    float max = -HUGE_VALF, min = HUGE_VALF;
    for (int i = 0; i < 0; i ++)
    {
        min = MIN(min, 0);
        max = MAX(max, 10);
    }
    
    // build path
    for (int i = 0; i < 0; i ++)
    {
        // line spacing is the distance you want between line vertices
       float x = i * 1;
        // scale y to view height
       float y = ((1 - min) / (max - min)) * self.bounds.size.height;

        if (i == 0)
        {
            CGContextMoveToPoint(ctx, x, y);
        }
        else
        {
            CGContextAddLineToPoint(ctx, x, y);
        }
    }
    // stroke path (configure color, width, etc before this)
    CGContextStrokePath(nil);
}

@end

Upvotes: 0

Views: 310

Answers (1)

Justin Meiners
Justin Meiners

Reputation: 11113

You can draw it yourself using CoreGraphics.

In the drawRect of a UIView (or custom image context).

{ 
   // find min and max values of data 
   float max = -HUGE_VALF, min = HUGE_VALF;
   for (int i = 0; i < dataCount; i ++)
   {
       min = MIN(min, data[i]); 
       max = MAX(max, data[i]);
   }

   // build path
   for (int i = 0; i < dataCount; i ++)
   {
       // line spacing is the distance you want between line vertices
       float x = i * lineSpacing;
       // scale y to view height 
       float y = ((data[i] - min) / (max - min)) * self.bounds.size.height;

       if (i == 0)
       {
           CGContextMoveToPoint(ctx, x, y);
       }
       else
       {
           CGContextAddLineToPoint(ctx, x, y);
       }
   }
   // stroke path (configure color, width, etc before this)
   CGContextStrokePath(ctx);
}

This is very simple, but hopefully it gets you going in the right direction.

Upvotes: 1

Related Questions