// // HorizontalBarChartViewController.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 "HorizontalBarChartViewController.h" #import "ChartsDemo_iOS-Swift.h" @interface HorizontalBarChartViewController () @property (nonatomic, strong) IBOutlet HorizontalBarChartView *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 HorizontalBarChartViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"Horizontal 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.drawAxisLineEnabled = YES; xAxis.drawGridLinesEnabled = NO; xAxis.granularity = 10.0; ChartYAxis *leftAxis = _chartView.leftAxis; leftAxis.labelFont = [UIFont systemFontOfSize:10.f]; leftAxis.drawAxisLineEnabled = YES; leftAxis.drawGridLinesEnabled = YES; leftAxis.axisMinimum = 0.0; // this replaces startAtZero = YES ChartYAxis *rightAxis = _chartView.rightAxis; rightAxis.enabled = YES; rightAxis.labelFont = [UIFont systemFontOfSize:10.f]; rightAxis.drawAxisLineEnabled = YES; rightAxis.drawGridLinesEnabled = NO; 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 = 8.0; l.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:11.f]; l.xEntrySpace = 4.0; _chartView.fitBars = YES; _sliderX.value = 12.0; _sliderY.value = 50.0; [self slidersValueChanged:nil]; [_chartView animateWithYAxisDuration: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 + 1 range:_sliderY.value]; } - (void)setDataCount:(int)count range:(double)range { double barWidth = 9.0; double spaceForBar = 10.0; NSMutableArray *yVals = [[NSMutableArray alloc] init]; for (int i = 0; i < count; i++) { double mult = (range + 1); double val = (double) (arc4random_uniform(mult)); [yVals addObject:[[BarChartDataEntry alloc] initWithX:i * spaceForBar y:val icon: [UIImage imageNamed:@"icon"]]]; } 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:@"DataSet"]; 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 = barWidth; _chartView.data = data; } } - (void)optionTapped:(NSString *)key { [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