当前位置:编程学习 > wap >>

iphone自适应三维柱状图

点击柱状图中的 柱 可获取 该柱的编号(从左到右,0-n)
能自动调整 柱 的宽度以及各 柱 之间的间隔
 
  1 //
  2 //  NTChartView.m
  3 //  chart
  4 //
  5 //  Created by wml on 11-04-10.
  6 //  Copyright 2009 __MyCompanyName__. All rights reserved.
  7 //
  8
  9 #import "NTChartView.h"
 10 #import "Alerter.h"
 11 #import "Rectangle.h"
 12
 13 static int MARGIN_LEFT = 50;
 14 static int MARGIN_BOTTOM = 30;
 15 static int MARGIN_TOP = 20;
 16 static int SHOW_SCALE_NUM = 7;
 17 static int WIDTH = 300;
 18 static int HEIGHT = 300;
 19
 20 @inte易做图ce NTChartView(private)
 21 -(void)drawColumn:(CGContextRef)context rect:(CGRect)_rect;
 22 -(void)drawScale:(CGContextRef)context rect:(CGRect)_rect;
 23 -(void)calcScales:(CGRect)_rect;
 24 @end
 25
 26 @implementation NTChartView
 27 @synthesize groupData;
 28 @synthesize groupRect;
 29 @synthesize frstTouch;
 30 @synthesize s易做图Touch;
 31
 32 - (void) dealloc
 33 {
 34     [groupRect release];
 35     [groupData release];
 36     [super dealloc];
 37 }
 38
 39
 40 -(void)drawRect:(CGRect)_rect{
 41     WIDTH = _rect.size.width;
 42     HEIGHT = _rect.size.height;
 43     groupRect = [[NSMutableArray alloc] init];
 44     MARGIN_LEFT = WIDTH/6.0;
 45     MARGIN_BOTTOM = HEIGHT/10.0;
 46     MARGIN_TOP = HEIGHT/15.0;
 47    
 48     // the _rect is the Canvas on which the bar chart should be painted
 49    
 50     //绘图上下文
 51     CGContextRef context = UIGraphicsGetCurrentContext();
 52     //设置画笔颜色
 53     CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
 54     //为该矩形填充颜色
 55     CGContextFillRect(context, _rect);
 56    
 57     //计算刻度
 58     [self calcScales:_rect];
 59     //画刻度
 60     [self drawScale:context rect:_rect];
 61     //画柱
 62     [self drawColumn:context rect:_rect];
 63    
 64 }
 65
 66 -(void)drawScale:(CGContextRef)context rect:(CGRect)_rect{
 67    
 68     //the coordinate axis
 69     CGPoint points[3];
 70     // the top "end" of the y-axis
 71     points[0] = CGPointMake(MARGIN_LEFT - WIDTH/30, MARGIN_TOP);
 72     // the coordinate center
 73     points[1] = CGPointMake(MARGIN_LEFT -WIDTH/30, _rect.size.height - MARGIN_BOTTOM + 1);
 74     // the right "end" of x-axis
 75     points[2] = CGPointMake(_rect.size.width - WIDTH/30, _rect.size.height - MARGIN_BOTTOM + 1);
 76     CGContextSetAllowsAntialiasing(context, NO);
 77     CGContextAddLines(context, points, 3);
 78     //the color of the scale number
 79     CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
 80    
 81     for(int i=1;i<SHOW_SCALE_NUM + 1; i++){
 82         maxScaleHeight = (_rect.size.height - MARGIN_BOTTOM) * ( i ) / (SHOW_SCALE_NUM + 1);
 83         int vScal = ceil(1.0 * maxScaleValue / (SHOW_SCALE_NUM ) * (i ));
 84         //the y-axis value of the current scale
 85         float y = (_rect.size.height - MARGIN_BOTTOM) - maxScaleHeight;
 86        
 87         NSString *scaleStr = [NSString stringWithFormat:@"%d",vScal];
 88        
 89         [scaleStr drawAtPoint:CGPointMake(MARGIN_LEFT - WIDTH/15.0 - [scaleStr sizeWithFont:[UIFont systemFontOfSize:10]].width, y - 7) withFont:[UIFont systemFontOfSize:10]];
 90         //the short line to the right of the scale number
 91         points[0] = CGPointMake(MARGIN_LEFT - WIDTH/30.0, y);// one end of the line
 92         points[1] = CGPointMake(MARGIN_LEFT - WIDTH/30.0-3, y);// another end
 93         CGContextSetLineDash(context, 0, NULL, 0);
 94         CGContextAddLines(context, points, 2);
 95        
 96         //draw those defined before
 97         CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
 98         CGContextDrawPath(context, kCGPathStroke);
 99        
100         //the horizontal referring line
101         points[0] = CGPointMake(MARGIN_LEFT - WIDTH/30.0, y);
102         points[1] = CGPointMake(_rect.size.width - WIDTH/30.0 , y);
103         //the length of the painted line segments and unpainted
104         //{2,3} means a dotted line with the painted segments of length 2 pixel and ...
105         float partren[] = {2,3};
106         CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:.9 green:.3 blue:.3 alpha:1].CGColor);
107        
108         CGContextSetLineDash(context, 0,partren , 2);
109         CGContextAddLines(context, points, 2);
110         //draw those defined after the last drawing
111         CGContextDrawPath(context, kCGPathStroke);
112        
113     }
114    
115 &

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,