Skip to content

axisMaximum & labelCount ot effective #5244

@taojeff

Description

@taojeff
@implementation LineChart1ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"Line Chart 1";
    
    self.options = @[
                     @{@"key": @"toggleValues", @"label": @"Toggle Values"},
                     @{@"key": @"toggleFilled", @"label": @"Toggle Filled"},
                     @{@"key": @"toggleCircles", @"label": @"Toggle Circles"},
                     @{@"key": @"toggleCubic", @"label": @"Toggle Cubic"},
                     @{@"key": @"toggleHorizontalCubic", @"label": @"Toggle Horizontal Cubic"},
                     @{@"key": @"toggleIcons", @"label": @"Toggle Icons"},
                     @{@"key": @"toggleStepped", @"label": @"Toggle Stepped"},
                     @{@"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"},
                     ];
    
    NSArray *xLabels = @[@"00:00", @"06:00", @"12:00", @"18:00", @"00:00"];
    NSArray *yValues = @[
        @65, @50, @80, @60, @75, @75,
        @65, @50, @80, @60, @75, @75,
        @65, @50, @80, @60, @75, @75,
        @65, @50, @80, @60, @75, @75
    ];
    NSMutableArray<ChartDataEntry *> *entries = [NSMutableArray array];
    for (int i = 0; i < yValues.count; i++) {
        [entries addObject:[[ChartDataEntry alloc] initWithX:i y:[yValues[i] doubleValue]]];
    }
    CGRect frame = _chartView.frame;
    frame.size = CGSizeMake([UIScreen mainScreen].bounds.size.width, 300);
    _chartView.frame = frame;
    _chartView.delegate = self;
    _chartView.chartDescription.enabled = NO;
    _chartView.dragEnabled = YES;
    [_chartView setScaleEnabled:NO];
    _chartView.pinchZoomEnabled = YES;
    _chartView.drawGridBackgroundEnabled = NO;
    _chartView.drawBordersEnabled = NO;
    _chartView.legend.enabled = NO;
    _chartView.xAxis.gridLineDashLengths = @[@5.f, @5.f];
    _chartView.xAxis.labelTextColor = UIColor.redColor;
    _chartView.xAxis.labelFont = [UIFont systemFontOfSize:16.f];
    _chartView.xAxis.labelPosition = XAxisLabelPositionBottom;
    _chartView.xAxis.granularity = 1;
    _chartView.xAxis.labelCount = 5;
    _chartView.xAxis.axisMinimum = 0;
    _chartView.xAxis.axisMaximum = 23.999;
    _chartView.xAxis.spaceMin = 0;
    _chartView.xAxis.spaceMax = 0;
    _chartView.extraLeftOffset = 30;
    _chartView.xAxis.valueFormatter = [[CustomAxisValueFormatter alloc] initWithLabels:xLabels];
    LineChartDataSet *dataSet = [[LineChartDataSet alloc] initWithEntries:entries];
    dataSet.drawIconsEnabled = NO;
    [dataSet setColor:UIColor.redColor];
    dataSet.lineWidth = 2.0;
    dataSet.drawCircleHoleEnabled = NO;
    dataSet.drawCirclesEnabled = NO;
    dataSet.drawValuesEnabled = NO;
    dataSet.mode = LineChartModeHorizontalBezier;
    
    NSArray *gradientColors = @[
        (id)[ChartColorTemplates colorFromString:@"#00FFC4C8"].CGColor,
        (id)[ChartColorTemplates colorFromString:@"#4DFFC4C8"].CGColor
    ];
    CGGradientRef gradient = CGGradientCreateWithColors(nil, (CFArrayRef)gradientColors, nil);
    dataSet.fillAlpha = 1.f;
    dataSet.fill = [[ChartLinearGradientFill alloc] initWithGradient:gradient angle:90.0f];
    dataSet.drawFilledEnabled = YES;
    CGGradientRelease(gradient);
    LineChartData *data = [[LineChartData alloc] initWithDataSets:@[dataSet]];
    _chartView.data = data;
    
    ChartYAxis *rightAxis = _chartView.rightAxis;
    [rightAxis removeAllLimitLines];
    rightAxis.axisMaximum = 100.0;
    rightAxis.axisMinimum = 0.0;
    rightAxis.gridLineDashLengths = @[@5.f, @5.f];
    rightAxis.drawZeroLineEnabled = NO;
    rightAxis.axisLineDashLengths = @[@5.f, @5.f];
    rightAxis.labelFont = [UIFont systemFontOfSize:16.f];
    rightAxis.labelTextColor = UIColor.redColor;
    rightAxis.drawLimitLinesBehindDataEnabled = YES;
    
    _chartView.leftAxis.enabled = NO;
    
    BalloonMarker *marker = [[BalloonMarker alloc]
                             initWithColor: [UIColor blueColor]
                             font: [UIFont systemFontOfSize:12.0]
                             textColor: UIColor.whiteColor
                             insets: UIEdgeInsetsMake(8.0, 8.0, 20.0, 8.0)];
    
    marker.chartView = _chartView;
    marker.minimumSize = CGSizeMake(80.f, 40.f);
    _chartView.marker = marker;
    
    [_chartView animateWithXAxisDuration:0];
}

#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");
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

@interface CustomAxisValueFormatter ()
@property (nonatomic, strong) NSArray<NSString *> *timeLabels;
@end

@implementation CustomAxisValueFormatter

- (instancetype)initWithLabels:(NSArray<NSString *> *)labels {
    if (self = [super init]) {
        self.timeLabels = labels;
    }
    return self;
}

- (NSString *)stringForValue:(double)value axis:(ChartAxisBase *)axis {
    NSInteger index = (NSInteger)round(value / 6.0); // 每6小时一个
    if (index >= 0 && index < self.timeLabels.count) {
        return self.timeLabels[index];
    }
    return @"111";
}

@end 

- (NSString *)stringForValue:(double)value axis:(ChartAxisBase *)axis {
    NSInteger index = (NSInteger)round(value / 6.0); // 每6小时一个
    if (index >= 0 && index < self.timeLabels.count) {
        return self.timeLabels[index];
    }
    return @"111";
}

Image

How to display one label every 6 hours and remove the extra spaces on the right side?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions