iOS_danielgindi/Charts柱形图barChartView使用

#import "BarChartsView.h"
#import "ChartsDemo-Bridging-Header.h"
#import "ChartsDemo-swift.h"
#import "KBValueFormatter.h"
#define kSelfWidth self.frame.size.width
#define kSelfHeight self.frame.size.height

@interface BarChartsView ()<IChartAxisValueFormatter, IChartValueFormatter>
@property(nonatomic, assign) BOOL yIsPercent;   //y轴是否是百分比
@end

@implementation BarChartsView {
    BarChartView *_barChartView;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self uiConfig];
    }
    return self;
}

- (void)uiConfig {
    _barChartView = [[BarChartView alloc] init];
    _barChartView.backgroundColor = [UIColor cyanColor];
    _barChartView.frame = CGRectMake(0, 0, kSelfWidth, kSelfHeight);
    _barChartView.dragEnabled = YES;
    _barChartView.pinchZoomEnabled = NO;   //捏合手势
    _barChartView.doubleTapToZoomEnabled = NO; //双击手势
    _barChartView.rightAxis.enabled = NO;
    _barChartView.legend.enabled = NO;
    _barChartView.chartDescription.enabled = NO;
    
    ChartXAxis *xAxis = _barChartView.xAxis;
    xAxis.labelPosition = XAxisLabelPositionBottom;
    xAxis.labelFont = [UIFont systemFontOfSize:8.f];
    xAxis.labelWidth = 10;
    xAxis.gridLineDashPhase = 0.f;
    xAxis.drawGridLinesEnabled = NO;
    xAxis.axisMinValue = -0.8;
    xAxis.labelRotationAngle = -20;
    xAxis.gridLineDashLengths = @[@5.f, @5.f];
    xAxis.granularity = 1.0;
    
    ChartYAxis *leftAxis = _barChartView.leftAxis;
    leftAxis.axisMaxValue = 100.0;
    leftAxis.axisMinValue = 0.0;
    leftAxis.axisLineWidth = 0;
    leftAxis.gridLineDashLengths = @[@5.f, @5.f];
    leftAxis.valueFormatter = self;
    [self addSubview:_barChartView];
    
    // 开始设值
    NSArray *statistics = @[
                            [NSNumber numberWithLong:5],
                            [NSNumber numberWithLong:5],
                            [NSNumber numberWithLong:7],
                            [NSNumber numberWithLong:8],
                            [NSNumber numberWithLong:8],
                            [NSNumber numberWithLong:10],
                            ];
    NSArray *xValues = @[@"百度", @"腾讯", @"阿里", @"京东", @"小米", @"苹果"];
    NSMutableArray *yVals = [NSMutableArray array];
    double leftAxisMin = 0;
    double leftAxisMax = 0;
    for (int i = 0; i < statistics.count; i++) {
        NSNumber *num = statistics[i];
        double value = [num doubleValue];
        leftAxisMax = MAX(value, leftAxisMax);
        leftAxisMin = MIN(value, leftAxisMin);
        [yVals addObject:[[BarChartDataEntry alloc] initWithX:i y:value]];
    }
    CGFloat topNum = leftAxisMax * (5.0/4.0);
    _barChartView.leftAxis.axisMaxValue = topNum;
    // 设置Y轴数据
    _barChartView.leftAxis.valueFormatter = self;
    
    // 设置柱形数值
    BarChartDataSet *set1 = nil;
    set1 = [[BarChartDataSet alloc] initWithValues:yVals label:@""];
    set1.valueFormatter = self;
    set1.highlightEnabled = NO;
    [set1 setColors:@[[UIColor redColor]]];
    
    NSMutableArray *dataSets = [[NSMutableArray alloc] init];
    [dataSets addObject:set1];
    
    BarChartData *data = [[BarChartData alloc] initWithDataSets:dataSets];
    [data setValueFont:[UIFont systemFontOfSize:10]];
    data.barWidth = 0.25f;
    
    // 设置X轴数据
    if (xValues.count > 0) {
        _barChartView.xAxis.axisMaxValue = xValues.count - 1 + 0.8;
        _barChartView.xAxis.labelCount = xValues.count;
        _barChartView.xAxis.valueFormatter = [[KBValueFormatter alloc] initWithDateArr:xValues];
    }
    _barChartView.data = data;
}

#pragma mark - 折线值
- (NSString *)stringForValue:(double)value entry:(ChartDataEntry *)entry dataSetIndex:(NSInteger)dataSetIndex viewPortHandler:(ChartViewPortHandler *)viewPortHandler {
    if (self.yIsPercent) {
        return [NSString stringWithFormat:@"%.2f%%", value];
    }
    return [NSString stringWithFormat:@"%.2f", value];
}

#pragma mark - y轴值
- (NSString *)stringForValue:(double)value axis:(ChartAxisBase *)axis {
    if (self.yIsPercent) {
        return [NSString stringWithFormat:@"%.1f%%", value];
    }
    return [NSString stringWithFormat:@"%.1f", value];
}


@end

KBValueFormatter.h

#import <Foundation/Foundation.h>

#import "ChartsDemo-swift.h"
#import "ChartsDemo-Bridging-Header.h"

@interface KBValueFormatter : NSObject <IChartAxisValueFormatter>

- (id)initWithDateArr:(NSArray *)arr;

@end

KBValueFormatter.m

#import "KBValueFormatter.h"

@implementation KBValueFormatter{
    NSArray *_dateArr;
}

- (id)initWithDateArr:(NSArray *)arr {
    if (self = [super init]) {
        _dateArr = [NSArray arrayWithArray:arr];
    }
    return self;
}

- (NSString *)stringForValue:(double)value axis:(ChartAxisBase *)axis {
    return _dateArr[(int)value];
}

@end

github demo

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小莫同学~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值