PNChart 折线图

//
// ViewController.m
// PNChart
//
// Created by 何壮壮 on 16/10/21.
// Copyright © 2016年 何壮壮. All rights reserved.
//

`#import "ViewController.h"

import

@interface ViewController ()
@property (nonatomic,strong) PNLineChart * lineChart;
@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    //折线图
    self.lineChart = [[PNLineChart alloc] initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 200)];
    self.lineChart.legendStyle = PNLegendItemStyleStacked;
    self.lineChart.delegate = self;

    self.lineChart.showCoordinateAxis = YES; //显示坐标系
    self.lineChart.showGenYLabels = YES;
    self.lineChart.showLabel = YES;
    self.lineChart.thousandsSeparator = YES;

[self.lineChart setXLabels:@[@"1月",@"2月",@"3月",@"4月",@"5月",@"6月",@"7月",@"8月",@"9月",@"10月",@"11月",@"12月"]];
NSArray *data01Array = @[@60.1, @160.1, @126.4, @262.2, @186.2, @60.1, @160.1, @126.4, @262.2, @186.2, @60.1, @160.1];

PNLineChartData *data01 = [PNLineChartData new]; //chart数据
data01.dataTitle = @"Alpha";
data01.color = PNFreshGreen; //折线的颜色
data01.itemCount = self.lineChart.xLabels.count; //x轴坐标item数
data01.inflexionPointStyle = PNLineChartPointStyleNone; //数值点的样式

data01.getData = ^(NSUInteger index) {
    
    CGFloat yValue = [data01Array[index] floatValue];
    return [PNLineChartDataItem dataItemWithY:yValue];
};

NSArray *data02Array = @[@180.1, @80.1, @26.4, @292.2, @106.2, @80.1, @60.1, @126.4, @162.2, @286.2, @80.1, @60.1];

PNLineChartData *data02 = [PNLineChartData new];
data02.dataTitle = @"Alpha";
data02.color = PNFreshGreen;
data02.itemCount = self.lineChart.xLabels.count;
data02.inflexionPointStyle = PNLineChartPointStyleCircle;

data02.getData = ^(NSUInteger index) {
    
    CGFloat yValue = [data02Array[index] floatValue];
    return [PNLineChartDataItem dataItemWithY:yValue];
};

self.lineChart.chartData = @[data01,data02];
[self.lineChart strokeChart]; //画折线,带动画
[self.view addSubview:self.lineChart];

}

//PNChartDelegate委托的实现

  • (void)userClickedOnLinePoint:(CGPoint)point lineIndex:(NSInteger)lineIndex {

    NSLog(@"Click on line %f, %f, line index is %d",point.x, point.y, (int)lineIndex);
    }

  • (void)userClickedOnLineKeyPoint:(CGPoint)point lineIndex:(NSInteger)lineIndex pointIndex:(NSInteger)pointIndex {

    NSLog(@"Click Key on line %f, %f line index is %d and point index is %d",point.x, point.y,(int)lineIndex, (int)pointIndex);

    //在这个位置弹出逻辑
    }

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

  • (IBAction)updateView:(UIButton *)sender {
    [self updateView];
    }
    //更新折线的数据

  • (void)updateView {

    NSArray *data01Array = @[@80.1, @180.1, @126.4, @292.2, @186.2, @80.1, @160.1, @126.4, @262.2, @186.2, @80.1, @160.1];

    PNLineChartData *data01 = [PNLineChartData new];
    data01.dataTitle = @"Alpha";
    data01.color = PNFreshGreen;
    data01.itemCount = self.lineChart.xLabels.count;
    data01.inflexionPointStyle = PNLineChartPointStyleCircle;

    data01.getData = ^(NSUInteger index) {

      CGFloat yValue = [data01Array[index] floatValue];
      return [PNLineChartDataItem dataItemWithY:yValue];
    

    };
    PNLineChartData *data02 = [PNLineChartData new];
    NSArray *data02Array = @[@180.1, @280.1, @226.4, @92.2, @86.2, @280.1, @260.1, @226.4, @62.2, @86.2, @280.1, @60.1];
    data02.dataTitle = @"hzz";
    data02.color = PNBlue;
    data02.itemCount = data02Array.count;
    data02.inflexionPointStyle = PNLineChartPointStyleCircle;

    data02.getData = ^ (NSUInteger index){
    CGFloat yValue = [data02Array[index] floatValue];
    return [PNLineChartDataItem dataItemWithY:yValue];
    };
    [self.lineChart updateChartData:@[data01,data02]];
    [self.lineChart strokeChart];
    }

@end
`

你可能感兴趣的:(PNChart 折线图)