Showing
2 changed files
with
92 additions
and
47 deletions
| @@ -21,9 +21,6 @@ | @@ -21,9 +21,6 @@ | ||
| 21 | @property (nonatomic) NSMutableArray *pointPath; // Array of point path, one for each line | 21 | @property (nonatomic) NSMutableArray *pointPath; // Array of point path, one for each line |
| 22 | @property (nonatomic) NSMutableArray *endPointsOfPath; // Array of start and end points of each line path, one for each line | 22 | @property (nonatomic) NSMutableArray *endPointsOfPath; // Array of start and end points of each line path, one for each line |
| 23 | 23 | ||
| 24 | -@property (strong, nonatomic) UIView *graphView; | ||
| 25 | - | ||
| 26 | -@property (assign, nonatomic) CGRect *chartFrame; | ||
| 27 | @end | 24 | @end |
| 28 | 25 | ||
| 29 | @implementation PNLineChart | 26 | @implementation PNLineChart |
| @@ -734,66 +731,52 @@ | @@ -734,66 +731,52 @@ | ||
| 734 | } | 731 | } |
| 735 | } | 732 | } |
| 736 | 733 | ||
| 737 | -- (UIView*) constructLegend{ | ||
| 738 | - if (self.hasLegend) { | ||
| 739 | - UIView *legend = [self constructLegend]; | ||
| 740 | - CGSize graphSize; | ||
| 741 | - /* Determine legend size */ | ||
| 742 | - switch (self.legendPosition) { | ||
| 743 | - case PNLegendPositionTop | PNLegendPositionBottom: | ||
| 744 | - graphSize = CGSizeMake(fmaxf(self.frame.size.width, legend.frame.size.width), self.frame.size.height + legend.frame.size.height); | ||
| 745 | - break; | ||
| 746 | - case PNLegendPositionLeft | PNLegendPositionRight: | ||
| 747 | - graphSize = CGSizeMake(self.frame.size.width + legend.frame.size.width, fmaxf(self.frame.size.height, legend.frame.size.height)); | ||
| 748 | - break; | ||
| 749 | - default: | ||
| 750 | - break; | ||
| 751 | - } | ||
| 752 | - CGPoint graphOrigin; | ||
| 753 | - switch (self.legendPosition) { | ||
| 754 | - case PNLegendPositionTop: | ||
| 755 | - graphOrigin.x = 2; | ||
| 756 | - break; | ||
| 757 | - case PNLegendPositionBottom: | ||
| 758 | - graphOrigin.x = 2; | ||
| 759 | - break; | ||
| 760 | - case PNLegendPositionLeft: | ||
| 761 | - | ||
| 762 | - case PNLegendPositionRight: | ||
| 763 | - graphOrigin.x = 2; | ||
| 764 | - break; | ||
| 765 | - default: | ||
| 766 | - break; | ||
| 767 | - } | ||
| 768 | - self.graphView = [[UIView alloc] initWithFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, graphSize.width, graphSize.height)]; | ||
| 769 | - [self.graphView addSubview:self]; | ||
| 770 | - [self.graphView addSubview:legend]; | ||
| 771 | - return self.graphView; | ||
| 772 | - }else{ | ||
| 773 | - [self strokeChart]; | ||
| 774 | - return self; | ||
| 775 | - } | ||
| 776 | -} | ||
| 777 | 734 | ||
| 778 | - (UIView*) getLegendWithMaxWidth:(CGFloat)mWidth{ | 735 | - (UIView*) getLegendWithMaxWidth:(CGFloat)mWidth{ |
| 779 | if ([self.chartData count] < 1) { | 736 | if ([self.chartData count] < 1) { |
| 780 | return nil; | 737 | return nil; |
| 781 | } | 738 | } |
| 782 | CGFloat legendLineWidth = 40; | 739 | CGFloat legendLineWidth = 40; |
| 783 | - CGFloat x = legendLineWidth; | 740 | + CGFloat x = 0; |
| 784 | CGFloat y = 0; | 741 | CGFloat y = 0; |
| 785 | CGFloat totalWidth = 0; | 742 | CGFloat totalWidth = 0; |
| 786 | CGFloat totalHeight = 0; | 743 | CGFloat totalHeight = 0; |
| 787 | 744 | ||
| 788 | NSMutableArray *legendLabels = [[NSMutableArray alloc] init]; | 745 | NSMutableArray *legendLabels = [[NSMutableArray alloc] init]; |
| 746 | + NSMutableArray *legendLines = [[NSMutableArray alloc] init]; | ||
| 747 | + NSMutableArray *legendInflexion = [[NSMutableArray alloc] init]; | ||
| 789 | for (PNLineChartData *pdata in self.chartData) { | 748 | for (PNLineChartData *pdata in self.chartData) { |
| 749 | + | ||
| 790 | CGFloat maxLabelWidth = self.legendStyle == PNLegendItemStyleStacked ? (mWidth - legendLineWidth) : (mWidth / [self.chartData count] - legendLineWidth); | 750 | CGFloat maxLabelWidth = self.legendStyle == PNLegendItemStyleStacked ? (mWidth - legendLineWidth) : (mWidth / [self.chartData count] - legendLineWidth); |
| 791 | 751 | ||
| 792 | CGSize labelsize = [PNLineChart sizeOfString:pdata.dataTitle | 752 | CGSize labelsize = [PNLineChart sizeOfString:pdata.dataTitle |
| 793 | withWidth:maxLabelWidth | 753 | withWidth:maxLabelWidth |
| 794 | font:[UIFont systemFontOfSize:self.legendFontSize]]; | 754 | font:[UIFont systemFontOfSize:self.legendFontSize]]; |
| 795 | 755 | ||
| 796 | - UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, maxLabelWidth, labelsize.height)]; | 756 | + /* draw line */ |
| 757 | + CGFloat inflexionWidthSpacer = pdata.inflexionPointStyle == PNLineChartPointStyleTriangle ? pdata.inflexionPointWidth / 2 : pdata.inflexionPointWidth; | ||
| 758 | + CGFloat t1 = (legendLineWidth * 0.8 - inflexionWidthSpacer)/2; | ||
| 759 | + UIView *line = [[UIView alloc] initWithFrame:CGRectMake(x + legendLineWidth * 0.1, y + (labelsize.height - pdata.lineWidth) / 2, t1, pdata.lineWidth)]; | ||
| 760 | + | ||
| 761 | + line.backgroundColor = pdata.color; | ||
| 762 | + line.alpha = pdata.alpha; | ||
| 763 | + [legendLines addObject:line]; | ||
| 764 | + | ||
| 765 | + line = [[UIView alloc] initWithFrame:CGRectMake(x + legendLineWidth * 0.1 + t1 + inflexionWidthSpacer, y + (labelsize.height - pdata.lineWidth) / 2, t1, pdata.lineWidth)]; | ||
| 766 | + line.backgroundColor = pdata.color; | ||
| 767 | + line.alpha = pdata.alpha; | ||
| 768 | + | ||
| 769 | + [legendLines addObject:line]; | ||
| 770 | + | ||
| 771 | + // Add inflexion type | ||
| 772 | + [legendInflexion addObject:[self drawInflexion:pdata.inflexionPointWidth | ||
| 773 | + center:CGPointMake(x + legendLineWidth / 2, y + labelsize.height / 2) | ||
| 774 | + strokeWidth:2 | ||
| 775 | + inflexionStyle:pdata.inflexionPointStyle | ||
| 776 | + andColor:pdata.color | ||
| 777 | + andAlpha:pdata.alpha]]; | ||
| 778 | + | ||
| 779 | + UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x + legendLineWidth, y, maxLabelWidth, labelsize.height)]; | ||
| 797 | label.text = pdata.dataTitle; | 780 | label.text = pdata.dataTitle; |
| 798 | x += self.legendStyle == PNLegendItemStyleStacked ? 0 : labelsize.width + legendLineWidth; | 781 | x += self.legendStyle == PNLegendItemStyleStacked ? 0 : labelsize.width + legendLineWidth; |
| 799 | y += self.legendStyle == PNLegendItemStyleStacked ? labelsize.height : 0; | 782 | y += self.legendStyle == PNLegendItemStyleStacked ? labelsize.height : 0; |
| @@ -801,13 +784,75 @@ | @@ -801,13 +784,75 @@ | ||
| 801 | totalWidth = self.legendStyle == PNLegendItemStyleStacked ? fmaxf(totalWidth, labelsize.width + legendLineWidth) : totalWidth + labelsize.width + legendLineWidth; | 784 | totalWidth = self.legendStyle == PNLegendItemStyleStacked ? fmaxf(totalWidth, labelsize.width + legendLineWidth) : totalWidth + labelsize.width + legendLineWidth; |
| 802 | totalHeight = self.legendStyle == PNLegendItemStyleStacked ? fmaxf(totalHeight, labelsize.height) : totalHeight + labelsize.height; | 785 | totalHeight = self.legendStyle == PNLegendItemStyleStacked ? fmaxf(totalHeight, labelsize.height) : totalHeight + labelsize.height; |
| 803 | [legendLabels addObject:label]; | 786 | [legendLabels addObject:label]; |
| 787 | + | ||
| 804 | } | 788 | } |
| 805 | 789 | ||
| 806 | - UIView *legend = [[UIView alloc] initWithFrame:CGRectMake(100, 100, totalWidth, totalHeight)]; | 790 | + UIView *legend = [[UIView alloc] initWithFrame:CGRectMake(100, 400, totalWidth, totalHeight)]; |
| 807 | for (UILabel *l in legendLabels) { | 791 | for (UILabel *l in legendLabels) { |
| 808 | [legend addSubview:l]; | 792 | [legend addSubview:l]; |
| 809 | } | 793 | } |
| 794 | + for (UIView* v in legendLines) { | ||
| 795 | + [legend addSubview:v]; | ||
| 796 | + } | ||
| 797 | + | ||
| 798 | + for (UIImageView *iv in legendInflexion) { | ||
| 799 | + | ||
| 800 | + [legend addSubview:iv]; | ||
| 801 | + } | ||
| 802 | + | ||
| 810 | return legend; | 803 | return legend; |
| 811 | } | 804 | } |
| 812 | 805 | ||
| 806 | +//// PaintCode Trial Version | ||
| 807 | +//// www.paintcodeapp.com | ||
| 808 | + | ||
| 809 | +- (UIImageView*)drawInflexion:(CGFloat)size center:(CGPoint)center strokeWidth: (CGFloat)sw inflexionStyle:(PNLineChartPointStyle)type andColor:(UIColor*)color andAlpha:(CGFloat) alfa | ||
| 810 | +{ | ||
| 811 | + //this is an arbitrary size for example | ||
| 812 | + CGSize aSize = CGSizeMake(size + sw, size + sw); | ||
| 813 | + | ||
| 814 | + //this can take any CGSize | ||
| 815 | + //it works like the frame.size would in the drawRect: method | ||
| 816 | + //in the way that it represents the context's size | ||
| 817 | + UIGraphicsBeginImageContextWithOptions(aSize, NO, 0.0); | ||
| 818 | + | ||
| 819 | + //this gets the graphic context | ||
| 820 | + CGContextRef context = UIGraphicsGetCurrentContext(); | ||
| 821 | + | ||
| 822 | + if (type == PNLineChartPointStyleCircle) { | ||
| 823 | + //// Oval Drawing | ||
| 824 | + CGContextAddArc(context, (size + sw)/2, (size + sw) / 2, size/2, 0, M_PI*2, YES); | ||
| 825 | + }else if (type == PNLineChartPointStyleSquare){ | ||
| 826 | + CGContextAddRect(context, CGRectMake(sw/2, sw/2, size, size)); | ||
| 827 | + }else if (type == PNLineChartPointStyleTriangle){ | ||
| 828 | + CGContextMoveToPoint(context, sw/2, size + sw/2); | ||
| 829 | + CGContextAddLineToPoint(context, size + sw/2, size + sw/2); | ||
| 830 | + CGContextAddLineToPoint(context, size/2 + sw/2, sw/2); | ||
| 831 | + CGContextAddLineToPoint(context, sw/2, size + sw/2); | ||
| 832 | + CGContextClosePath(context); | ||
| 833 | + } | ||
| 834 | + | ||
| 835 | + //Set the width of the pen mark | ||
| 836 | + CGContextSetLineWidth(context, sw); | ||
| 837 | + | ||
| 838 | + CGContextSetAlpha(context, alfa); | ||
| 839 | + | ||
| 840 | + CGContextSetStrokeColorWithColor(context, color.CGColor); | ||
| 841 | + CGContextDrawPath(context, kCGPathStroke); | ||
| 842 | + | ||
| 843 | + //now get the image from the context | ||
| 844 | + UIImage *squareImage = UIGraphicsGetImageFromCurrentImageContext(); | ||
| 845 | + | ||
| 846 | + UIGraphicsEndImageContext(); | ||
| 847 | + | ||
| 848 | + //// Variable Declarations | ||
| 849 | + CGFloat originX = center.x - (size + sw) / 2.0; | ||
| 850 | + CGFloat originY = center.y - (size + sw) / 2.0; | ||
| 851 | + | ||
| 852 | + UIImageView *squareImageView = [[UIImageView alloc]initWithImage:squareImage]; | ||
| 853 | + [squareImageView setFrame:CGRectMake(originX, originY, size + sw, size + sw)]; | ||
| 854 | + return squareImageView; | ||
| 855 | + | ||
| 856 | +} | ||
| 857 | + | ||
| 813 | @end | 858 | @end |
| @@ -51,7 +51,7 @@ | @@ -51,7 +51,7 @@ | ||
| 51 | data02.color = PNTwitterColor; | 51 | data02.color = PNTwitterColor; |
| 52 | data02.alpha = 0.5f; | 52 | data02.alpha = 0.5f; |
| 53 | data02.itemCount = data02Array.count; | 53 | data02.itemCount = data02Array.count; |
| 54 | - data02.inflexionPointStyle = PNLineChartPointStyleSquare; | 54 | + data02.inflexionPointStyle = PNLineChartPointStyleCircle; |
| 55 | data02.getData = ^(NSUInteger index) { | 55 | data02.getData = ^(NSUInteger index) { |
| 56 | CGFloat yValue = [data02Array[index] floatValue]; | 56 | CGFloat yValue = [data02Array[index] floatValue]; |
| 57 | return [PNLineChartDataItem dataItemWithY:yValue]; | 57 | return [PNLineChartDataItem dataItemWithY:yValue]; |
-
Please register or login to post a comment