123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- //
- // LineChart1ViewController.m
- // ChartsDemo
- //
- // Copyright 2015 Daniel Cohen Gindi & Philipp Jahoda
- // A port of MPAndroidChart for iOS
- // Licensed under Apache License 2.0
- //
- // https://github.com/danielgindi/Charts
- //
- #import "LineChart1ViewController.h"
- #import "ChartsDemo_iOS-Swift.h"
- @interface LineChart1ViewController () <ChartViewDelegate>
- @property (nonatomic, strong) IBOutlet LineChartView *chartView;
- @property (nonatomic, strong) IBOutlet UISlider *sliderX;
- @property (nonatomic, strong) IBOutlet UISlider *sliderY;
- @property (nonatomic, strong) IBOutlet UITextField *sliderTextX;
- @property (nonatomic, strong) IBOutlet UITextField *sliderTextY;
- @end
- @implementation LineChart1ViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
- self.title = @"Line Chart 1";
-
- self.options = @[
- @{@"key": @"toggleValues", @"label": @"Toggle Values"},
- @{@"key": @"toggleFilled", @"label": @"Toggle Filled"},
- @{@"key": @"toggleCircles", @"label": @"Toggle Circles"},
- @{@"key": @"toggleCubic", @"label": @"Toggle Cubic"},
- @{@"key": @"toggleHorizontalCubic", @"label": @"Toggle Horizontal Cubic"},
- @{@"key": @"toggleIcons", @"label": @"Toggle Icons"},
- @{@"key": @"toggleStepped", @"label": @"Toggle Stepped"},
- @{@"key": @"toggleHighlight", @"label": @"Toggle Highlight"},
- @{@"key": @"animateX", @"label": @"Animate X"},
- @{@"key": @"animateY", @"label": @"Animate Y"},
- @{@"key": @"animateXY", @"label": @"Animate XY"},
- @{@"key": @"saveToGallery", @"label": @"Save to Camera Roll"},
- @{@"key": @"togglePinchZoom", @"label": @"Toggle PinchZoom"},
- @{@"key": @"toggleAutoScaleMinMax", @"label": @"Toggle auto scale min/max"},
- @{@"key": @"toggleData", @"label": @"Toggle Data"},
- ];
-
- _chartView.delegate = self;
-
- _chartView.chartDescription.enabled = NO;
-
- _chartView.dragEnabled = YES;
- [_chartView setScaleEnabled:YES];
- _chartView.pinchZoomEnabled = YES;
- _chartView.drawGridBackgroundEnabled = NO;
- // x-axis limit line
- ChartLimitLine *llXAxis = [[ChartLimitLine alloc] initWithLimit:10.0 label:@"Index 10"];
- llXAxis.lineWidth = 4.0;
- llXAxis.lineDashLengths = @[@(10.f), @(10.f), @(0.f)];
- llXAxis.labelPosition = ChartLimitLabelPositionRightBottom;
- llXAxis.valueFont = [UIFont systemFontOfSize:10.f];
-
- //[_chartView.xAxis addLimitLine:llXAxis];
-
- _chartView.xAxis.gridLineDashLengths = @[@10.0, @10.0];
- _chartView.xAxis.gridLineDashPhase = 0.f;
-
- ChartLimitLine *ll1 = [[ChartLimitLine alloc] initWithLimit:150.0 label:@"Upper Limit"];
- ll1.lineWidth = 4.0;
- ll1.lineDashLengths = @[@5.f, @5.f];
- ll1.labelPosition = ChartLimitLabelPositionRightTop;
- ll1.valueFont = [UIFont systemFontOfSize:10.0];
-
- ChartLimitLine *ll2 = [[ChartLimitLine alloc] initWithLimit:-30.0 label:@"Lower Limit"];
- ll2.lineWidth = 4.0;
- ll2.lineDashLengths = @[@5.f, @5.f];
- ll2.labelPosition = ChartLimitLabelPositionRightBottom;
- ll2.valueFont = [UIFont systemFontOfSize:10.0];
-
- ChartYAxis *leftAxis = _chartView.leftAxis;
- [leftAxis removeAllLimitLines];
- [leftAxis addLimitLine:ll1];
- [leftAxis addLimitLine:ll2];
- leftAxis.axisMaximum = 200.0;
- leftAxis.axisMinimum = -50.0;
- leftAxis.gridLineDashLengths = @[@5.f, @5.f];
- leftAxis.drawZeroLineEnabled = NO;
- leftAxis.drawLimitLinesBehindDataEnabled = YES;
-
- _chartView.rightAxis.enabled = NO;
-
- //[_chartView.viewPortHandler setMaximumScaleY: 2.f];
- //[_chartView.viewPortHandler setMaximumScaleX: 2.f];
-
- BalloonMarker *marker = [[BalloonMarker alloc]
- initWithColor: [UIColor colorWithWhite:180/255. alpha:1.0]
- font: [UIFont systemFontOfSize:12.0]
- textColor: UIColor.whiteColor
- insets: UIEdgeInsetsMake(8.0, 8.0, 20.0, 8.0)];
- marker.chartView = _chartView;
- marker.minimumSize = CGSizeMake(80.f, 40.f);
- _chartView.marker = marker;
-
- _chartView.legend.form = ChartLegendFormLine;
-
- _sliderX.value = 45.0;
- _sliderY.value = 100.0;
- [self slidersValueChanged:nil];
-
- [_chartView animateWithXAxisDuration:2.5];
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (void)updateChartData
- {
- if (self.shouldHideData)
- {
- _chartView.data = nil;
- return;
- }
-
- [self setDataCount:_sliderX.value range:_sliderY.value];
- }
- - (void)setDataCount:(int)count range:(double)range
- {
- NSMutableArray *values = [[NSMutableArray alloc] init];
-
- for (int i = 0; i < count; i++)
- {
- double val = arc4random_uniform(range) + 3;
- [values addObject:[[ChartDataEntry alloc] initWithX:i y:val icon: [UIImage imageNamed:@"icon"]]];
- }
-
- LineChartDataSet *set1 = nil;
- if (_chartView.data.dataSetCount > 0)
- {
- set1 = (LineChartDataSet *)_chartView.data.dataSets[0];
- [set1 replaceEntries: values];
- [_chartView.data notifyDataChanged];
- [_chartView notifyDataSetChanged];
- }
- else
- {
- set1 = [[LineChartDataSet alloc] initWithEntries:values label:@"DataSet 1"];
-
- set1.drawIconsEnabled = NO;
-
- set1.lineDashLengths = @[@5.f, @2.5f];
- set1.highlightLineDashLengths = @[@5.f, @2.5f];
- [set1 setColor:UIColor.blackColor];
- [set1 setCircleColor:UIColor.blackColor];
- set1.lineWidth = 1.0;
- set1.circleRadius = 3.0;
- set1.drawCircleHoleEnabled = NO;
- set1.valueFont = [UIFont systemFontOfSize:9.f];
- set1.formLineDashLengths = @[@5.f, @2.5f];
- set1.formLineWidth = 1.0;
- set1.formSize = 15.0;
-
- NSArray *gradientColors = @[
- (id)[ChartColorTemplates colorFromString:@"#00ff0000"].CGColor,
- (id)[ChartColorTemplates colorFromString:@"#ffff0000"].CGColor
- ];
- CGGradientRef gradient = CGGradientCreateWithColors(nil, (CFArrayRef)gradientColors, nil);
-
- set1.fillAlpha = 1.f;
- set1.fill = [[ChartLinearGradientFill alloc] initWithGradient:gradient angle:90.0f];
- set1.drawFilledEnabled = YES;
-
- CGGradientRelease(gradient);
-
- NSMutableArray *dataSets = [[NSMutableArray alloc] init];
- [dataSets addObject:set1];
-
- LineChartData *data = [[LineChartData alloc] initWithDataSets:dataSets];
-
- _chartView.data = data;
- }
- }
- - (void)optionTapped:(NSString *)key
- {
- if ([key isEqualToString:@"toggleFilled"])
- {
- for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
- {
- set.drawFilledEnabled = !set.isDrawFilledEnabled;
- }
-
- [_chartView setNeedsDisplay];
- return;
- }
-
- if ([key isEqualToString:@"toggleCircles"])
- {
- for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
- {
- set.drawCirclesEnabled = !set.isDrawCirclesEnabled;
- }
-
- [_chartView setNeedsDisplay];
- return;
- }
-
- if ([key isEqualToString:@"toggleCubic"])
- {
- for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
- {
- set.mode = set.mode == LineChartModeCubicBezier ? LineChartModeLinear : LineChartModeCubicBezier;
- }
-
- [_chartView setNeedsDisplay];
- return;
- }
- if ([key isEqualToString:@"toggleStepped"])
- {
- for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
- {
- switch (set.mode) {
- case LineChartModeLinear:
- case LineChartModeCubicBezier:
- case LineChartModeHorizontalBezier:
- set.mode = LineChartModeStepped;
- break;
- case LineChartModeStepped: set.mode = LineChartModeLinear;
- }
- }
- [_chartView setNeedsDisplay];
- }
-
- if ([key isEqualToString:@"toggleHorizontalCubic"])
- {
- for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
- {
- set.mode = set.mode == LineChartModeHorizontalBezier ? LineChartModeCubicBezier : LineChartModeHorizontalBezier;
- }
-
- [_chartView setNeedsDisplay];
- return;
- }
-
- [super handleOption:key forChartView:_chartView];
- }
- #pragma mark - Actions
- - (IBAction)slidersValueChanged:(id)sender
- {
- _sliderTextX.text = [@((int)_sliderX.value) stringValue];
- _sliderTextY.text = [@((int)_sliderY.value) stringValue];
-
- [self updateChartData];
- }
- #pragma mark - ChartViewDelegate
- - (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry highlight:(ChartHighlight * __nonnull)highlight
- {
- NSLog(@"chartValueSelected");
- }
- - (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
- {
- NSLog(@"chartValueNothingSelected");
- }
- @end
|