Skip to content

Commit 7566439

Browse files
committed
新增折线图、柱状图实时刷新时动态调整纵坐标
1.新增折线图、柱状图刷新数据时动态调整纵坐标 2.新增饼状图刷新数据的方法
1 parent 730d8d2 commit 7566439

File tree

9 files changed

+164
-22
lines changed

9 files changed

+164
-22
lines changed

SCChart.xcodeproj/project.pbxproj

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,26 +177,26 @@
177177
250383E41AB2CAA50034BB22 /* SCChart */ = {
178178
isa = PBXGroup;
179179
children = (
180-
250383E51AB2CAA50034BB22 /* SCBar.h */,
181-
250383E61AB2CAA50034BB22 /* SCBar.m */,
182-
250383E71AB2CAA50034BB22 /* SCBarChart.h */,
183-
250383E81AB2CAA50034BB22 /* SCBarChart.m */,
184180
250383E91AB2CAA50034BB22 /* SCChart.h */,
185181
250383EA1AB2CAA50034BB22 /* SCChart.m */,
182+
250383EF1AB2CAA50034BB22 /* SCLineChart.h */,
183+
250383F01AB2CAA50034BB22 /* SCLineChart.m */,
184+
250383E71AB2CAA50034BB22 /* SCBarChart.h */,
185+
250383E81AB2CAA50034BB22 /* SCBarChart.m */,
186+
25FA1F3A1AC15F4300F4C43F /* SCCircleChart.h */,
187+
25FA1F3B1AC15F4300F4C43F /* SCCircleChart.m */,
188+
25FA1F4D1AC1708800F4C43F /* SCPieChart.h */,
189+
25FA1F4E1AC1708800F4C43F /* SCPieChart.m */,
190+
250383E51AB2CAA50034BB22 /* SCBar.h */,
191+
250383E61AB2CAA50034BB22 /* SCBar.m */,
186192
250383EB1AB2CAA50034BB22 /* SCChartLabel.h */,
187193
250383EC1AB2CAA50034BB22 /* SCChartLabel.m */,
188194
250383ED1AB2CAA50034BB22 /* SCColor.h */,
189195
250383EE1AB2CAA50034BB22 /* SCColor.m */,
190-
250383EF1AB2CAA50034BB22 /* SCLineChart.h */,
191-
250383F01AB2CAA50034BB22 /* SCLineChart.m */,
192196
250383F11AB2CAA50034BB22 /* SCTool.h */,
193197
250383F21AB2CAA50034BB22 /* SCTool.m */,
194-
25FA1F3A1AC15F4300F4C43F /* SCCircleChart.h */,
195-
25FA1F3B1AC15F4300F4C43F /* SCCircleChart.m */,
196198
25FA1F3D1AC15F4B00F4C43F /* UICountingLabel.h */,
197199
25FA1F3E1AC15F4B00F4C43F /* UICountingLabel.m */,
198-
25FA1F4D1AC1708800F4C43F /* SCPieChart.h */,
199-
25FA1F4E1AC1708800F4C43F /* SCPieChart.m */,
200200
25FA1F4F1AC1708800F4C43F /* SCPieChartDataItem.h */,
201201
25FA1F501AC1708800F4C43F /* SCPieChartDataItem.m */,
202202
25FA1F561AC1718200F4C43F /* SCGenericChart.h */,

SCChart/SCBarCell.m

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,30 @@ @interface SCBarCell ()<SCChartDataSource>
1818

1919
@implementation SCBarCell
2020

21+
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
22+
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
23+
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
24+
[btn setTitle:@"点击刷新数据" forState:UIControlStateNormal];
25+
btn.titleLabel.font = [UIFont systemFontOfSize:12.0];;
26+
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
27+
btn.frame = CGRectMake(0, 0, SCREEN_WIDTH, 27);
28+
[btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
29+
[self addSubview:btn];
30+
}
31+
return self;
32+
}
33+
34+
- (void)btnPressed:(id)sender {
35+
[chartView strokeChart];
36+
}
37+
2138
- (void)configUI:(NSIndexPath *)indexPath {
2239
if (chartView) {
2340
[chartView removeFromSuperview];
2441
chartView = nil;
2542
}
2643
path = indexPath;
27-
chartView = [[SCChart alloc] initwithSCChartDataFrame:CGRectMake(10, 10, [UIScreen mainScreen].bounds.size.width - 20, 150)
44+
chartView = [[SCChart alloc] initwithSCChartDataFrame:CGRectMake(10, (self.frame.size.height-150)/2, [UIScreen mainScreen].bounds.size.width - 20, 150)
2845
withSource:self
2946
withStyle:SCChartBarStyle];
3047
[chartView showInView:self.contentView];
@@ -50,14 +67,29 @@ - (NSArray *)SCChart_yValueArray:(SCChart *)chart {
5067
switch (path.row) {
5168
case 0:
5269
{
53-
NSArray *ary = @[@"0.2",@"0.14",@"0.12",@"0.44",@"0.4",@"0.13",@"0.21"];
70+
NSMutableArray *ary = [NSMutableArray array];
71+
for (NSInteger i = 0; i < 7; i++) {
72+
CGFloat num = arc4random_uniform(100);
73+
NSString *str = [NSString stringWithFormat:@"%f",num];
74+
[ary addObject:str];
75+
}
5476
return @[ary];
5577
break;
5678
}
5779
case 1:
5880
{
59-
NSArray *ary = @[@"22",@"44",@"15",@"22",@"44",@"15",@"22"];
60-
NSArray *ary2 = @[@"52",@"13",@"43",@"12",@"4",@"65",@"12"];
81+
NSMutableArray *ary = [NSMutableArray array];
82+
NSMutableArray *ary2 = [NSMutableArray array];
83+
for (NSInteger i = 0; i < 7; i++) {
84+
CGFloat num = arc4random_uniform(1000) / 100;
85+
NSString *str = [NSString stringWithFormat:@"%f",num];
86+
[ary addObject:str];
87+
}
88+
for (NSInteger i = 0; i < 7; i++) {
89+
CGFloat num = arc4random_uniform(1000) / 100;
90+
NSString *str = [NSString stringWithFormat:@"%f",num];
91+
[ary2 addObject:str];
92+
}
6193
return @[ary,ary2];
6294
break;
6395
}

SCChart/SCChart/SCChart.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ - (id)initWithFrame:(CGRect)frame
4040

4141
-(void)setUpChart{
4242
if (self.chartStyle == SCChartLineStyle) {
43+
44+
[self.lineChart removeFromSuperview];
45+
self.lineChart = nil;
46+
4347
if(!_lineChart){
4448
_lineChart = [[SCLineChart alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
4549
[self addSubview:_lineChart];
@@ -92,6 +96,10 @@ -(void)setUpChart{
9296

9397
}else if (self.chartStyle == SCChartBarStyle)
9498
{
99+
100+
[self.barChart removeFromSuperview];
101+
self.barChart = nil;
102+
95103
if (!_barChart) {
96104
_barChart = [[SCBarChart alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
97105
[self addSubview:_barChart];

SCChart/SCChart/SCPieChart.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040

4141
@property (nonatomic, weak) id<SCChartDelegate> delegate;
4242

43+
- (void)updateChartByNumbers:(NSArray *)numbers;
44+
4345
- (void)strokeChart;
4446

4547
@end

SCChart/SCChart/SCPieChart.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,4 +432,16 @@ - (UIImageView*)drawInflexion:(CGFloat)size center:(CGPoint)center andColor:(UIC
432432
[squareImageView setFrame:CGRectMake(originX, originY, size, size)];
433433
return squareImageView;
434434
}
435+
436+
- (void)updateChartByNumbers:(NSArray *)numbers {
437+
for (SCPieChartDataItem *item in self.items) {
438+
NSInteger index = [self.items indexOfObject:item];
439+
if (index >= numbers.count) {
440+
break;
441+
}
442+
item.value = [numbers[index] integerValue];
443+
}
444+
[self strokeChart];
445+
}
446+
435447
@end

SCChart/SCChartCell.m

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,30 @@ @interface SCChartCell ()<SCChartDataSource>
1818

1919
@implementation SCChartCell
2020

21+
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
22+
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
23+
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
24+
[btn setTitle:@"点击刷新数据" forState:UIControlStateNormal];
25+
btn.titleLabel.font = [UIFont systemFontOfSize:12.0];;
26+
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
27+
btn.frame = CGRectMake(0, 0, SCREEN_WIDTH, 27);
28+
[btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
29+
[self addSubview:btn];
30+
}
31+
return self;
32+
}
33+
34+
- (void)btnPressed:(id)sender {
35+
[chartView strokeChart];
36+
}
37+
2138
- (void)configUI:(NSIndexPath *)indexPath {
2239
if (chartView) {
2340
[chartView removeFromSuperview];
2441
chartView = nil;
2542
}
2643
path = indexPath;
27-
chartView = [[SCChart alloc] initwithSCChartDataFrame:CGRectMake(10, 10, [UIScreen mainScreen].bounds.size.width - 20, 150)
44+
chartView = [[SCChart alloc] initwithSCChartDataFrame:CGRectMake(10, (self.frame.size.height-150)/2, [UIScreen mainScreen].bounds.size.width - 20, 150)
2845
withSource:self
2946
withStyle:SCChartLineStyle];
3047
[chartView showInView:self.contentView];
@@ -50,22 +67,52 @@ - (NSArray *)SCChart_yValueArray:(SCChart *)chart {
5067
switch (path.row) {
5168
case 0:
5269
{
53-
NSArray *ary = @[@"0.2",@"0.14",@"0.12",@"0.44",@"0.4",@"0.13",@"0.21"];
70+
NSMutableArray *ary = [NSMutableArray array];
71+
for (NSInteger i = 0; i < 7; i++) {
72+
CGFloat num = arc4random_uniform(100);
73+
NSString *str = [NSString stringWithFormat:@"%f",num];
74+
[ary addObject:str];
75+
}
5476
return @[ary];
5577
break;
5678
}
5779
case 1:
5880
{
59-
NSArray *ary = @[@"22",@"44",@"15",@"22",@"44",@"15",@"22"];
60-
NSArray *ary2 = @[@"16",@"63",@"64",@"23",@"133",@"64",@"15"];
81+
NSMutableArray *ary = [NSMutableArray array];
82+
NSMutableArray *ary2 = [NSMutableArray array];
83+
for (NSInteger i = 0; i < 7; i++) {
84+
CGFloat num = arc4random_uniform(1000) / 100;
85+
NSString *str = [NSString stringWithFormat:@"%f",num];
86+
[ary addObject:str];
87+
}
88+
for (NSInteger i = 0; i < 7; i++) {
89+
CGFloat num = arc4random_uniform(1000) / 100;
90+
NSString *str = [NSString stringWithFormat:@"%f",num];
91+
[ary2 addObject:str];
92+
}
6193
return @[ary,ary2];
6294
break;
6395
}
6496
case 2:
6597
{
66-
NSArray *ary = @[@"0.2",@"0.14",@"0.12",@"0.44",@"0.4",@"0.13",@"0.21"];
67-
NSArray *ary2 = @[@"0.16",@"0.63",@"0.64",@"0.23",@"0.133",@"0.64",@"0.15"];
68-
NSArray *ary3 = @[@"0.6",@"0.3",@"0.4",@"0.3",@"0.3",@"0.4",@"0.5"];
98+
NSMutableArray *ary = [NSMutableArray array];
99+
NSMutableArray *ary2 = [NSMutableArray array];
100+
NSMutableArray *ary3 = [NSMutableArray array];
101+
for (NSInteger i = 0; i < 7; i++) {
102+
CGFloat num = arc4random_uniform(500);
103+
NSString *str = [NSString stringWithFormat:@"%f",num];
104+
[ary addObject:str];
105+
}
106+
for (NSInteger i = 0; i < 7; i++) {
107+
CGFloat num = arc4random_uniform(1000);
108+
NSString *str = [NSString stringWithFormat:@"%f",num];
109+
[ary2 addObject:str];
110+
}
111+
for (NSInteger i = 0; i < 7; i++) {
112+
CGFloat num = arc4random_uniform(2000);
113+
NSString *str = [NSString stringWithFormat:@"%f",num];
114+
[ary3 addObject:str];
115+
}
69116
return @[ary,ary2,ary3];
70117
break;
71118
}

SCChart/SCCircleCell.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@ @interface SCCircleCell()
1717

1818
@implementation SCCircleCell
1919

20+
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
21+
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
22+
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
23+
[btn setTitle:@"点击刷新数据" forState:UIControlStateNormal];
24+
btn.titleLabel.font = [UIFont systemFontOfSize:12.0];;
25+
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
26+
btn.frame = CGRectMake(0, 0, SCREEN_WIDTH, 27);
27+
[btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
28+
[self addSubview:btn];
29+
}
30+
return self;
31+
}
32+
33+
- (void)btnPressed:(id)sender {
34+
CGFloat num = arc4random_uniform(60);
35+
[chartView updateChartByCurrent:@(num)];
36+
}
37+
2038
- (void)configUI:(NSIndexPath *)indexPath {
2139
if (chartView) {
2240
[chartView removeFromSuperview];

SCChart/SCPieCell.m

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,29 @@ @interface SCPieCell()
1717

1818
@implementation SCPieCell
1919

20+
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
21+
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
22+
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
23+
[btn setTitle:@"点击刷新数据" forState:UIControlStateNormal];
24+
btn.titleLabel.font = [UIFont systemFontOfSize:12.0];;
25+
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
26+
btn.frame = CGRectMake(0, 0, SCREEN_WIDTH, 27);
27+
[btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
28+
[self addSubview:btn];
29+
}
30+
return self;
31+
}
32+
33+
- (void)btnPressed:(id)sender {
34+
NSMutableArray *ary = [NSMutableArray array];
35+
for (NSInteger i = 0; i < 3; i++) {
36+
CGFloat num = arc4random_uniform(100);
37+
NSString *str = [NSString stringWithFormat:@"%f",num];
38+
[ary addObject:str];
39+
}
40+
[chartView updateChartByNumbers:ary];
41+
}
42+
2043
- (void)configUI:(NSIndexPath *)indexPath {
2144
if (chartView) {
2245
[chartView removeFromSuperview];

SCChart/SCViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
121121
}
122122

123123
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
124-
return 170;
124+
return 200;
125125
}
126126

127127
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

0 commit comments

Comments
 (0)