ColoredLineChartViewController.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // ColoredLineChartViewController.m
  3. // ChartsDemo
  4. //
  5. // Copyright 2015 Daniel Cohen Gindi & Philipp Jahoda
  6. // A port of MPAndroidChart for iOS
  7. // Licensed under Apache License 2.0
  8. //
  9. // https://github.com/danielgindi/Charts
  10. //
  11. #import "ColoredLineChartViewController.h"
  12. #import "ChartsDemo_iOS-Swift.h"
  13. @interface ColoredLineChartViewController () <ChartViewDelegate>
  14. @property (nonatomic, strong) IBOutletCollection(LineChartView) NSArray *chartViews;
  15. @end
  16. @implementation ColoredLineChartViewController
  17. - (void)viewDidLoad
  18. {
  19. [super viewDidLoad];
  20. self.title = @"Colored Line Chart";
  21. for (int i = 0; i < _chartViews.count; i++)
  22. {
  23. LineChartData *data = [self dataWithCount:36 range:100];
  24. [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:7.f]];
  25. NSArray *colors = @[
  26. [UIColor colorWithRed:137/255.f green:230/255.f blue:81/255.f alpha:1.f],
  27. [UIColor colorWithRed:240/255.f green:240/255.f blue:30/255.f alpha:1.f],
  28. [UIColor colorWithRed:89/255.f green:199/255.f blue:250/255.f alpha:1.f],
  29. [UIColor colorWithRed:250/255.f green:104/255.f blue:104/255.f alpha:1.f],
  30. ];
  31. [self setupChart:_chartViews[i] data:data color:colors[i % colors.count]];
  32. }
  33. }
  34. - (void)setupChart:(LineChartView *)chart data:(LineChartData *)data color:(UIColor *)color
  35. {
  36. [(LineChartDataSet *)[data dataSetAtIndex:0] setCircleHoleColor:color];
  37. chart.delegate = self;
  38. chart.backgroundColor = color;
  39. chart.chartDescription.enabled = NO;
  40. chart.drawGridBackgroundEnabled = NO;
  41. chart.dragEnabled = YES;
  42. [chart setScaleEnabled:YES];
  43. chart.pinchZoomEnabled = NO;
  44. [chart setViewPortOffsetsWithLeft:10.0 top:0.0 right:10.0 bottom:0.0];
  45. chart.legend.enabled = NO;
  46. chart.leftAxis.enabled = NO;
  47. chart.leftAxis.spaceTop = 0.4;
  48. chart.leftAxis.spaceBottom = 0.4;
  49. chart.rightAxis.enabled = NO;
  50. chart.xAxis.enabled = NO;
  51. chart.data = data;
  52. [chart animateWithXAxisDuration:2.5];
  53. }
  54. - (void)didReceiveMemoryWarning
  55. {
  56. [super didReceiveMemoryWarning];
  57. // Dispose of any resources that can be recreated.
  58. }
  59. - (LineChartData *)dataWithCount:(int)count range:(double)range
  60. {
  61. NSMutableArray *yVals = [[NSMutableArray alloc] init];
  62. for (int i = 0; i < count; i++)
  63. {
  64. double val = (double) (arc4random_uniform(range)) + 3;
  65. [yVals addObject:[[ChartDataEntry alloc] initWithX:i y:val]];
  66. }
  67. LineChartDataSet *set1 = [[LineChartDataSet alloc] initWithEntries:yVals label:@"DataSet 1"];
  68. set1.lineWidth = 1.75;
  69. set1.circleRadius = 5.0;
  70. set1.circleHoleRadius = 2.5f;
  71. [set1 setColor:UIColor.whiteColor];
  72. [set1 setCircleColor:UIColor.whiteColor];
  73. set1.highlightColor = UIColor.whiteColor;
  74. set1.drawValuesEnabled = NO;
  75. return [[LineChartData alloc] initWithDataSet:set1];
  76. }
  77. #pragma mark - ChartViewDelegate
  78. - (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry highlight:(ChartHighlight * __nonnull)highlight
  79. {
  80. NSLog(@"chartValueSelected");
  81. }
  82. - (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
  83. {
  84. NSLog(@"chartValueNothingSelected");
  85. }
  86. @end