core-plot iOS: How to make a graph start at the bottom with a certain amount?

StackOverflow https://stackoverflow.com/questions/21642598

  •  08-10-2022
  •  | 
  •  

Frage

I have a graph that uses the core-plot framework and it's lowest value on the Y axis is probably around 155. I wanted to make it so the 155 started at the bottom of the graph, right up against the X axis.

Here's my code for the Y axis:

    NSInteger majorIncrement = 1;
    NSInteger minorIncrement = 1;
    CGFloat yMax = [[prices valueForKeyPath:@"@max.self"] doubleValue];  // should determine dynamically based on max price
    NSMutableSet *yLabels = [NSMutableSet set];
    NSMutableSet *yMajorLocations = [NSMutableSet set];
    NSMutableSet *yMinorLocations = [NSMutableSet set];
    for (NSInteger j = [[prices valueForKeyPath:@"@min.self"] doubleValue]; j <= yMax; j += minorIncrement) {
        NSUInteger mod = j % majorIncrement;
        if (mod == 0) {
            CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%i", j] textStyle:y.labelTextStyle];
            NSDecimal location = CPTDecimalFromInteger(j);
            label.tickLocation = location;
            label.offset = -y.majorTickLength - y.labelOffset;
            if (label) {
                [yLabels addObject:label];
            }
            [yMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];
        } else {
            [yMinorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInteger(j)]];
        }
    }
    y.axisLabels = yLabels;
    y.majorTickLocations = yMajorLocations;
    y.minorTickLocations = yMinorLocations;
War es hilfreich?

Lösung

Try setting orthogonalCoordinateDecimal:

CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecimalFromFloat(155.0);

Andere Tipps

The visible area of the graph is controlled by the plot space. Set the yRange to a range that encloses your data:

CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromCGFloat(yMin)
                                                length:CPTDecimalFromCGFloat(yMax - yMin)];

Another option is to tell the plot space to automatically fit the current plot data:

[plotSpace scaleToFitPlots:[graph allPlots]];
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top