Description: chart and animation parameters are determined.
#import "Charting/Chart.h"
#import "Charting/ColumnSeries.h"
#import "Charting/AreaSeries.h"
#import "DataSource.h"
#import "Charting/BarSeries.h"
#import "Charting/PieSeries.h"
#import "Charting/LineSeries.h"
#import "Charting/LinePoint.h"
#import "Charting/ChartTouchesDelegate.h"
#import "Charting/ChartLabel.h"
#import "Charting/BubbleSeries.h"
#import "ChartTypes.h"
#import "Charting/ChartAnimationScale.h"
#import "Charting/ChartAnimationFade.h"
#import "Charting/ChartAnimationMove.h"
#import "Charting/ChartAnimationRotate.h"
#import "Charting/ChartAnimationPieMove.h"
#import "Charting/ChartAnimationDelegate.h"
@interface ViewController : UIViewController<ChartAnimationDelegate>{
Chart *chart;
float max;
float min;
NSMutableDictionary *m_numberFormat;
int axisType;
BOOL hideAxis;
int chartType;
NSArray *m_colors;
DataSource *datasource;
}
- (ChartLabel *)createDefaultLabel;
- (void) setChartSettings;
- (void) drawAllSeries;
- (NSString *) animationClassName:(id) sender;
- (void) rotate;
- (void) move;
- (void) scale;
- (void) fade;
- (void) movePie;
@end
#import "ViewController.h"
#define ColorWithRGBA(_r, _g, _b, _a) \
[UIColor colorWithRed:(_r) / 255.0f green:(_g) / 255.0f blue:(_b) / 255.0f alpha:(_a) / 255.0f]
@implementation ViewController
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
m_numberFormat = [[NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], @"asInSource",
[NSNumber numberWithInt:2], @"decNumbers",
[NSNumber numberWithBool:YES], @"separator",
@"", @"prefix",
@"", @"suffix",
[NSNumber numberWithBool:NO], @"negativeRed",
nil] retain];
m_colors = [[NSArray arrayWithObjects:
ColorWithRGBA(19, 88, 138, 255 ),
ColorWithRGBA(109, 167, 192, 255 ),
ColorWithRGBA(41, 171, 226, 255 ),
ColorWithRGBA(0, 169, 157, 255 ),
ColorWithRGBA(103, 124, 139, 255 ),
ColorWithRGBA(221, 241, 250, 255 ),
ColorWithRGBA(169, 221, 243, 255 ),
ColorWithRGBA(102, 203, 196, 255 ),
ColorWithRGBA(208, 222, 232, 255 ),
ColorWithRGBA(190, 160, 186, 255 ),
nil] retain];
// Create a chart and set size
chart = [[Chart alloc] initWithName:@"Chart"];
chart.frame=CGRectMake(0, 0, 600, 550);
chart.backgroundColor = [UIColor whiteColor];
chart.delegate = self;
chart.plotArea.zoomEnabled = NO;
// Determine title parameters
chart.caption.text = @"Chart";
chart.caption.textColor = [UIColor blackColor];
chart.caption.maxTextLength = 100;
chart.caption.blockAlignment = AlignmentCenter;
SolidColorBrush *br = [SolidColorBrush new];
br.color = [UIColor lightGrayColor];
// Determine legend parameters
chart.legend.background = [br autorelease];
chart.legend.borderRadius = 2;
chart.legend.borderThickness = 1;
chart.legend.font = [UIFont fontWithName:@"Arial" size:12];
chart.legend.legendOrientation = LegendOrientationBottom;
chart.legend.textColor = [UIColor blackColor];
chart.legend.maxEntriesCount = 5;
// Create a data source
datasource = [[DataSource alloc] init];
[self setChartSettings];
[self drawAllSeries];
// Set link to data source
[chart setDataSource :datasource];
// Determine parameters of animation buttons
UIToolbar *bar = [[UIToolbar new] autorelease];
bar.frame = CGRectMake(0, 0, 600, 40);
[bar setBarStyle:UIBarStyleDefault];
UIBarButtonItem *rotate = [[[UIBarButtonItem alloc] initWithTitle:@"rotate" style:UIBarButtonItemStyleDone target:self action:@selector(rotate)] autorelease];
UIBarButtonItem *movePie = [[[UIBarButtonItem alloc] initWithTitle:@"MovePie" style:UIBarButtonItemStyleDone target:self action:@selector(movePie)] autorelease];
UIBarButtonItem *move = [[[UIBarButtonItem alloc] initWithTitle:@"move" style:UIBarButtonItemStyleDone target:self action:@selector(move)] autorelease];
UIBarButtonItem *fade = [[[UIBarButtonItem alloc] initWithTitle:@"fade" style:UIBarButtonItemStyleDone target:self action:@selector(fade)] autorelease];
UIBarButtonItem *scale = [[[UIBarButtonItem alloc] initWithTitle:@"scale" style:UIBarButtonItemStyleDone target:self action:@selector(scale)] autorelease];
UIBarButtonItem *item = [[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
UIBarButtonItem *spacer = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
[bar setItems:[NSArray arrayWithObjects:rotate,movePie,move,fade,scale,spacer,item,nil]];
chart.frame = CGRectMake(0, 40, chart.frame.size.width, chart.frame.size.height - 40);
// Set maximum and minimum Y axis values for data source
[datasource prepare];
[chart forceDataUpdate]; // Refresh data. Animation disabled
chart.animationsStartingEnabled = YES; //Enable animation
// Execute custom example
[self executeExample];
// Display chart
[self.view addSubview:bar];
[self.view addSubview:chart];
}
// Function for example execution
-(void) executeExample{
};
- (void)drawAllSeries
{
[chart deleteAllSeries];
int seriesCount = datasource.countSeries;
for (int i = 0; i < seriesCount; ++i)
{
PieSeries *ser = [[PieSeries new] autorelease];
SolidColorBrush *brush = [[SolidColorBrush new] autorelease];
brush.color =[m_colors objectAtIndex:i % [m_colors count]];
[ser setBackground:brush];
ser.dataIndex = i;
ser.holeRadius = 0.5;
ser.defaultLabel = [self createDefaultLabel];
[chart addSeries:ser];
}
[chart forceDataUpdate];
((ValueAxis *)[chart.axes objectForKey:[NSNumber numberWithInt:AxisLineX]]).valueAxisType = ValueAxisAbsolute;
((ValueAxis *)[chart.axes objectForKey:[NSNumber numberWithInt:AxisLineY]]).valueAxisType = ValueAxisAbsolute;
((ValueAxis *)[chart.axes objectForKey:[NSNumber numberWithInt:AxisLineX]]).visible = YES;
((ValueAxis *)[chart.axes objectForKey:[NSNumber numberWithInt:AxisLineY]]).visible = YES;
}
- (void )viewDidUnload
{
[super viewDidUnload];
}
- (BOOL )shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void )chartTouchesTapInView:(UIView *)v withPoint:(CGPoint)point
{
UIView *sender = [v hitTest:point withEvent:nil];
if ([sender isKindOfClass:[ChartPoint class]])
{
ChartPoint *pt = (ChartPoint *)sender;
if (pt.chartLabel)
{
pt.chartLabelVisible = !pt.chartLabelVisible;
if (pt.chartLabelVisible)
[pt moveAboveAllByZOrder];
else
[pt restoreZOrder];
}
[chart setNeedsLayout];
}
}
- (void)setChartSettings
{
((ValueAxis *)[chart.axes objectForKey:[NSNumber numberWithInt:AxisLineX]]).axisThickness = 1; // X axis thickness
AxisTicks *t = [[AxisTicks alloc] initWithColor:[UIColor blackColor] size:3 thickness:3];
((ValueAxis *)[chart.axes objectForKey:[NSNumber numberWithInt:AxisLineX]]).majorTicks = t; // Major X axis tick marks
((ValueAxis *)[chart.axes objectForKey:[NSNumber numberWithInt:AxisLineX]]).minorTicks.thickness = 0; // Thickness of minor Y axis tick marks
// Y axis parameters
ValueAxis *axisY = ((ValueAxis *)[chart.axes objectForKey:[NSNumber numberWithInt:AxisLineY]]);
axisY.axisThickness = 1; // Axis thickness
// Determine axis major tick mark parameters
AxisTicks *Ticks = [AxisTicks new];
Ticks.type = AxisTickOuter;
Ticks.visible = YES;
Ticks.color = [UIColor blueColor];
Ticks.thickness = 1;
Ticks.size = 3;
axisY.majorTicks = Ticks;
// Determine axis minor tick marks parameters
AxisTicks *MinTick = [AxisTicks axisTicksWithColor:[UIColor brownColor] size:2 thickness:1];
axisY.minorTicks=MinTick;
// Determine axis grid and font parameters
axisY.showGrid = YES; // Display grid
axisY.gridThickness = 1; // Grid thickness
axisY.gridColor = ColorWithRGBA(200, 200, 200, 255);
axisY.font = [UIFont fontWithName:@"Arial" size:14];
axisY.textColor = [UIColor blackColor];
axisY.axisLineVisible = YES;
axisY.color = [UIColor blueColor];
// Determine timeline parameters
TimeAxis *timeAxis = ((TimeAxis *)[chart.axes objectForKey:[NSNumber numberWithInt:AxisTypeTimeline]]);
timeAxis.visible = NO;
Thickness th = {10,0,0,20}; // Margins
chart.legend.margin = th; // Legend margins
th.bottom = 10;
th.left = th.right = th.top = 0;
chart.chartArea.padding = th; // Chart area margins
chart.plotArea.selectionEnabled = NO;
}
- (ChartLabel *)createDefaultLabel
{
ChartLabel *result = [[[ChartLabel alloc] initWithFrame:CGRectZero] autorelease]; // Create a label
SolidColorBrush *br = [[SolidColorBrush new] autorelease];
br.color = [UIColor whiteColor];
result.background = br; // Set color
result.borderColor = [UIColor blackColor]; // Border color
result.borderThickness = 1;
result.borderRadius = 10;
result.font = [UIFont fontWithName:@"Arial" size:18]; // Font
result.textAlignment = (Alignment)UITextAlignmentCenter;
result.textColor = [UIColor blackColor];
result.mask = [NSString stringWithFormat:@"%@#FVAL%@",
(NSString *)[m_numberFormat objectForKey:@"prefix"],
(NSString *)[m_numberFormat objectForKey:@"suffix"]];
result.countOfDecimalNumbers = [((NSNumber *)[m_numberFormat objectForKey:@"decNumbers"]) intValue];
result.displayNegativesInRed = [(NSNumber *)[m_numberFormat objectForKey:@"negativeRed"] boolValue];
result.hasHorizontalNoteLine = result.hasVerticalNoteLine = result.hasFootNoteLine = NO;
result.minWidth = 0;
result.hasFootNoteLine = YES;
result.hasHorizontalNoteLine = YES;
result.hasVerticalNoteLine = YES;
result.noteLineColor = [UIColor redColor];
result.noteLineThickness = 2.0;
result.visible = YES;
return result;
}
- (void) rotate
{
NSArray *series = [chart seriesList]; // Get all series
ChartAnimationRotate *rotate = [[ChartAnimationRotate alloc] initWithAnimationName:@"rotate"]; // Create rotatation animation with the name "rotate"
rotate.angle = -6.28; //Rotation angle in radians (if angle < 0, rotation is clockwise, otherwise rotations is anticlockwise)
rotate.startDelay = 0; // Delay time in seconds
rotate.duration = 3; // Duration in seconds
rotate.delegate = self;
// Apply animation to all series
for (ChartSeries *ser in series) {
[ser addAnimation:rotate];
}
// Play animation
[chart forceUpdateAnimations];
}
- (void)movePie
{
NSArray *series = [chart seriesList]; // Get all series
PieSeries *ser = [series objectAtIndex:1];
ChartAnimationPieMove *move = [[ChartAnimationPieMove alloc] initWithAnimationName:@"movePie"];// Pie chart sector move animation
move.delegate = self;
if(ser.cutoutLength != 0)
{
move.shift = 50;
ser.cutoutLength = 0;
}
else
{
move.shift = -50;
ser.cutoutLength = 50;
}
move.startDelay = 0;
move.duration = 2;
[ser addAnimation:move];
[chart forceUpdateAnimations];
}
- (void)move
{
NSArray *series = [chart seriesList]; // Get all series
ChartAnimationMove *move = [[ChartAnimationMove alloc] initWithAnimationName:@"move"]; // MOve animation
move.offset = CGPointMake(100, 100); // Move point
move.startDelay = 0;
move.duration = 3;
move.delegate = self;
for (ChartSeries *ser in series) {
[ser addAnimation:move];
}
[chart forceUpdateAnimations];
}
- (void) fade
{
NSArray *series = [chart seriesList]; // Get all series
ChartAnimationFade *fade = [[ChartAnimationFade alloc] initWithAnimationName:@"fade"]; // Fade animation
fade.alpha = 0.1; // Initial transparency value
fade.startDelay = 0;
fade.duration = 3;
fade.delegate = self;
for (ChartSeries *ser in series) {
[ser addAnimation:fade];
}
[chart forceUpdateAnimations];
}
- (void) scale
{
NSArray *series = [chart seriesList]; // Get all series
ChartAnimationScale *scale = [[ChartAnimationScale alloc] initWithAnimationName:@"scale"]; // Scale animation
scale.scale = 0.1; // Initial scale
scale.startDelay = 0;
scale.duration = 3;
scale.delegate = self;
ChartAnimationScale *scale2 = [[ChartAnimationScale alloc] initWithAnimationName:@"scale 2"];
scale2.scale = 0.1;
scale2.startDelay = 2;
scale2.duration = 3;
scale2.delegate = self;
[[series objectAtIndex:0] addAnimation:scale];
[[series objectAtIndex:2] addAnimation:scale2];
[chart forceUpdateAnimations];
}
// Animation is completed
- (void)animationComplete:(id)sender
{
NSArray *subviews = [self.view subviews]; // Get all displayed elements
UIToolbar *bar = (UIToolbar *)[subviews objectAtIndex:0]; // Get link to button panel
((UIBarButtonItem *)[bar.items lastObject]).title = [NSString stringWithFormat:@"Completed %@",[self animationClassName:sender]]; // Display label
}
- (void)animationStarted:(id)sender
{
NSArray *subviews = [self.view subviews];
UIToolbar *bar = (UIToolbar *)[subviews objectAtIndex:0];
((UIBarButtonItem *)[bar.items lastObject]).title = [NSString stringWithFormat:@"Started %@",[self animationClassName:sender]];
}
// Get animation type based on class name
- (NSString *)animationClassName:(id)sender
{
NSString *str = NSStringFromClass([sender class]); // Get class name as a string
NSRange range = [NSStringFromClass([sender class]) rangeOfString:@"ChartAnimation"]; // Get occurrence of substring to string
int pos = range.length;
NSString *animationName = [str substringFromIndex:pos]; // Get substring starting from pos position
if([animationName isEqual: @"Rotate"])
animationName = @"Rotate";
if([animationName isEqual: @"PieMove"])
animationName = @"PieMove";
if([animationName isEqual: @"Move"])
animationName = @"Move";
if([animationName isEqual: @"Fade"])
animationName = @"Fade";
if([animationName isEqual: @"Scale"])
animationName = @"Scale";
return animationName; // Return animation type name (Rotate, Move and so on)
}
@end
See also: