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 @interface 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 scndTouch;
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(con
相关新闻>>
- 发表评论
-
- 最新评论 更多>>