Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need detailed explanation about how to use this library #50

Open
Gargo opened this issue Dec 1, 2019 · 1 comment
Open

Need detailed explanation about how to use this library #50

Gargo opened this issue Dec 1, 2019 · 1 comment

Comments

@Gargo
Copy link

Gargo commented Dec 1, 2019

No description provided.

@ZhipingYang
Copy link
Owner

Easy demo

image
image

//
//  ViewController.m
//  XYChart
//
//  Created by Daniel Yang on 2019/12/6.
//  Copyright © 2019 Daniel. All rights reserved.
//

#import "ViewController.h"
#import <XYChart/XYChart.h>
#import <XYChart/XYChartItem.h>

@interface ViewController ()<XYChartDataSource, XYChartDelegate>
@property (nonatomic, strong) XYChart *chart;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
        
    _chart = [[XYChart alloc] initWithType:XYChartTypeBar];
    _chart.dataSource = self;
    _chart.delegate = self;
    [self.view addSubview:_chart];
    
    // layout
    _chart.translatesAutoresizingMaskIntoConstraints = false;
    if (@available(iOS 11.0, *)) {
        [NSLayoutConstraint activateConstraints:@[
            [_chart.leadingAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.leadingAnchor constant:10],
            [_chart.trailingAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.trailingAnchor constant:-10],
            [_chart.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor constant:30],
            [_chart.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor constant:-30]
        ]];
    } else if (@available(iOS 9.0, *)) {
        [NSLayoutConstraint activateConstraints:@[
            [_chart.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:10],
            [_chart.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-10],
            [_chart.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:30],
            [_chart.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor constant:-30]
        ]];
    } else {
        _chart.frame = CGRectInset(self.view.bounds, 30, 100);
    }
}

#pragma mark - XYChartDataSource

- (NSUInteger)numberOfSectionsInChart:(XYChart *)chart
{
    return 2;
}

- (NSUInteger)numberOfRowsInChart:(XYChart *)chart
{
    return 8;
}

- (NSAttributedString *)chart:(XYChart *)chart titleOfRowAtIndex:(NSUInteger)index
{
    NSDictionary *attributes = @{ NSFontAttributeName: [UIFont systemFontOfSize:13] };
    NSString *rowName = [NSString stringWithFormat:@"%lu", (unsigned long)index];
    return [[NSAttributedString alloc] initWithString:rowName attributes:attributes];
}

- (NSAttributedString *)chart:(XYChart *)chart titleOfSectionAtValue:(CGFloat)sectionValue
{
    NSDictionary *attributes = @{ NSFontAttributeName: [UIFont systemFontOfSize:10] };
    NSString *section = [NSString stringWithFormat:@"%0.f", sectionValue];
    return [[NSMutableAttributedString alloc] initWithString:section attributes:attributes];
}

- (id<XYChartItem>)chart:(XYChart *)chart itemOfIndex:(NSIndexPath *)index
{
    XYChartItem *item = [[XYChartItem alloc] init];
    item.color = index.section ? UIColor.redColor : UIColor.greenColor;
    item.value = index.section ? [NSNumber numberWithInteger:index.row+1]
                               : [NSNumber numberWithDouble:pow(index.row-3, 2)];
    item.showName = [NSString stringWithFormat:@"value: %@", item.value];
    return item;
}

- (XYRange)visibleRangeInChart:(XYChart *)chart
{
    return XYRangeMake(-5, 20);
}

- (NSUInteger)numberOfLevelInChart:(XYChart *)chart
{
    return 5;
}

- (CGFloat)rowWidthOfChart:(XYChart *)chart
{
    return 0;
}

- (BOOL)autoSizingRowInChart:(XYChart *)chart
{
    return true;
}

#pragma mark - XYChartDelegate

- (BOOL)chart:(XYChart *)chart shouldShowMenu:(NSIndexPath *)index
{
    return YES;
}

- (void)chart:(XYChart *)chart itemDidClick:(id<XYChartItem>)item
{
    // click event
}

- (CAAnimation *)chart:(XYChart *)chart clickAnimationOfIndex:(NSIndexPath *)index
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.duration = 0.2;
    animation.repeatCount = 1;
    animation.removedOnCompletion = true;
    animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1.0)];
    animation.autoreverses = true;
    return animation;
}

@end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants