Merge branch 'master' of github.com:kevinzhow/PNChart
Showing
11 changed files
with
269 additions
and
3 deletions
PNChart/PNScatterChart.h
0 → 100644
| 1 | +// | ||
| 2 | +// PNScatterChart.h | ||
| 3 | +// PNChartDemo | ||
| 4 | +// | ||
| 5 | +// Created by Alireza Arabi on 12/4/14. | ||
| 6 | +// Copyright (c) 2014 kevinzhow. All rights reserved. | ||
| 7 | +// | ||
| 8 | + | ||
| 9 | +#import <UIKit/UIKit.h> | ||
| 10 | +#import <QuartzCore/QuartzCore.h> | ||
| 11 | +#import "PNChartDelegate.h" | ||
| 12 | +#import "PNScatterChartData.h" | ||
| 13 | +#import "PNScatterChartDataItem.h" | ||
| 14 | + | ||
| 15 | +@interface PNScatterChart : UIView | ||
| 16 | + | ||
| 17 | +@property (nonatomic, retain) id<PNChartDelegate> delegate; | ||
| 18 | + | ||
| 19 | +/** Array of `ScatterChartData` objects, one for each line. */ | ||
| 20 | +@property (nonatomic) NSArray *chartData; | ||
| 21 | + | ||
| 22 | +/** Controls whether to show the coordinate axis. Default is NO. */ | ||
| 23 | +@property (nonatomic, getter = isShowCoordinateAxis) BOOL showCoordinateAxis; | ||
| 24 | +@property (nonatomic) UIColor *axisColor; | ||
| 25 | +@property (nonatomic) CGFloat axisWidth; | ||
| 26 | + | ||
| 27 | +/** String formatter for float values in y-axis labels. If not set, defaults to @"%1.f" */ | ||
| 28 | +@property (nonatomic, strong) NSString *yLabelFormat; | ||
| 29 | + | ||
| 30 | +/** Default is true. */ | ||
| 31 | +@property (nonatomic) BOOL showLabel; | ||
| 32 | + | ||
| 33 | +/** Default is 18-point Avenir Medium. */ | ||
| 34 | +@property (nonatomic) UIFont *descriptionTextFont; | ||
| 35 | + | ||
| 36 | +/** Default is white. */ | ||
| 37 | +@property (nonatomic) UIColor *descriptionTextColor; | ||
| 38 | + | ||
| 39 | +/** Default is black, with an alpha of 0.4. */ | ||
| 40 | +@property (nonatomic) UIColor *descriptionTextShadowColor; | ||
| 41 | + | ||
| 42 | +/** Default is CGSizeMake(0, 1). */ | ||
| 43 | +@property (nonatomic) CGSize descriptionTextShadowOffset; | ||
| 44 | + | ||
| 45 | +/** Default is 1.0. */ | ||
| 46 | +@property (nonatomic) NSTimeInterval duration; | ||
| 47 | + | ||
| 48 | +@property (nonatomic) CGFloat AxisX_minValue; | ||
| 49 | +@property (nonatomic) CGFloat AxisX_maxValue; | ||
| 50 | + | ||
| 51 | +@property (nonatomic) CGFloat AxisY_minValue; | ||
| 52 | +@property (nonatomic) CGFloat AxisY_maxValue; | ||
| 53 | + | ||
| 54 | +- (void) setAxisXWithMinimumValue:(CGFloat)minVal andMaxValue:(CGFloat)maxVal toTicks:(int)numberOfTicks; | ||
| 55 | +- (void) setAxisYWithMinimumValue:(CGFloat)minVal andMaxValue:(CGFloat)maxVal toTicks:(int)numberOfTicks; | ||
| 56 | +- (void) setup; | ||
| 57 | +- (void) drawLineFromPoint : (CGPoint) startPoint ToPoint : (CGPoint) endPoint WithLineWith : (CGFloat) lineWidth AndWithColor : (UIColor*) color; | ||
| 58 | + | ||
| 59 | +/** | ||
| 60 | + * Update Chart Value | ||
| 61 | + */ | ||
| 62 | + | ||
| 63 | +- (void)updateChartData:(NSArray *)data; | ||
| 64 | + | ||
| 65 | +@end |
PNChart/PNScatterChart.m
0 → 100644
This diff is collapsed. Click to expand it.
PNChart/PNScatterChartData.h
0 → 100644
| 1 | +// | ||
| 2 | +// PNScatterChartData.h | ||
| 3 | +// PNChartDemo | ||
| 4 | +// | ||
| 5 | +// Created by Alireza Arabi on 12/4/14. | ||
| 6 | +// Copyright (c) 2014 kevinzhow. All rights reserved. | ||
| 7 | +// | ||
| 8 | + | ||
| 9 | +#import <Foundation/Foundation.h> | ||
| 10 | + | ||
| 11 | +typedef NS_ENUM(NSUInteger, PNScatterChartPointStyle) { | ||
| 12 | + PNScatterChartPointStyleCircle = 0, | ||
| 13 | + PNScatterChartPointStyleSquare = 1, | ||
| 14 | +}; | ||
| 15 | + | ||
| 16 | +@class PNScatterChartDataItem; | ||
| 17 | + | ||
| 18 | +typedef PNScatterChartDataItem *(^LCScatterChartDataGetter)(NSUInteger item); | ||
| 19 | + | ||
| 20 | +@interface PNScatterChartData : NSObject | ||
| 21 | + | ||
| 22 | +@property (strong) UIColor *fillColor; | ||
| 23 | +@property (strong) UIColor *strokeColor; | ||
| 24 | + | ||
| 25 | +@property NSUInteger itemCount; | ||
| 26 | +@property (copy) LCScatterChartDataGetter getData; | ||
| 27 | + | ||
| 28 | +@property (nonatomic, assign) PNScatterChartPointStyle inflexionPointStyle; | ||
| 29 | + | ||
| 30 | +/** | ||
| 31 | + * If PNLineChartPointStyle is circle, this returns the circle's diameter. | ||
| 32 | + * If PNLineChartPointStyle is square, each point is a square with each side equal in length to this value. | ||
| 33 | + */ | ||
| 34 | +@property (nonatomic, assign) CGFloat size; | ||
| 35 | + | ||
| 36 | + | ||
| 37 | +@end |
PNChart/PNScatterChartData.m
0 → 100644
| 1 | +// | ||
| 2 | +// PNScatterChartData.m | ||
| 3 | +// PNChartDemo | ||
| 4 | +// | ||
| 5 | +// Created by Alireza Arabi on 12/4/14. | ||
| 6 | +// Copyright (c) 2014 kevinzhow. All rights reserved. | ||
| 7 | +// | ||
| 8 | + | ||
| 9 | +#import "PNScatterChartData.h" | ||
| 10 | + | ||
| 11 | +@implementation PNScatterChartData | ||
| 12 | + | ||
| 13 | +- (id)init | ||
| 14 | +{ | ||
| 15 | + self = [super init]; | ||
| 16 | + if (self) { | ||
| 17 | + [self setDefaultValues]; | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + return self; | ||
| 21 | +} | ||
| 22 | + | ||
| 23 | +- (void)setDefaultValues | ||
| 24 | +{ | ||
| 25 | + _inflexionPointStyle = PNScatterChartPointStyleCircle; | ||
| 26 | + _fillColor = [UIColor grayColor]; | ||
| 27 | + _strokeColor = [UIColor clearColor]; | ||
| 28 | + _size = 3 ; | ||
| 29 | +} | ||
| 30 | + | ||
| 31 | +@end |
PNChart/PNScatterChartDataItem.h
0 → 100644
| 1 | +// | ||
| 2 | +// PNScatterChartDataItem.h | ||
| 3 | +// PNChartDemo | ||
| 4 | +// | ||
| 5 | +// Created by Alireza Arabi on 12/4/14. | ||
| 6 | +// Copyright (c) 2014 kevinzhow. All rights reserved. | ||
| 7 | +// | ||
| 8 | + | ||
| 9 | +#import <Foundation/Foundation.h> | ||
| 10 | + | ||
| 11 | +@interface PNScatterChartDataItem : NSObject | ||
| 12 | + | ||
| 13 | ++ (PNScatterChartDataItem *)dataItemWithX:(CGFloat)x AndWithY:(CGFloat)y; | ||
| 14 | + | ||
| 15 | +@property (readonly) CGFloat x; // should be within the x range | ||
| 16 | +@property (readonly) CGFloat y; // should be within the y range | ||
| 17 | + | ||
| 18 | +@end |
PNChart/PNScatterChartDataItem.m
0 → 100644
| 1 | +// | ||
| 2 | +// PNScatterChartDataItem.m | ||
| 3 | +// PNChartDemo | ||
| 4 | +// | ||
| 5 | +// Created by Alireza Arabi on 12/4/14. | ||
| 6 | +// Copyright (c) 2014 kevinzhow. All rights reserved. | ||
| 7 | +// | ||
| 8 | + | ||
| 9 | +#import "PNScatterChartDataItem.h" | ||
| 10 | + | ||
| 11 | +@interface PNScatterChartDataItem () | ||
| 12 | + | ||
| 13 | +- (id)initWithX:(CGFloat)x AndWithY:(CGFloat)y; | ||
| 14 | + | ||
| 15 | +@property (readwrite) CGFloat x; // should be within the x range | ||
| 16 | +@property (readwrite) CGFloat y; // should be within the y range | ||
| 17 | + | ||
| 18 | +@end | ||
| 19 | + | ||
| 20 | +@implementation PNScatterChartDataItem | ||
| 21 | + | ||
| 22 | ++ (PNScatterChartDataItem *)dataItemWithX:(CGFloat)x AndWithY:(CGFloat)y | ||
| 23 | +{ | ||
| 24 | + return [[PNScatterChartDataItem alloc] initWithX:x AndWithY:y]; | ||
| 25 | +} | ||
| 26 | + | ||
| 27 | +- (id)initWithX:(CGFloat)x AndWithY:(CGFloat)y | ||
| 28 | +{ | ||
| 29 | + if ((self = [super init])) { | ||
| 30 | + self.x = x; | ||
| 31 | + self.y = y; | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + return self; | ||
| 35 | +} | ||
| 36 | + | ||
| 37 | +@end |
| @@ -142,6 +142,24 @@ | @@ -142,6 +142,24 @@ | ||
| 142 | <segue destination="Tha-Wr-sPW" kind="push" identifier="pieChart" id="pvQ-oy-a9a"/> | 142 | <segue destination="Tha-Wr-sPW" kind="push" identifier="pieChart" id="pvQ-oy-a9a"/> |
| 143 | </connections> | 143 | </connections> |
| 144 | </tableViewCell> | 144 | </tableViewCell> |
| 145 | + <tableViewCell contentMode="scaleToFill" selectionStyle="blue" accessoryType="disclosureIndicator" indentationLevel="1" indentationWidth="0.0" textLabel="YOU-SK-mQU" style="IBUITableViewCellStyleDefault" id="JJR-oU-C7n"> | ||
| 146 | + <rect key="frame" x="0.0" y="0.0" width="320" height="44"/> | ||
| 147 | + <autoresizingMask key="autoresizingMask"/> | ||
| 148 | + <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="JJR-oU-C7n" id="iJk-3W-tcy"> | ||
| 149 | + <autoresizingMask key="autoresizingMask"/> | ||
| 150 | + <subviews> | ||
| 151 | + <label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="ScatterChart" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="YOU-SK-mQU"> | ||
| 152 | + <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> | ||
| 153 | + <fontDescription key="fontDescription" type="system" pointSize="18"/> | ||
| 154 | + <color key="textColor" cocoaTouchSystemColor="darkTextColor"/> | ||
| 155 | + <nil key="highlightedColor"/> | ||
| 156 | + </label> | ||
| 157 | + </subviews> | ||
| 158 | + </tableViewCellContentView> | ||
| 159 | + <connections> | ||
| 160 | + <segue destination="Tha-Wr-sPW" kind="push" identifier="scatterChart" id="V7s-JV-4Nx"/> | ||
| 161 | + </connections> | ||
| 162 | + </tableViewCell> | ||
| 145 | </cells> | 163 | </cells> |
| 146 | </tableViewSection> | 164 | </tableViewSection> |
| 147 | </sections> | 165 | </sections> |
| @@ -174,6 +192,6 @@ | @@ -174,6 +192,6 @@ | ||
| 174 | <simulatedScreenMetrics key="destination" type="retina4"/> | 192 | <simulatedScreenMetrics key="destination" type="retina4"/> |
| 175 | </simulatedMetricsContainer> | 193 | </simulatedMetricsContainer> |
| 176 | <inferredMetricsTieBreakers> | 194 | <inferredMetricsTieBreakers> |
| 177 | - <segue reference="pvQ-oy-a9a"/> | 195 | + <segue reference="V7s-JV-4Nx"/> |
| 178 | </inferredMetricsTieBreakers> | 196 | </inferredMetricsTieBreakers> |
| 179 | </document> | 197 | </document> |
| @@ -16,6 +16,7 @@ | @@ -16,6 +16,7 @@ | ||
| 16 | @property (nonatomic) PNBarChart * barChart; | 16 | @property (nonatomic) PNBarChart * barChart; |
| 17 | @property (nonatomic) PNCircleChart * circleChart; | 17 | @property (nonatomic) PNCircleChart * circleChart; |
| 18 | @property (nonatomic) PNPieChart *pieChart; | 18 | @property (nonatomic) PNPieChart *pieChart; |
| 19 | +@property (nonatomic) PNScatterChart *scatterChart; | ||
| 19 | 20 | ||
| 20 | @property (weak, nonatomic) IBOutlet UILabel *titleLabel; | 21 | @property (weak, nonatomic) IBOutlet UILabel *titleLabel; |
| 21 | 22 |
| @@ -7,6 +7,7 @@ | @@ -7,6 +7,7 @@ | ||
| 7 | // | 7 | // |
| 8 | 8 | ||
| 9 | #import "PCChartViewController.h" | 9 | #import "PCChartViewController.h" |
| 10 | +#define ARC4RANDOM_MAX 0x100000000 | ||
| 10 | 11 | ||
| 11 | @implementation PCChartViewController | 12 | @implementation PCChartViewController |
| 12 | 13 | ||
| @@ -120,6 +121,41 @@ | @@ -120,6 +121,41 @@ | ||
| 120 | [self.view addSubview:self.pieChart]; | 121 | [self.view addSubview:self.pieChart]; |
| 121 | self.changeValueButton.hidden = YES; | 122 | self.changeValueButton.hidden = YES; |
| 122 | } | 123 | } |
| 124 | + else if ([self.title isEqualToString:@"Scatter Chart"]) | ||
| 125 | + { | ||
| 126 | + self.titleLabel.text = @"Scatter Chart"; | ||
| 127 | + | ||
| 128 | + self.scatterChart = [[PNScatterChart alloc] initWithFrame:CGRectMake(SCREEN_WIDTH /6.0 - 30, 135, 280, 200)]; | ||
| 129 | + [self.scatterChart setAxisXWithMinimumValue:20 andMaxValue:100 toTicks:6]; | ||
| 130 | + [self.scatterChart setAxisYWithMinimumValue:30 andMaxValue:50 toTicks:5]; | ||
| 131 | + | ||
| 132 | + NSArray * data01Array = [self randomSetOfObjects]; | ||
| 133 | + PNScatterChartData *data01 = [PNScatterChartData new]; | ||
| 134 | + data01.strokeColor = PNGreen; | ||
| 135 | + data01.fillColor = PNFreshGreen; | ||
| 136 | + data01.size = 2; | ||
| 137 | + data01.itemCount = [[data01Array objectAtIndex:0] count]; | ||
| 138 | + data01.inflexionPointStyle = PNScatterChartPointStyleCircle; | ||
| 139 | + __block NSMutableArray *XAr1 = [NSMutableArray arrayWithArray:[data01Array objectAtIndex:0]]; | ||
| 140 | + __block NSMutableArray *YAr1 = [NSMutableArray arrayWithArray:[data01Array objectAtIndex:1]]; | ||
| 141 | + data01.getData = ^(NSUInteger index) { | ||
| 142 | + CGFloat xValue = [[XAr1 objectAtIndex:index] floatValue]; | ||
| 143 | + CGFloat yValue = [[YAr1 objectAtIndex:index] floatValue]; | ||
| 144 | + return [PNScatterChartDataItem dataItemWithX:xValue AndWithY:yValue]; | ||
| 145 | + }; | ||
| 146 | + | ||
| 147 | + [self.scatterChart setup]; | ||
| 148 | + self.scatterChart.chartData = @[data01]; | ||
| 149 | +/*** | ||
| 150 | + this is for drawing line to compare | ||
| 151 | + CGPoint start = CGPointMake(20, 35); | ||
| 152 | + CGPoint end = CGPointMake(80, 45); | ||
| 153 | + [self.scatterChart drawLineFromPoint:start ToPoint:end WithLineWith:2 AndWithColor:PNBlack]; | ||
| 154 | +***/ | ||
| 155 | + self.scatterChart.delegate = self; | ||
| 156 | + self.changeValueButton.hidden = YES; | ||
| 157 | + [self.view addSubview:self.scatterChart]; | ||
| 158 | + } | ||
| 123 | } | 159 | } |
| 124 | 160 | ||
| 125 | 161 | ||
| @@ -171,6 +207,10 @@ | @@ -171,6 +207,10 @@ | ||
| 171 | { | 207 | { |
| 172 | [self.circleChart updateChartByCurrent:@(arc4random() % 100)]; | 208 | [self.circleChart updateChartByCurrent:@(arc4random() % 100)]; |
| 173 | } | 209 | } |
| 210 | + else if ([self.title isEqualToString:@"Scatter Chart"]) | ||
| 211 | + { | ||
| 212 | + // will be code soon. | ||
| 213 | + } | ||
| 174 | 214 | ||
| 175 | } | 215 | } |
| 176 | 216 | ||
| @@ -195,4 +235,19 @@ | @@ -195,4 +235,19 @@ | ||
| 195 | [bar.layer addAnimation:animation forKey:@"Float"]; | 235 | [bar.layer addAnimation:animation forKey:@"Float"]; |
| 196 | } | 236 | } |
| 197 | 237 | ||
| 238 | +/* this function is used only for creating random points */ | ||
| 239 | +- (NSArray *) randomSetOfObjects{ | ||
| 240 | + NSMutableArray *array = [NSMutableArray array]; | ||
| 241 | + NSString *LabelFormat = @"%1.f"; | ||
| 242 | + NSMutableArray *XAr = [NSMutableArray array]; | ||
| 243 | + NSMutableArray *YAr = [NSMutableArray array]; | ||
| 244 | + for (int i = 0; i < 25 ; i++) { | ||
| 245 | + [XAr addObject:[NSString stringWithFormat:LabelFormat,(((double)arc4random() / ARC4RANDOM_MAX) * (self.scatterChart.AxisX_maxValue - self.scatterChart.AxisX_minValue) + self.scatterChart.AxisX_minValue)]]; | ||
| 246 | + [YAr addObject:[NSString stringWithFormat:LabelFormat,(((double)arc4random() / ARC4RANDOM_MAX) * (self.scatterChart.AxisY_maxValue - self.scatterChart.AxisY_minValue) + self.scatterChart.AxisY_minValue)]]; | ||
| 247 | + } | ||
| 248 | + [array addObject:XAr]; | ||
| 249 | + [array addObject:YAr]; | ||
| 250 | + return (NSArray*) array; | ||
| 251 | +} | ||
| 252 | + | ||
| 198 | @end | 253 | @end |
| @@ -40,9 +40,12 @@ | @@ -40,9 +40,12 @@ | ||
| 40 | //Add pie chart | 40 | //Add pie chart |
| 41 | 41 | ||
| 42 | viewController.title = @"Pie Chart"; | 42 | viewController.title = @"Pie Chart"; |
| 43 | + } else if ([segue.identifier isEqualToString:@"scatterChart"]) | ||
| 44 | + { | ||
| 45 | + //Add scatter chart | ||
| 46 | + | ||
| 47 | + viewController.title = @"Scatter Chart"; | ||
| 43 | } | 48 | } |
| 44 | } | 49 | } |
| 45 | 50 | ||
| 46 | - | ||
| 47 | - | ||
| 48 | @end | 51 | @end |
-
Please register or login to post a comment