123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- //
- // CubicLineChartViewController.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 "CubicLineChartViewController.h"
- #import "ChartsDemo_iOS-Swift.h"
- @interface CubicLineSampleFillFormatter : NSObject <ChartFillFormatter>
- {
- }
- @end
- @implementation CubicLineSampleFillFormatter
- - (CGFloat)getFillLinePositionWithDataSet:(LineChartDataSet *)dataSet dataProvider:(id<LineChartDataProvider>)dataProvider
- {
- return -10.f;
- }
- @end
- @interface CubicLineChartViewController () <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 CubicLineChartViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
- self.title = @"Cubic Line Chart";
-
- 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": @"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 setViewPortOffsetsWithLeft:0.f top:20.f right:0.f bottom:0.f];
- _chartView.backgroundColor = [UIColor colorWithRed:104/255.f green:241/255.f blue:175/255.f alpha:1.f];
- _chartView.chartDescription.enabled = NO;
-
- _chartView.dragEnabled = YES;
- [_chartView setScaleEnabled:YES];
- _chartView.pinchZoomEnabled = NO;
- _chartView.drawGridBackgroundEnabled = NO;
- _chartView.maxHighlightDistance = 300.0;
-
- _chartView.xAxis.enabled = NO;
-
- ChartYAxis *yAxis = _chartView.leftAxis;
- yAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:12.f];
- [yAxis setLabelCount:6 force:NO];
- yAxis.labelTextColor = UIColor.whiteColor;
- yAxis.labelPosition = YAxisLabelPositionInsideChart;
- yAxis.drawGridLinesEnabled = NO;
- yAxis.axisLineColor = UIColor.whiteColor;
-
- _chartView.rightAxis.enabled = NO;
- _chartView.legend.enabled = NO;
-
- _sliderX.value = 45.0;
- _sliderY.value = 100.0;
- [self slidersValueChanged:nil];
-
- [_chartView animateWithXAxisDuration:2.0 yAxisDuration:2.0];
- }
- - (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 + 1 range:_sliderY.value];
- }
- - (void)setDataCount:(int)count range:(double)range
- {
- NSMutableArray *yVals1 = [[NSMutableArray alloc] init];
-
- for (int i = 0; i < count; i++)
- {
- double mult = (range + 1);
- double val = (double) (arc4random_uniform(mult)) + 20;
- [yVals1 addObject:[[ChartDataEntry alloc] initWithX:i y:val]];
- }
-
- LineChartDataSet *set1 = nil;
- if (_chartView.data.dataSetCount > 0)
- {
- set1 = (LineChartDataSet *)_chartView.data.dataSets[0];
- [set1 replaceEntries:yVals1];
- [_chartView.data notifyDataChanged];
- [_chartView notifyDataSetChanged];
- }
- else
- {
- set1 = [[LineChartDataSet alloc] initWithEntries:yVals1 label:@"DataSet 1"];
- set1.mode = LineChartModeCubicBezier;
- set1.cubicIntensity = 0.2;
- set1.drawCirclesEnabled = NO;
- set1.lineWidth = 1.8;
- set1.circleRadius = 4.0;
- [set1 setCircleColor:UIColor.whiteColor];
- set1.highlightColor = [UIColor colorWithRed:244/255.f green:117/255.f blue:117/255.f alpha:1.f];
- [set1 setColor:UIColor.whiteColor];
- set1.fillColor = UIColor.whiteColor;
- set1.fillAlpha = 1.f;
- set1.drawHorizontalHighlightIndicatorEnabled = NO;
- set1.fillFormatter = [[CubicLineSampleFillFormatter alloc] init];
-
- LineChartData *data = [[LineChartData alloc] initWithDataSet:set1];
- [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:9.f]];
- [data setDrawValues:NO];
-
- _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)
- {
- set.mode = set.mode == LineChartModeStepped ? LineChartModeLinear : LineChartModeStepped;
- }
-
- [_chartView setNeedsDisplay];
- }
-
- if ([key isEqualToString:@"toggleHorizontalCubic"])
- {
- for (id<LineChartDataSetProtocol> set in _chartView.data.dataSets)
- {
- set.mode = set.mode == LineChartModeCubicBezier ? LineChartModeHorizontalBezier : LineChartModeCubicBezier;
- }
-
- [_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
|