Kevin

Merge pull request #130 from MrWooJ/feature-ScatterGraph

Feature Scatter Graph
... ... @@ -17,3 +17,4 @@
#import "PNCircleChart.h"
#import "PNChartDelegate.h"
#import "PNPieChart.h"
#import "PNScatterChart.h"
... ...
//
// PNScatterChart.h
// PNChartDemo
//
// Created by Alireza Arabi on 12/4/14.
// Copyright (c) 2014 kevinzhow. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "PNChartDelegate.h"
#import "PNScatterChartData.h"
#import "PNScatterChartDataItem.h"
@interface PNScatterChart : UIView
@property (nonatomic, retain) id<PNChartDelegate> delegate;
/** Array of `ScatterChartData` objects, one for each line. */
@property (nonatomic) NSArray *chartData;
/** Controls whether to show the coordinate axis. Default is NO. */
@property (nonatomic, getter = isShowCoordinateAxis) BOOL showCoordinateAxis;
@property (nonatomic) UIColor *axisColor;
@property (nonatomic) CGFloat axisWidth;
/** String formatter for float values in y-axis labels. If not set, defaults to @"%1.f" */
@property (nonatomic, strong) NSString *yLabelFormat;
/** Default is true. */
@property (nonatomic) BOOL showLabel;
/** Default is 18-point Avenir Medium. */
@property (nonatomic) UIFont *descriptionTextFont;
/** Default is white. */
@property (nonatomic) UIColor *descriptionTextColor;
/** Default is black, with an alpha of 0.4. */
@property (nonatomic) UIColor *descriptionTextShadowColor;
/** Default is CGSizeMake(0, 1). */
@property (nonatomic) CGSize descriptionTextShadowOffset;
/** Default is 1.0. */
@property (nonatomic) NSTimeInterval duration;
@property (nonatomic) CGFloat AxisX_minValue;
@property (nonatomic) CGFloat AxisX_maxValue;
@property (nonatomic) CGFloat AxisY_minValue;
@property (nonatomic) CGFloat AxisY_maxValue;
- (void) setAxisXWithMinimumValue:(CGFloat)minVal andMaxValue:(CGFloat)maxVal toTicks:(int)numberOfTicks;
- (void) setAxisYWithMinimumValue:(CGFloat)minVal andMaxValue:(CGFloat)maxVal toTicks:(int)numberOfTicks;
- (void) setup;
- (void) drawLineFromPoint : (CGPoint) startPoint ToPoint : (CGPoint) endPoint WithLineWith : (CGFloat) lineWidth AndWithColor : (UIColor*) color;
/**
* Update Chart Value
*/
- (void)updateChartData:(NSArray *)data;
@end
... ...
This diff is collapsed. Click to expand it.
//
// PNScatterChartData.h
// PNChartDemo
//
// Created by Alireza Arabi on 12/4/14.
// Copyright (c) 2014 kevinzhow. All rights reserved.
//
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSUInteger, PNScatterChartPointStyle) {
PNScatterChartPointStyleCircle = 0,
PNScatterChartPointStyleSquare = 1,
};
@class PNScatterChartDataItem;
typedef PNScatterChartDataItem *(^LCScatterChartDataGetter)(NSUInteger item);
@interface PNScatterChartData : NSObject
@property (strong) UIColor *fillColor;
@property (strong) UIColor *strokeColor;
@property NSUInteger itemCount;
@property (copy) LCScatterChartDataGetter getData;
@property (nonatomic, assign) PNScatterChartPointStyle inflexionPointStyle;
/**
* If PNLineChartPointStyle is circle, this returns the circle's diameter.
* If PNLineChartPointStyle is square, each point is a square with each side equal in length to this value.
*/
@property (nonatomic, assign) CGFloat size;
@end
... ...
//
// PNScatterChartData.m
// PNChartDemo
//
// Created by Alireza Arabi on 12/4/14.
// Copyright (c) 2014 kevinzhow. All rights reserved.
//
#import "PNScatterChartData.h"
@implementation PNScatterChartData
- (id)init
{
self = [super init];
if (self) {
[self setDefaultValues];
}
return self;
}
- (void)setDefaultValues
{
_inflexionPointStyle = PNScatterChartPointStyleCircle;
_fillColor = [UIColor grayColor];
_strokeColor = [UIColor clearColor];
_size = 3 ;
}
@end
... ...
//
// PNScatterChartDataItem.h
// PNChartDemo
//
// Created by Alireza Arabi on 12/4/14.
// Copyright (c) 2014 kevinzhow. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface PNScatterChartDataItem : NSObject
+ (PNScatterChartDataItem *)dataItemWithX:(CGFloat)x AndWithY:(CGFloat)y;
@property (readonly) CGFloat x; // should be within the x range
@property (readonly) CGFloat y; // should be within the y range
@end
... ...
//
// PNScatterChartDataItem.m
// PNChartDemo
//
// Created by Alireza Arabi on 12/4/14.
// Copyright (c) 2014 kevinzhow. All rights reserved.
//
#import "PNScatterChartDataItem.h"
@interface PNScatterChartDataItem ()
- (id)initWithX:(CGFloat)x AndWithY:(CGFloat)y;
@property (readwrite) CGFloat x; // should be within the x range
@property (readwrite) CGFloat y; // should be within the y range
@end
@implementation PNScatterChartDataItem
+ (PNScatterChartDataItem *)dataItemWithX:(CGFloat)x AndWithY:(CGFloat)y
{
return [[PNScatterChartDataItem alloc] initWithX:x AndWithY:y];
}
- (id)initWithX:(CGFloat)x AndWithY:(CGFloat)y
{
if ((self = [super init])) {
self.x = x;
self.y = y;
}
return self;
}
@end
... ...
... ... @@ -142,6 +142,24 @@
<segue destination="Tha-Wr-sPW" kind="push" identifier="pieChart" id="pvQ-oy-a9a"/>
</connections>
</tableViewCell>
<tableViewCell contentMode="scaleToFill" selectionStyle="blue" accessoryType="disclosureIndicator" indentationLevel="1" indentationWidth="0.0" textLabel="YOU-SK-mQU" style="IBUITableViewCellStyleDefault" id="JJR-oU-C7n">
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="JJR-oU-C7n" id="iJk-3W-tcy">
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="ScatterChart" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="YOU-SK-mQU">
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="18"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
</tableViewCellContentView>
<connections>
<segue destination="Tha-Wr-sPW" kind="push" identifier="scatterChart" id="V7s-JV-4Nx"/>
</connections>
</tableViewCell>
</cells>
</tableViewSection>
</sections>
... ... @@ -174,6 +192,6 @@
<simulatedScreenMetrics key="destination" type="retina4"/>
</simulatedMetricsContainer>
<inferredMetricsTieBreakers>
<segue reference="pvQ-oy-a9a"/>
<segue reference="V7s-JV-4Nx"/>
</inferredMetricsTieBreakers>
</document>
... ...
... ... @@ -16,6 +16,7 @@
@property (nonatomic) PNBarChart * barChart;
@property (nonatomic) PNCircleChart * circleChart;
@property (nonatomic) PNPieChart *pieChart;
@property (nonatomic) PNScatterChart *scatterChart;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
... ...
... ... @@ -7,6 +7,7 @@
//
#import "PCChartViewController.h"
#define ARC4RANDOM_MAX 0x100000000
@implementation PCChartViewController
... ... @@ -120,6 +121,41 @@
[self.view addSubview:self.pieChart];
self.changeValueButton.hidden = YES;
}
else if ([self.title isEqualToString:@"Scatter Chart"])
{
self.titleLabel.text = @"Scatter Chart";
self.scatterChart = [[PNScatterChart alloc] initWithFrame:CGRectMake(SCREEN_WIDTH /6.0 - 30, 135, 280, 200)];
[self.scatterChart setAxisXWithMinimumValue:20 andMaxValue:100 toTicks:6];
[self.scatterChart setAxisYWithMinimumValue:30 andMaxValue:50 toTicks:5];
NSArray * data01Array = [self randomSetOfObjects];
PNScatterChartData *data01 = [PNScatterChartData new];
data01.strokeColor = PNGreen;
data01.fillColor = PNFreshGreen;
data01.size = 2;
data01.itemCount = [[data01Array objectAtIndex:0] count];
data01.inflexionPointStyle = PNScatterChartPointStyleCircle;
__block NSMutableArray *XAr1 = [NSMutableArray arrayWithArray:[data01Array objectAtIndex:0]];
__block NSMutableArray *YAr1 = [NSMutableArray arrayWithArray:[data01Array objectAtIndex:1]];
data01.getData = ^(NSUInteger index) {
CGFloat xValue = [[XAr1 objectAtIndex:index] floatValue];
CGFloat yValue = [[YAr1 objectAtIndex:index] floatValue];
return [PNScatterChartDataItem dataItemWithX:xValue AndWithY:yValue];
};
[self.scatterChart setup];
self.scatterChart.chartData = @[data01];
/***
this is for drawing line to compare
CGPoint start = CGPointMake(20, 35);
CGPoint end = CGPointMake(80, 45);
[self.scatterChart drawLineFromPoint:start ToPoint:end WithLineWith:2 AndWithColor:PNBlack];
***/
self.scatterChart.delegate = self;
self.changeValueButton.hidden = YES;
[self.view addSubview:self.scatterChart];
}
}
... ... @@ -171,6 +207,10 @@
{
[self.circleChart updateChartByCurrent:@(arc4random() % 100)];
}
else if ([self.title isEqualToString:@"Scatter Chart"])
{
// will be code soon.
}
}
... ... @@ -195,4 +235,19 @@
[bar.layer addAnimation:animation forKey:@"Float"];
}
/* this function is used only for creating random points */
- (NSArray *) randomSetOfObjects{
NSMutableArray *array = [NSMutableArray array];
NSString *LabelFormat = @"%1.f";
NSMutableArray *XAr = [NSMutableArray array];
NSMutableArray *YAr = [NSMutableArray array];
for (int i = 0; i < 25 ; i++) {
[XAr addObject:[NSString stringWithFormat:LabelFormat,(((double)arc4random() / ARC4RANDOM_MAX) * (self.scatterChart.AxisX_maxValue - self.scatterChart.AxisX_minValue) + self.scatterChart.AxisX_minValue)]];
[YAr addObject:[NSString stringWithFormat:LabelFormat,(((double)arc4random() / ARC4RANDOM_MAX) * (self.scatterChart.AxisY_maxValue - self.scatterChart.AxisY_minValue) + self.scatterChart.AxisY_minValue)]];
}
[array addObject:XAr];
[array addObject:YAr];
return (NSArray*) array;
}
@end
... ...
... ... @@ -40,9 +40,12 @@
//Add pie chart
viewController.title = @"Pie Chart";
} else if ([segue.identifier isEqualToString:@"scatterChart"])
{
//Add scatter chart
viewController.title = @"Scatter Chart";
}
}
@end
... ...