How to draw multiple sections in the core

I need to draw multiple graphs on the same graph at different times. Take a look at the image below:

Need graph like this

Except that the number of graphs will change dynamically. Sometimes I only needed blue and orange datasets several times all four and several times only 3. I can control for one bar like this.

CPTScatterPlot *plot = [[[CPTScatterPlot alloc] init] autorelease]; plot.dataSource = self; plot.identifier = @"mainplot"; plot.dataLineStyle = lineStyle; plot.plotSymbol = plotSymbol; [self.graph addPlot:plot]; 

In my case, I can put them in a for loop and do [self.graph addplot:plot] on each iteration. But how do I manage a data source. How do I manage the code below if the number of datasets changes dynamically.

 -(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index { if ( [plot.identifier isEqual:@"mainplot"] ) { NSValue *value = [self.graphData objectAtIndex:index]; CGPoint point = [value CGPointValue]; // FieldEnum determines if we return an X or Y value. if ( fieldEnum == CPTScatterPlotFieldX ) { return [NSNumber numberWithFloat:point.x]; } else // Y-Axis { return [NSNumber numberWithFloat:point.y]; } } return [NSNumber numberWithFloat:0]; } 
+4
source share
2 answers

I did it before and it worked! You can use NSArray for plots and create some graph data and add them as objects in an NSDictionary . in more detail you can see this example:

 NSDictionary *firstLineDic = [NSDictionary dictionaryWithObjectsAndKeys:@"firstLine", PLOT_IDENTIFIER, firstLineData, PLOT_DATA, nil]; NSDictionary *secondLineDic = [NSDictionary dictionaryWithObjectsAndKeys:@"secondLine", PLOT_IDENTIFIER, secondLineData, PLOT_DATA, nil]; NSArray *arrayData = [NSArray arrayWithObjects:firstLineDic, secondLineDic, nil]; scatterPlot = [[ScatterPlot alloc] initWithHostingView:plotView data:arrayData]; [scatterPlot initialisePlot]; 

Now in the ScatterPlot class write the following functions:

 -(id)initWithHostingView:(CPTGraphHostingView *)_hostingView data:(NSArray *)_data{ self = [super init]; if ( self != nil ) { self.hostingView = _hostingView; data = [[NSArray alloc] initWithArray:_data]; self.graph = nil; } return self; } -(void)initialisePlot { ... for (NSDictionary *dic in data) { CPTScatterPlot *plot = [[[CPTScatterPlot alloc] init] autorelease]; plot.dataSource = self; plot.identifier = [dic objectForKey:PLOT_IDENTIFIER]; plot.dataLineStyle = [lineStyles objectAtIndex:[dic objectForKey:PLOT_COLOR]]; plot.plotSymbol = plotSymbol; [self.graph addPlot:plot]; } ... } -(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot { for (NSDictionary *dic in data) { NSString *identity = [dic objectForKey:PLOT_IDENTIFIER]; if([plot.identifier isEqual:identity]){ NSArray *arr = [dic objectForKey:PLOT_DATA]; return [arr count]; } } return 0; } 
+2
source

Check the different plot identifiers and then return the other data.

0
source

Source: https://habr.com/ru/post/1402087/


All Articles