Описание: создание линейной диаграммы, использующей заданный источник данных.
#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];
// Определяем ссылку на выбранный ряд
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];
// Создаем диаграмму и определяем её размеры
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;
// Создаем источник данных
DataSource *datasource = [[DataSource alloc] init];
// Создаем ряды линейной диаграммы
// Создаем 1-й ряд
LineSeries *series = [LineSeries new];
series.dataIndex = 0; // Индекс ряда, нумерация начинается с нуля
series.defaultLabel = [self createDefaultLabel];
[series setColor:[UIColor blueColor]]; // Цвет линии ряда
series.isLegendVisible = YES; // Отображаем в легенде
[series setShadowColor:[UIColor blueColor]]; // Цвет тени
series.shadowOpacity = 0.2;// Прозрачность тени
series.shadowRadius = 0.01;// Радиус тени
series.showShadow = YES; // Отображаем тень
// Создаем 2-й ряд
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;
// Создаем 3-й ряд
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;
// Создаем 4-й ряд
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;
// Устанавливаем источник данных
[chart setDataSource:datasource];
[chart setDelegate:self];
// Получаем максимальное и минимальное значения шкалы для оси Y
[datasource prepare];
// Добавляем созданные ряды на диаграмму
[chart addSeries:series];
[chart addSeries:series1];
[chart addSeries:series2];
[chart addSeries:series3];
// Обновляем данные
[chart forceDataUpdate];
// Выполняем пользовательский пример
[self executeExample];
// Отображаем диаграмму
[self.view addSubview:chart];
// Рассчитываем размеры для прорисовки диаграммы
min = datasource.yMin;
max = datasource.yMax;
}
// Функция для выполнения примера
-(void) executeExample{
};
// Создаем подписи точек
- (ChartLabel *)createDefaultLabel
{
ChartLabel *result = [[[ChartLabel alloc] initWithFrame:CGRectZero] autorelease];
result.background = [[SolidColorBrush new] autorelease]; // Фон
result.borderColor = [UIColor blackColor]; // Цвет границ
result.borderThickness = 1; // Толщина границ
result.borderRadius = 10; // Радиус скругления углов
result.font = [UIFont fontWithName:@"Arial" size:18]; // Шрифт
result.formatter = [NSNumberFormatter new];
result.textAlignment = AlignmentCenter; // Выравнивание текста
result.textColor = [UIColor blackColor]; // Цвет текста
// Определяем маску форматирования текста
result.mask = [NSString stringWithFormat:@"%@#FVAL%@",
(NSString *)[m_numberFormat objectForKey:@"prefix"],
(NSString *)[m_numberFormat objectForKey:@"suffix"]];
result.countOfDecimalNumbers = 3; // Число отображаемых десятичных знаков
result.displayNegativesInRed = NO; // Формат отображения отрицательных значений
// Скрываем дополнительные линии
result.hasHorizontalNoteLine = result.hasVerticalNoteLine = result.hasFootNoteLine = NO;
// Устанавливаем минимальную ширину
result.minWidth = 0;
return result;
}
@end
См. также: