ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

iOS 图表工具charts之BarChartView

2019-07-11 19:00:34  阅读:588  来源: 互联网

标签:BarChartView NO leftAxis self iOS charts UIColor xAxis chartView


BarChartView在charts中可以用来绘制柱状图,由于charts是基于swift开发的,如果需要和objective-C混编(通过pod的方式不用管),可以参考我的上几篇文章《iOS OC中桥接swift第三方库》,这里主要讲的是LineChartView的一些常用属性和一些基本用法,实际情况以开发为准

chartView的更加细节的属性设置 请查看我的上篇文章《iOS 图表工具charts之LineChartView》,因为chartView大部分基础属性都差不多,这里就不详细写注释了
BarChartView的基础设置

-(void)setupUI{
    //BarChartView默认纵向展示柱状图, 如果需要横向展示 则创建HorizontalBarChartView即可
    BarChartView *chartView = [[BarChartView alloc] init];
    //设置偏移
    [chartView setExtraOffsetsWithLeft:10 top:10 right:10 bottom:10];
    //开启border
    chartView.drawBordersEnabled = YES;
    chartView.borderLineWidth = .5f;
    chartView.borderColor = UIColor.blackColor;
    //设置背景
    chartView.drawGridBackgroundEnabled = NO;
    chartView.gridBackgroundColor = [UIColor grayColor];
    //无内容显示
    chartView.noDataText = @"";
    //关闭描述
    chartView.chartDescription.enabled = NO;
    chartView.chartDescription.text = @"tiny`s barChart demo";
    //关闭图例
    chartView.legend.enabled = NO;
    //缩放
    chartView.scaleXEnabled = NO;
    chartView.scaleYEnabled = NO;
    chartView.autoScaleMinMaxEnabled = YES;
    chartView.highlightPerTapEnabled = NO;
    chartView.highlightPerDragEnabled = NO;
    chartView.pinchZoomEnabled = NO;  //手势捏合
    chartView.dragEnabled = YES;
    chartView.dragDecelerationFrictionCoef = 0.5;  //0 1 惯性
    //代理
    chartView.delegate = self;
    
    //leftAxis
    ChartYAxis *leftAxis = chartView.leftAxis;
    leftAxis.enabled = YES;
    leftAxis.labelPosition = YAxisLabelPositionOutsideChart;
    leftAxis.drawGridLinesEnabled = YES;
    leftAxis.gridLineDashLengths = @[@2,@4];
    leftAxis.labelTextColor = UIColor.blackColor;
    leftAxis.labelFont = [UIFont systemFontOfSize:10];
    leftAxis.decimals = 2;
    //设置样式
    LeftAxisFormatter *leftFormatter = [LeftAxisFormatter new];
    leftAxis.valueFormatter = leftFormatter;
    
    //rightAxis
    ChartYAxis *rightAxis = chartView.rightAxis;
    rightAxis.enabled = NO;
    
    //xAxis
    ChartXAxis *xAxis = chartView.xAxis;
    xAxis.enabled = YES;
    xAxis.labelPosition = XAxisLabelPositionBottom;
    //不画线
    xAxis.drawGridLinesEnabled = NO;
    BarxAxisFormatter *xFormatter = [BarxAxisFormatter new];
    xAxis.valueFormatter = xFormatter;
    xFormatter.titles = @[@"语文",@"数学",@"外语",@"物理"];
    self.charView = chartView;
    [self addSubview:self.charView];
    [self.charView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(0);
    }];
    
    //draw
    [self drawData];
    //执行动画
    [self.charView animateWithYAxisDuration:1.f];
}


-(void)drawData{
    NSArray *datas = @[@100,@90,@76,@55,@45,@77,@98,@62];
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 0; i < datas.count; i++) {
        BarChartDataEntry *entry = [[BarChartDataEntry alloc] initWithX:i y:[datas[i] integerValue]];
        [array addObject:entry];
    }
    //set
    BarChartDataSet *set = [[BarChartDataSet alloc] initWithEntries:array label:@"Bar DataSet"];
    [set setColors:@[UIColor.redColor,UIColor.blueColor,UIColor.blueColor,UIColor.blackColor,UIColor.cyanColor,UIColor.grayColor,UIColor.greenColor,UIColor.cyanColor]];
    //显示柱图值并格式化
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    numberFormatter.positiveSuffix = @"分";
    ChartDefaultValueFormatter *formatter = [[ChartDefaultValueFormatter alloc] initWithFormatter:numberFormatter];
    [set setValueFormatter:formatter];
    set.highlightEnabled = NO;
    BarChartData *data = [[BarChartData alloc] initWithDataSet:set];
    self.charView.data = data;
}

#pragma mark - ChartViewDelegate
#pragma mark 图表中数值被选中
-(void)chartValueSelected:(ChartViewBase *)chartView entry:(ChartDataEntry *)entry highlight:(ChartHighlight *)highlight{
    
//    NSLog(@"图表中数值被选中");
}

#pragma mark 图表中的空白区域被选中
-(void)chartValueNothingSelected:(ChartViewBase *)chartView{
//    NSLog(@"空白区域被选中");
}

#pragma mark 图表被缩放
-(void)chartScaled:(ChartViewBase *)chartView scaleX:(CGFloat)scaleX scaleY:(CGFloat)scaleY{
//    NSLog(@"图表被缩放");
}

#pragma mark 图表被移动
-(void)chartTranslated:(ChartViewBase *)chartView dX:(CGFloat)dX dY:(CGFloat)dY{
//    NSLog(@"图表被移动");
}

标签:BarChartView,NO,leftAxis,self,iOS,charts,UIColor,xAxis,chartView
来源: https://www.cnblogs.com/qqcc1388/p/11171939.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有