Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve dr-charts #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dr-charts/Classes/BarChart/BarChart.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#import "BarChart.h"
#import "DRScrollView.h"
#import "Constants.h"
#import "DrConstants.h"

@interface BarChartDataRenderer : NSObject

Expand Down
4 changes: 2 additions & 2 deletions dr-charts/Classes/CircularChart/CircularChart.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

#import "CircularChart.h"
#import "Constants.h"
#import "DrConstants.h"

@interface CircularChartDataRenderer : NSObject

Expand Down Expand Up @@ -306,4 +306,4 @@ - (instancetype)init{
}


@end
@end
File renamed without changes.
2 changes: 1 addition & 1 deletion dr-charts/Classes/DrGraphs.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
#import "BarChart.h"
#import "CircularChart.h"
#import "LegendView.h"
#import "Constants.h"
#import "DrConstants.h"

#endif /* DrGraphs_h */
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

#import "HorizontalStackBarChart.h"
#import "Constants.h"
#import "DrConstants.h"

@interface HorizontalStackBarData : NSObject

Expand Down Expand Up @@ -347,4 +347,4 @@ - (instancetype)init{
return self;
}

@end
@end
2 changes: 1 addition & 1 deletion dr-charts/Classes/Legend/LegendView.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

#import "LegendView.h"
#import "Constants.h"
#import "DrConstants.h"

@implementation LegendView

Expand Down
4 changes: 2 additions & 2 deletions dr-charts/Classes/LineChart/LineGraphMarker.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

#import "LineGraphMarker.h"
#import "Constants.h"
#import "DrConstants.h"

@interface LineGraphMarker()

Expand Down Expand Up @@ -60,4 +60,4 @@ - (void)drawAtPoint:(CGPoint)point{
[self setFrame:CGRectMake(x, point.y - (HEIGHT(self.markerLabel) + 10), WIDTH(self.markerLabel) + 10, HEIGHT(self.markerLabel) + 10)];
}

