简体   繁体   中英

ios simple bar graph with animation

I need to show 2 bar graphs in my app, I dont want to use core-plot as is overkill to show just 2 bars with variant height, [UIimageView with box.png]

i have done some tests just using UIView animations, wich seem to be enough to make the bar grow or shrink, [the problem is that the height is dependant on the starting point, so my bar doesnt have a point 0 for Y, wich changes with the height for the bar]

so how could i set a point 0 for my Y axis?, so I just have to think of heigh, and not the starting point as well?

or shall i go with some bar generator framework that is not so over kill as core-plot ,,

what is the simplest, lightest weight?

power-plot

ecgraph , Other?

PS. the other point is that i need to have a custom background for the chart, and custom bars thats why im using box.png for the bars]

the code...

viewDidLoad { ***
CGRect chartbackGroundImageRect = CGRectMake(150, 20, 632, 653);
UIImageView *chartbackGroundImage = [[UIImageView alloc] initWithFrame:chartbackGroundImageRect];
[chartbackGroundImage setImage:[UIImage imageNamed:@"chartArtBgnd.png"]];
chartbackGroundImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:chartbackGroundImage];
[chartbackGroundImage release];
CGRect leftBarImageRect = CGRectMake(550, 550, 81, 40);
leftBarImage = [[UIImageView alloc] initWithFrame:leftBarImageRect];
[leftBarImage setImage:[UIImage imageNamed:@"leftBar.png"]];
leftBarImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:leftBarImage];
[leftBarImage release];
***}

testBarAnimation {*** 
 // Setup the animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2]; //animation for arrow
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationBeginsFromCurrentState:YES];

CGRect newFrame = CGRectMake(550, 300, 81, 290);

leftBarImage.frame = newFrame;


[UIView commitAnimations];
***}

thanks a lot!

I dont know, it's applicable to your project. But I used different approach, as iOS doesn't have good libraries for Chart.

I used UIWebview with HTML5 /CSS based free chart engines available on Web. For me its working fine, Bit slow than native charts would be but enough for my project

Define a utility function:

CGRect modifyRectByHeight(CGRect originalRect, CGFloat newHeight)
{
    CGRect result = originalRect;

    result.origin.y += height;
    result.size.height += height;

    return result;
}

Then use as follows:

leftBarImage.frame = modifyRectByHeight( leftBarImage.frame, 150);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM