mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-14 00:04:15 +09:00
203 lines
6.0 KiB
Objective-C
203 lines
6.0 KiB
Objective-C
//
|
|
// HalfPieChartViewController.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 "HalfPieChartViewController.h"
|
|
#import "ChartsDemo_iOS-Swift.h"
|
|
|
|
@interface HalfPieChartViewController () <ChartViewDelegate>
|
|
|
|
@property (nonatomic, strong) IBOutlet PieChartView *chartView;
|
|
|
|
@end
|
|
|
|
@implementation HalfPieChartViewController
|
|
|
|
- (void)viewDidLoad
|
|
{
|
|
[super viewDidLoad];
|
|
|
|
self.title = @"Half Pie 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": @"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.delegate = self;
|
|
|
|
_chartView.holeColor = UIColor.whiteColor;
|
|
_chartView.transparentCircleColor = [UIColor.whiteColor colorWithAlphaComponent:0.43];
|
|
_chartView.holeRadiusPercent = 0.58;
|
|
_chartView.rotationEnabled = NO;
|
|
_chartView.highlightPerTapEnabled = YES;
|
|
|
|
_chartView.maxAngle = 180.0; // Half chart
|
|
_chartView.rotationAngle = 180.0; // Rotate to make the half on the upper side
|
|
_chartView.centerTextOffset = CGPointMake(0.0, -20.0);
|
|
|
|
ChartLegend *l = _chartView.legend;
|
|
l.horizontalAlignment = ChartLegendHorizontalAlignmentCenter;
|
|
l.verticalAlignment = ChartLegendVerticalAlignmentTop;
|
|
l.orientation = ChartLegendOrientationHorizontal;
|
|
l.drawInside = NO;
|
|
l.xEntrySpace = 7.0;
|
|
l.yEntrySpace = 0.0;
|
|
l.yOffset = 0.0;
|
|
|
|
// entry label styling
|
|
_chartView.entryLabelColor = UIColor.whiteColor;
|
|
_chartView.entryLabelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:12.f];
|
|
|
|
[self updateChartData];
|
|
|
|
[_chartView animateWithXAxisDuration: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:4 range:100];
|
|
}
|
|
|
|
- (void)setDataCount:(int)count range:(double)range
|
|
{
|
|
double mult = range;
|
|
|
|
NSMutableArray *values = [[NSMutableArray alloc] init];
|
|
|
|
// IMPORTANT: In a PieChart, no values (Entry) should have the same xIndex (even if from different DataSets), since no values can be drawn above each other.
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
[values addObject:[[PieChartDataEntry alloc] initWithValue:(arc4random_uniform(mult) + mult / 5) label:parties[i % parties.count]]];
|
|
}
|
|
|
|
PieChartDataSet *dataSet = [[PieChartDataSet alloc] initWithEntries:values label:@"Election Results"];
|
|
dataSet.sliceSpace = 3.0;
|
|
dataSet.selectionShift = 5.0;
|
|
|
|
dataSet.colors = ChartColorTemplates.material;
|
|
|
|
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.whiteColor];
|
|
|
|
_chartView.data = data;
|
|
|
|
[_chartView setNeedsDisplay];
|
|
}
|
|
|
|
- (void)optionTapped:(NSString *)key
|
|
{
|
|
if ([key isEqualToString:@"toggleXValues"])
|
|
{
|
|
_chartView.drawEntryLabelsEnabled = !_chartView.drawEntryLabelsEnabled;
|
|
|
|
[_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:@"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 easingOption:ChartEasingOptionEaseInCubic];
|
|
return;
|
|
}
|
|
|
|
[super handleOption:key forChartView:_chartView];
|
|
}
|
|
|
|
#pragma mark - Action
|
|
|
|
#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
|