Description: creating of a line chart that uses the specified data source.
#import <Charting/ChartTouchesDelegate.h> #import <Charting/Chart.h> #import <Charting/LineSeries.h> @interface ViewController : UIViewController<ChartTouchesDelegate> { Chart *chart; float max; float min; LineSeries *currSeries; NSMutableDictionary *m_numberFormat; } - (void) executeExample; - (ChartLabel *)createDefaultLabel; @end
#import "ViewController.h" #import "DataSource.h" #import "TimeIndexDelegate.h" #import "LevelLineHolderDelegate.h" @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Determine link to selected series currSeries = nil; m_numberFormat = [[NSMutableDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:NO], @"asInSource", [NSNumber numberWithInt:2], @"decNumbers", [NSNumber numberWithBool:YES], @"separator", @"", @"prefix", @"", @"prefix", [NSNumber numberWithBool:NO], @"negativeRed", nil] retain]; // Create a chart and determine its size chart = [[Chart alloc] initWithName:@"chart"]; chart.frame = CGRectMake(0, 0, 600, 550); chart.backgroundColor = [UIColor whiteColor]; ValueAxis *ax = [chart.axes objectForKey:[NSNumber numberWithInt:AxisLineX]]; ax.axisLabelsVertical = NO; // Create a data source DataSource *datasource = [[DataSource alloc] init]; // Create line chart series // Create the first series LineSeries *series = [LineSeries new]; series.dataIndex = 0; // Series index, numbering starts from zero series.defaultLabel = [self createDefaultLabel]; [series setColor:[UIColor blueColor]]; // Series line color series.isLegendVisible = YES; // Display in legend [series setShadowColor:[UIColor blueColor]]; // Shadow color series.shadowOpacity = 0.2;// Shadow transparency series.shadowRadius = 0.01;// Shadow radius series.showShadow = YES; // Display shadow // Create the second series LineSeries *series1 = [LineSeries new]; series1.dataIndex = 1; series1.defaultLabel = [self createDefaultLabel]; [series1 setColor:[UIColor redColor]]; series1.isLegendVisible = YES; [series1 setShadowColor:[UIColor redColor]]; series1.shadowOpacity = 0.2; series1.shadowRadius = 0.01; series1.showShadow = YES; // Create the third series LineSeries *series2 = [LineSeries new]; series2.dataIndex = 2; series2.defaultLabel = [self createDefaultLabel]; [series2 setColor:[UIColor greenColor]]; series2.isLegendVisible = YES; [series2 setShadowColor:[UIColor greenColor]]; series2.shadowOpacity = 0.2; series2.shadowRadius = 0.01; series2.showShadow = YES; // Create the fourth series LineSeries *series3 = [LineSeries new]; series3.dataIndex = 3; series3.defaultLabel = [self createDefaultLabel]; [series3 setColor:[UIColor darkGrayColor]]; series3.isLegendVisible = YES; [series3 setShadowColor:[UIColor darkGrayColor]]; series3.shadowOpacity = 0.2; series3.shadowRadius = 0.01; series3.showShadow = YES; // Set data source [chart setDataSource:datasource]; [chart setDelegate:self]; // Get maximum and minimum scale values of Y axis [datasource prepare]; // Add created series to chart [chart addSeries:series]; [chart addSeries:series1]; [chart addSeries:series2]; [chart addSeries:series3]; // Refresh data [chart forceDataUpdate]; // Execute custom example [self executeExample]; // Display chart [self.view addSubview:chart]; // Calculate size for chart drawing min = datasource.yMin; max = datasource.yMax; } // Function for example execution -(void) executeExample{ }; // Create point labels - (ChartLabel *)createDefaultLabel { ChartLabel *result = [[[ChartLabel alloc] initWithFrame:CGRectZero] autorelease]; result.background = [[SolidColorBrush new] autorelease]; // Background result.borderColor = [UIColor blackColor]; // Border color result.borderThickness = 1; // Border thickness result.borderRadius = 10; // Angle rounding radius result.font = [UIFont fontWithName:@"Arial" size:18]; // Font result.formatter = [NSNumberFormatter new]; result.textAlignment = AlignmentCenter; // Text alignment result.textColor = [UIColor blackColor]; // Text color // Determine text formatting mask result.mask = [NSString stringWithFormat:@"%@#FVAL%@", (NSString *)[m_numberFormat objectForKey:@"prefix"], (NSString *)[m_numberFormat objectForKey:@"suffix"]]; result.countOfDecimalNumbers = 3; // Number of displayed decimal places result.displayNegativesInRed = NO; // Negative value display format // Hide additional lines result.hasHorizontalNoteLine = result.hasVerticalNoteLine = result.hasFootNoteLine = NO; // Set minimum width result.minWidth = 0; return result; } @end
See also: