单军华
2018-07-19 83b9d5c682b21d88133f24da0f94dd56bd79e687
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
//
//  DynamicViewController.m
//  screendisplay
//
//  Created by 单军华 on 2018/7/10.
//  Copyright © 2018年 单军华. All rights reserved.
//
 
#import "DynamicViewController.h"
#import "JHLineChart.h"
#import "DynamicService.h"
 
@interface DynamicViewController ()
{
   NSTimer * myTimer; // 更新设备状态
}
 
/** digest */
@property (nonatomic, strong) DynamicService *dynamicService;
/** digest */
@property (nonatomic, strong) JHLineChart * historyRecordLineChart;
 
@end
 
@implementation DynamicViewController
 
#pragma mark - getter
 
- (DynamicService *)dynamicService
{
    if(_dynamicService == nil)
    {
        _dynamicService = [[DynamicService alloc] init];
    }
    return _dynamicService;
}
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = kUIColorFromRGB(0x0b2f76);
    
    UIImageView * viewBackground = [[UIImageView alloc] initWithFrame:CGRectMake(AdaptedWidth(5), NavBar_Height+AdaptedHeight(26), SCREEN_WIDTH-AdaptedWidth(10), AdaptedHeight(354+26+30+13+5+10+5))];
    viewBackground.image = [UIImage imageNamed:@"dynamic_back"];
    [self.view addSubview:viewBackground];
    
    // 2, 自动计算高度的
    CGSize aSize = CGSizeMake(kScreenWidth - 20, INFINITY);
   
    NSMutableAttributedString *attStrM0 = [[NSMutableAttributedString alloc] initWithString: [NSString stringWithFormat:@"%@动态走势图",self.detailModel.name] attributes:@{NSFontAttributeName : AdaptedFontSize(14), NSForegroundColorAttributeName : [UIColor whiteColor]}];
    attStrM0.yy_lineSpacing = 10;
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:aSize text:attStrM0];
    
    YYLabel *titelLabel = [[YYLabel alloc] init];
    titelLabel.backgroundColor = [UIColor clearColor];
    titelLabel.frame = CGRectMake((SCREEN_WIDTH-layout.textBoundingSize.width)/2, NavBar_Height+AdaptedHeight(26+13), layout.textBoundingSize.width, layout.textBoundingSize.height);
    titelLabel.textLayout = layout;
    [self.view addSubview:titelLabel];
    
    UIImageView * icon_circle = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-layout.textBoundingSize.width)/2-10-10, NavBar_Height+AdaptedHeight(26+13+3), AdaptedWidth(10), AdaptedHeight(10))];
    icon_circle.image = [UIImage imageNamed:@"icon_circle"];
    [self.view addSubview:icon_circle];
 
    UIImageView * icon_arrow = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-layout.textBoundingSize.width)/2+layout.textBoundingSize.width+10, NavBar_Height+AdaptedHeight(26+13+3), AdaptedWidth(10), AdaptedHeight(10))];
    icon_arrow.image = [UIImage imageNamed:@"icon_arrow"];
    [self.view addSubview:icon_arrow];
    
    UILabel* unitLabel = [[UILabel alloc]initWithFrame:CGRectMake(AdaptedWidth(20), NavBar_Height+AdaptedHeight(26+13+5+10+5), AdaptedWidth(100), AdaptedHeight(20))];
    unitLabel.font = AdaptedFontSize(10);
    unitLabel.textColor = kUIColorFromRGB(0x00fcff);
    unitLabel.textAlignment = NSTextAlignmentLeft;
    unitLabel.text = [NSString stringWithFormat:@"单位:(%@)",self.detailModel.unit];
    [self.view addSubview:unitLabel];
    
    //每1秒运行一次function方法。
    myTimer =  [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(updateDynamicData) userInfo:nil repeats:YES];
    
    [self GetDynamicDataList:8];
}
 
 
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    self.navigationController.navigationBarHidden = YES;
    
    if (myTimer == nil)
        myTimer =  [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(updateDynamicData) userInfo:nil repeats:YES];
    else
        //开启定时器
        [myTimer setFireDate:[NSDate distantPast]];
}
 
 
-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:YES];
    
    //关闭定时器
    if(myTimer)
        [myTimer setFireDate:[NSDate distantFuture]];
}
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
-(void) reDrawLine
{
//    if( modelArray.count == 10 )
//    {
//        [_historyRecordLineChart.xLineDataArr removeAllObjects];
//        [_historyRecordLineChart.valueArr removeAllObjects];
//    }
//
//    for (int i = 0; i < modelArray.count; i++)
//    {
//        //LiveDataModel * model = [modelArray objectAtIndex:i];
//
//        //[_historyRecordLineChart.valueArr addObject:model.x5];
//        [_historyRecordLineChart.xLineDataArr addObject:[NSString stringWithFormat:@"%d",i]];
//    }
//
//    /*Start animation*/
//    [_historyRecordLineChart  showAnimation];
}
 
-(void)drawHistoryRecordLine
{
    /*     Create object        */
    _historyRecordLineChart = [[JHLineChart alloc] initWithFrame:CGRectMake(AdaptedWidth(15), NavBar_Height+AdaptedHeight(39+50), SCREEN_WIDTH-AdaptedWidth(20), AdaptedHeight(354)) andLineChartType:JHChartLineEveryValueForEveryX];
    
    /* The scale value of the X axis can be passed into the NSString or NSNumber type and the data structure changes with the change of the line chart type. The details look at the document or other quadrant X axis data source sample.*/
    
    //co2LineChart.xLineDataArr = @[@"10:00",@"11:21",@"12:32",@"13:33",@"14:43",@"15:33",@"16:11",@"17:21"];
    
    /* The different types of the broken line chart, according to the quadrant division, different quadrant correspond to different X axis scale data source and different value data source. */
    
    _historyRecordLineChart.lineChartQuadrantType = JHLineChartQuadrantTypeFirstQuardrant;
    
    _historyRecordLineChart.lineWidth = 2.0;
    _historyRecordLineChart.valueArr = [NSMutableArray array];
    NSMutableArray * valueTeampArr = [[NSMutableArray alloc] init];
    for (int i = 0; i < self.dynamicService.dynamicViewModels.count; i++)
    {
        DynamicModel * model = [self.dynamicService.dynamicViewModels objectAtIndex:i];
        
        [valueTeampArr addObject:model.value];
        [_historyRecordLineChart.xLineDataArr addObject:[NSString stringWithFormat:@"%d",i]];
    }
 
    [_historyRecordLineChart.valueArr addObject:valueTeampArr];
    
    [_historyRecordLineChart updateYScale];
    
    /*  是否显示坐标点的曲线*/
    _historyRecordLineChart.showYLevelLine = YES;
    /*         *  whether this chart shows leading lines for value point or not,default is YES  是否显示坐标点的曲线*/
    _historyRecordLineChart.showValueLeadingLine = NO;
    //_historyRecordLineChart.showYLine = NO;
 
    if( [StringUtil isPureInt:self.detailModel.upper] || [StringUtil isPureFloat:self.detailModel.upper])
    {
        if([self.detailModel.upper intValue] <= 1)
        {
            _historyRecordLineChart.yLineDataArr  = @[@"0.1",@"0.2",@"0.3",@"0.4",@"0.5",@"0.6",@"0.7",@"0.8",@"0.9",@"1"];
        }
        else
        {
            _historyRecordLineChart.yLineDataArr  = [NSMutableArray array];
            float fMaxValue = [self.detailModel.upper floatValue];
            if( fMaxValue < [self.detailModel.value floatValue])
            {
                fMaxValue = [self.detailModel.value floatValue];
            }
            
            float fMinValue = 0;//[self.detailModel.lower floatValue];
            float fOffSetValue = fMaxValue - fMinValue;
            float fAvgValue = fOffSetValue / 9;
            int fCurrentValue = fAvgValue;
            for (int index = 0; index < 10; index++)
            {
                if (index == 0)
                {
                    fCurrentValue = fAvgValue;
                }
                else if(index == 9)
                {
                    fCurrentValue = fMaxValue;
                }
                else
                {
                    fCurrentValue = fCurrentValue+fAvgValue;
                }
                
                NSString * currentStr = @"";
                if(fCurrentValue < 0)
                {
                    currentStr = [NSString stringWithFormat:@"-%d",fCurrentValue];
                }
                else
                {
                    currentStr = [NSString stringWithFormat:@"%d",fCurrentValue];
                }
                
                [_historyRecordLineChart.yLineDataArr addObject:currentStr];
            }
        }
    }
   else
   {
      _historyRecordLineChart.yLineDataArr  = @[@"10",@"20",@"30",@"40",@"50",@"60",@"70",@"80",@"90",@"100"];//[NSMutableArray array];
   }
 
    // 二氧化碳
    /* Line Chart colors 曲线颜色 */
    _historyRecordLineChart.valueLineColorArr =@[RgbColor(255, 255, 255)];
    /* Colors for every line chart 坐标点的颜色*/
    _historyRecordLineChart.pointColorArr = @[RgbColor(36, 209, 215)];
    
    _historyRecordLineChart.xyFontSize = AdaptedWidth(7);
    _historyRecordLineChart.numberFontSize = AdaptedWidth(10);
    /* color for XY axis XY轴线条的颜色*/
    _historyRecordLineChart.xAndYLineColor = kUIColorFromRGB(0x00fcff);
    /* XY axis scale color XY轴显示数值的颜色*/
    _historyRecordLineChart.xAndYNumberColor = kUIColorFromRGB(0x00fcff);
    /* Dotted line color of the coordinate point */
    _historyRecordLineChart.positionLineColorArr = @[RgbColor(255, 255, 255)];
    /*  坐标点数值的颜色*/
    _historyRecordLineChart.pointNumberColorArr = @[RgbColor(255, 255, 255)];
    /*        Set whether to fill the content, the default is False   是否填充曲线内部区域      */
    _historyRecordLineChart.contentFill = NO;
    /*        Set whether the curve path         */
    _historyRecordLineChart.pathCurve = YES;
    /*        Set fill color array      曲线内部区域填充的颜色    */
    _historyRecordLineChart.contentFillColorArr = @[[UIColor orangeColor]];
    
    [self.view addSubview:_historyRecordLineChart];
    /*       Start animation        */
    [_historyRecordLineChart showAnimation];
    
    //GloriaLabel * timeTips = [[GloriaLabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-200)/2, 215+150, 200, 30)];
    //[timeTips setText:@"单位(天)"];
    //timeTips.textColor = RgbColor(171, 172, 173);
    //timeTips.textAlignment = UITextAlignmentCenter;
    //timeTips.font = [UIFont boldSystemFontOfSize:14];
    //[self.view addSubview:timeTips];
    
}
 
-(void)updateDynamicData
{
    [self GetDynamicDataList:1];
}
 
-(void)GetDynamicDataList:(int)pageSize
{
    NSMutableDictionary * params = [[NSMutableDictionary alloc] init];
    [params setValue:[UserDefault stringForKey:@"mac"] forKey:@"mac"];
    [params setValue:self.detailModel.sensor_key forKey:@"sensorKey"];
    [params setValue:[NSString stringWithFormat:@"%d",pageSize] forKey:@"size"];
 
    TWWeak(self);
    [self.dynamicService getDynamicListWithParameters:params dataCount:pageSize completion:^(NSString *desc, int code) {
        if(code == 1)
        {
            if( pageSize > 1)
            {
                [weakself drawHistoryRecordLine];
            }
            else if(pageSize == 1)
            {
                NSMutableArray * valueTeampArr = [[NSMutableArray alloc] init];
                for (int i = 0; i < self.dynamicService.dynamicViewModels.count; i++)
                {
                    DynamicModel * model = [self.dynamicService.dynamicViewModels objectAtIndex:i];
                    
                    [valueTeampArr addObject:model.value];
                }
                
                [weakself.historyRecordLineChart.valueArr removeAllObjects];
                [weakself.historyRecordLineChart.valueArr addObject:valueTeampArr];
                [weakself.historyRecordLineChart clearLine];
            }
        }
        else
        {
            [Global alertMessageEx:desc title:@"获取失败" okTtitle:nil cancelTitle:@"OK" delegate:self];
        }
    }];
}
 
#pragma mark - LMJNavUIBaseViewControllerDataSource
 
- (UIStatusBarStyle)navUIBaseViewControllerPreferStatusBarStyle:(LMJNavUIBaseViewController *)navUIBaseViewController
{
    return UIStatusBarStyleDefault;
}
 
/**头部标题*/
- (NSMutableAttributedString*)lmjNavigationBarTitle:(LMJNavigationBar *)navigationBar
{
    return [self changeTitle:@"动态走势" changeColor:[UIColor whiteColor]];
}
 
//背景图片
//- (UIImage *)lmjNavigationBarBackgroundImage:(LMJNavigationBar *)navigationBar
//{
//    return [UIImage imageNamed:@"map_top_bar_bk"];
//}
 
/** 背景色 */
- (UIColor *)lmjNavigationBackgroundColor:(LMJNavigationBar *)navigationBar
{
    return kUIColorFromRGB(0x0b2f76);
}
 
/** 是否隐藏底部黑线 */
- (BOOL)lmjNavigationIsHideBottomLine:(LMJNavigationBar *)navigationBar
{
    return YES;
}
 
/** 导航条左边的按钮 */
- (UIImage *)lmjNavigationBarLeftButtonImage:(UIButton *)leftButton navigationBar:(LMJNavigationBar *)navigationBar
{
    return [UIImage imageNamed:@"map_back"];
}
/** 导航条右边的按钮 */
- (UIImage *)lmjNavigationBarRightButtonImage:(UIButton *)rightButton navigationBar:(LMJNavigationBar *)navigationBar
{
    //return [UIImage imageNamed:@"map_back"];
    
    //    [rightButton setTitle:@"立即注册" forState: UIControlStateNormal];
    //    [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    //    [rightButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    //    rightButton.titleLabel.font = [UIFont systemFontOfSize:15];
    //
    //    rightButton.lmj_width = 80;
    
    return nil;
}
 
#pragma mark - LMJNavUIBaseViewControllerDelegate
/** 左边的按钮的点击 */
-(void)leftButtonEvent:(UIButton *)sender navigationBar:(LMJNavigationBar *)navigationBar
{
    [self.navigationController popViewControllerAnimated:YES];
    //[self dismissViewControllerAnimated:YES completion:nil];
}
 
 
/** 右边的按钮的点击 */
-(void)rightButtonEvent:(UIButton *)sender navigationBar:(LMJNavigationBar *)navigationBar
{
    
}
 
/*
#pragma mark - Navigation
 
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
 
@end