@end
@end
22 changes: 22 additions & 0 deletions dr-charts/Classes/LineChart/MultiLineGraphView.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,19 @@ typedef enum {
//Set xData for Line on Line Graph for corresponding yData
//If LineType is LineParallelYAxis, Set xData for the Line on Line Graph

- (NSMutableArray *)dataForXAxisForAllLines;
//Set xData for All Line on Line Graph for corresponding yData

@optional
- (UIView *)customViewForLineChartTouchWithXValue:(id)xValue andYValue:(id)yValue;
//Set Custom View for touch on each item in a Line Chart

- (NSString *)customYAxisFormat:(float)yAxisValue;
//Set Custom Format for yAxix Value

- (NSString *)customXAxisFormat:(NSString *)xAxisValue;
//Set Custom Format for xAxix Value

@end

@interface MultiLineGraphView : UIView
Expand Down Expand Up @@ -105,6 +114,19 @@ typedef enum {
//Set LEGEND TYPE Horizontal or Vertical
@property (nonatomic) LegendType legendViewType; //Default is LegendTypeVertical i.e. VERTICAL

//Set step number for xAxis display value
@property (nonatomic) int xAixsStepNumber;
//Set step number for yAxis display value
@property (nonatomic) int yAixsStepNumber;
//Set animation duration time
@property (nonatomic) float animationDuration;
//Set default min value for yAxis
@property (nonatomic) float yAixsDefaultMin;
//Set default max value for yAxis
@property (nonatomic) float yAixsDefaultMax;
//Set axis Line color (not same with gridLineColor)
@property (nonatomic, strong) UIColor *axisLineColor;

//To draw the graph
- (void)drawGraph;

Expand Down
76 changes: 50 additions & 26 deletions dr-charts/Classes/LineChart/MultiLineGraphView.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#import "MultiLineGraphView.h"
#import "LineGraphMarker.h"
#import "DRScrollView.h"
#import "Constants.h"
#import "DrConstants.h"

@interface LineChartDataRenderer : NSObject

Expand All @@ -30,6 +30,9 @@ @interface LineChartDataRenderer : NSObject
@interface MultiLineGraphView()<UIScrollViewDelegate>{
float stepY;
float stepX;
float yAxisMinValue;
float yAxisMaxValue;


NSString *selectedXData;
NSString *selectedYData;
Expand All @@ -54,6 +57,8 @@ @interface MultiLineGraphView()<UIScrollViewDelegate>{
@property (nonatomic, strong) NSMutableArray *legendArray;
@property (nonatomic, strong) NSMutableArray *lineDataArray;

@property (nonatomic, copy) NSString *axisAlignmentMode;

@end

@implementation MultiLineGraphView
Expand All @@ -66,6 +71,7 @@ - (instancetype)initWithFrame:(CGRect)frame{

self.gridLineColor = [UIColor lightGrayColor];
self.gridLineWidth = 0.3;
self.axisLineColor = self.gridLineColor;

self.textFontSize = 11;
self.textColor = [UIColor blackColor];
Expand All @@ -82,6 +88,13 @@ - (instancetype)initWithFrame:(CGRect)frame{

self.showCustomMarkerView = FALSE;

self.xAixsStepNumber = 5;
self.yAixsStepNumber = 5;
self.animationDuration = ANIMATION_DURATION;
self.yAixsDefaultMin = -5;
self.yAixsDefaultMax = 5;
self.axisAlignmentMode = kCAAlignmentCenter;

scaleHeight = 0;
lastScale = 1;
}
Expand Down Expand Up @@ -111,12 +124,11 @@ - (void)getDataFromDataSource{
[data setLegendText:lineData.graphName];
[data setLegendColor:lineData.lineColor];
[self.legendArray addObject:data];

if (self.xAxisArray.count < lineData.xAxisArray.count) {
[self.xAxisArray removeAllObjects];
[self.xAxisArray addObjectsFromArray:lineData.xAxisArray];
}
}

[self.xAxisArray removeAllObjects];
[self.xAxisArray addObjectsFromArray:[self.dataSource dataForXAxisForAllLines]];

}

#pragma mark Draw Graph
Expand Down Expand Up @@ -172,8 +184,8 @@ - (void)drawGraph{

#pragma Draw Shape Layer
- (void)createYAxisLine{
float minY = 0.0;
float maxY = 0.0;
float minY = _yAixsDefaultMin;
float maxY = _yAixsDefaultMax;

for (LineChartDataRenderer *lineData in self.lineDataArray) {

Expand All @@ -194,15 +206,18 @@ - (void)createYAxisLine{
}
}

int gridYCount = 5;
int gridYCount = _yAixsStepNumber;

if (scaleHeight > height) {
gridYCount = ceil(gridYCount * lastScale);
}

float step = (maxY - minY) / gridYCount;
yAxisMinValue = minY;
yAxisMaxValue = maxY;

stepY = (HEIGHT(self.graphView) - (OFFSET_Y * 2)) / (maxY - minY);
self.axisAlignmentMode = kCAAlignmentRight;

for (int i = 0; i <= gridYCount; i++) {
int y = (i * step) * stepY;
Expand All @@ -212,6 +227,9 @@ - (void)createYAxisLine{
CGPoint endPoint = CGPointMake(WIDTH(self.graphView) - OFFSET_X, HEIGHT(self.graphView) - (y + OFFSET_Y));

NSString *numberString = [NSString stringWithFormat:@"%.1f",value];
if ([self.dataSource respondsToSelector:@selector(customYAxisFormat:)]) {
numberString = [self.dataSource customYAxisFormat:value];
}

BOOL drawGrid = TRUE;
if (self.drawGridY) {
Expand All @@ -228,24 +246,24 @@ - (void)createYAxisLine{
NSAttributedString *attrString = [LegendView getAttributedString:numberString withFont:self.textFont];
CGSize size = [attrString boundingRectWithSize:CGSizeMake(WIDTH(self) - LEGEND_VIEW, MAXFLOAT) options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin context:nil].size;

[self drawLineForGridWithStartPoint:startPoint endPoint:endPoint text:numberString textFrame:CGRectMake(OFFSET_PADDING, HEIGHT(self.graphView) - (y + OFFSET_Y + size.height/2), size.width , size.height) drawGrid:drawGrid];
[self drawLineForGridWithStartPoint:startPoint endPoint:endPoint text:numberString textFrame:CGRectMake(startPoint.x - size.width - 1, HEIGHT(self.graphView) - (y + OFFSET_Y + size.height/2), size.width , size.height) drawGrid:drawGrid isAxisLine:i == 0];
}
}

- (void)createXAxisLine{
float step = 0;
NSInteger maxStep = [self.xAxisArray count];

if ([self.xAxisArray count] > 5) {
int stepCount = 5;
if ([self.xAxisArray count] > _xAixsStepNumber) {
int stepCount = _xAixsStepNumber;

if (widht > WIDTH(self)) {
stepCount = ceil(stepCount * lastScale);
}

NSInteger count = [self.xAxisArray count] - 1;

for (int i = 4; i < 8; i++) {
for (int i = _xAixsStepNumber - 1; i < (_xAixsStepNumber - 1) * 2; i++) {
if (count % i == 0) {
stepCount = i;
break;
Expand All @@ -259,6 +277,7 @@ - (void)createXAxisLine{
}

stepX = (WIDTH(self.graphView) - (OFFSET_X * 2)) / (self.xAxisArray.count - 1);
self.axisAlignmentMode = kCAAlignmentCenter;

for (int i = 0; i <= maxStep; i++) {
int x = (i * step) * stepX;
Expand Down Expand Up @@ -288,10 +307,15 @@ - (void)createXAxisLine{
drawGrid = FALSE;
}
}
NSAttributedString *attrString = [LegendView getAttributedString:[self.xAxisArray objectAtIndex:index] withFont:self.textFont];

NSString *xAxisValue = [self.xAxisArray objectAtIndex:index];
if ([self.dataSource respondsToSelector:@selector(customXAxisFormat:)]) {
xAxisValue = [self.dataSource customXAxisFormat:xAxisValue];
}
NSAttributedString *attrString = [LegendView getAttributedString:xAxisValue withFont:self.textFont];
CGSize size = [attrString boundingRectWithSize:CGSizeMake(WIDTH(self) - LEGEND_VIEW, MAXFLOAT) options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin context:nil].size;

[self drawLineForGridWithStartPoint:startPoint endPoint:endPoint text:[self.xAxisArray objectAtIndex:index] textFrame:CGRectMake(x + size.width/2, HEIGHT(self.graphView) - (size.height + INNER_PADDING), size.width, size.height) drawGrid:drawGrid];
[self drawLineForGridWithStartPoint:startPoint endPoint:endPoint text:xAxisValue textFrame:CGRectMake(x + size.width/2, HEIGHT(self.graphView) - (size.height + INNER_PADDING), size.width, size.height) drawGrid:drawGrid isAxisLine:i == 0];
}
}

Expand All @@ -306,7 +330,7 @@ - (void)createGraph{
NSInteger itemIndex = [self.xAxisArray indexOfObject:[lineData.xAxisArray objectAtIndex:0]];

x = itemIndex * stepX;
y = [[lineData.yAxisArray objectAtIndex:0] floatValue] * stepY;
y = ([[lineData.yAxisArray objectAtIndex:0] floatValue] - yAxisMinValue) * stepY;

CGPoint startPoint = CGPointMake(x + OFFSET_X, HEIGHT(self.graphView) - (OFFSET_Y + y));
CGPoint firstPoint = startPoint;
Expand All @@ -322,7 +346,7 @@ - (void)createGraph{
for (int i = 1; i < lineData.yAxisArray.count; i++){
NSInteger xIndex = [self.xAxisArray indexOfObject:[lineData.xAxisArray objectAtIndex:i]];
x = xIndex * stepX;
y = [[lineData.yAxisArray objectAtIndex:i] floatValue] * stepY;
y = ([[lineData.yAxisArray objectAtIndex:i] floatValue] - yAxisMinValue) * stepY;

endPoint = CGPointMake(x + OFFSET_X, HEIGHT(self.graphView) - ( y + OFFSET_Y));

Expand All @@ -348,7 +372,7 @@ - (void)createGraph{
[shapeLayer setContentsScale:[[UIScreen mainScreen] scale]];

CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
[pathAnimation setDuration:ANIMATION_DURATION];
[pathAnimation setDuration:_animationDuration];
[pathAnimation setFromValue:[NSNumber numberWithFloat:0.0f]];
[pathAnimation setToValue:[NSNumber numberWithFloat:1.0f]];
[shapeLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
Expand All @@ -371,7 +395,7 @@ - (void)createGraph{
int y = 0;

for (int i = 0; i < lineData.yAxisArray.count; i++){
y = [[lineData.yAxisArray objectAtIndex:i] floatValue] * stepY;
y = ([[lineData.yAxisArray objectAtIndex:i] floatValue] - yAxisMinValue) * stepY;

CGPoint startPoint = CGPointMake(OFFSET_X, HEIGHT(self.graphView) - (OFFSET_Y + y));
CGPoint endPoint = CGPointMake(WIDTH(self.graphView) - OFFSET_X, HEIGHT(self.graphView) - (OFFSET_Y + y));
Expand All @@ -389,10 +413,10 @@ - (void)createGraph{

if (lineData.fillGraph) {

y = [[lineData.yAxisArray firstObject] floatValue] * stepY;
y = ([[lineData.yAxisArray firstObject] floatValue] - yAxisMinValue) * stepY;
CGPoint point1 = CGPointMake(OFFSET_X, HEIGHT(self.graphView) - (OFFSET_Y + y));

y = [[lineData.yAxisArray lastObject] floatValue] * stepY;
y = ([[lineData.yAxisArray lastObject] floatValue] - yAxisMinValue) * stepY;
CGPoint point2 = CGPointMake(WIDTH(self.graphView) - OFFSET_X, HEIGHT(self.graphView) - (OFFSET_Y + y));

UIBezierPath *fillPath = [UIBezierPath bezierPath];
Expand Down Expand Up @@ -614,7 +638,7 @@ - (void)findValueForTouch:(UITouch *)touch{
for (int i = 0; i < lineData.yAxisArray.count; i++){
NSInteger xIndex = [self.xAxisArray indexOfObject:[lineData.xAxisArray objectAtIndex:i]];
x = xIndex * stepX;
y = [[lineData.yAxisArray objectAtIndex:i] floatValue] * stepY;
y = ([[lineData.yAxisArray objectAtIndex:i] floatValue] - yAxisMinValue) * stepY;

CGPoint point = CGPointMake(x + OFFSET_X, HEIGHT(self.graphView) - ( y + OFFSET_Y));

Expand Down Expand Up @@ -704,7 +728,7 @@ - (void)fillGraphBackgroundWithPath:(UIBezierPath *)path color:(UIColor *)color{
[shapeLayer setOpacity:0.1];

CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"fill"];
[pathAnimation setDuration:ANIMATION_DURATION];
[pathAnimation setDuration:_animationDuration];
[pathAnimation setFillMode:kCAFillModeForwards];
[pathAnimation setFromValue:(id)[[UIColor clearColor] CGColor]];
[pathAnimation setToValue:(id)[color CGColor]];
Expand All @@ -717,12 +741,12 @@ - (void)fillGraphBackgroundWithPath:(UIBezierPath *)path color:(UIColor *)color{
}

#pragma mark Draw Grid Lines
- (void)drawLineForGridWithStartPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint text:(NSString *)text textFrame:(CGRect)frame drawGrid:(BOOL)draw{
- (void)drawLineForGridWithStartPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint text:(NSString *)text textFrame:(CGRect)frame drawGrid:(BOOL)draw isAxisLine:(BOOL)isAxisLine{

if (draw) {
CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
[shapeLayer setPath:[[self drawPathWithStartPoint:startPoint endPoint:endPoint] CGPath]];
[shapeLayer setStrokeColor:self.gridLineColor.CGColor];
[shapeLayer setStrokeColor:isAxisLine ? self.axisLineColor.CGColor : self.gridLineColor.CGColor];
[shapeLayer setLineWidth:self.gridLineWidth];
[self.graphView.layer addSublayer:shapeLayer];
}
Expand All @@ -732,7 +756,7 @@ - (void)drawLineForGridWithStartPoint:(CGPoint)startPoint endPoint:(CGPoint)endP
[textLayer setFontSize:self.textFontSize];
[textLayer setFrame:frame];
[textLayer setString:text];
[textLayer setAlignmentMode:kCAAlignmentCenter];
[textLayer setAlignmentMode:self.axisAlignmentMode];
[textLayer setForegroundColor:self.textColor.CGColor];
[textLayer setShouldRasterize:YES];
[textLayer setRasterizationScale:[[UIScreen mainScreen] scale]];
Expand Down
4 changes: 2 additions & 2 deletions dr-charts/Classes/PieChart/PieChart.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

#import "PieChart.h"
#import "Constants.h"
#import "DrConstants.h"

@interface PieChartDataRenderer : NSObject

Expand Down Expand Up @@ -347,4 +347,4 @@ - (instancetype)init{
return self;
}

@end
@end
Loading