// // 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