123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- //
- // PiePolylineChartViewController.h
- // ChartsDemo
- //
- // Created by Jack Wang on 3/21/16.
- // Copyright © 2016 Jack Wang
- //
- #import "PiePolylineChartViewController.h"
- #import "ChartsDemo_iOS-Swift.h"
- @interface PiePolylineChartViewController () <ChartViewDelegate>
- @property (nonatomic, strong) IBOutlet PieChartView *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 PiePolylineChartViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
- self.title = @"Pie Poly Line Chart";
-
- self.options = @[
- @{@"key": @"toggleValues", @"label": @"Toggle Y-Values"},
- @{@"key": @"toggleXValues", @"label": @"Toggle X-Values"},
- @{@"key": @"togglePercent", @"label": @"Toggle Percent"},
- @{@"key": @"toggleHole", @"label": @"Toggle Hole"},
- @{@"key": @"toggleLabelsMinimumAngle", @"label": @"Toggle Labels Minimum Angle"},
- @{@"key": @"animateX", @"label": @"Animate X"},
- @{@"key": @"animateY", @"label": @"Animate Y"},
- @{@"key": @"animateXY", @"label": @"Animate XY"},
- @{@"key": @"spin", @"label": @"Spin"},
- @{@"key": @"drawCenter", @"label": @"Draw CenterText"},
- @{@"key": @"saveToGallery", @"label": @"Save to Camera Roll"},
- @{@"key": @"toggleData", @"label": @"Toggle Data"},
- ];
-
- [self setupPieChartView:_chartView];
-
- _chartView.legend.enabled = NO;
- _chartView.delegate = self;
-
- [_chartView setExtraOffsetsWithLeft:20.f top:0.f right:20.f bottom:0.f];
-
- _sliderX.value = 4.0;
- _sliderY.value = 100.0;
- [self slidersValueChanged:nil];
-
- [_chartView animateWithYAxisDuration:1.4 easingOption:ChartEasingOptionEaseOutBack];
- }
- - (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
- {
- double mult = range;
-
- NSMutableArray *entries = [[NSMutableArray alloc] init];
-
- for (int i = 0; i < count; i++)
- {
- [entries addObject:[[PieChartDataEntry alloc] initWithValue:(arc4random_uniform(mult) + mult / 5) label:parties[i % parties.count]]];
- }
-
- PieChartDataSet *dataSet = [[PieChartDataSet alloc] initWithEntries:entries label:@"Election Results"];
- dataSet.sliceSpace = 2.0;
-
- // add a lot of colors
-
- NSMutableArray *colors = [[NSMutableArray alloc] init];
- [colors addObjectsFromArray:ChartColorTemplates.vordiplom];
- [colors addObjectsFromArray:ChartColorTemplates.joyful];
- [colors addObjectsFromArray:ChartColorTemplates.colorful];
- [colors addObjectsFromArray:ChartColorTemplates.liberty];
- [colors addObjectsFromArray:ChartColorTemplates.pastel];
- [colors addObject:[UIColor colorWithRed:51/255.f green:181/255.f blue:229/255.f alpha:1.f]];
-
- dataSet.colors = colors;
-
- dataSet.valueLinePart1OffsetPercentage = 0.8;
- dataSet.valueLinePart1Length = 0.2;
- dataSet.valueLinePart2Length = 0.4;
- //dataSet.xValuePosition = PieChartValuePositionOutsideSlice;
- dataSet.yValuePosition = PieChartValuePositionOutsideSlice;
-
- PieChartData *data = [[PieChartData alloc] initWithDataSet:dataSet];
-
- NSNumberFormatter *pFormatter = [[NSNumberFormatter alloc] init];
- pFormatter.numberStyle = NSNumberFormatterPercentStyle;
- pFormatter.maximumFractionDigits = 1;
- pFormatter.multiplier = @1.f;
- pFormatter.percentSymbol = @" %";
- [data setValueFormatter:[[ChartDefaultValueFormatter alloc] initWithFormatter:pFormatter]];
- [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:11.f]];
- [data setValueTextColor:UIColor.blackColor];
-
- _chartView.data = data;
- [_chartView highlightValues:nil];
- }
- - (void)optionTapped:(NSString *)key
- {
- if ([key isEqualToString:@"toggleXValues"])
- {
- _chartView.drawEntryLabelsEnabled = !_chartView.isDrawEntryLabelsEnabled;
-
- [_chartView setNeedsDisplay];
- return;
- }
-
- if ([key isEqualToString:@"togglePercent"])
- {
- _chartView.usePercentValuesEnabled = !_chartView.isUsePercentValuesEnabled;
-
- [_chartView setNeedsDisplay];
- return;
- }
-
- if ([key isEqualToString:@"toggleHole"])
- {
- _chartView.drawHoleEnabled = !_chartView.isDrawHoleEnabled;
-
- [_chartView setNeedsDisplay];
- return;
- }
-
- if ([key isEqualToString:@"toggleLabelsMinimumAngle"])
- {
- CGFloat newMinimum = _chartView.sliceTextDrawingThreshold == 20.0 ? 0.0 : 20.0;
- _chartView.sliceTextDrawingThreshold = newMinimum;
- }
- if ([key isEqualToString:@"drawCenter"])
- {
- _chartView.drawCenterTextEnabled = !_chartView.isDrawCenterTextEnabled;
-
- [_chartView setNeedsDisplay];
- return;
- }
-
- if ([key isEqualToString:@"animateX"])
- {
- [_chartView animateWithXAxisDuration:1.4];
- return;
- }
-
- if ([key isEqualToString:@"animateY"])
- {
- [_chartView animateWithYAxisDuration:1.4];
- return;
- }
-
- if ([key isEqualToString:@"animateXY"])
- {
- [_chartView animateWithXAxisDuration:1.4 yAxisDuration:1.4];
- return;
- }
-
- if ([key isEqualToString:@"spin"])
- {
- [_chartView spinWithDuration:2.0 fromAngle:_chartView.rotationAngle toAngle:_chartView.rotationAngle + 360.f];
- 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
|