Objective c 如何使用objective c绘制基于json数据的无键Shinobi图
我在下面这个结构中绘制json数据时遇到了问题Objective c 如何使用objective c绘制基于json数据的无键Shinobi图,objective-c,json,graph,shinobi,Objective C,Json,Graph,Shinobi,我在下面这个结构中绘制json数据时遇到了问题 {"response":{"hash":"0fea72f2bea9820c18227a655b42fe66","resp_code":"GRAPH_RETRIEVED","code":200},"resultstats":{"record_count":46},"ana_uoms":["°C"],"data":[{"asset_id":204,"company_id":30,"asset_name":"WIF","stats":{"min":1.4
{"response":{"hash":"0fea72f2bea9820c18227a655b42fe66","resp_code":"GRAPH_RETRIEVED","code":200},"resultstats":{"record_count":46},"ana_uoms":["°C"],"data":[{"asset_id":204,"company_id":30,"asset_name":"WIF","stats":{"min":1.4,"max":6.9,"min_tstamps_count":1,"min_tstamps":["2015-03-17 08:30:00"],"max_tstamps":["2015-03-17 11:00:00","2015-03-17 11:30:00","2015-03-17 13:40:00","2015-03-17 14:50:00"],"mkt":5.91,"max_tstamps_count":4,"avg":5.8},"component_type":"ana_in","component_description":"WFt","site_name":"Kll","alarms":[],"sampling_rate":10,"region_name":"ll","component_id":8340,"company_name":"lb","uom":"°C","uom_decimal_places":1,"unit_sn":"3310","resolves":[],"records":[["2015-03-17 08:00",2.6],["2015-03-17 08:10",3.9],["2015-03-17 08:20",2.4],["2015-03-17 08:30",1.4],["2015-03-17 08:40",3.1],["2015-03-17 08:50",4.4],["2015-03-17 09:00",5.1],["2015-03-17 09:10",5.5],["2015-03-17 09:20",6.1],["2015-03-17 09:30",6.2],["2015-03-17 09:40",5.3],["2015-03-17 09:50",6.4],["2015-03-17 10:00",6.6],["2015-03-17 10:10",6.7],["2015-03-17 10:20",6.8],["2015-03-17 10:30",5.2],["2015-03-17 10:40",6.3],["2015-03-17 10:50",6.7],["2015-03-17 11:00",6.9],["2015-03-17 11:10",6],["2015-03-17 11:20",6.6],["2015-03-17 11:30",6.9],["2015-03-17 11:40",5.9],["2015-03-17 11:50",6.6],["2015-03-17 12:00",6.8],["2015-03-17 12:10",6.1],["2015-03-17 12:20",6.6],["2015-03-17 12:30",5.6],["2015-03-17 12:40",6.2],["2015-03-17 12:50",6.8],["2015-03-17 13:00",5.7],["2015-03-17 13:10",6.6],["2015-03-17 13:20",5.8],["2015-03-17 13:30",6.4],["2015-03-17 13:40",6.9],["2015-03-17 13:50",6],["2015-03-17 14:00",6.7],["2015-03-17 14:10",5.2],["2015-03-17 14:20",6.5],["2015-03-17 14:30",5.8],["2015-03-17 14:40",6.3],["2015-03-17 14:50",6.9],["2015-03-17 15:00",6.6],["2015-03-17 15:10",5.9],["2015-03-17 15:20",6.3],["2015-03-17 15:30",6.5]]},{"asset_id":204,"company_id":30,"asset_name":"Wft","stats":{"min":2.9,"max":6.8,"min_tstamps_count":1,"min_tstamps":["2015-03-17 08:30:00"],"max_tstamps":["2015-03-17 11:00:00","2015-03-17 11:30:00","2015-03-17 12:00:00","2015-03-17 13:40:00"],"mkt":5.8,"max_tstamps_count":4,"avg":5.7},"component_type":"ana_in","component_description":"WIF Right Back","site_name":"Ktore","alarms":[],"sampling_rate":10,"region_name":"Kano","component_id":8341,"company_name":"lls","uom":"°C","uom_decimal_places":1,"unit_sn":"3310","resolves":[],"records":[["2015-03-17 08:00",3.2],["2015-03-17 08:10",4.2],["2015-03-17 08:20",3.1],["2015-03-17 08:30",2.9],["2015-03-17 08:40",3.7],["2015-03-17 08:50",4.5],["2015-03-17 09:00",5.2],["2015-03-17 09:10",5.6],["2015-03-17 09:20",6.1],["2015-03-17 09:30",6.3],["2015-03-17 09:40",4.8],["2015-03-17 09:50",6.3],["2015-03-17 10:00",6.5],["2015-03-17 10:10",6.6],["2015-03-17 10:20",6.7],["2015-03-17 10:30",4.5],["2015-03-17 10:40",6.3],["2015-03-17 10:50",6.6],["2015-03-17 11:00",6.8],["2015-03-17 11:10",5.9],["2015-03-17 11:20",6.5],["2015-03-17 11:30",6.8],["2015-03-17 11:40",5.7],["2015-03-17 11:50",6.5],["2015-03-17 12:00",6.8],["2015-03-17 12:10",5.9],["2015-03-17 12:20",6.6],["2015-03-17 12:30",5],["2015-03-17 12:40",6.2],["2015-03-17 12:50",6.7],["2015-03-17 13:00",5.5],["2015-03-17 13:10",6.5],["2015-03-17 13:20",5.2],["2015-03-17 13:30",6.2],["2015-03-17 13:40",6.8],["2015-03-17 13:50",5.8],["2015-03-17 14:00",6.6],["2015-03-17 14:10",4.5],["2015-03-17 14:20",6.4],["2015-03-17 14:30",5.2],["2015-03-17 14:40",6.2],["2015-03-17 14:50",6.7],["2015-03-17 15:00",6.4],["2015-03-17 15:10",5.2],["2015-03-17 15:20",6.2],["2015-03-17 15:30",6.1]]}],"ana_thresholds":[["<1.0"],[">8.0"]],"parameters":{"end_date":"2015-03-17 15:30","start_date":"2015-03-17 08:00","sampling_rate":10}}
{“response”:{“hash”:“0FEA72FEA9820C18227A655B42FE66”,“resp code”:“检索到的图形”,“代码”:200},“结果状态”:{“记录计数”:46},“分析计量”:[“C”],“数据”:[{“资产id”:204,“公司id”:30,“资产名称”:“WIF”,“统计”:{“最小值”:1.4,“最大值”:6.9,“最小值”:6.9,“最小值”:1,“最小值”,“2015-03-17 11:30:00”、“2015-03-17 13:40:00”、“2015-03-17 14:50:00”],“mkt”:5.91,“最大统计数”:4,“平均值”:5.8},“部件类型”:“分析输入”,“部件描述”:“WFt”,“现场名称”:“Kll”,“报警”:[],“采样率”:10,“区域名称”:“ll”,“部件id”:8340,“公司名称”:“lb”,“M”:“C”,“uom”:1”,“单位”,“sn 3310”:":[["2015-03-17 08:00",2.6],["2015-03-17 08:10",3.9],["2015-03-17 08:20",2.4],["2015-03-17 08:30",1.4],["2015-03-17 08:40",3.1],["2015-03-17 08:50",4.4],["2015-03-17 09:00",5.1],["2015-03-17 09:10",5.5],["2015-03-17 09:20",6.1],["2015-03-17 09:30",6.2],["2015-03-17 09:40",5.3],["2015-03-17 09:50",6.4],["2015-03-17 10:00",6.6],["2015-03-17 10:10",6.7],["2015-03-17 10:20",6.8],["2015-03-17 10:30",5.2],["2015-03-17 10:40",6.3],["2015-03-17 10:50",6.7],["2015-03-17 11:00",6.9],["2015-03-17 11:10",6],["2015-03-17 11:20",6.6],["2015-03-17 11:30",6.9],["2015-03-17 11:40",5.9],["2015-03-17 11:50",6.6],["2015-03-17 12:00",6.8],["2015-03-17 12:10",6.1],["2015-03-17 12:20",6.6],["2015-03-17 12:30",5.6],["2015-03-17 12:40",6.2],["2015-03-17 12:50",6.8],["2015-03-17 13:00",5.7],["2015-03-17 13:10",6.6],["2015-03-17 13:20",5.8],["2015-03-17 13:30",6.4],["2015-03-17 13:40",6.9],["2015-03-17 13:50",6],["2015-03-17 14:00",6.7],["2015-03-17 14:10",5.2],["2015-03-17 14:20",6.5],["2015-03-17 14:30",5.8],["2015-03-17 14:40",6.3],["2015-03-17 14:50"6.9,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,40:00“],“mkt”:5.8,“最大统计数”:4,“平均数”:5.7},“成分类型”:“分析”、“成分描述”:“WIF右后卫”、“场地名称”:“Ktore”、“警报”:[],“采样率”:10,“区域名称”:“卡诺”、“成分id”:8341,“公司名称”:“lls”、“计量单位”:“C”、“计量单位小数点”:1,“单位编号”:“3310”、“S”:“,”记录:[“2015-03-17:00”、“3.08-10:08”,4.2],["2015-03-17 08:20",3.1],["2015-03-17 08:30",2.9],["2015-03-17 08:40",3.7],["2015-03-17 08:50",4.5],["2015-03-17 09:00",5.2],["2015-03-17 09:10",5.6],["2015-03-17 09:20",6.1],["2015-03-17 09:30",6.3],["2015-03-17 09:40",4.8],["2015-03-17 09:50",6.3],["2015-03-17 10:00",6.5],["2015-03-17 10:10",6.6],["2015-03-17 10:20",6.7],["2015-03-17 10:30",4.5],["2015-03-17 10:40",6.3],["2015-03-17 10:50",6.6],["2015-03-17 11:00",6.8],["2015-03-17 11:10",5.9],["2015-03-17 11:20",6.5],["2015-03-17 11:30",6.8],["2015-03-17 11:40",5.7],["2015-03-17 11:50",6.5],["2015-03-17 12:00",6.8],["2015-03-17 12:10",5.9],["2015-03-17 12:20",6.6],["2015-03-17 12:30",5],["2015-03-17 12:40",6.2],["2015-03-17 12:50",6.7],["2015-03-17 13:00",5.5],["2015-03-17 13:10",6.5],["2015-03-17 13:20",5.2],["2015-03-17 13:30",6.2],["2015-03-17 13:40",6.8],["2015-03-17 13:50",5.8],["2015-03-17 14:00",6.6],["2015-03-17 14:10",4.5],["2015-03-17 14:20",6.4],["2015-03-17 14:30",5.2],["2015-03-17 14:40",6.2],["2015-03-17 14:50",6.7],["2015-03-17 15:00",6.4],["2015-03-17 15:10",5.2],“2015-03-17 15:20”,6.2],“2015-03-17 15:30”,6.1],“分析阈值”:[“8.0”],“参数”:{“结束日期”:“2015-03-17 15:30”,“开始日期”:“2015-03-17 08:00”,“采样率”:10}
我基本上是在尝试使用Shinobi graph library来绘制这些json数据,但我完全不知道如何解析上面的对象。在json中,我有日期时间和温度值。我设法在控制台中看到解析响应。但是,该图没有要绘制的对象数据,因为我没有向该系列传递任何json值,因为我不知道它是如何完成的。请帮忙,因为我在stackoverflow中没有看到类似的问题
这是我的代码片段
- (void) loadData{
// Parse units offline from the webservice.
@try{
NSString *delete = [[NSString alloc] initWithFormat:@""];
NSString *logOut = [NSString stringWithFormat:@"MY URL"];
NSURL *url=[NSURL URLWithString:logOut];
NSLog(@"Asset list: %@", url);
NSData *postData =[delete dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [ NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:postData];
//[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Reponse code: %ld", (long)[response statusCode]);
if ([response statusCode] >= 200 && [response statusCode] < 300)
{
NSLog(@"Response ==> %@", responseData);
@try{
NSError *error = nil;
// NSMutableArray* jsonDetails = [[NSMutableArray alloc]init];
NSDictionary *jsonDetails = [NSJSONSerialization JSONObjectWithData:urlData options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error];
NSArray *graphData = [ jsonDetails objectForKey:@"records"];
NSLog(@"Retrieving graph: %@", jsonDetails);
for (NSDictionary* jsonPoint in graphData) {
SChartDataPoint* datapoint = [self dataPointForDate:jsonPoint[@"date"]
andValue:jsonPoint[@"temp"]];
NSLog(@"SChart point: %@", datapoint);
[_timeSeries addObject:datapoint];
}
}
@catch (NSException *e){
NSLog(@"Try catch block: %@", e);
}
@finally{
// [self.tblRegion reloadData];
NSLog(@"finally");
}
}
}
@catch (NSException * e) {
NSLog(@"Exception: %@", e);
}
-(无效)加载数据{
//从webservice脱机分析单元。
@试一试{
NSString*delete=[[NSString alloc]initWithFormat:@'';
NSString*注销=[NSString stringWithFormat:@“我的URL”];
NSURL*url=[NSURL URLWithString:logOut];
NSLog(@“资产列表:%@”,url);
NSData*postData=[删除数据使用编码:NSASCIIStringEncoding allowLossyConversion:是];
NSString*postLength=[NSString stringWithFormat:@“%lu”,(无符号长)[postData长度]];
NSMutableURLRequest*请求=[[NSMutableURLRequest alloc]init];
[请求设置url:url];
[请求设置HttpMethod:@“获取”];
[请求设置值:HttpHeaderField的postLength:@“内容长度”];
[请求设置值:@“应用程序/json”用于HttpHeaderField:@“接受”];
[请求setHTTPBody:postData];
//[NSURLRequest setAllowsAnyHttpSCCertificate:YES for主机:[url主机]];
NSError*error=[[NSError alloc]init];
NSHTTPURLResponse*响应=nil;
NSData*urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&响应错误:&错误];
NSString*responseData=[[NSString alloc]initWithData:urlData编码:NSUTF8StringEncoding];
NSLog(@“响应代码:%ld”,(长)[响应状态代码]);
如果([response statusCode]>=200&&[response statusCode]<300)
{
NSLog(@“响应==>%@”,响应数据);
@试一试{
n错误*错误=nil;
//NSMUTABLEARRY*jsonDetails=[[NSMUTABLEARRY alloc]init];
NSDictionary*jsonDetails=[NSJSONSerialization JSONObjectWithData:urlData选项:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves错误:&error];
NSArray*graphData=[jsonDetails objectForKey
"records":[["2015-03-17 08:00",2.6],["2015-03-17 08:10",3.9],...
for (NSDictionary* jsonPoint in graphData) {
SChartDataPoint* datapoint = [self dataPointForDate:jsonPoint[@"date"]
andValue:jsonPoint[@"temp"]];
NSLog(@"SChart point: %@", datapoint);
[_timeSeries addObject:datapoint];
}
for (NSArray* jsonPoint in graphData) {
SChartDataPoint* datapoint = [self dataPointForDate:jsonPoint[0]
andValue:jsonPoint[1]];
NSLog(@"SChart point: %@", datapoint);
[_timeSeries addObject:datapoint];
}
{
NSLog(@"Response ==> %@", responseData);
NSError *error = nil;
jsonDetails = [NSJSONSerialization JSONObjectWithData:urlData options:kNilOptions error:&error];
NSArray *keys = [jsonDetails allKeys];
// values in foreach loop
for (NSString *key in keys) {
// NSLog(@"%@ is %@",key, [jsonDetails objectForKey:key]);
data = [jsonDetails objectForKey:@"data"];
graphData = [data[0]objectForKey:@"records"];
datapoint = [[SChartDataPoint alloc] init];
// NSLog(@"Record for the graph: %@", graphData);
// results = [jsonDetails objectForKey:@"resultstats"];
// recordCount = [results objectForKey:@"record_count"];
for (int i=0; i< [graphData count]; i++){
NSArray *record = graphData[i];
// NSLog(@"Record value: %@", record);
datapoint.xValue = record[0]; // date
NSLog(@"Date/time: %@", datapoint.xValue);
datapoint.yValue = record[1]; // number
NSLog(@"Temp: %@", datapoint.yValue);
datapoint = [self dataPointForDate:datapoint.xValue
andValue:datapoint.yValue];
NSLog(@"Adding datapoint to series: %@", datapoint);
[_timeSeries addObject:datapoint];
}
}
}
@interface ChartPoint : NSObject
@property(strong,nonatomic) NSDate *date;
@property(strong,nonatomic) NSNumber *value;
@end
@interface ChartData : NSObject
@property (strong,nonatomic) NSString *siteName;
@property (strong,nonatomic) NSMutableArray *chartPoints;
@end
-(void)setUpChart
{
schart = [[ShinobiChart alloc] initWithFrame:self.chartView.bounds];
schart.delegate=self;
schart.datasource = self;
schart.autoresizingMask = ~UIViewAutoresizingNone;
schart.licenseKey = @"your licence";
schart.legend.hidden=NO;
//X AXIS CONFIGURATION
SChartDateTimeAxis *xAxis = [[SChartDateTimeAxis alloc] init];
xAxis.style.interSeriesPadding = @0;
xAxis.style.majorTickStyle.showLabels = YES;
xAxis.style.majorTickStyle.showTicks = YES;
schart.xAxis = xAxis;
//Y AXIS CONFIGURATION
SChartAxis *yAxis = [[SChartNumberAxis alloc] init];
yAxis.style.majorGridLineStyle.showMajorGridLines=YES;
yAxis.style.majorTickStyle.showLabels = YES;
yAxis.style.majorTickStyle.showTicks = YES;
schart.yAxis = yAxis;
// add to the view
[self.chartView addSubview:schart];
}
-(void)parseResponse:(NSDictionary*)response
{
chartSeries = [[NSMutableArray alloc] init];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
NSArray *data = [response objectForKey:@"data"];
for (NSDictionary *d in data)
{
NSString *sitename = [d objectForKey:@"site_name"];
NSArray *records = [d objectForKey:@"records"];
ChartData *cd = [[ChartData alloc] init];
cd.siteName=sitename;
cd.chartPoints = [[NSMutableArray alloc] init];
for (NSArray *r in records)
{
NSString *dateString = [NSString stringWithFormat:@"%@",[r objectAtIndex:0]];
NSDate *date = [formatter dateFromString:dateString];
NSNumber *value = [r objectAtIndex:1];
ChartPoint *point =[[ChartPoint alloc] init];
point.date=date;
point.value = value;
[cd.chartPoints addObject:point];
}
[chartSeries addObject:cd];
}
[schart reloadData];
[schart redrawChart];
}
- (NSInteger)numberOfSeriesInSChart:(ShinobiChart *)chart
{
return chartSeries.count;
}
- (NSInteger)sChart:(ShinobiChart *)chart numberOfDataPointsForSeriesAtIndex:(NSInteger)seriesIndex
{
ChartData * data = [chartSeries objectAtIndex:seriesIndex];
return data.chartPoints.count;
}
-(SChartSeries *)sChart:(ShinobiChart *)chart seriesAtIndex:(NSInteger)index
{
ChartData * data = [chartSeries objectAtIndex:index];
SChartColumnSeries *columnSeries = [[SChartColumnSeries alloc] init];
columnSeries.title=data.siteName;
return columnSeries;
}
- (id<SChartData>)sChart:(ShinobiChart *)chart dataPointAtIndex:(NSInteger)dataIndex forSeriesAtIndex:(NSInteger)seriesIndex
{
ChartData * data = [chartSeries objectAtIndex:seriesIndex];
ChartPoint *point = [data.chartPoints objectAtIndex:dataIndex];
SChartDataPoint *dp = [[SChartDataPoint alloc] init];
dp.yValue = point.value;
dp.xValue =point.date;
return dp;
}