Core Plot-Filling Special area between coordinates with color

I am drawing a graph in which I want to fill the area under the data line with color. But the problem is, I want to use a different color for different spacing. Consider the following graph in the provided link.

Graphic link

in the above graph. How can I fill the area between coordinates (0,0) and (30,30) with green and rest with red?

Edit: I was able to solve my problem by using two different CPTScatterPlot.One for the Green area and others for the red.And I added the following code to plot the data source method.

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index;
{           
        if ([(NSString *)plot.identifier isEqualToString:@"Green"])
        {
            if(index<=30)
            {
        NSString *key = (fieldEnum == CPTScatterPlotFieldX ? @"x" : @"y");
        num = [[plotData objectAtIndex:index] valueForKey:key];

        if ( fieldEnum == CPTScatterPlotFieldY ) {
            num = [NSNumber numberWithDouble:[num doubleValue]];
        }
            }

        }
    else if([(NSString *)plot.identifier isEqualToString:@"Red"])
    {
        if(index>=30)
        {
        NSString *key = (fieldEnum == CPTScatterPlotFieldX ? @"x" : @"y");
        num = [[plotData objectAtIndex:index] valueForKey:key];

        if ( fieldEnum == CPTScatterPlotFieldY ) {
            num = [NSNumber numberWithDouble:[num doubleValue]];
        }
        }
    }                                                       
    return num;
}

      

+3


source to share


1 answer


I was able to solve my problem by using two different CPTScatterPlot.One for the Green area and others for the red.And I added the following code to plot the data source method.



-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index;
{           
        if ([(NSString *)plot.identifier isEqualToString:@"Green"])
        {
            if(index<=30)
            {
        NSString *key = (fieldEnum == CPTScatterPlotFieldX ? @"x" : @"y");
        num = [[plotData objectAtIndex:index] valueForKey:key];

        if ( fieldEnum == CPTScatterPlotFieldY ) {
            num = [NSNumber numberWithDouble:[num doubleValue]];
        }
            }

        }
    else if([(NSString *)plot.identifier isEqualToString:@"Red"])
    {
        if(index>=30)
        {
        NSString *key = (fieldEnum == CPTScatterPlotFieldX ? @"x" : @"y");
        num = [[plotData objectAtIndex:index] valueForKey:key];

        if ( fieldEnum == CPTScatterPlotFieldY ) {
            num = [NSNumber numberWithDouble:[num doubleValue]];
        }
        }
    }                                                       
    return num;
}

      

+2


source







All Articles