Files
macos-stats/Carthage/Checkouts/Charts/ChartsDemo-iOS/Objective-C/Demos/BarChartViewController.m
2019-09-09 17:57:18 +02:00

201 lines
6.7 KiB
Objective-C

//
// BarChartViewController.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 "BarChartViewController.h"
#import "ChartsDemo_iOS-Swift.h"
#import "DayAxisValueFormatter.h"
@interface BarChartViewController () <ChartViewDelegate>
@property (nonatomic, strong) IBOutlet BarChartView *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 BarChartViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Bar Chart";
self.options = @[
@{@"key": @"toggleValues", @"label": @"Toggle Values"},
@{@"key": @"toggleIcons", @"label": @"Toggle Icons"},
@{@"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"},
@{@"key": @"toggleBarBorders", @"label": @"Show Bar Borders"},
];
[self setupBarLineChartView:_chartView];
_chartView.delegate = self;
_chartView.drawBarShadowEnabled = NO;
_chartView.drawValueAboveBarEnabled = YES;
_chartView.maxVisibleCount = 60;
ChartXAxis *xAxis = _chartView.xAxis;
xAxis.labelPosition = XAxisLabelPositionBottom;
xAxis.labelFont = [UIFont systemFontOfSize:10.f];
xAxis.drawGridLinesEnabled = NO;
xAxis.granularity = 1.0; // only intervals of 1 day
xAxis.labelCount = 7;
xAxis.valueFormatter = [[DayAxisValueFormatter alloc] initForChart:_chartView];
NSNumberFormatter *leftAxisFormatter = [[NSNumberFormatter alloc] init];
leftAxisFormatter.minimumFractionDigits = 0;
leftAxisFormatter.maximumFractionDigits = 1;
leftAxisFormatter.negativeSuffix = @" $";
leftAxisFormatter.positiveSuffix = @" $";
ChartYAxis *leftAxis = _chartView.leftAxis;
leftAxis.labelFont = [UIFont systemFontOfSize:10.f];
leftAxis.labelCount = 8;
leftAxis.valueFormatter = [[ChartDefaultAxisValueFormatter alloc] initWithFormatter:leftAxisFormatter];
leftAxis.labelPosition = YAxisLabelPositionOutsideChart;
leftAxis.spaceTop = 0.15;
leftAxis.axisMinimum = 0.0; // this replaces startAtZero = YES
ChartYAxis *rightAxis = _chartView.rightAxis;
rightAxis.enabled = YES;
rightAxis.drawGridLinesEnabled = NO;
rightAxis.labelFont = [UIFont systemFontOfSize:10.f];
rightAxis.labelCount = 8;
rightAxis.valueFormatter = leftAxis.valueFormatter;
rightAxis.spaceTop = 0.15;
rightAxis.axisMinimum = 0.0; // this replaces startAtZero = YES
ChartLegend *l = _chartView.legend;
l.horizontalAlignment = ChartLegendHorizontalAlignmentLeft;
l.verticalAlignment = ChartLegendVerticalAlignmentBottom;
l.orientation = ChartLegendOrientationHorizontal;
l.drawInside = NO;
l.form = ChartLegendFormSquare;
l.formSize = 9.0;
l.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:11.f];
l.xEntrySpace = 4.0;
XYMarkerView *marker = [[XYMarkerView 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)
xAxisValueFormatter: _chartView.xAxis.valueFormatter];
marker.chartView = _chartView;
marker.minimumSize = CGSizeMake(80.f, 40.f);
_chartView.marker = marker;
_sliderX.value = 12.0;
_sliderY.value = 50.0;
[self slidersValueChanged:nil];
}
- (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
{
double start = 1.0;
NSMutableArray *yVals = [[NSMutableArray alloc] init];
for (int i = start; i < start + count + 1; i++)
{
double mult = (range + 1);
double val = (double) (arc4random_uniform(mult));
if (arc4random_uniform(100) < 25) {
[yVals addObject:[[BarChartDataEntry alloc] initWithX:i y:val icon: [UIImage imageNamed:@"icon"]]];
} else {
[yVals addObject:[[BarChartDataEntry alloc] initWithX:i y:val]];
}
}
BarChartDataSet *set1 = nil;
if (_chartView.data.dataSetCount > 0)
{
set1 = (BarChartDataSet *)_chartView.data.dataSets[0];
[set1 replaceEntries: yVals];
[_chartView.data notifyDataChanged];
[_chartView notifyDataSetChanged];
}
else
{
set1 = [[BarChartDataSet alloc] initWithEntries:yVals label:@"The year 2017"];
[set1 setColors:ChartColorTemplates.material];
set1.drawIconsEnabled = NO;
NSMutableArray *dataSets = [[NSMutableArray alloc] init];
[dataSets addObject:set1];
BarChartData *data = [[BarChartData alloc] initWithDataSets:dataSets];
[data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:10.f]];
data.barWidth = 0.9f;
_chartView.data = data;
}
}
- (void)optionTapped:(NSString *)key
{
[super handleOption:key forChartView:_chartView];
}
#pragma mark - Actions
- (IBAction)slidersValueChanged:(id)sender
{
_sliderTextX.text = [@((int)_sliderX.value + 2) 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