XYChart
XYChart copied to clipboard
Need detailed explanation about how to use this library
Easy demo
//
// 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