kevinzhow

Merge branch 'master' of github.com:kevinzhow/PNChart

... ... @@ -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
... ...
//
// PNScatterChart.m
// PNChartDemo
//
// Created by Alireza Arabi on 12/4/14.
// Copyright (c) 2014 kevinzhow. All rights reserved.
//
#import "PNScatterChart.h"
#import "PNColor.h"
#import "PNChartLabel.h"
#import "PNScatterChartData.h"
#import "PNScatterChartDataItem.h"
@interface PNScatterChart ()
@property (nonatomic, weak) CAShapeLayer *pathLayer;
@property (nonatomic, weak) NSMutableArray *verticalLineLayer;
@property (nonatomic, weak) NSMutableArray *horizentalLinepathLayer;
@property (nonatomic) CGPoint startPoint;
@property (nonatomic) CGPoint startPointVectorX;
@property (nonatomic) CGPoint endPointVecotrX;
@property (nonatomic) CGPoint startPointVectorY;
@property (nonatomic) CGPoint endPointVecotrY;
@property (nonatomic) CGFloat vectorX_Steps;
@property (nonatomic) CGFloat vectorY_Steps;
@property (nonatomic) CGFloat vectorX_Size;
@property (nonatomic) CGFloat vectorY_Size;
@property (nonatomic) NSMutableArray *axisX_labels;
@property (nonatomic) NSMutableArray *axisY_labels;
@property (nonatomic) int AxisX_partNumber ;
@property (nonatomic) int AxisY_partNumber ;
@property (nonatomic) CGFloat AxisX_step ;
@property (nonatomic) CGFloat AxisY_step ;
@property (nonatomic) CGFloat AxisX_Margin;
@property (nonatomic) CGFloat AxisY_Margin;
@property (nonatomic) BOOL isForUpdate;
- (void)setDefaultValues;
@end
@implementation PNScatterChart
#pragma mark initialization
- (id)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
[self setDefaultValues];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setDefaultValues];
}
return self;
}
- (void) setup
{
[self vectorXSetup];
[self vectorYSetup];
}
- (void)setDefaultValues
{
// Initialization code
self.backgroundColor = [UIColor whiteColor];
self.clipsToBounds = YES;
_showLabel = YES;
_isForUpdate = NO;
self.userInteractionEnabled = YES;
// Coordinate Axis Default Values
_showCoordinateAxis = YES;
_axisColor = [UIColor colorWithRed:0.4f green:0.4f blue:0.4f alpha:1.f];
_axisWidth = 1.f;
// Initialization code
_AxisX_Margin = 30 ;
_AxisY_Margin = 30 ;
// self.frame = CGRectMake((SCREEN_WIDTH - self.frame.size.width) / 2, 200, self.frame.size.width, self.frame.size.height) ;
self.backgroundColor = [UIColor clearColor];
_startPoint.y = self.frame.size.height - self.AxisY_Margin ;
_startPoint.x = self.AxisX_Margin ;
_axisX_labels = [NSMutableArray array];
_axisY_labels = [NSMutableArray array];
_descriptionTextColor = [UIColor blackColor];
_descriptionTextFont = [UIFont fontWithName:@"Avenir-Medium" size:9.0];
_descriptionTextShadowColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
_descriptionTextShadowOffset = CGSizeMake(0, 1);
_duration = 1.0;
}
#pragma mark calculating axis
- (void) setAxisXWithMinimumValue:(CGFloat)minVal andMaxValue:(CGFloat)maxVal toTicks:(int)numberOfTicks
{
_AxisX_minValue = minVal ;
_AxisX_maxValue = maxVal ;
_AxisX_partNumber = numberOfTicks - 1;
_AxisX_step = (float)((maxVal - minVal)/_AxisX_partNumber);
NSString *LabelFormat = self.yLabelFormat ? : @"%1.f";
CGFloat tempValue = minVal ;
UILabel *label = [[UILabel alloc] init];
label.text = [NSString stringWithFormat:LabelFormat,minVal] ;
[_axisX_labels addObject:label];
for (int i = 0 ; i < _AxisX_partNumber; i++) {
tempValue = tempValue + _AxisX_step;
UILabel *tempLabel = [[UILabel alloc] init];
tempLabel.text = [NSString stringWithFormat:LabelFormat,tempValue] ;
[_axisX_labels addObject:tempLabel];
}
}
- (void) setAxisYWithMinimumValue:(CGFloat)minVal andMaxValue:(CGFloat)maxVal toTicks:(int)numberOfTicks
{
_AxisY_minValue = minVal ;
_AxisY_maxValue = maxVal ;
_AxisY_partNumber = numberOfTicks - 1;
_AxisY_step = (float)((maxVal - minVal)/_AxisY_partNumber);
NSString *LabelFormat = self.yLabelFormat ? : @"%1.f";
CGFloat tempValue = minVal ;
UILabel *label = [[UILabel alloc] init];
label.text = [NSString stringWithFormat:LabelFormat,minVal] ;
[_axisY_labels addObject:label];
for (int i = 0 ; i < _AxisY_partNumber; i++) {
tempValue = tempValue + _AxisY_step;
UILabel *tempLabel = [[UILabel alloc] init];
tempLabel.text = [NSString stringWithFormat:LabelFormat,tempValue] ;
[_axisY_labels addObject:tempLabel];
}
}
- (void) vectorXSetup
{
_AxisX_partNumber += 1;
_vectorX_Size = self.frame.size.width - (_AxisX_Margin) - 15 ;
_vectorX_Steps = (_vectorX_Size) / (_AxisX_partNumber) ;
_endPointVecotrX = CGPointMake(_startPoint.x + _vectorX_Size, _startPoint.y) ;
_startPointVectorX = _startPoint ;
}
- (void) vectorYSetup
{
_AxisY_partNumber += 1;
_vectorY_Size = self.frame.size.height - (_AxisY_Margin) - 15;
_vectorY_Steps = (_vectorY_Size) / (_AxisY_partNumber);
_endPointVecotrY = CGPointMake(_startPoint.x, _startPoint.y - _vectorY_Size) ;
_startPointVectorY = _startPoint ;
}
- (void) showXLabel : (UILabel *) descriptionLabel InPosition : (CGPoint) point
{
CGRect frame = CGRectMake(point.x, point.y, 30, 10);
descriptionLabel.frame = frame;
descriptionLabel.font = _descriptionTextFont;
descriptionLabel.textColor = _descriptionTextColor;
descriptionLabel.shadowColor = _descriptionTextShadowColor;
descriptionLabel.shadowOffset = _descriptionTextShadowOffset;
descriptionLabel.textAlignment = NSTextAlignmentCenter;
descriptionLabel.backgroundColor = [UIColor clearColor];
[self addSubview:descriptionLabel];
}
- (void)setChartData:(NSArray *)data
{
__block CGFloat yFinilizeValue , xFinilizeValue;
__block CGFloat yValue , xValue;
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
pathAnimation.duration = _duration;
pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pathAnimation.fromValue = @(0.0f);
pathAnimation.toValue = @(1.0f);
pathAnimation.fillMode = kCAFillModeForwards;
self.layer.opacity = 1;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[NSThread sleepForTimeInterval:1];
// update UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
for (PNScatterChartData *chartData in data) {
for (NSUInteger i = 0; i < chartData.itemCount; i++) {
yValue = chartData.getData(i).y;
xValue = chartData.getData(i).x;
if (!(xValue >= _AxisX_minValue && xValue <= _AxisX_maxValue) || !(yValue >= _AxisY_minValue && yValue <= _AxisY_maxValue)) {
NSLog(@"input is not in correct range.");
exit(0);
}
xFinilizeValue = [self mappingIsForAxisX:true WithValue:xValue];
yFinilizeValue = [self mappingIsForAxisX:false WithValue:yValue];
CAShapeLayer *shape = [self drawingPointsForChartData:chartData AndWithX:xFinilizeValue AndWithY:yFinilizeValue];
self.pathLayer = shape ;
[self.layer addSublayer:self.pathLayer];
[self.pathLayer addAnimation:pathAnimation forKey:@"fade"];
}
}
});
});
}
- (CGFloat) mappingIsForAxisX : (BOOL) isForAxisX WithValue : (CGFloat) value{
if (isForAxisX) {
float temp = _startPointVectorX.x + (_vectorX_Steps / 2) ;
CGFloat xPos = temp + (((value - _AxisX_minValue)/_AxisX_step) * _vectorX_Steps) ;
return xPos;
}
else {
float temp = _startPointVectorY.y - (_vectorY_Steps / 2) ;
CGFloat yPos = temp - (((value - _AxisY_minValue) /_AxisY_step) * _vectorY_Steps);
return yPos;
}
return 0;
}
#pragma mark - Update Chart Data
- (void)updateChartData:(NSArray *)data
{
_chartData = data;
// will be work in future.
}
#pragma drawing methods
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
if (_showCoordinateAxis) {
CGContextSetStrokeColorWithColor(context, [_axisColor CGColor]);
CGContextSetLineWidth(context, _axisWidth);
//drawing x vector
CGContextMoveToPoint(context, _startPoint.x, _startPoint.y);
CGContextAddLineToPoint(context, _endPointVecotrX.x, _endPointVecotrX.y);
//drawing y vector
CGContextMoveToPoint(context, _startPoint.x, _startPoint.y);
CGContextAddLineToPoint(context, _endPointVecotrY.x, _endPointVecotrY.y);
//drawing x arrow vector
CGContextMoveToPoint(context, _endPointVecotrX.x, _endPointVecotrX.y);
CGContextAddLineToPoint(context, _endPointVecotrX.x - 5, _endPointVecotrX.y + 3);
CGContextMoveToPoint(context, _endPointVecotrX.x, _endPointVecotrX.y);
CGContextAddLineToPoint(context, _endPointVecotrX.x - 5, _endPointVecotrX.y - 3);
//drawing y arrow vector
CGContextMoveToPoint(context, _endPointVecotrY.x, _endPointVecotrY.y);
CGContextAddLineToPoint(context, _endPointVecotrY.x - 3, _endPointVecotrY.y + 5);
CGContextMoveToPoint(context, _endPointVecotrY.x, _endPointVecotrY.y);
CGContextAddLineToPoint(context, _endPointVecotrY.x + 3, _endPointVecotrY.y + 5);
}
if (_showLabel) {
NSString *str;
//drawing x steps vector and putting axis x labels
float temp = _startPointVectorX.x + (_vectorX_Steps / 2) ;
for (int i = 0; i < _axisX_labels.count; i++) {
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(temp, _startPointVectorX.y - 2)];
[path addLineToPoint:CGPointMake(temp, _startPointVectorX.y + 3)];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = [path CGPath];
shapeLayer.strokeColor = [_axisColor CGColor];
shapeLayer.lineWidth = _axisWidth;
shapeLayer.fillColor = [_axisColor CGColor];
[self.horizentalLinepathLayer addObject:shapeLayer];
[self.layer addSublayer:shapeLayer];
UILabel *lb = [_axisX_labels objectAtIndex:i] ;
str = lb.text;
[self showXLabel:lb InPosition:CGPointMake(temp - 15, _startPointVectorX.y + 10 )];
temp = temp + _vectorX_Steps ;
}
//drawing y steps vector and putting axis x labels
temp = _startPointVectorY.y - (_vectorY_Steps / 2) ;
for (int i = 0; i < _axisY_labels.count; i++) {
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(_startPointVectorY.x - 3, temp)];
[path addLineToPoint:CGPointMake( _startPointVectorY.x + 2, temp)];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = [path CGPath];
shapeLayer.strokeColor = [_axisColor CGColor];
shapeLayer.lineWidth = _axisWidth;
shapeLayer.fillColor = [_axisColor CGColor];
[self.verticalLineLayer addObject:shapeLayer];
[self.layer addSublayer:shapeLayer];
UILabel *lb = [_axisY_labels objectAtIndex:i];
str = lb.text;
[self showXLabel:lb InPosition:CGPointMake(_startPointVectorY.x - 30, temp - 5)];
temp = temp - _vectorY_Steps ;
}
}
CGContextDrawPath(context, kCGPathStroke);
}
- (CAShapeLayer*) drawingPointsForChartData : (PNScatterChartData *) chartData AndWithX : (CGFloat) X AndWithY : (CGFloat) Y
{
if (chartData.inflexionPointStyle == PNScatterChartPointStyleCircle) {
float radius = chartData.size;
CAShapeLayer *circle = [CAShapeLayer layer];
// Make a circular shape
circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(X - radius, Y - radius, 2.0*radius, 2.0*radius)
cornerRadius:radius].CGPath;
// Configure the apperence of the circle
circle.fillColor = [chartData.fillColor CGColor];
circle.strokeColor = [chartData.strokeColor CGColor];
circle.lineWidth = 1;
// Add to parent layer
return circle;
}
else if (chartData.inflexionPointStyle == PNScatterChartPointStyleSquare) {
float side = chartData.size;
CAShapeLayer *square = [CAShapeLayer layer];
// Make a circular shape
square.path = [UIBezierPath bezierPathWithRect:CGRectMake(X - (side/2) , Y - (side/2), side, side)].CGPath ;
// Configure the apperence of the circle
square.fillColor = [chartData.fillColor CGColor];
square.strokeColor = [chartData.strokeColor CGColor];
square.lineWidth = 1;
// Add to parent layer
return square;
}
else {
// you cann add your own scatter chart poin here
}
return nil ;
}
- (void) drawLineFromPoint : (CGPoint) startPoint ToPoint : (CGPoint) endPoint WithLineWith : (CGFloat) lineWidth AndWithColor : (UIColor*) color{
// call the same method on a background thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[NSThread sleepForTimeInterval:2];
// calculating start and end point
__block CGFloat startX = [self mappingIsForAxisX:true WithValue:startPoint.x];
__block CGFloat startY = [self mappingIsForAxisX:false WithValue:startPoint.y];
__block CGFloat endX = [self mappingIsForAxisX:true WithValue:endPoint.x];
__block CGFloat endY = [self mappingIsForAxisX:false WithValue:endPoint.y];
// update UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
// drawing path between two points
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(startX, startY)];
[path addLineToPoint:CGPointMake(endX, endY)];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = [path CGPath];
shapeLayer.strokeColor = [color CGColor];
shapeLayer.lineWidth = lineWidth;
shapeLayer.fillColor = [color CGColor];
// adding animation to path
CABasicAnimation *animateStrokeEnd = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animateStrokeEnd.duration = _duration;
animateStrokeEnd.fromValue = [NSNumber numberWithFloat:0.0f];
animateStrokeEnd.toValue = [NSNumber numberWithFloat:1.0f];
[shapeLayer addAnimation:animateStrokeEnd forKey:nil];
[self.layer addSublayer:shapeLayer];
});
});
}
@end
... ...
//
// 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
... ...